extract_expe_info_subject_scenes.py 5.6 KB

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