save_model_result_in_md.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. from ipfml import image_processing
  4. from PIL import Image
  5. import sys, os, getopt
  6. import subprocess
  7. import time
  8. current_dirpath = os.getcwd()
  9. threshold_map_folder = "threshold_map"
  10. threshold_map_file_prefix = "treshold_map_"
  11. markdowns_folder = "models_info"
  12. zones = np.arange(16)
  13. def main():
  14. if len(sys.argv) <= 1:
  15. print('Run with default parameters...')
  16. print('python save_model_result_in_md.py --interval "0,20" --model path/to/xxxx.joblib --mode ["svd", "svdn", "svdne"] --metric ["lab", "mscn"]')
  17. sys.exit(2)
  18. try:
  19. opts, args = getopt.getopt(sys.argv[1:], "ht:m:o:l", ["help=", "interval=", "model=", "mode=", "metric="])
  20. except getopt.GetoptError:
  21. # print help information and exit:
  22. print('python save_model_result_in_md.py --interval "xx,xx" --model path/to/xxxx.joblib --mode ["svd", "svdn", "svdne"] --metric ["lab", "mscn"]')
  23. sys.exit(2)
  24. for o, a in opts:
  25. if o == "-h":
  26. print('python save_model_result_in_md.py --interval "xx,xx" --model path/to/xxxx.joblib --mode ["svd", "svdn", "svdne"] --metric ["lab", "mscn"]')
  27. sys.exit()
  28. elif o in ("-t", "--interval"):
  29. p_interval = list(map(int, a.split(',')))
  30. elif o in ("-m", "--model"):
  31. p_model_file = a
  32. elif o in ("-o", "--mode"):
  33. p_mode = a
  34. if p_mode != 'svdn' and p_mode != 'svdne' and p_mode != 'svd':
  35. assert False, "Mode not recognized"
  36. elif o in ("-me", "--metric"):
  37. p_metric = a
  38. else:
  39. assert False, "unhandled option"
  40. # call model and get global result in scenes
  41. begin, end = p_interval
  42. bash_cmd = "bash testModelByScene.sh '" + str(begin) + "' '" + str(end) + "' '" + p_model_file + "' '" + p_mode + "' '" + p_metric + "'"
  43. print(bash_cmd)
  44. ## call command ##
  45. p = subprocess.Popen(bash_cmd, stdout=subprocess.PIPE, shell=True)
  46. (output, err) = p.communicate()
  47. ## Wait for result ##
  48. p_status = p.wait()
  49. if not os.path.exists(markdowns_folder):
  50. os.makedirs(markdowns_folder)
  51. # get model name to construct model
  52. md_model_path = os.path.join(markdowns_folder, p_model_file.split('/')[-1].replace('.joblib', '.md'))
  53. with open(md_model_path, 'w') as f:
  54. f.write(output.decode("utf-8"))
  55. # read each threshold_map information if exists
  56. model_map_info_path = os.path.join(threshold_map_folder, p_model_file.replace('saved_models/', ''))
  57. if not os.path.exists(model_map_info_path):
  58. f.write('\n\n No threshold map information')
  59. else:
  60. maps_files = os.listdir(model_map_info_path)
  61. # get all map information
  62. for t_map_file in maps_files:
  63. file_path = os.path.join(model_map_info_path, t_map_file)
  64. with open(file_path, 'r') as map_file:
  65. title_scene = t_map_file.replace(threshold_map_file_prefix, '')
  66. f.write('\n\n## ' + title_scene + '\n')
  67. content = map_file.readlines()
  68. # getting each map line information
  69. for line in content:
  70. f.write(line)
  71. f.close()
  72. if __name__== "__main__":
  73. main()