extract_expe_info_zones_scenes.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # main imports
  2. import sys, os, argparse
  3. import math
  4. import numpy as np
  5. import pickle
  6. # processing imports
  7. import matplotlib.pyplot as plt
  8. import scipy.stats as stats
  9. # modules imports
  10. sys.path.insert(0, '') # trick to enable import of main folder module
  11. import custom_config as cfg
  12. import utils as utils_functions
  13. # variables
  14. data_expe_folder = cfg.data_expe_folder
  15. position_file_pattern = cfg.position_file_pattern
  16. click_line_pattern = cfg.click_line_pattern
  17. min_x = cfg.min_x_coordinate
  18. min_y = cfg.min_y_coordinate
  19. def main():
  20. parser = argparse.ArgumentParser(description="Compute expe data into output file")
  21. parser.add_argument('--output', type=str, help="output folder expected", required=True)
  22. parser.add_argument('--n', type=int, help="number of first clicks per zone wished per user")
  23. args = parser.parse_args()
  24. p_output = args.output
  25. p_n = args.n
  26. # list all folders
  27. subjects = os.listdir(data_expe_folder)
  28. print('Number of subjects', len(subjects))
  29. # initiate list which will contains `n` first clicks (if exists) on zone for each subject on each scene
  30. scenes = {}
  31. for scene in cfg.scenes_names:
  32. zones_list = {}
  33. for zone_index in cfg.zones_indices:
  34. zones_list[zone_index] = {}
  35. # construct for each scene
  36. zones_list[zone_index]['x'] = []
  37. zones_list[zone_index]['y'] = []
  38. scenes[scene] = zones_list
  39. # for each subjects process data
  40. for index, subject in enumerate(subjects):
  41. subject_folder = os.path.join(data_expe_folder, subject)
  42. data_files = os.listdir(subject_folder)
  43. pos_file = [f for f in data_files if position_file_pattern in f][0]
  44. pos_filepath = os.path.join(subject_folder, pos_file)
  45. previous_path_scene = ""
  46. path_scene = ""
  47. new_scene = True
  48. number_of_scenes = 0
  49. scene_name = ""
  50. # open pos file and extract click information
  51. with open(pos_filepath, 'r') as f:
  52. # for each subject check `p_n` on each zone
  53. zones_filled = {}
  54. # first init
  55. for zone_index in cfg.zones_indices:
  56. zones_filled[zone_index] = 0
  57. for line in f.readlines():
  58. if click_line_pattern in line and scene_name in cfg.scenes_names:
  59. x, y = utils_functions.extract_click_coordinate(line)
  60. # only accept valid coordinates
  61. if utils_functions.check_coordinates(x, y):
  62. p_x = x - min_x
  63. p_y = y - min_y
  64. zone_index = utils_functions.get_zone_index(p_x, p_y)
  65. # check number of points saved for this specific zone
  66. # add only if wished
  67. if zones_filled[zone_index] < p_n:
  68. scenes[scene_name][zone_index]['x'].append(p_x)
  69. scenes[scene_name][zone_index]['y'].append(p_y)
  70. zones_filled[zone_index] += 1
  71. elif click_line_pattern not in line:
  72. path_scene = line
  73. if previous_path_scene != path_scene:
  74. previous_path_scene = path_scene
  75. new_scene = True
  76. scene_name = path_scene.split('/')[4]
  77. if scene_name in cfg.scenes_names:
  78. number_of_scenes += 1
  79. # reinit for each scene
  80. for zone_index in cfg.zones_indices:
  81. zones_filled[zone_index] = 0
  82. else:
  83. new_scene = False
  84. filepath = os.path.join(cfg.extracted_data_folder, p_output)
  85. if not os.path.exists(cfg.extracted_data_folder):
  86. os.makedirs(cfg.extracted_data_folder)
  87. with open(filepath, 'wb') as f:
  88. pickle.dump(scenes, f)
  89. print('Data object are saved into', filepath)
  90. if __name__== "__main__":
  91. main()