0001 function [studyinfo,options] = dsSetupStudy(base_model,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 opts=dsCheckOptions(varargin,{...
0013 'modifications_set',[],[],...
0014 'simulator_options',[],[],...
0015 'process_id',[],[],...
0016 },false);
0017
0018 if isempty(opts.simulator_options)
0019 error('dsSetupStudy must be given a simulator_options structure from dsSimulate');
0020 end
0021
0022 modifications_set = opts.modifications_set;
0023 process_id = opts.process_id;
0024 options = opts.simulator_options;
0025
0026
0027 if options.verbose_flag
0028 fprintf('PREPARING STUDY:\n');
0029 end
0030
0031 if options.save_data_flag || options.save_results_flag || options.parallel_flag
0032
0033
0034
0035
0036
0037 if isempty(options.study_dir)
0038 if ~options.auto_gen_test_data_flag && ~options.unit_test_flag
0039
0040 options.study_dir=fullfile(options.project_dir,[options.prefix '_' datestr(now,'yyyymmddHHMMSS')]);
0041 else
0042 options.study_dir=fullfile(options.project_dir,[options.prefix '_unitTest']);
0043 end
0044 end
0045
0046
0047 options.study_dir = getAbsolutePath(options.study_dir);
0048
0049
0050 if ~isdir(options.study_dir)
0051 if options.verbose_flag
0052 fprintf('Creating study directory: %s\n',options.study_dir);
0053 end
0054 mkdir(options.study_dir);
0055 end
0056
0057
0058 if isempty(options.solve_file)
0059
0060 [~,fname]=fileparts(options.study_dir);
0061 fname=['solve_ode_' fname];
0062
0063
0064
0065 fname=regexprep(fname,'[^\w]','_');
0066
0067
0068 options.solve_file = fullfile(options.study_dir,'solve',[fname '.m']);
0069 end
0070
0071
0072 if ischar(options.study_dir) && isdir(options.study_dir) && exist(fullfile(options.study_dir,'studyinfo.mat'),'file')
0073
0074 studyinfo=dsCheckStudyinfo(options.study_dir,'process_id',process_id, varargin{:});
0075 orig_studyinfo=studyinfo;
0076 else
0077 orig_studyinfo=[];
0078
0079
0080 if ~isempty(base_model)
0081 warning off;
0082 studyinfo.simulations.sim_id = 1;
0083 warning on;
0084 else
0085 studyinfo = [];
0086 end
0087
0088 studyinfo=dsCheckStudyinfo(studyinfo,'process_id',process_id, varargin{:});
0089 end
0090
0091
0092 studyinfo.study_dir = options.study_dir;
0093 studyinfo.project_id = [];
0094 if isempty(studyinfo.base_model)
0095 studyinfo.base_model = base_model;
0096 end
0097
0098 if isempty(studyinfo.base_simulator_options)
0099 studyinfo.base_simulator_options = options;
0100 end
0101
0102 if isempty(studyinfo.base_solve_file)
0103 studyinfo.base_solve_file = options.solve_file;
0104 end
0105
0106 if isempty(studyinfo.base_data_files)
0107 studyinfo.base_data_files = {};
0108 end
0109
0110
0111 if ~isdir(options.study_dir)
0112 if options.verbose_flag
0113 fprintf('Creating study directory: %s\n',options.study_dir);
0114 end
0115 mkdir(options.study_dir);
0116 end
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 data_dir = fullfile(options.study_dir,'data');
0129 if ~isdir(data_dir)
0130 if options.verbose_flag
0131 fprintf('Creating data directory: %s\n',data_dir);
0132 end
0133 mkdir(data_dir);
0134 end
0135
0136
0137 if ~isempty(options.plot_functions)
0138 plot_dir = fullfile(options.study_dir,'plots');
0139 if ~isdir(plot_dir)
0140 if options.verbose_flag
0141 fprintf('Creating plot directory: %s\n',plot_dir);
0142 end
0143 mkdir(plot_dir);
0144 end
0145 end
0146
0147
0148
0149 for k = 1:length(modifications_set)
0150 if length(studyinfo.simulations)<k || isempty(studyinfo.simulations(k).status)
0151 studyinfo.simulations(k).sim_id=k;
0152 studyinfo.simulations(k).modifications=modifications_set{k};
0153 fname=[options.prefix '_sim' num2str(k) '_data.mat'];
0154 studyinfo.simulations(k).data_file=fullfile(data_dir,fname);
0155 fname=[options.prefix '_sim' num2str(k) '_model.mat'];
0156
0157 studyinfo.simulations(k).status='initialized';
0158
0159
0160 for kk = 1:length(options.analysis_functions)
0161 studyinfo.simulations(k).result_functions{end+1} = options.analysis_functions{kk};
0162 studyinfo.simulations(k).result_options{end+1} = options.analysis_options{kk};
0163
0164 fname = [options.prefix '_sim' num2str(k) '_analysis' num2str(kk) '_' func2str(options.analysis_functions{kk}) '.mat'];
0165 studyinfo.simulations(k).result_files{end+1} = fullfile(data_dir,fname);
0166 end
0167
0168
0169 for kk=1:length(options.plot_functions)
0170 studyinfo.simulations(k).result_functions{end+1}=options.plot_functions{kk};
0171 studyinfo.simulations(k).result_options{end+1}=options.plot_options{kk};
0172 fname=[options.prefix '_sim' num2str(k) '_plot' num2str(kk) '_' func2str(options.plot_functions{kk})];
0173
0174
0175 studyinfo.simulations(k).result_files{end+1}=fullfile(plot_dir,fname);
0176 end
0177
0178
0179
0180 end
0181 end
0182
0183
0184
0185
0186 if ~isequal(orig_studyinfo,studyinfo)
0187
0188 study_file=fullfile(options.study_dir,'studyinfo.mat');
0189 dsStudyinfoIO(studyinfo,study_file,process_id,options.verbose_flag);
0190 end
0191 else
0192
0193 if isempty(options.study_dir)
0194
0195 options.study_dir = pwd;
0196 end
0197
0198 if ~isdir(options.study_dir)
0199 if options.verbose_flag
0200 fprintf('Creating study directory: %s\n',options.study_dir);
0201 end
0202
0203 mkdir(options.study_dir);
0204 end
0205
0206 studyinfo=[];
0207 end