display_svd_zone_scene.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Sep 14 21:02:42 2018
  5. @author: jbuisine
  6. """
  7. from __future__ import print_function
  8. import sys, os, getopt
  9. import numpy as np
  10. import random
  11. import time
  12. import json
  13. from PIL import Image
  14. from ipfml import processing, metrics, utils
  15. from skimage import color
  16. import matplotlib.pyplot as plt
  17. from modules.utils.data import get_svd_data
  18. from modules.utils import config as cfg
  19. # getting configuration information
  20. config_filename = cfg.config_filename
  21. zone_folder = cfg.zone_folder
  22. min_max_filename = cfg.min_max_filename_extension
  23. # define all scenes values
  24. scenes_list = cfg.scenes_names
  25. scenes_indices = cfg.scenes_indices
  26. choices = cfg.normalization_choices
  27. path = cfg.dataset_path
  28. zones = cfg.zones_indices
  29. seuil_expe_filename = cfg.seuil_expe_filename
  30. metric_choices = cfg.metric_choices_labels
  31. generic_output_file_svd = '_random.csv'
  32. max_nb_bits = 8
  33. min_value_interval = sys.maxsize
  34. max_value_interval = 0
  35. def get_min_max_value_interval(_scene, _interval, _metric):
  36. global min_value_interval, max_value_interval
  37. scenes = os.listdir(path)
  38. # remove min max file from scenes folder
  39. scenes = [s for s in scenes if min_max_filename not in s]
  40. for id_scene, folder_scene in enumerate(scenes):
  41. # only take care of current scene
  42. if folder_scene == _scene:
  43. scene_path = os.path.join(path, folder_scene)
  44. zones_folder = []
  45. # create zones list
  46. for index in zones:
  47. index_str = str(index)
  48. if len(index_str) < 2:
  49. index_str = "0" + index_str
  50. zones_folder.append("zone"+index_str)
  51. for id_zone, zone_folder in enumerate(zones_folder):
  52. zone_path = os.path.join(scene_path, zone_folder)
  53. data_filename = _metric + "_svd" + generic_output_file_svd
  54. data_file_path = os.path.join(zone_path, data_filename)
  55. # getting number of line and read randomly lines
  56. f = open(data_file_path)
  57. lines = f.readlines()
  58. # check if user select current scene and zone to be part of training data set
  59. for line in lines:
  60. begin, end = _interval
  61. line_data = line.split(';')
  62. metrics = line_data[begin+1:end+1]
  63. metrics = [float(m) for m in metrics]
  64. min_value = min(metrics)
  65. max_value = max(metrics)
  66. if min_value < min_value_interval:
  67. min_value_interval = min_value
  68. if max_value > max_value_interval:
  69. max_value_interval = max_value
  70. def display_svd_values(p_scene, p_interval, p_indices, p_zone, p_metric, p_mode, p_step, p_norm, p_ylim):
  71. """
  72. @brief Method which gives information about svd curves from zone of picture
  73. @param p_scene, scene expected to show svd values
  74. @param p_interval, interval [begin, end] of svd data to display
  75. @param p_interval, interval [begin, end] of samples or minutes from render generation engine
  76. @param p_zone, zone's identifier of picture
  77. @param p_metric, metric computed to show
  78. @param p_mode, normalization's mode
  79. @param p_step, step of images indices
  80. @param p_norm, normalization or not of selected svd data
  81. @param p_ylim, ylim choice to better display of data
  82. @return nothing
  83. """
  84. scenes = os.listdir(path)
  85. # remove min max file from scenes folder
  86. scenes = [s for s in scenes if min_max_filename not in s]
  87. begin_data, end_data = p_interval
  88. begin_index, end_index = p_indices
  89. data_min_max_filename = os.path.join(path, p_metric + min_max_filename)
  90. # go ahead each scenes
  91. for id_scene, folder_scene in enumerate(scenes):
  92. if p_scene == folder_scene:
  93. scene_path = os.path.join(path, folder_scene)
  94. config_file_path = os.path.join(scene_path, config_filename)
  95. with open(config_file_path, "r") as config_file:
  96. last_image_name = config_file.readline().strip()
  97. prefix_image_name = config_file.readline().strip()
  98. start_index_image = config_file.readline().strip()
  99. end_index_image = config_file.readline().strip()
  100. step_counter = int(config_file.readline().strip())
  101. # construct each zones folder name
  102. zones_folder = []
  103. # get zones list info
  104. for index in zones:
  105. index_str = str(index)
  106. if len(index_str) < 2:
  107. index_str = "0" + index_str
  108. current_zone = "zone"+index_str
  109. zones_folder.append(current_zone)
  110. zones_images_data = []
  111. images_indices = []
  112. zone_folder = zones_folder[p_zone]
  113. zone_path = os.path.join(scene_path, zone_folder)
  114. current_counter_index = int(start_index_image)
  115. end_counter_index = int(end_index_image)
  116. # get threshold information
  117. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  118. # open treshold path and get this information
  119. with open(path_seuil, "r") as seuil_file:
  120. seuil_learned = int(seuil_file.readline().strip())
  121. threshold_image_found = False
  122. while(current_counter_index <= end_counter_index):
  123. current_counter_index_str = str(current_counter_index)
  124. while len(start_index_image) > len(current_counter_index_str):
  125. current_counter_index_str = "0" + current_counter_index_str
  126. if current_counter_index % p_step == 0:
  127. if current_counter_index >= begin_index and current_counter_index <= end_index:
  128. images_indices.append(current_counter_index_str)
  129. if seuil_learned < int(current_counter_index) and not threshold_image_found:
  130. threshold_image_found = True
  131. threshold_image_zone = current_counter_index_str
  132. current_counter_index += step_counter
  133. # all indices of picture to plot
  134. print(images_indices)
  135. for index in images_indices:
  136. img_path = os.path.join(scene_path, prefix_image_name + str(index) + ".png")
  137. current_img = Image.open(img_path)
  138. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  139. # getting expected block id
  140. block = img_blocks[p_zone]
  141. # get data from mode
  142. # Here you can add the way you compute data
  143. data = get_svd_data(p_metric, block)
  144. # TODO : improve part of this code to get correct min / max values
  145. if p_norm:
  146. data = data[begin_data:end_data]
  147. ##################
  148. # Data mode part #
  149. ##################
  150. if p_mode == 'svdne':
  151. # getting max and min information from min_max_filename
  152. if not p_norm:
  153. with open(data_min_max_filename, 'r') as f:
  154. min_val = float(f.readline())
  155. max_val = float(f.readline())
  156. else:
  157. min_val = min_value_interval
  158. max_val = max_value_interval
  159. data = utils.normalize_arr_with_range(data, min_val, max_val)
  160. if p_mode == 'svdn':
  161. data = utils.normalize_arr(data)
  162. if not p_norm:
  163. zones_images_data.append(data[begin_data:end_data])
  164. else:
  165. zones_images_data.append(data)
  166. plt.title(p_scene + ' scene interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + ']' + p_metric + ' metric, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=20)
  167. plt.ylabel('Image samples or time (minutes) generation', fontsize=14)
  168. plt.xlabel('Vector features', fontsize=16)
  169. for id, data in enumerate(zones_images_data):
  170. p_label = p_scene + "_" + images_indices[id]
  171. if images_indices[id] == threshold_image_zone:
  172. plt.plot(data, label=p_label, lw=4, color='red')
  173. else:
  174. plt.plot(data, label=p_label)
  175. plt.legend(bbox_to_anchor=(0.8, 1), loc=2, borderaxespad=0.2, fontsize=14)
  176. start_ylim, end_ylim = p_ylim
  177. plt.ylim(start_ylim, end_ylim)
  178. plt.show()
  179. def main():
  180. # by default p_step value is 10 to enable all photos
  181. p_step = 10
  182. p_norm = 0
  183. p_ylim = (0, 1)
  184. if len(sys.argv) <= 1:
  185. print('Run with default parameters...')
  186. print('python display_svd_zone_scene.py --scene A --interval "0,200" --indices "0, 900" --zone 3 --metric lab --mode svdne --step 50 --norm 0 --ylim "0, 0.1"')
  187. sys.exit(2)
  188. try:
  189. opts, args = getopt.getopt(sys.argv[1:], "hs:i:i:z:l:m:s:n:y", ["help=", "scene=", "interval=", "indices=", "zone=", "metric=", "mode=", "step=", "norm=", "ylim="])
  190. except getopt.GetoptError:
  191. # print help information and exit:
  192. print('python display_svd_zone_scene.py --scene A --interval "0,200" --indices "0, 900" --zone 3 --metric lab --mode svdne --step 50 --norm 0 --ylim "0, 0.1"')
  193. sys.exit(2)
  194. for o, a in opts:
  195. if o == "-h":
  196. print('python display_svd_zone_scene.py --scene A --interval "0,200" --indices "0, 900" --zone 3 --metric lab --mode svdne --step 50 --norm 0 --ylim "0, 0.1"')
  197. sys.exit()
  198. elif o in ("-s", "--scene"):
  199. p_scene = a
  200. if p_scene not in scenes_indices:
  201. assert False, "Invalid scene choice"
  202. else:
  203. p_scene = scenes_list[scenes_indices.index(p_scene)]
  204. elif o in ("-i", "--interval"):
  205. p_interval = list(map(int, a.split(',')))
  206. elif o in ("-i", "--indices"):
  207. p_indices = list(map(int, a.split(',')))
  208. elif o in ("-z", "--zone"):
  209. p_zone = int(a)
  210. elif o in ("-m", "--metric"):
  211. p_metric = a
  212. if p_metric not in metric_choices:
  213. assert False, "Invalid metric choice"
  214. elif o in ("-m", "--mode"):
  215. p_mode = a
  216. if p_mode not in choices:
  217. assert False, "Invalid normalization choice, expected ['svd', 'svdn', 'svdne']"
  218. elif o in ("-s", "--step"):
  219. p_step = int(a)
  220. elif o in ("-n", "--norm"):
  221. p_norm = int(a)
  222. elif o in ("-y", "--ylim"):
  223. p_ylim = list(map(float, a.split(',')))
  224. else:
  225. assert False, "unhandled option"
  226. if p_norm:
  227. get_min_max_value_interval(p_scene, p_interval, p_metric)
  228. display_svd_values(p_scene, p_interval, p_indices, p_zone, p_metric, p_mode, p_step, p_norm, p_ylim)
  229. if __name__== "__main__":
  230. main()