noise_svd_visualization.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import sys, os, getopt
  2. from PIL import Image
  3. from ipfml import processing, utils
  4. from modules.utils import config as cfg
  5. from modules.utils import data_type as dt
  6. from modules import noise
  7. import matplotlib.pyplot as plt
  8. noise_list = cfg.noise_labels
  9. generated_folder = cfg.generated_folder
  10. filename_ext = cfg.filename_ext
  11. metric_choices = cfg.metric_choices_labels
  12. normalization_choices = cfg.normalization_choices
  13. pictures_folder = cfg.pictures_output_folder
  14. step_picture = 10
  15. def main():
  16. # default values
  17. p_step = 1
  18. p_color = 0
  19. p_norm = 0
  20. p_ylim = (0, 1)
  21. max_value_svd = 0
  22. min_value_svd = sys.maxsize
  23. if len(sys.argv) <= 1:
  24. print('python noise_svd_visualization.py --prefix generated/prefix/noise --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --color 1 --norm 1 --ylim "0, 1"')
  25. sys.exit(2)
  26. try:
  27. opts, args = getopt.getopt(sys.argv[1:], "h:p:m:m:n:i:s:c:n:y", ["help=", "prefix=", "metric=", "mode=", "n=", "interval=", "step=", "color=", "norm=", "ylim="])
  28. except getopt.GetoptError:
  29. # print help information and exit:
  30. print('python noise_svd_visualization.py --prefix generated/prefix/noise --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --color 1 --norm 1 --ylim "0, 1"')
  31. sys.exit(2)
  32. for o, a in opts:
  33. if o == "-h":
  34. print('python noise_svd_visualization.py --prefix generated/prefix/noise --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --color 1 --norm 1 --ylim "0, 1"')
  35. sys.exit()
  36. elif o in ("-p", "--prefix"):
  37. p_path = a
  38. elif o in ("-m", "--mode"):
  39. p_mode = a
  40. if not p_mode in normalization_choices:
  41. assert False, "Unknown normalization choice, %s" % normalization_choices
  42. elif o in ("-m", "--metric"):
  43. p_metric = a
  44. if not p_metric in metric_choices:
  45. assert False, "Unknown metric choice, %s" % metric_choices
  46. elif o in ("-n", "--n"):
  47. p_n = int(a)
  48. elif o in ("-n", "--norm"):
  49. p_norm = int(a)
  50. elif o in ("-c", "--color"):
  51. p_color = int(a)
  52. elif o in ("-i", "--interval"):
  53. p_interval = list(map(int, a.split(',')))
  54. elif o in ("-s", "--step"):
  55. p_step = int(a)
  56. elif o in ("-y", "--ylim"):
  57. p_ylim = list(map(float, a.split(',')))
  58. else:
  59. assert False, "unhandled option"
  60. p_prefix = p_path.split('/')[1].replace('_', '')
  61. noise_name = p_path.split('/')[2]
  62. if p_color:
  63. file_path = p_path + "/" + p_prefix + "_" + noise_name + "_color_{}." + filename_ext
  64. else:
  65. file_path = p_path + "/" + p_prefix + "_" + noise_name + "_{}." + filename_ext
  66. begin, end = p_interval
  67. all_svd_data = []
  68. svd_data = []
  69. image_indices = []
  70. # get all data from images
  71. for i in range(1, p_n):
  72. if i % step_picture == 0:
  73. image_path = file_path.format(str(i))
  74. img = Image.open(image_path)
  75. svd_values = dt.get_svd_data(p_metric, img)
  76. if p_norm:
  77. svd_values = svd_values[begin:end]
  78. all_svd_data.append(svd_values)
  79. # update min max values
  80. min_value = svd_values.min()
  81. max_value = svd_values.max()
  82. if min_value < min_value_svd:
  83. min_value_svd = min_value
  84. if max_value > max_value_svd:
  85. max_value_svd = max_value
  86. print('%.2f%%' % ((i + 1) / p_n * 100))
  87. sys.stdout.write("\033[F")
  88. for id, data in enumerate(all_svd_data):
  89. if (id * step_picture) % p_step == 0:
  90. current_data = data
  91. if p_mode == 'svdn':
  92. current_data = utils.normalize_arr(current_data)
  93. if p_mode == 'svdne':
  94. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  95. svd_data.append(current_data)
  96. image_indices.append(str(id * step_picture))
  97. # display all data using matplotlib (configure plt)
  98. plt.rcParams['figure.figsize'] = (25, 18)
  99. plt.title(p_prefix + ' noise, interval information ['+ str(begin) +', '+ str(end) +'], ' + p_metric + ' metric, step ' + str(p_step) + ' normalization ' + p_mode, fontsize=20)
  100. plt.ylabel('Importance of noise [1, 999]', fontsize=14)
  101. plt.xlabel('Vector features', fontsize=16)
  102. for id, data in enumerate(svd_data):
  103. p_label = p_prefix + str(image_indices[id])
  104. plt.plot(data, label=p_label)
  105. plt.legend(bbox_to_anchor=(0.8, 1), loc=2, borderaxespad=0.2, fontsize=14)
  106. if not p_norm:
  107. plt.xlim(begin, end)
  108. # adapt ylim
  109. y_begin, y_end = p_ylim
  110. plt.ylim(y_begin, y_end)
  111. output_filename = p_prefix + "_" + noise_name + "_1_to_" + str(p_n) + "_B" + str(begin) + "_E" + str(end) + "_" + p_metric + "_S" + str(p_step) + "_norm" + str(p_norm )+ "_" + p_mode
  112. if p_color:
  113. output_filename = output_filename + '_color'
  114. print("Generation of output figure... %s" % output_filename)
  115. output_path = os.path.join(pictures_folder, output_filename)
  116. if not os.path.exists(pictures_folder):
  117. os.makedirs(pictures_folder)
  118. plt.savefig(output_path, dpi=(200))
  119. if __name__== "__main__":
  120. main()