extract_expe_info_zones.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. 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. def main():
  21. parser = argparse.ArgumentParser(description="Compute expe data into output file")
  22. parser.add_argument('--output', type=str, help="output folder expected", required=True)
  23. parser.add_argument('--n', type=int, help="number of first clicks per zone wished per user")
  24. args = parser.parse_args()
  25. p_output = args.output
  26. p_n = args.n
  27. # list all folders
  28. subjects = os.listdir(data_expe_folder)
  29. print('Number of subjects', len(subjects))
  30. # initiate list which will contains `n` first clicks (if exists) on zone for each subject on each scene
  31. scenes = {}
  32. for scene in cfg.scenes_names:
  33. zones_list = {}
  34. for zone_index in cfg.zones_indices:
  35. zones_list[zone_index] = {}
  36. # construct for each scene
  37. zones_list[zone_index]['x'] = []
  38. zones_list[zone_index]['y'] = []
  39. scenes[scene] = zones_list
  40. # for each subjects process data
  41. for index, subject in enumerate(subjects):
  42. subject_folder = os.path.join(data_expe_folder, subject)
  43. data_files = os.listdir(subject_folder)
  44. pos_file = [f for f in data_files if position_file_pattern in f][0]
  45. pos_filepath = os.path.join(subject_folder, pos_file)
  46. previous_path_scene = ""
  47. path_scene = ""
  48. new_scene = True
  49. number_of_scenes = 0
  50. scene_name = ""
  51. # open pos file and extract click information
  52. with open(pos_filepath, 'r') as f:
  53. # for each subject check `p_n` on each zone
  54. zones_filled = {}
  55. # first init
  56. for zone_index in cfg.zones_indices:
  57. zones_filled[zone_index] = 0
  58. for line in f.readlines():
  59. if click_line_pattern in line and scene_name in cfg.scenes_names:
  60. x, y = utils_functions.extract_click_coordinate(line)
  61. p_x = x - min_x
  62. p_y = y - min_y
  63. # only accept valid coordinates
  64. if utils_functions.check_coordinates(p_x, p_y):
  65. # TODO : need to reverse `y` axis for correct zone index (zone 0 on from top left)
  66. zone_index = utils_functions.get_zone_index(p_x, p_y)
  67. plt.scatter([p_x], [p_y])
  68. plt.show()
  69. time.sleep(2)
  70. # check number of points saved for this specific zone
  71. # add only if wished
  72. if zones_filled[zone_index] < p_n:
  73. scenes[scene_name][zone_index]['x'].append(p_x)
  74. scenes[scene_name][zone_index]['y'].append(p_y)
  75. zones_filled[zone_index] += 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. # reinit for each scene
  85. for zone_index in cfg.zones_indices:
  86. zones_filled[zone_index] = 0
  87. else:
  88. new_scene = False
  89. filepath = os.path.join(cfg.extracted_data_folder, p_output)
  90. if not os.path.exists(cfg.extracted_data_folder):
  91. os.makedirs(cfg.extracted_data_folder)
  92. with open(filepath, 'wb') as f:
  93. pickle.dump(scenes, f)
  94. print('Data object are saved into', filepath)
  95. if __name__== "__main__":
  96. main()