def map_fixations(rec_path = select_fixations_data()): """ This function takes the fixations on the surface csv file and the reference image as inputs, plots the fixation points onto the reference image. It also saves and prints the different fixation values. """ fixations = rec_path.groupby(by=['fixation_id', 'duration'])[['norm_pos_x', 'norm_pos_y']].mean().reset_index() # update following with the path to the image with AOIs defined. reference = cv2.imread("/home/fiza/Documents/ClarkLabProjects/SF_Immersion_Data/AOI_definedregions.png")[..., ::-1] # tells you where/what the error is: assert not isinstance(reference, type(None)), 'image not found' grid = reference.shape[0:2] # width, height of the loaded image x = fixations["norm_pos_x"] y = fixations["norm_pos_y"] # flips the fixation point from the original coordinate system, where the origin # is at the bottom left, to the image coordinate system, where the origin is at top left y = 1 - y # scale up the normalized coordinates for x and y x *= grid[1] y *= grid[0] point_scale = fixations["duration"] id_labels = list(fixations["fixation_id"]) # defining a colormap cdict = aoi_dict() # Create a colormap based on comparison of x and y position coordinates of the surface fixations in the excel # file to coordinates defined in cdict. Then these values are replaced by values from 1 - 5. # 0 is anything not categorized. c = [0] * len(x) for i, pt in enumerate(zip(x, y)): if ((pt[0] > cdict['Reels'][0][0]) and (pt[1] > cdict['Reels'][0][1]) and (pt[0] < cdict['Reels'][1][0]) and (pt[1] < cdict['Reels'][1][1])): c[i] = 1 if ((pt[0] > cdict['Credits'][0][0]) and (pt[1] > cdict['Credits'][0][1]) and (pt[0] < cdict['Credits'][1][0]) and (pt[1] < cdict['Credits'][1][1])): c[i] = 2 if ((pt[0] > cdict['Win'][0][0]) and (pt[1] > cdict['Win'][0][1]) and (pt[0] < cdict['Win'][1][0]) and (pt[1] < cdict['Win'][1][1])): c[i] = 3 fixations.to_csv('/home/fiza/Documents/ClarkLabProjects/SF_Immersion_Data/Fixdurations.csv', index=False) # Specifying colours and classes for mapping and legend. colors = ListedColormap(['black', 'blue', 'red', 'green']) classes = ["Others", "Reels", "Credits", "Win"] # display reference image plt.figure(figsize=(16, 16)) plt.imshow(reference, alpha=0.5) # use the duration to determine the scatter plot circle radius points = plt.scatter(x, y, s=point_scale * 0.05, c=c, alpha=0.2, cmap=colors) plt.legend(handles=points.legend_elements()[0], labels=classes, bbox_to_anchor=(1.08, 1.01)) plt.show() # print number of fixations as values: reels = c.count(1) credits = c.count(2) win = c.count(3) others = c.count(0) # save the above variables temporarily in a file. These values will be called in a later module and saved in # an excel file. stem_PID = str(Path(fsp.RECORDING).stem) list_ID = stem_PID.split("_") mapped_fixations = {'ID': int(list_ID[0]), 'duration': fixations['duration'], 'reels': reels, 'credit': credits, 'win': win, 'uncategorized': others} with open('/home/fiza/Documents/ClarkLabProjects/SF_Immersion_Data/Temp.p', "wb") as pickle_file: pickle.dump(mapped_fixations, pickle_file, protocol=pickle.HIGHEST_PROTOCOL)