extract_expe_info_zones_scenes.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. # variables
  13. data_expe_folder = cfg.data_expe_folder
  14. position_file_pattern = cfg.position_file_pattern
  15. click_line_pattern = cfg.click_line_pattern
  16. # utils variables
  17. zone_width, zone_height = cfg.image_zone_size
  18. scene_width, scene_height = cfg.image_scene_size
  19. nb_x_parts = math.floor(scene_width / zone_width)
  20. min_x = 100
  21. min_y = 100
  22. def get_zone_index(p_x, p_y):
  23. zone_index = math.floor(p_x / zone_width) + math.floor(p_y / zone_height) * nb_x_parts
  24. return zone_index
  25. def check_coordinates(p_x, p_y):
  26. if p_x < min_x or p_y < min_y:
  27. return False
  28. if p_x >= min_x + scene_width or p_y >= min_y + scene_height:
  29. return False
  30. return True
  31. def extract_click_coordinate(line):
  32. data = line.split(' : ')[1].split(',')
  33. p_x, p_y = (int(data[0]), int(data[1]))
  34. return (p_x, p_y)
  35. def main():
  36. parser = argparse.ArgumentParser(description="Compute expe data into output file")
  37. parser.add_argument('--output', type=str, help="output folder expected", required=True)
  38. args = parser.parse_args()
  39. p_output = args.output
  40. # list all folders
  41. subjects = os.listdir(data_expe_folder)
  42. print('Number of subjects', len(subjects))
  43. # initiate list which will contains `n` first clicks (if exists) on zone for each subject on each scene
  44. scenes = {}
  45. for scene in cfg.scenes_names:
  46. zones_list = {}
  47. for zone_index in cfg.zones_indices:
  48. zones_list[zone_index] = {}
  49. # construct for each scene
  50. zones_list[zone_index]['x'] = []
  51. zones_list[zone_index]['y'] = []
  52. scenes[scene] = zones_list
  53. # for each subjects process data
  54. for index, subject in enumerate(subjects):
  55. subject_folder = os.path.join(data_expe_folder, subject)
  56. data_files = os.listdir(subject_folder)
  57. pos_file = [f for f in data_files if position_file_pattern in f][0]
  58. pos_filepath = os.path.join(subject_folder, pos_file)
  59. previous_path_scene = ""
  60. path_scene = ""
  61. new_scene = True
  62. number_of_scenes = 0
  63. counter = 0
  64. scene_name = ""
  65. # open pos file and extract click information
  66. with open(pos_filepath, 'r') as f:
  67. for line in f.readlines():
  68. if click_line_pattern in line and scene_name in cfg.scenes_names:
  69. x, y = extract_click_coordinate(line)
  70. # only accept valid coordinates
  71. if check_coordinates(x, y):
  72. p_x = x - min_x
  73. p_y = y - min_y
  74. zone_index = get_zone_index(p_x, p_y)
  75. scenes[scene_name][zone_index]['x'].append(p_x)
  76. scenes[scene_name][zone_index]['y'].append(p_y)
  77. elif click_line_pattern not in line:
  78. path_scene = line
  79. if previous_path_scene != path_scene:
  80. previous_path_scene = path_scene
  81. new_scene = True
  82. scene_name = path_scene.split('/')[4]
  83. if scene_name in cfg.scenes_names:
  84. number_of_scenes += 1
  85. else:
  86. new_scene = False
  87. if new_scene:
  88. counter = 0
  89. with open(p_output, 'wb') as f:
  90. pickle.dump(scenes, f)
  91. if __name__== "__main__":
  92. main()