Home > data-analysis > extraction > temporal_files > temporal_profile_save_csv_plot.m

temporal_profile_save_csv_plot

PURPOSE ^

SYNOPSIS ^

function temporal_profile_save_csv_plot(profile_data, save_dir)

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function temporal_profile_save_csv_plot(profile_data, save_dir)
0002 
0003 if nargin < 2
0004     save_dir = '.';
0005 end
0006 
0007 expr_module = 'eye_roi|inhand|naming|vision_size|joint';
0008 cevent_name = profile_data.cevent_name;
0009 var_name = profile_data.var_name;
0010 
0011 if iscell(var_name)
0012     example_var_name = var_name{1};
0013     is_var_cell = true;
0014     num_vars = length(var_name);
0015     str_var_type = get_data_type(example_var_name);
0016 else
0017     example_var_name = var_name;
0018     is_var_cell = false; % meaning the profile_data variable only has one cstream
0019     num_vars = 1;
0020     str_var_type = get_data_type(example_var_name);
0021 end
0022 
0023 str_module_cevent = regexp(cevent_name, expr_module, 'match');
0024 str_module_cevent = str_module_cevent{1};
0025 str_module_var = regexp(example_var_name, expr_module, 'match');
0026 str_module_var = str_module_var{1};
0027 
0028 time_base = profile_data.time_base;
0029 num_groupids = length(profile_data.group_list);
0030 
0031 csv_header = {'sub ID', 'exp ID', 'onset', 'offset', 'category', ...
0032      'trial ID', 'instance ID'};
0033  csv_header_group = cell(1, num_groupids);
0034  if isfield(profile_data, 'groupid_label')
0035      csv_header_group = profile_data.groupid_label;
0036  else
0037      for cgidx = 1:num_groupids
0038          csv_header_group{cgidx} = ['group#' num2str(profile_data.group_list(cgidx))];
0039      end
0040  end
0041  figure_legend = csv_header_group;
0042  length_time = length(time_base);
0043  length_profile_chunk = length_time*num_groupids;
0044 
0045 csv_len_header = length(csv_header);
0046 csv_column_profile = csv_len_header+num_groupids;
0047 
0048 csv_header_row = cell(4, length_profile_chunk+csv_column_profile);
0049 mask_fill =cellfun('isempty', csv_header_row);
0050 csv_header_row(mask_fill) = {' '};
0051 csv_header_row{1, 3} = cevent_name;
0052 
0053 csv_column_var = csv_len_header+1;
0054 if strcmp(str_var_type, 'cont')
0055     str_probs_mean = sprintf('average %s', str_module_var);
0056 elseif strcmp(str_var_type, 'cstream')
0057    str_probs_mean = sprintf('probabilities of %s', str_module_var);
0058 end
0059 
0060 if is_var_cell
0061     csv_header_row(1, csv_column_var:csv_column_var+num_vars-1) = var_name;
0062 else
0063     csv_header_row{1, csv_column_var} = var_name;
0064 end
0065 
0066 csv_header_row{3, csv_column_var} = [str_probs_mean ' per instance'];
0067 tmp_idx = csv_column_profile+1;
0068 for gidx = 1:num_groupids
0069     csv_header_row{3, tmp_idx+length_time*(gidx-1)} = ['temporal profile of ' str_probs_mean ' of ' figure_legend{gidx}];
0070 end
0071 csv_header_row(4, 1:length(csv_header)) = csv_header;
0072 csv_header_row(4, length(csv_header)+1:csv_column_profile) = csv_header_group;
0073 csv_header_row(4, csv_column_profile+1:end) = num2cell(repmat(time_base, 1, num_groupids));
0074 
0075 csv_chunks = horzcat(profile_data.profile_data_mat{:});
0076 
0077 % subID    expID    onset    offset    category    trialsID    instanceID
0078 csv_data_sub = [profile_data.sub_list profile_data.exp_list profile_data.cevents ...
0079     profile_data.cevent_trialid profile_data.cevent_instanceid ...
0080     profile_data.probs_mean_per_instance csv_chunks];
0081 
0082 result_csv_name_start = sprintf('temporal_profile_of_%s_group_by_%s', str_module_var, str_module_cevent);
0083 
0084 % cstream_results = cstream_cal_stats(csv_chunks, profile_data, 1);
0085 % cstream_count = cstream_results.temporal_count
0086 %%%% start plotting, one line per subject
0087 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0088 % start plotting
0089 % profile_data.figure_legend = {'target', 'non-target', 'face'};
0090 % profile_data.xlabel = str_module_cevent;
0091 % profile_data.ylabel = str_module_var;
0092 % profile_data.xlim = [profile_data.interval(1) profile_data.interval(2)-1];
0093 vert_line = [0];
0094 sub_list = unique(profile_data.sub_list);
0095 exp_list = unique(profile_data.exp_list);
0096 
0097 % profile_line_sub = nan(length(sub_list), length_time);
0098 
0099 colormap = {[0 0 1]; [1 0 1]; [1 0 0]; [0 1 1]; [0 1 0]; [1 1 0]; ...
0100     [0 0 0]; [0 0 0.7]; [0.7 0 0.7]; [0 0.7 0.7]; [0.7 0 0]; [0 0.7 0]};
0101 
0102 for eidx = 1:length(exp_list)
0103     exp_id = exp_list(eidx);
0104     title_str = sprintf('%s_exp%d', result_csv_name_start, exp_id);
0105     max_y = 0;
0106     h = figure('Position', [50 50 1200 900], 'Visible', 'off');
0107     hold on;
0108 
0109     for gidx = 1:num_groupids
0110 
0111         profile_mat = profile_data.profile_data_mat{gidx};
0112         mask_select = profile_data.exp_list == exp_id;
0113         profile_mat_exp = profile_mat(mask_select, :);
0114 
0115         if strcmp(str_var_type, 'cont')
0116             profile_line_plot = nanmean(profile_mat_exp, 1);
0117         else
0118             num_valid_data = sum(~isnan(profile_mat_exp), 1);
0119             num_matches = sum(profile_mat_exp > 0 & profile_mat_exp < 2, 1);
0120             profile_line_plot = num_matches ./ num_valid_data;
0121         end
0122 
0123         plot(time_base, profile_line_plot, 'Color', colormap{gidx});
0124         
0125         max_y = nanmax(nanmax(profile_line_plot), max_y);
0126     end
0127 
0128     hold off;
0129     legend(figure_legend, 'Location', 'bestoutside');
0130     title(no_underline(title_str));
0131     % xlim([profile_data.interval(1) profile_data.interval(2)-1]);
0132     ylim([0 max_y+0.005]);
0133 %
0134 %     for vidx = 1:length(vert_line)
0135 %         x = [vert_line(vidx), vert_line(vidx), vert_line(vidx)+0.01, vert_line(vidx)+0.01];
0136 %         y = [0, max_y, max_y, 0];
0137 %         color = [1 0 0];
0138 %         fill(x, y, 'r', 'EdgeColor', color);
0139 %     end
0140 
0141     saveas(h, fullfile(save_dir, [title_str '.png']));
0142     close(h);
0143 end
0144 
0145 % saving the data using cell2csv function, extremely slow...
0146 csv_save_path = fullfile(save_dir, sprintf('%s.csv', result_csv_name_start));
0147 cell2csv(csv_save_path, [csv_header_row; num2cell(csv_data_sub)]);
0148 fprintf('Profile results saved under %s\n', csv_save_path);
0149

Generated on Tue 23-May-2017 03:00:58 by m2html © 2005