extract_expe_info_scenes.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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('--n', type=int, help="`n` first clicks", required=True)
  22. parser.add_argument('--output', type=str, help="output folder expected", required=True)
  23. args = parser.parse_args()
  24. p_n = args.n
  25. p_output = args.output
  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. scenes[scene] = {}
  33. # construct for each scene
  34. scenes[scene]['x'] = []
  35. scenes[scene]['y'] = []
  36. for index, subject in enumerate(subjects):
  37. subject_folder = os.path.join(data_expe_folder, subject)
  38. data_files = os.listdir(subject_folder)
  39. pos_file = [f for f in data_files if position_file_pattern in f][0]
  40. pos_filepath = os.path.join(subject_folder, pos_file)
  41. previous_path_scene = ""
  42. path_scene = ""
  43. new_scene = True
  44. number_of_scenes = 0
  45. counter = 0
  46. scene_name = ""
  47. # open pos file and extract click information
  48. with open(pos_filepath, 'r') as f:
  49. points_x = []
  50. points_y = []
  51. for line in f.readlines():
  52. if click_line_pattern in line and scene_name in cfg.scenes_names:
  53. x, y = utils_functions.extract_click_coordinate(line)
  54. p_x = x - min_x
  55. p_y = y - min_y
  56. # only accept valid coordinates
  57. if utils_functions.check_coordinates(p_x, p_y):
  58. if counter < p_n:
  59. scenes[scene_name]['x'].append(p_x)
  60. scenes[scene_name]['y'].append(p_y)
  61. counter += 1
  62. elif click_line_pattern not in line:
  63. path_scene = line
  64. if previous_path_scene != path_scene:
  65. if previous_path_scene != "":
  66. pass
  67. #plt.title(subject + ' - ' + scene_name)
  68. #plt.scatter(points_x, points_y)
  69. #plt.show()
  70. previous_path_scene = path_scene
  71. new_scene = True
  72. scene_name = path_scene.split('/')[4]
  73. if scene_name in cfg.scenes_names:
  74. number_of_scenes += 1
  75. else:
  76. new_scene = False
  77. if new_scene:
  78. counter = 0
  79. filepath = os.path.join(cfg.extracted_data_folder, p_output)
  80. if not os.path.exists(cfg.extracted_data_folder):
  81. os.makedirs(cfg.extracted_data_folder)
  82. # save information about scenes
  83. with open(filepath, 'wb') as f:
  84. pickle.dump(scenes, f)
  85. print('Data object are saved into', filepath)
  86. if __name__== "__main__":
  87. main()