save_model_result_in_md.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 ["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 ["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 ["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. md_model_path = markdowns_folder + '/' + p_model_file.split('/')[1].replace('.joblib', '.md')
  50. with open(md_model_path, 'w') as f:
  51. f.write(output.decode("utf-8"))
  52. # read each threshold_map information if exists
  53. model_map_info_path = threshold_map_folder + '/' + p_model_file.replace('saved_models/', '')
  54. if not os.path.exists(model_map_info_path):
  55. f.write('\n\n No threshold map information')
  56. else:
  57. maps_files = os.listdir(model_map_info_path)
  58. # get all map information
  59. for t_map_file in maps_files:
  60. file_path = model_map_info_path + '/' + t_map_file
  61. with open(file_path, 'r') as map_file:
  62. title_scene = t_map_file.replace(threshold_map_file_prefix, '')
  63. f.write('\n\n## ' + title_scene + '\n')
  64. content = map_file.readlines()
  65. # getting each map line information
  66. for line in content:
  67. f.write(line)
  68. f.close()
  69. if __name__== "__main__":
  70. main()