save_model_result_in_md.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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"]')
  17. sys.exit(2)
  18. try:
  19. opts, args = getopt.getopt(sys.argv[1:], "ht:m:o:l", ["help=", "interval=", "model=", "mode="])
  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"]')
  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"]')
  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. else:
  37. assert False, "unhandled option"
  38. # call model and get global result in scenes
  39. begin, end = p_interval
  40. bash_cmd = "bash testModelByScene.sh '" + str(begin) + "' '" + str(end) + "' '" + p_model_file + "' '" + p_mode + "'"
  41. print(bash_cmd)
  42. ## call command ##
  43. p = subprocess.Popen(bash_cmd, stdout=subprocess.PIPE, shell=True)
  44. (output, err) = p.communicate()
  45. ## Wait for result ##
  46. p_status = p.wait()
  47. if not os.path.exists(markdowns_folder):
  48. os.makedirs(markdowns_folder)
  49. # get model name to construct model
  50. md_model_path = markdowns_folder + '/' + p_model_file.split('/')[-1].replace('.joblib', '.md')
  51. with open(md_model_path, 'w') as f:
  52. f.write(output.decode("utf-8"))
  53. # read each threshold_map information if exists
  54. model_map_info_path = threshold_map_folder + '/' + p_model_file.replace('saved_models/', '')
  55. if not os.path.exists(model_map_info_path):
  56. f.write('\n\n No threshold map information')
  57. else:
  58. maps_files = os.listdir(model_map_info_path)
  59. # get all map information
  60. for t_map_file in maps_files:
  61. file_path = model_map_info_path + '/' + t_map_file
  62. with open(file_path, 'r') as map_file:
  63. title_scene = t_map_file.replace(threshold_map_file_prefix, '')
  64. f.write('\n\n## ' + title_scene + '\n')
  65. content = map_file.readlines()
  66. # getting each map line information
  67. for line in content:
  68. f.write(line)
  69. f.close()
  70. if __name__== "__main__":
  71. main()