save_model_result_in_md.py 3.4 KB

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