Home > data-vis > generate_gaze_data_heatmap.m

generate_gaze_data_heatmap

PURPOSE ^

SYNOPSIS ^

function [gaze_data] = generate_gaze_data_heatmap(EXP_LIST, SUBJECT, FOLDER, EVENT_VAR, NEGATE_EVENT, TARGET_RESOLUTION)

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [gaze_data] = generate_gaze_data_heatmap(EXP_LIST, SUBJECT, FOLDER, EVENT_VAR, NEGATE_EVENT, TARGET_RESOLUTION)
0002 
0003     addpath('supporting_functions');
0004 
0005     if nargin < 4
0006         EVENT_VAR = 'no_event';
0007     end
0008 
0009     if nargin < 5
0010         NEGATE_EVENT = false;
0011     end
0012     
0013     if nargin < 6
0014         TARGET_RESOLUTION = [480 640];
0015     end
0016 
0017     % get all subjects that have EVENT/CEVENT variable
0018     SUB_LIST = get_subjects_with_event(EXP_LIST, EVENT_VAR);
0019     
0020     % for each subject, check if it has the GAZE DATA variable
0021     GAZE_VAR = ['cont2_eye_xy_' SUBJECT];
0022     SUB_LIST = get_subjects_with_cont_data(SUB_LIST, GAZE_VAR);
0023     
0024     sc = 1;
0025     for SUBJ = SUB_LIST
0026         disp(['Working on subject ' num2str(SUBJ) ' (' num2str(sc) '/' num2str(length(SUB_LIST)) ')']);
0027         
0028         % check if gaze resolution needs to be adjusted
0029         frame_res = get_frame_resolution_for_subject(SUBJ, GAZE_VAR);
0030         res_fac = [1 1];
0031         if ~all(frame_res == TARGET_RESOLUTION)
0032             disp('Eye gaze resolution for this subject DOES NOT fit target resolution. Eye gaze will be resized.');
0033             res_fac = TARGET_RESOLUTION ./ frame_res;
0034         end
0035         
0036         % get gaze data
0037         gaze = get_variable_by_trial(SUBJ, ['cont2_eye_xy_' SUBJECT]);
0038         num_trials = length(gaze);
0039         
0040         % get event data (if required)
0041         if ~strcmp(EVENT_VAR, 'no_event')
0042             event = get_variable_by_trial(SUBJ, EVENT_VAR);
0043 
0044             % negate event
0045             if NEGATE_EVENT
0046                 trial_times = get_trial_times(SUBJ);
0047                 for t = 1:num_trials
0048                     event{t} = event_NOT(event{t}, trial_times(t, :));
0049                 end
0050             end
0051         end
0052 
0053         % pre allocate heat map for all n trials
0054         gaze_data = struct('num_frames', {}, 'gaze_map', {});
0055         
0056         % go over each trial
0057         for tr = 1:num_trials
0058             if ~strcmp(EVENT_VAR, 'no_event')
0059                 ev = event{tr};
0060             end
0061             ga = gaze{tr};
0062             
0063             % get rid of NaNs
0064             nan_i = isnan(ga(:,1)) | isnan(ga(:,2));
0065             ga(nan_i, :) = [];
0066             
0067             % scale to target resolution
0068             res_fac_m = repmat(res_fac, size(ga,1), 1);
0069             ga(:,2:3) = ga(:,2:3) .* res_fac_m;
0070             
0071             % translate such that trial mean is centered
0072             t = [TARGET_RESOLUTION(1)/2-mean(ga(:,3)) TARGET_RESOLUTION(2)/2-mean(ga(:,2))];
0073             ga(:,3) = ga(:,3) + t(1);
0074             ga(:,2) = ga(:,2) + t(2);
0075             
0076             % only use gaze during event (if required)
0077             if ~strcmp(EVENT_VAR, 'no_event')
0078                 event_i = false(size(ga, 1), 1);
0079                 for e = 1:size(ev, 1)
0080                    event_i(ga(:,1) >= ev(e,1) & ga(:,1) <= ev(e,2)) = 1;
0081                 end
0082                 ga = ga(event_i, :);
0083             end
0084             
0085             % turn list of 2d points into map
0086             map = gaze_points_to_2d_distribution(ga, TARGET_RESOLUTION);
0087 
0088             % store data
0089             gaze_data(tr).num_frames = size(ga, 1);
0090             gaze_data(tr).gaze_map = map; 
0091         end
0092 
0093         draw_and_save_heatmap_image(SUBJ, gaze_data, FOLDER, EVENT_VAR, SUBJECT, num_trials, TARGET_RESOLUTION, NEGATE_EVENT);
0094         
0095         sc = sc + 1;
0096     end
0097     
0098     %visualize_overall_map(all_gaze_maps, EVENT_VAR, TARGET_RESOLUTION);
0099     
0100 end
0101 
0102 function [] = visualize_overall_map(all_gaze_maps, EVENT_VAR, TARGET_RESOLUTION)
0103     % add up all heat maps
0104     master_map.no_event = zeros(TARGET_RESOLUTION);
0105     for s = 1:length(all_gaze_maps)
0106         master_map.no_event = master_map.no_event + all_gaze_maps(s).no_event;
0107     end
0108     if ~strcmp(EVENT_VAR, 'no_event')
0109         master_map.event = zeros(TARGET_RESOLUTION);
0110         for s = 1:length(all_gaze_maps)
0111             master_map.event = master_map.event + all_gaze_maps(s).event;
0112         end
0113     end
0114     
0115 
0116     if strcmp(EVENT_VAR, 'no_event')
0117         imshow(turn_into_heatmap_img(master_map.no_event));
0118         title(['Gaze map for ' num2str(length(all_gaze_maps)) ' subjects)']);
0119     else
0120         subplot(1,2,1);
0121         %imagesc(master_map.event);
0122         imshow(turn_into_heatmap_img(master_map.event));
0123         title(['Gaze map (' num2str(length(all_gaze_maps)) ' subjects) during ' strrep(EVENT_VAR, '_', '-')]);
0124         subplot(1,2,2);
0125         %imagesc(master_map.no_event);
0126         imshow(turn_into_heatmap_img(master_map.no_event));
0127         title(['Gaze map (' num2str(length(all_gaze_maps)) ' subjects) otherwise']);
0128     end
0129 end
0130 
0131 function img = turn_into_heatmap_img(dist)
0132     
0133     % first blurr
0134     H = fspecial('gaussian', [20 20], 5);
0135     dist = imfilter(dist, H);
0136     
0137     m = max(dist(:));
0138     img = ind2rgb(gray2ind(uint8(255*dist/m), 255), jet(255));
0139 end
0140 
0141 function map = gaze_points_to_2d_distribution(points, target_resolution)
0142     map = zeros(target_resolution);
0143     for g = 1:length(points)
0144         r = round(points(g, 3));
0145         c = round(points(g, 2));
0146         r = min(max(1,r), target_resolution(1));
0147         c = min(max(1,c), target_resolution(2));
0148         map(r,c) = map(r,c) + 1;
0149     end
0150 end
0151 
0152 function frame_res = get_frame_resolution_for_subject(SUBJ, CONT_VAR)
0153     
0154     subj_dir = get_subject_dir(SUBJ);
0155     
0156     child_or_parent = strsplit(CONT_VAR);
0157     child_or_parent = child_or_parent{end};
0158     
0159     if strcmp(child_or_parent, 'child')
0160         jpg_folder = fullfile(subj_dir, 'cam07_frames_p');   % cam07 = child, cam08 = parent
0161     else
0162         jpg_folder = fullfile(subj_dir, 'cam07_frames_p');
0163     end
0164     %jpg_list = dir(fullfile(jpg_folder, 'img_*.jpg'));  % hard coding
0165     
0166     trials = get_trials(SUBJ);
0167     for i = 1:trials(1,2)
0168         im_src = fullfile(jpg_folder, ['img_' num2str(i) '.jpg']);
0169         if exist(im_src, 'file') == 2
0170             img = imread(im_src);
0171             break;
0172         end
0173     end
0174     
0175     frame_res = size(img(:,:,1));
0176 end
0177 
0178 function SUB_LIST = get_subjects_with_event(EXP_LIST, EVENT)
0179 
0180     if size(EXP_LIST, 2) == 1
0181         EXP_LIST = EXP_LIST';
0182     end
0183 
0184     if strcmp(EVENT, 'no_event')
0185         if length(num2str(EXP_LIST(1))) == 4
0186             SUB_LIST = EXP_LIST;
0187         else
0188             SUB_LIST = list_subjects(EXP_LIST);
0189         end
0190     else
0191         if length(num2str(EXP_LIST(1))) == 4
0192             SUB_LIST = [];
0193             for s = EXP_LIST
0194                if has_variable(s, EVENT)
0195                    SUB_LIST = [SUB_LIST s];
0196                end
0197             end
0198         else
0199             SUB_LIST = find_subjects(EVENT, EXP_LIST);
0200         end
0201     end
0202 
0203     disp(['Found a total of ' num2str(length(SUB_LIST)) ' subjects with EVENT = ' num2str(EVENT)]);
0204 end
0205 
0206 function [SUB_LIST_OUT] = get_subjects_with_cont_data(SUB_LIST_IN, CONT_VAR)
0207     SUB_LIST_OUT = [];
0208     
0209     if size(SUB_LIST_IN, 2) == 1
0210         SUB_LIST_IN = SUB_LIST_IN';
0211     end
0212         
0213     for s = SUB_LIST_IN
0214         if has_variable(s, CONT_VAR)
0215             SUB_LIST_OUT = [SUB_LIST_OUT s];
0216         end
0217     end 
0218     
0219     disp(['Of those, found a total of ' num2str(length(SUB_LIST_OUT)) ' subjects with CONT VAR = ' CONT_VAR]);    
0220 end
0221 
0222 function subj = get_subject_infos(subj)
0223     trials = get_trials(subj.id);
0224     subj.num_trials = size(trials,1);
0225 
0226     %build image path
0227     subj.img_path = fullfile(get_subject_dir(subj.id), subj.folder);
0228 
0229     %get num/cols for maps
0230     frame_1 = num2str(trials(1,1));
0231     [subj.n_rows, subj.n_cols] = size(logical(imread([subj.img_path '/img_' frame_1 '_seg.' subj.ext])/255));
0232 end
0233 
0234 %function [] = draw_and_save_heatmap_image(subj, maps, FOLDER, CEVENT_VAR, SUBJECT)
0235 function [] = draw_and_save_heatmap_image(SUBJ, gaze_data, FOLDER, EVENT_VAR, SUBJECT, num_trials, TARGET_RESOLUTION, NEGATE_EVENT)
0236 
0237     %create heatmap image
0238     f = figure;
0239     for tr = 1:num_trials+1
0240         % get map for current trial (or add all maps up for the last heat map)
0241         if tr == num_trials+1
0242             map = zeros(TARGET_RESOLUTION(1), TARGET_RESOLUTION(2));
0243             num_frames = 0; 
0244             for numtr = 1:num_trials
0245                 map = map + gaze_data(numtr).gaze_map;
0246                 num_frames = num_frames + gaze_data(numtr).num_frames;
0247             end
0248         else
0249             map = gaze_data(tr).gaze_map;
0250             num_frames = gaze_data(tr).num_frames;
0251         end
0252 
0253         % create heat map image for taget and distractor
0254         img = turn_into_heatmap_img(map);
0255 
0256         % plot image at the right place
0257         if tr == num_trials+1
0258             subplot(3,2, [5,6]);
0259             subimage(imresize(img, 2));
0260         else
0261             subplot(3,2, tr);
0262             subimage(img);
0263         end
0264 
0265         % get number of seconds going into each heat map
0266         t_info = get_timing(SUBJ);
0267         secs = round(num_frames * 1/t_info.camRate);
0268 
0269         % label things
0270         if tr == num_trials+1
0271             title(['ALL TRIALS (' num2str(secs) ' secs)'], 'FontSize', 16);
0272         else
0273             title(['Trial ' num2str(tr) ' (' num2str(secs) ' secs)'], 'FontSize', 14);
0274         end
0275         axis off;
0276     end
0277     if NEGATE_EVENT
0278         neg = ' NOT';
0279     else
0280         neg = '';
0281     end
0282     mtit([num2str(SUBJ) ', ' strrep(EVENT_VAR, '_', ' ') neg ', ' SUBJECT ' view'], 'FontSize', 16, 'xoff', 0, 'yoff', 0.03, 'zoff', 0);
0283 
0284     %set up width/height ratio for saved figure
0285     set(f, 'PaperPositionMode', 'manual');
0286     set(f, 'PaperUnits', 'inches');
0287     set(f, 'PaperPosition', [0 0 10 10]);
0288 
0289     %create save directory
0290     exp_str = num2str(SUBJ);
0291     save_dir = ['/bell/multiwork/experiment_' exp_str(1:2) '/included/data_vis/' FOLDER '/'];
0292     if ~exist(save_dir)
0293         mkdir(save_dir);
0294     end
0295 
0296     if NEGATE_EVENT
0297         neg = 'NOT_';
0298     else
0299         neg = '';
0300     end
0301     print(f, [save_dir num2str(SUBJ) '_eyegaze_' EVENT_VAR '_' neg lower(SUBJECT) 'view.png'], '-dpng');
0302     save([save_dir num2str(SUBJ) '_eyegaze_' EVENT_VAR '_' neg lower(SUBJECT) 'view.mat'], 'gaze_data');
0303 end

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005