extract_expe_info_scenes.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # main imports
  2. import sys, os, argparse
  3. import math
  4. import numpy as np
  5. # processing imports
  6. import matplotlib.pyplot as plt
  7. import scipy.stats as stats
  8. # modules imports
  9. sys.path.insert(0, '') # trick to enable import of main folder module
  10. import custom_config as cfg
  11. # variables
  12. data_expe_folder = cfg.data_expe_folder
  13. position_file_pattern = cfg.position_file_pattern
  14. click_line_pattern = cfg.click_line_pattern
  15. # utils variables
  16. zone_width, zone_height = cfg.image_zone_size
  17. scene_width, scene_height = cfg.image_scene_size
  18. nb_x_parts = math.floor(scene_width / zone_width)
  19. min_x = 100
  20. min_y = 100
  21. def get_zone_index(p_x, p_y):
  22. zone_index = math.floor(p_x / zone_width) + math.floor(p_y / zone_height) * nb_x_parts
  23. return zone_index
  24. def check_coordinates(p_x, p_y):
  25. if p_x < min_x or p_y < min_y:
  26. return False
  27. if p_x > min_x + scene_width or p_y > min_y + scene_height:
  28. return False
  29. return True
  30. def extract_click_coordinate(line):
  31. data = line.split(' : ')[1].split(',')
  32. p_x, p_y = (int(data[0]), int(data[1]))
  33. return (p_x, p_y)
  34. def main():
  35. parser = argparse.ArgumentParser(description="Compute expe data into output file")
  36. parser.add_argument('--n', type=int, help="`n` first clicks", required=True)
  37. parser.add_argument('--output', type=str, help="output folder expected", required=True)
  38. args = parser.parse_args()
  39. p_n = args.n
  40. p_output = args.output
  41. # list all folders
  42. subjects = os.listdir(data_expe_folder)
  43. print('Number of subjects', len(subjects))
  44. # initiate list which will contains `n` first clicks (if exists) on zone for each subject on each scene
  45. scenes = {}
  46. for scene in cfg.scenes_names:
  47. scenes[scene] = {}
  48. # construct for each scene
  49. scenes[scene]['x'] = []
  50. scenes[scene]['y'] = []
  51. for index, subject in enumerate(subjects):
  52. subject_folder = os.path.join(data_expe_folder, subject)
  53. data_files = os.listdir(subject_folder)
  54. pos_file = [f for f in data_files if position_file_pattern in f][0]
  55. pos_filepath = os.path.join(subject_folder, pos_file)
  56. previous_path_scene = ""
  57. path_scene = ""
  58. new_scene = True
  59. number_of_scenes = 0
  60. counter = 0
  61. scene_name = ""
  62. # open pos file and extract click information
  63. with open(pos_filepath, 'r') as f:
  64. points_x = []
  65. points_y = []
  66. for line in f.readlines():
  67. if click_line_pattern in line and scene_name in cfg.scenes_names:
  68. x, y = extract_click_coordinate(line)
  69. points_x.append(x)
  70. points_y.append(y)
  71. # only accept valid coordinates
  72. if check_coordinates(x, y):
  73. if counter < p_n:
  74. scenes[scene_name]['x'].append(x - min_x)
  75. scenes[scene_name]['y'].append(y - min_y)
  76. counter += 1
  77. elif click_line_pattern not in line:
  78. path_scene = line
  79. if previous_path_scene != path_scene:
  80. if previous_path_scene != "":
  81. pass
  82. #plt.title(subject + ' - ' + scene_name)
  83. #plt.scatter(points_x, points_y)
  84. #plt.show()
  85. previous_path_scene = path_scene
  86. new_scene = True
  87. scene_name = path_scene.split('/')[4]
  88. if scene_name in cfg.scenes_names:
  89. number_of_scenes += 1
  90. points_x = []
  91. points_y = []
  92. else:
  93. new_scene = False
  94. if new_scene:
  95. counter = 0
  96. #print('clicks for', subject, ':', zones_clicks)
  97. print(scenes)
  98. for k, v in scenes.items():
  99. print(len(v['x']))
  100. #plt.title(k)
  101. #plt.scatter(v['x'], v['y'])
  102. #plt.show()
  103. '''points_x.sort()
  104. hmean = np.mean(points_x)
  105. hstd = np.std(points_x)
  106. pdf = stats.norm.pdf(points_x, hmean, hstd)
  107. plt.plot(points_x, pdf)
  108. plt.show()'''
  109. if __name__== "__main__":
  110. main()