extract_expe_info_subject_zones.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # main imports
  2. import sys, os, argparse
  3. import math
  4. import numpy as np
  5. import pickle
  6. # processing imports
  7. from PIL import Image
  8. import matplotlib.pyplot as plt
  9. import scipy.stats as stats
  10. # modules imports
  11. sys.path.insert(0, '') # trick to enable import of main folder module
  12. import custom_config as cfg
  13. import utils as utils_functions
  14. # variables
  15. data_expe_folder = cfg.data_expe_folder
  16. position_file_pattern = cfg.position_file_pattern
  17. click_line_pattern = cfg.click_line_pattern
  18. min_x = cfg.min_x_coordinate
  19. min_y = cfg.min_y_coordinate
  20. image_scene_size = cfg.image_scene_size
  21. scene_width, scene_height = image_scene_size
  22. def main():
  23. parser = argparse.ArgumentParser(description="Compute expe data into output file")
  24. parser.add_argument('--n', type=int, help="`n` first clicks", required=True)
  25. parser.add_argument('--folder', type=str, help="output folder expected", required=True)
  26. parser.add_argument('--reverse', type=int, help="reverse or not y axis clicks", default=False)
  27. args = parser.parse_args()
  28. p_n = args.n
  29. p_folder = args.folder
  30. p_reverse = bool(args.reverse)
  31. print(p_reverse)
  32. # list all folders
  33. subjects = os.listdir(data_expe_folder)
  34. print('Number of subjects', len(subjects))
  35. output_folder_path = os.path.join(cfg.media_data_folder, p_folder)
  36. if not os.path.exists(output_folder_path):
  37. os.makedirs(output_folder_path)
  38. # keep scene_data in memory
  39. scenes_data = {}
  40. for scene in cfg.scenes_names:
  41. zones_list = {}
  42. for zone_index in cfg.zones_indices:
  43. zones_list[zone_index] = {}
  44. # construct for each scene
  45. zones_list[zone_index]['x'] = []
  46. zones_list[zone_index]['y'] = []
  47. scenes_data[scene] = zones_list
  48. for _, subject in enumerate(subjects):
  49. subject_folder = os.path.join(data_expe_folder, subject)
  50. data_files = os.listdir(subject_folder)
  51. pos_file = [f for f in data_files if position_file_pattern in f][0]
  52. pos_filepath = os.path.join(subject_folder, pos_file)
  53. previous_path_scene = ""
  54. path_scene = ""
  55. new_scene = True
  56. number_of_scenes = 0
  57. scene_name = ""
  58. print('Extract images clicks of subject', subject)
  59. # open pos file and extract click information
  60. with open(pos_filepath, 'r') as f:
  61. # for each subject check `p_n` on each zone
  62. zones_filled = {}
  63. zones_clicks_of_subject = {}
  64. for zone_index in cfg.zones_indices:
  65. zones_filled[zone_index] = 0
  66. zones_clicks_of_subject[zone_index] = {}
  67. zones_clicks_of_subject[zone_index]['x'] = []
  68. zones_clicks_of_subject[zone_index]['y'] = []
  69. for line in f.readlines():
  70. if click_line_pattern in line and scene_name in cfg.scenes_names:
  71. x, y = utils_functions.extract_click_coordinate(line)
  72. p_x = x - min_x
  73. p_y = y - min_y
  74. # only accept valid coordinates (need to substract `x_min` and `y_min` before check)
  75. if utils_functions.check_coordinates(p_x, p_y):
  76. if p_reverse:
  77. # add reversed points here
  78. p_y = scene_height - p_y
  79. # get zone indice
  80. zone_index = utils_functions.get_zone_index(p_x, p_y)
  81. # check number of points saved for this specific zone
  82. # add only if wished
  83. if zones_filled[zone_index] < p_n:
  84. zones_clicks_of_subject[zone_index]['x'].append(p_x)
  85. zones_clicks_of_subject[zone_index]['y'].append(p_y)
  86. zones_filled[zone_index] += 1
  87. elif click_line_pattern not in line:
  88. path_scene = line
  89. if previous_path_scene != path_scene:
  90. previous_path_scene = path_scene
  91. new_scene = True
  92. scene_name = path_scene.split('/')[4]
  93. if scene_name in cfg.scenes_names:
  94. number_of_scenes += 1
  95. if previous_path_scene != "":
  96. subject_path = os.path.join(output_folder_path, subject)
  97. if not os.path.exists(subject_path):
  98. os.makedirs(subject_path)
  99. output_image_name = subject + '_' + scene_name + '_' + str(p_n) + '.png'
  100. img_path = os.path.join(subject_path, output_image_name)
  101. title = subject + ' - ' + scene_name + ' (' + str(p_n) + ' clicks)'
  102. # save image plot
  103. x_points = zones_clicks_of_subject[zone_index]['x']
  104. y_points = zones_clicks_of_subject[zone_index]['y']
  105. utils_functions.save_img_plot(scene_name, x_points, y_points, title, img_path)
  106. # save scene data
  107. for i in cfg.zones_indices:
  108. scenes_data[scene_name][i]['x'] = scenes_data[scene_name][i]['x'] + x_points
  109. scenes_data[scene_name][i]['y'] = scenes_data[scene_name][i]['y'] + y_points
  110. # reinit zones list
  111. for zone_index in cfg.zones_indices:
  112. zones_filled[zone_index] = 0
  113. zones_clicks_of_subject[zone_index] = {}
  114. zones_clicks_of_subject[zone_index]['x'] = []
  115. zones_clicks_of_subject[zone_index]['y'] = []
  116. else:
  117. new_scene = False
  118. all_path_folder = os.path.join(output_folder_path, cfg.all_subjects_data_folder)
  119. if not os.path.exists(all_path_folder):
  120. os.makedirs(all_path_folder)
  121. print('Merge images clicks of subjects into', all_path_folder)
  122. for k, v in scenes_data.items():
  123. current_x_points = []
  124. current_y_points = []
  125. for i in cfg.zones_indices:
  126. current_x_points = current_x_points + v[i]['x']
  127. current_y_points = current_y_points + v[i]['y']
  128. title = k + ' scene with all subjects (with ' + str(p_n) + ' clicks per subject)'
  129. img_filename = cfg.all_subjects_data_folder + '_' + k + '_' + str(p_n) + '.png'
  130. img_path = os.path.join(all_path_folder, img_filename)
  131. # save figure `all` subjects `p_n` clicks
  132. utils_functions.save_img_plot(k, current_x_points, current_y_points, title, img_path)
  133. print('Images are saved into', output_folder_path)
  134. if __name__== "__main__":
  135. main()