0001 function result = dsAnalyze(src,funcIn,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055 if ~nargin
0056 output = localfunctions;
0057 return
0058 end
0059
0060
0061 options=dsCheckOptions(varargin,{...
0062 'result_file','result',[],...
0063 'save_results_flag',0,{0,1},...
0064 'format','svg',{'svg','jpg','eps','png','fig'},...
0065 'varied_filename_flag',0,{0,1},...
0066 'plot_type','waveform',{'waveform','rastergram','raster','power','rates','imagesc','heatmapFR','heatmap_sortedFR','meanFR','meanFRdens','FRpanel'},...
0067 'save_prefix',[],[],...
0068 'function_options',{},[],...
0069 'simIDs',[],[],...
0070 'load_all_data_flag',0,{0,1},...
0071 'auto_gen_test_data_flag',0,{0,1},...
0072 'unit_test_flag',0,{0,1},...
0073 'parallel_flag',0,{0,1},...
0074 'auto_gen_test_data_flag',0,{0,1},...
0075 },false);
0076
0077
0078 if options.auto_gen_test_data_flag
0079 varargs = varargin;
0080 varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0081 varargs(end+1:end+2) = {'unit_test_flag',1};
0082 argin = [{src},{funcIn}, varargs];
0083 end
0084
0085
0086
0087 if nargout<1
0088 options.save_results_flag = 1;
0089 end
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 [data, studyinfo] = parseSrc(src, options, varargin{:});
0104
0105
0106
0107
0108
0109 if isempty(studyinfo)
0110 studyinfoBool = false;
0111 else
0112 studyinfoBool = true;
0113 if ~isfield(studyinfo,'study_dir') || isempty(studyinfo.study_dir) || ~isdir(studyinfo.study_dir)
0114 studyinfo.study_dir = pwd;
0115 end
0116 end
0117
0118
0119
0120
0121
0122
0123 for j = 1:length(data)
0124 for k = 1:length(data(j).labels)
0125 fld = data(j).labels{k};
0126 data(j).(fld) = double(data(j).(fld));
0127 end
0128 end
0129
0130
0131 [funcIn, nFunc] = parseFuncIn(funcIn);
0132
0133
0134 postSimBool = studyinfoBool || (length(data) > 1);
0135
0136 for fInd = 1:nFunc
0137 func = funcIn{fInd};
0138
0139
0140 func = parseFunc(func);
0141
0142
0143 plotFnBool = ~isempty(regexpi(func2str(func), 'plot'));
0144
0145
0146 if options.varied_filename_flag && isfield(data, 'varied')
0147 options.result_file = filenameFromVaried(options.result_file, func, data, plotFnBool, options, varargin{:});
0148 end
0149
0150
0151 fprintf('\tExecuting post-processing function: %s\n',func2str(func));
0152 tstart = tic;
0153
0154
0155 if length(data)==1 || postSimBool
0156 result = evalFnWithArgs(fInd, data, func, options, varargin{:});
0157 else
0158 result = [];
0159 end
0160
0161
0162
0163
0164 if ~isempty(result)
0165 nResults = length(result);
0166 elseif ~isempty(data)
0167 nResults = length(data);
0168 else
0169 nResults = length(siminfo.simulations);
0170 end
0171
0172 fprintf('\t\tElapsed time: %g sec\n',toc(tstart));
0173
0174
0175
0176
0177 if isstruct(result)
0178 if isfield(result,'hcurr')
0179 result = result.hcurr;
0180 end
0181 end
0182
0183
0184 if all(ishandle(result)) || plotFnBool
0185
0186
0187
0188 if options.save_results_flag
0189
0190 for iResult = 1:nResults
0191 extension = ['.' options.format];
0192
0193 if ~postSimBool
0194 fname = [options.result_file extension];
0195 fPath = fname;
0196
0197 thisResult = result(iResult);
0198 elseif studyinfoBool
0199 simID = studyinfo.simulations(iResult).sim_id;
0200 prefix = func2str(func);
0201 fname = [prefix '_sim' num2str(simID) '_plot' num2str(fInd) '_' func2str(func)];
0202
0203 if (postSimBool && ~options.load_all_data_flag)
0204 data = loadDataFromSingleSim(simID, options, varargin);
0205
0206
0207 if isempty(data)
0208 continue
0209 end
0210
0211
0212 thisResult = evalFnWithArgs(fInd, data, func, options, varargin);
0213 end
0214
0215
0216 if options.varied_filename_flag && isfield(data, 'varied')
0217 fname = filenameFromVaried(fname, func, data, plotFnBool, options, varargin{:});
0218 end
0219
0220
0221 fDir = fullfile(studyinfo.study_dir, 'postSimPlots');
0222 if ~exist(fDir,'dir')
0223 mkdir(fDir)
0224 end
0225 fPath = fullfile(fDir,fname);
0226 else
0227 fname = [options.result_file '_page' num2str(iResult) extension];
0228
0229
0230 fDir = fullfile(studyinfo.study_dir, 'postSimPlots');
0231 if ~exist(fDir,'dir')
0232 mkdir(fDir)
0233 end
0234 fPath = fullfile(fDir,fname);
0235
0236 thisResult = result(iResult);
0237 end
0238
0239
0240
0241
0242 set(thisResult, 'PaperPositionMode','auto');
0243 fprintf('\t\tSaving plot: %s\n',fname);
0244
0245 switch extension
0246 case '.svg'
0247 plot2svg(fPath,thisResult);
0248 case '.jpg'
0249 print(thisResult,fPath,'-djpeg');
0250 case '.eps'
0251 print(thisResult,fPath,'-depsc');
0252 case '.png'
0253 print(thisResult,fPath,'-dpng');
0254 case '.fig'
0255 savefig(thisResult,fPath);
0256 end
0257
0258 if nResults > 1
0259 close(thisResult)
0260 end
0261 end
0262 end
0263 else
0264
0265 if isstruct(result)
0266 result = add_modifications(result, data, varargin{:});
0267
0268 for iResult = 1:length(result)
0269
0270 if length(varargin)>1
0271 for j = 1:2:length(varargin)
0272 result(iResult).options.(varargin{j}) = varargin{j+1};
0273 end
0274 else
0275 result(iResult).options = [];
0276 end
0277 end
0278 end
0279
0280
0281 if options.save_results_flag
0282 if studyinfoBool
0283 allResults = result;
0284 clear result;
0285
0286 for iResult = 1:nResults
0287 simID = studyinfo.simulations(iResult).sim_id;
0288
0289 if options.load_all_data_flag
0290 result = allResults(iResult);
0291 else
0292 data = loadDataFromSingleSim(simID, options, varargin);
0293
0294
0295 if isempty(data)
0296 continue
0297 end
0298
0299
0300 result = evalFnWithArgs(fInd, data, func, options, varargin);
0301 end
0302
0303 prefix = func2str(func);
0304 fname = [prefix '_sim' num2str(simID) '_analysis' num2str(fInd) '_' func2str(func) '.mat'];
0305
0306
0307 if options.varied_filename_flag && isfield(data, 'varied')
0308 fname = filenameFromVaried(fname, func, data, plotFnBool, options, varargin{:});
0309 end
0310
0311
0312 fDir = fullfile(studyinfo.study_dir, 'analyzedData');
0313 if ~exist(fDir,'dir')
0314 mkdir(fDir)
0315 end
0316 fPath = fullfile(fDir,fname);
0317
0318 fprintf('\t\tSaving derived data: %s\n', fPath);
0319 save(fPath,'result','-v7.3');
0320 end
0321 else
0322 fname = options.result_file;
0323 extension = '.mat';
0324
0325 if ~strcmp(fname(end-3:end), extension)
0326 fname = [fname extension];
0327 end
0328
0329 fprintf('\t\tSaving derived data: %s\n', fname);
0330 save(fname,'result','-v7.3');
0331 end
0332 end
0333 end
0334 end
0335
0336
0337 if options.auto_gen_test_data_flag
0338 argout = {result};
0339
0340
0341 end
0342
0343 end
0344
0345
0346
0347
0348
0349
0350 function [data, studyinfo] = parseSrc(src, options, varargin)
0351
0352
0353 options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0354 if options.auto_gen_test_data_flag
0355 varargs = varargin;
0356 varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0357 varargs(end+1:end+2) = {'unit_test_flag',1};
0358 argin = [{src},{options}, varargs];
0359 end
0360
0361
0362 if isstruct(src) && isfield(src,'time')
0363 data = src;
0364 studyinfo = [];
0365 elseif ischar(src)
0366 if options.load_all_data_flag
0367 [data,studyinfo] = dsImport(src, varargin{:});
0368 else
0369 data = [];
0370 studyinfo = dsCheckStudyinfo(src);
0371 end
0372
0373
0374 if exist(src, 'file') && ~isempty(strfind(src, 'studyinfo'))
0375 studyinfo.study_dir = fileparts2(src);
0376 elseif isdir(src)
0377 studyinfo.study_dir = src;
0378 end
0379 end
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426 if options.auto_gen_test_data_flag
0427 argout = {data, studyinfo};
0428
0429
0430 end
0431
0432 end
0433
0434
0435 function [funcIn, nFunc] = parseFuncIn(funcIn)
0436
0437
0438 if isa(funcIn,'function_handle')
0439 nFunc = 1;
0440 funcIn = {funcIn};
0441 elseif ischar(funcIn)
0442 funcIn = {str2func(funcIn)};
0443
0444 if ~isfunction(funcIn)
0445 error('Post-processing function must be supplied as a function handle or function name string');
0446 end
0447
0448 nFunc = 1;
0449 else
0450 nFunc = numel(funcIn);
0451 end
0452 end
0453
0454
0455 function func = parseFunc(func)
0456 if ~isa(func,'function_handle')
0457 if ischar(func)
0458 func = str2func(func);
0459
0460 if ~isfunction(func)
0461 error('Post-processing function must be supplied as a function handle or function name string');
0462 end
0463 else
0464 error('Post-processing function must be supplied as a function handle or function name string');
0465 end
0466 end
0467 end
0468
0469
0470 function filename = filenameFromVaried(filename, func, data, plotFnBool, options, varargin)
0471
0472
0473
0474
0475 options = catstruct(options, dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false));
0476
0477 if options.auto_gen_test_data_flag
0478 varargs = varargin;
0479 varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0480 varargs(end+1:end+2) = {'unit_test_flag',1};
0481 argin = [{filename}, {func}, {data}, {plotFnBool}, {options}, varargs];
0482 end
0483
0484 if isfield(options, 'save_prefix') && ~isempty(options.save_prefix)
0485 prefix = options.save_prefix;
0486 else
0487 if plotFnBool
0488 if isempty(options.function_options)
0489 plot_options = options;
0490 else
0491 plot_options = options.function_options{fInd};
0492 end
0493
0494 fInd = regexp(options.result_file, 'plot(\d+)', 'tokens');
0495 fInd = fInd{1}{1};
0496
0497
0498 plot_options = dsCheckOptions(plot_options,{'plot_type',['plot' fInd],[]},false);
0499
0500 prefix = plot_options.plot_type;
0501 else
0502 prefix = func2str(func);
0503 end
0504 end
0505
0506 filename = dsNameFromVaried(data, prefix, filename);
0507
0508
0509 if options.auto_gen_test_data_flag
0510 argout = {filename};
0511
0512
0513 end
0514
0515 end
0516
0517
0518 function result = evalFnWithArgs(fInd, data, func, options, varargin)
0519
0520 if strcmp(reportUI,'matlab')
0521 p = gcp('nocreate');
0522 end
0523
0524 if isempty(options.function_options)
0525 if options.parallel_flag && ~isempty(p)
0526 parfor dInd = 1:length(data)
0527 result(dInd) = feval(func,data(dInd),varargin{:});
0528 end
0529 else
0530 for dInd = 1:length(data)
0531 result(dInd) = feval(func,data(dInd),varargin{:});
0532 end
0533 end
0534 else
0535 function_options = options.function_options{fInd};
0536
0537 if options.parallel_flag && ~isempty(p)
0538 parfor dInd = 1:length(data)
0539 result(dInd) = feval(func,data(dInd),function_options{:});
0540 end
0541 else
0542 for dInd = 1:length(data)
0543 result(dInd) = feval(func,data(dInd),function_options{:});
0544 end
0545 end
0546 end
0547 end
0548
0549
0550 function data = loadDataFromSingleSim(simID, options, varargin)
0551
0552 if isempty(options.simIDs)
0553 simIDs = simID;
0554 else
0555 simIDs = intersect(simID, options.simIDs);
0556
0557 if isempty(simIDs)
0558 return
0559 end
0560 end
0561
0562 varinputs = varargin;
0563 if isstruct(varinputs{1})
0564 varinputs.simIDs = simIDs;
0565 else
0566
0567 varinputs{find(~cellfun(@isempty,strfind(varinputs(1:2:end), 'simIDs')))*2} = simIDs;
0568 end
0569
0570 data = ImportData(src, varinputs{:});
0571 end
0572
0573
0574 function result = add_modifications(result, data, varargin)
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585 options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0586 if options.auto_gen_test_data_flag
0587 varargs = varargin;
0588 varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0589 varargs(end+1:end+2) = {'unit_test_flag',1};
0590 argin = [{result}, {data}, varargs];
0591 end
0592
0593
0594
0595 if ~isempty(data(1).simulator_options.modifications)
0596 varied = {};
0597 mods = data(1).simulator_options.modifications;
0598 for ii = 1:length(result)
0599 for jj = 1:size(mods,1)
0600
0601 fld = [mods{jj,1} '_' mods{jj,2}];
0602
0603
0604 fld = regexprep(fld,'(->)|(<-)|(-)|(\.)','_');
0605
0606
0607 fld = regexprep(fld,'[\[\]\(\)\{\}]','');
0608 result(ii).(fld) = mods{jj,3};
0609 varied{end+1} = fld;
0610 end
0611 result(ii).varied = varied;
0612 result(ii).modifications = mods;
0613 end
0614 elseif isfield(data,'varied') && length(data) == 1
0615
0616 for ii = 1:length(result)
0617 result(ii).varied = data(1).varied;
0618 for jj = 1:length(data(1).varied)
0619 result(ii).(data(1).varied{jj}) = data(1).(data(1).varied{jj});
0620 end
0621 end
0622 end
0623
0624
0625 if options.auto_gen_test_data_flag
0626 argout = {result};
0627
0628
0629 end
0630 end