Home > data-vis > generate_toy_heatmaps.m

generate_toy_heatmaps

PURPOSE ^

GENERATE_TOY_HEATMAPS Summary of this function goes here

SYNOPSIS ^

function [SUB_LIST] = generate_toy_heatmaps(CEVENT_VAR, SUBJECT, FOLDER, EXP_LIST)

DESCRIPTION ^

GENERATE_TOY_HEATMAPS Summary of this function goes here
   This functions generates spatial heatmaps of the toy objects,
   conditioned on the cevent CEVENT_VAR:
   -   Images are saved under
       /ein/multiwork/experiment_XX/included/data_vis/toy_heatmaps/
   -   A .mat file with the raw heatmap data is saved at the same place
   Function Arguments:
   -   CEVENT_VAR: string of the cevent to condition the heatmaps on
   -   SUBJECT: string saying either 'child' (default) or 'parent' --
       determines whether the heatmaps are from the child's or the
       parent's view
   -   EXP_LIST: list of experiments (i.e. 32) to run. The function will
       automatically find all subjects for which both the CEVENT and the
       toy map data exits -- default: all experiments

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [SUB_LIST] = generate_toy_heatmaps(CEVENT_VAR, SUBJECT, FOLDER, EXP_LIST)
0002 %GENERATE_TOY_HEATMAPS Summary of this function goes here
0003 %   This functions generates spatial heatmaps of the toy objects,
0004 %   conditioned on the cevent CEVENT_VAR:
0005 %   -   Images are saved under
0006 %       /ein/multiwork/experiment_XX/included/data_vis/toy_heatmaps/
0007 %   -   A .mat file with the raw heatmap data is saved at the same place
0008 %   Function Arguments:
0009 %   -   CEVENT_VAR: string of the cevent to condition the heatmaps on
0010 %   -   SUBJECT: string saying either 'child' (default) or 'parent' --
0011 %       determines whether the heatmaps are from the child's or the
0012 %       parent's view
0013 %   -   EXP_LIST: list of experiments (i.e. 32) to run. The function will
0014 %       automatically find all subjects for which both the CEVENT and the
0015 %       toy map data exits -- default: all experiments
0016 
0017 
0018 %   cevent_inhand_child = [23 28 29 32 34 35 39 41 43 44 70];
0019 %   cevent_inhand_parent = [23 29 32 34 35 39 41 43 44 70];
0020     
0021     addpath('supporting_functions');
0022 
0023     %parse input arguments
0024     if nargin < 1
0025         error('Need at least one argument (CEVENT)');
0026     end
0027     if nargin == 1
0028         SUBJECT = 'child';
0029         FOLDER = 'heatmap_1'
0030     end
0031     if nargin == 2
0032         FOLDER = 'heatmap_1'
0033     end
0034     if nargin == 3
0035         EXP_LIST = 0;
0036     end
0037         
0038     % get all subjects that have CEVENT_VAR
0039     SUB_LIST = get_subjects_with_cevent(EXP_LIST, CEVENT_VAR);
0040     
0041     % for each subject, check if a mask image exists (and store where)
0042     SUB_LIST = get_subjects_with_toy_masks(SUB_LIST, SUBJECT);
0043 
0044     % go over all subjects and create toy view heatmaps based on cevent
0045     for subj = SUB_LIST
0046 
0047         disp(['Working on subject ' num2str(subj.id) ' (This is slow as it takes about 40ms to read in each toy image mask...)']);
0048 
0049         % get cevent vatiable
0050         CEVENT = get_variable(subj.id, CEVENT_VAR);
0051 
0052         % create frame to target/distractor mapping, e.g. FRAME_MAPPING =
0053         % frame toy1 toy2 toy3
0054         % 1234  0    0    1
0055         % 1235  0    1    0
0056         FRAME_MAPPING = get_frame_to_target_mapping(subj, CEVENT);
0057 
0058         % get necessary subject infos: subj.num_trials, subj.img_path, subj.n_rows, subj.n_cols
0059         subj = get_subject_infos(subj);
0060 
0061         % initialize heatmap structs
0062         for tr = 1:subj.num_trials
0063             maps(tr).target_map = zeros(2*subj.n_rows, 2*subj.n_cols);
0064             maps(tr).dist_map = zeros(2*subj.n_rows, 2*subj.n_cols);
0065             maps(tr).num_frames = 0;
0066         end
0067 
0068         % go over all cevent frames
0069         %parfor_progress(length(FRAME_MAPPING(:,1)'));
0070         f_idx = 1;
0071         for frame = FRAME_MAPPING(:,1)'
0072 
0073             img_src = [subj.img_path '/img_' num2str(frame) '_seg.' subj.ext];  
0074             if exist(img_src, 'file')
0075                 
0076                 %parfor_progress();
0077 
0078                 % get mask image
0079                 toys = logical(imread(img_src)/255);
0080                 toys(subj.n_rows/2, :) = 0;
0081                 toys(:, subj.n_cols/2) = 0;
0082 
0083                 % crop out the right toy from the mask image and scale it back up
0084                 blue_toy_mask = imresize(toys(1:subj.n_rows/2, subj.n_cols/2+1:subj.n_cols), [2*subj.n_rows 2*subj.n_cols], 'nearest');
0085                 green_toy_mask = imresize(toys(subj.n_rows/2+1:subj.n_rows, 1:subj.n_cols/2), [2*subj.n_rows 2*subj.n_cols], 'nearest');
0086                 red_toy_mask = imresize(toys(subj.n_rows/2+1:subj.n_rows, subj.n_cols/2+1:subj.n_cols), [2*subj.n_rows 2*subj.n_cols], 'nearest');
0087 
0088                 % get current trial
0089                 tr = get_current_trial(subj, frame);
0090 
0091                 % add the toy either to the target or the distractor heat map
0092                 if FRAME_MAPPING(f_idx, 2)
0093                     maps(tr).target_map= maps(tr).target_map+ blue_toy_mask;
0094                 else
0095                     maps(tr).dist_map = maps(tr).dist_map + blue_toy_mask;
0096                 end
0097                 if FRAME_MAPPING(f_idx, 3)
0098                     maps(tr).target_map = maps(tr).target_map + green_toy_mask;
0099                 else
0100                     maps(tr).dist_map = maps(tr).dist_map + green_toy_mask;
0101                 end
0102                 if FRAME_MAPPING(f_idx, 4)
0103                     maps(tr).target_map = maps(tr).target_map + red_toy_mask;
0104                 else
0105                     maps(tr).dist_map = maps(tr).dist_map + red_toy_mask;
0106                 end
0107 
0108                 % increase frame count
0109                 maps(tr).num_frames = maps(tr).num_frames + 1;
0110             end
0111             f_idx = f_idx + 1;
0112         end
0113 
0114         draw_and_save_heatmap_image(subj, maps, FOLDER, CEVENT_VAR, SUBJECT);
0115 
0116         %parfor_progress(0);
0117     end
0118 end
0119 
0120 function SUB_LIST = get_subjects_with_cevent(EXP_LIST, CEVENT_VAR)
0121     disp(['Finding all subjects with cevent = ' CEVENT_VAR]);
0122     if EXP_LIST
0123         SUB_LIST = find_subjects(CEVENT_VAR, EXP_LIST);
0124     else
0125         SUB_LIST = find_subjects(CEVENT_VAR);
0126     end
0127     disp(['Found a total of ' num2str(length(SUB_LIST)) ' subjects.']);
0128 end
0129 
0130 function SUB_LIST_OUT = get_subjects_with_toy_masks(SUB_LIST_IN, SUBJECT)
0131     SUB_LIST_OUT = [];
0132     subj_i = 1;
0133     disp('Finding subset of subjects with toy mask.');
0134     for subj = SUB_LIST_IN'
0135 
0136         %possible folders to check
0137         if strcmp(SUBJECT, 'child')
0138             input_image_folder = {'cam01_frames_p', 'cam07_frames_p'};
0139         else
0140             input_image_folder = {'cam02_frames_p', 'cam08_frames_p'};
0141         end
0142     
0143         %possible file extensions to check
0144         file_extensions = {'png', 'jpg', 'jpeg', 'tif', 'tiff'};
0145     
0146         %check files and store info
0147         for imfo = 1:length(input_image_folder)
0148             for fiex = 1:length(file_extensions)
0149                 frame_1 = get_trials(subj);
0150                 frame_1 = num2str(frame_1(1,1));
0151                 if exist([fullfile(get_subject_dir(subj), input_image_folder{imfo}) '/img_' frame_1 '_seg.' file_extensions{fiex}], 'file')
0152                     SUB_LIST_OUT(subj_i).id = subj;
0153                     SUB_LIST_OUT(subj_i).folder = input_image_folder{imfo};
0154                     SUB_LIST_OUT(subj_i).ext = file_extensions{fiex};
0155                     subj_i = subj_i + 1;
0156                     break;
0157                 end
0158             end
0159         end
0160     end
0161     disp(['Found a total of ' num2str(length(SUB_LIST_OUT)) ' subjects.']);
0162 end
0163 
0164 function FRAME_MAPPING = get_frame_to_target_mapping(subj, CEVENT)
0165     %turn times into frame numbers
0166     CEVENT = [time2frame_num(CEVENT(:,1:2), subj.id) CEVENT(:, 3)];
0167 
0168     %get all frames with a cevent and store which toys are involved
0169     %i.e. the following two loops create a matrix of the form
0170     % frame toy1 toy2 toy3
0171     % 1234  0    0    1
0172     FRAME_MAPPING = [];
0173     for ev_idx = 1:size(CEVENT, 1)
0174         if CEVENT(ev_idx, 3) >= 0 && CEVENT(ev_idx, 3) < 4 %only care about toys (e.g. number 1 - 3), no faces (4) or other things...
0175             FRAME_MAPPING = union(FRAME_MAPPING, CEVENT(ev_idx, 1):CEVENT(ev_idx, 2));
0176         end
0177     end
0178     FRAME_MAPPING = [FRAME_MAPPING zeros(length(FRAME_MAPPING), 3)];
0179     for ev_idx = 1:size(CEVENT, 1)
0180         if CEVENT(ev_idx, 3) >= 0 && CEVENT(ev_idx, 3) < 4 %only care about toys (e.g. number 1 - 3), no faces (4) or other things...
0181             from = find(FRAME_MAPPING(:,1) == CEVENT(ev_idx, 1));
0182             to = find(FRAME_MAPPING(:,1) == CEVENT(ev_idx, 2));
0183             FRAME_MAPPING(from:to, CEVENT(ev_idx, 3)+1) = 1;
0184         end
0185     end
0186 end
0187 
0188 function subj = get_subject_infos(subj)
0189     trials = get_trials(subj.id);
0190     subj.num_trials = size(trials,1);
0191 
0192     %build image path
0193     subj.img_path = fullfile(get_subject_dir(subj.id), subj.folder);
0194 
0195     %get num/cols for maps
0196     frame_1 = num2str(trials(1,1));
0197     [subj.n_rows, subj.n_cols] = size(logical(imread([subj.img_path '/img_' frame_1 '_seg.' subj.ext])/255));
0198 end
0199 
0200 function trial = get_current_trial(subj, frame)
0201     trials = get_trials(subj.id);
0202     trial = 1;
0203     for tnum = 2:subj.num_trials
0204         if frame >= trials(tnum, 1)
0205             trial = tnum;
0206         end
0207     end
0208 end
0209 
0210 function [] = draw_and_save_heatmap_image(subj, maps, FOLDER, CEVENT_VAR, SUBJECT)
0211 
0212     %create heatmap image
0213     f = figure;
0214     for tr = 1:subj.num_trials+1
0215         % get map for current trial (or add all map up for the last heat map)
0216         if tr == subj.num_trials+1
0217             target_map = zeros(size(maps(1).target_map));
0218             dist_map = target_map;
0219             num_frames = 0; 
0220             for numtr = 1:subj.num_trials
0221                 target_map = target_map + maps(numtr).target_map;
0222                 dist_map = dist_map + maps(numtr).dist_map;
0223                 num_frames = num_frames + maps(numtr).num_frames;
0224             end
0225         else
0226             target_map = maps(tr).target_map;
0227             dist_map = maps(tr).dist_map;
0228             num_frames = maps(tr).num_frames;
0229         end
0230 
0231         % create heat map image for taget and distractor
0232         m = max(target_map(:));
0233         img_tar = ind2rgb(gray2ind(uint8(255*target_map/m), 255), jet(255));
0234         m = max(dist_map(:));
0235         img_dis = ind2rgb(gray2ind(uint8(255*dist_map/m), 255), jet(255));
0236         img = cat(2, img_tar, img_dis);
0237         img(:, 2*subj.n_cols-2:2*subj.n_cols+2, :) = 255;
0238 
0239         % plot image at the right place
0240         if tr == subj.num_trials+1
0241             subplot(3,2, [5,6]);
0242             subimage(imresize(img, 2));
0243         else
0244             subplot(3,2, tr);
0245             subimage(img);
0246         end
0247 
0248         % get number of seconds going into each heat map
0249         t_info = get_timing(subj.id);
0250         secs = round(num_frames * 1/t_info.camRate);
0251 
0252         % label things
0253         if tr == subj.num_trials+1
0254             title(['ALL TRIALS, left: target, right: distractor (' num2str(secs) ' secs)'], 'FontSize', 16);
0255         else
0256             title(['Trial ' num2str(tr) ', left: target, right: distractor (' num2str(secs) ' secs)'], 'FontSize', 14);
0257         end
0258         axis off;
0259     end
0260     mtit([num2str(subj.id) ', ' strrep(CEVENT_VAR, '_', ' ') ', child view'], 'FontSize', 16);
0261 
0262     %set up width/height ratio for saved figure
0263     set(f, 'PaperPositionMode', 'manual');
0264     set(f, 'PaperUnits', 'inches');
0265     set(f, 'PaperPosition', [0 0 10 10]);
0266 
0267     %create save directory
0268     exp_str = num2str(subj.id);
0269     save_dir = ['/bell/multiwork/experiment_' exp_str(1:2) '/included/data_vis/' FOLDER '/'];
0270     if ~exist(save_dir)
0271         mkdir(save_dir);
0272     end
0273 
0274     print(f, [save_dir num2str(subj.id) '_' CEVENT_VAR '_' lower(SUBJECT) 'view.png'], '-dpng');
0275     save([save_dir num2str(subj.id) '_' CEVENT_VAR '_' lower(SUBJECT) 'view.mat'], 'maps');
0276     close(f)
0277 end

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005