noise_svd_visualization.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import sys, os, getopt
  2. from PIL import Image
  3. from ipfml import processing
  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. def main():
  15. p_step = 1
  16. max_value_svd = 0
  17. min_value_svd = sys.maxsize
  18. if len(sys.argv) <= 1:
  19. print('python noise_svd_visualization.py --prefix path/with/prefix --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --output filename')
  20. sys.exit(2)
  21. try:
  22. opts, args = getopt.getopt(sys.argv[1:], "h:p:m:m:n:i:s:o", ["help=", "prefix=", "metric=", "mode=", "n=", "interval=", "step=", "output="])
  23. except getopt.GetoptError:
  24. # print help information and exit:
  25. print('python noise_svd_visualization.py --prefix path/with/prefix --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --output filename')
  26. sys.exit(2)
  27. for o, a in opts:
  28. if o == "-h":
  29. print('python noise_svd_visualization.py --prefix path/with/prefix --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --output filename')
  30. sys.exit()
  31. elif o in ("-p", "--prefix"):
  32. p_prefix = a
  33. elif o in ("-m", "--mode"):
  34. p_mode = a
  35. if not p_mode in normalization_choices:
  36. assert False, "Unknown normalization choice, %s" % normalization_choices
  37. elif o in ("-m", "--metric"):
  38. p_metric = a
  39. if not p_metric in metric_choices:
  40. assert False, "Unknown metric choice, %s" % metric_choices
  41. elif o in ("-n", "--n"):
  42. p_n = int(a)
  43. elif o in ("-i", "--interval"):
  44. p_interval = list(map(int, a.split(',')))
  45. elif o in ("-s", "--step"):
  46. p_step = int(a)
  47. elif o in ("-o", "--output"):
  48. p_output = a
  49. else:
  50. assert False, "unhandled option"
  51. noise_name = p_prefix.split('/')[1].replace('_', '')
  52. file_path = p_prefix + "{}." + filename_ext
  53. begin, end = p_interval
  54. all_svd_data = []
  55. svd_data = []
  56. image_indices = []
  57. # get all data from images
  58. for i in range(1, p_n):
  59. image_path = file_path.format(str(i))
  60. img = Image.open(image_path)
  61. svd_values = dt.get_svd_data(p_metric, img)
  62. svd_values = svd_values[begin:end]
  63. all_svd_data.append(svd_values)
  64. # update min max values
  65. min_value = svd_values.min()
  66. max_value = svd_values.max()
  67. if min_value < min_value_svd:
  68. min_value_svd = min_value
  69. if max_value > min_value_svd:
  70. max_value_svd = max_value
  71. print('%.2f%%' % ((i + 1) / p_n * 100))
  72. sys.stdout.write("\033[F")
  73. print("Generation of output figure...")
  74. for id, data in enumerate(all_svd_data):
  75. if id % p_step == 0:
  76. current_data = data
  77. if p_mode == 'svdn':
  78. current_data = processing.normalize_arr(current_data)
  79. if p_mode == 'svdne':
  80. current_data = processing.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  81. svd_data.append(current_data)
  82. image_indices.append(id)
  83. # display all data using matplotlib
  84. plt.title(noise_name + ' noise, interval information ['+ str(begin) +', '+ str(end) +'], ' + p_metric + ' metric, step ' + str(p_step), fontsize=20)
  85. plt.ylabel('Importance of noise [1, 999]', fontsize=14)
  86. plt.xlabel('Vector features', fontsize=16)
  87. for id, data in enumerate(svd_data):
  88. p_label = p_prefix + str(image_indices[id])
  89. plt.plot(data, label=p_label)
  90. plt.legend(bbox_to_anchor=(0.8, 1), loc=2, borderaxespad=0.2, fontsize=14)
  91. plt.ylim(0, 0.1)
  92. plt.show()
  93. output_filename = noise_name + "1_to_" + str(p_n) + "_B" + str(begin) + "_E" + str(end) + "_" + p_metric + "_S" + str(p_step) + "_" + p_mode
  94. output_path = os.path.join(pictures_folder, output_filename)
  95. if not os.path.exists(pictures_folder):
  96. os.makedirs(pictures_folder)
  97. plt.savefig(output_path)
  98. if __name__== "__main__":
  99. main()