Home > data-vis > generate_cont_data_heatmap.m

generate_cont_data_heatmap

PURPOSE ^

SYNOPSIS ^

function [all_gaze_maps] = generate_cont_data_heatmap(EXP_LIST, CONT_VAR, FOLDER, EVENT_VAR, TARGET_RESOLUTION)

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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

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