-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmT_scheduleFits.m
248 lines (191 loc) · 8.76 KB
/
mT_scheduleFits.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
function DSet = mT_scheduleFits(mode, DSet, Settings, scheduleFolder)
% INPUT
% mode str. 'cluster' schedules for the cluster without a parfor
% loop, 'clusterPar' schedules for the cluster with a parfor
% loop used on the cluster, and 'local' runs immediately
% DSet Should follow the standard data format. See README.
% Settings Structure. See README. If Settings
% is an array of such structures, a fit is scheduled for each
% strcuture. (One structure describes one model.)
% scheduleFolder
% Only used in 'cluster' mode. Specifies the folder in
% which to store schedued jobs.
% OUTPUT
% DSet Results of modelling are stored in DSet.P(i).Models, unless this
% already exists, in which case 'Models' becomes a struct array and
% the current results are placed in the first free space.
% HISTORY
% Reviewed 2020
jobsPerContainer = Settings.JobsPerContainer;
for iSet = 1 : length(Settings)
if isfield(Settings(iSet), 'ReseedRng') && (~Settings(iSet).ReseedRng)
error('Option to not reseed the random generator has been removed')
% Reseeding takes place in mT_runOnCluster
end
end
if strcmp(mode, 'local')
% Reseeding takes place in mT_runOnCluster when running on cluster
rng('shuffle')
end
% Work out where we will store the results of the modelling? This depends
% of how many different models have been applied previously.
if ~isfield(DSet.P(1), 'Models'); prevModels = 0;
else; prevModels = length(DSet.P(1).Models); end
% Check that all participants have had the same models applied previously
if isfield(DSet.P, 'Models')
mT_findAppliedModels(DSet);
end
% Save participant data for later use if running on cluster
PtpntDataSaveDir = cell(length(DSet.P), 1);
if any(strcmp(mode, {'cluster', 'clusterPar'}))
for iPtpnt = 1 : length(DSet.P)
PtpntData = DSet.P(iPtpnt).Data;
PtpntDataSaveDir{iPtpnt} = tempname(scheduleFolder);
PtpntData = mT_removeFunctionHandles(PtpntData, ...
{'FindSampleSize', 'FindIncludedTrials'});
save(PtpntDataSaveDir{iPtpnt}, 'PtpntData')
% Check the save
saveFile = dir([PtpntDataSaveDir{iPtpnt}, '.mat']);
fileSize = saveFile.bytes;
if ~(fileSize > 10000); error('Bug'); end
end
else
assert(strcmp(mode, 'local'))
end
% If we will be running on the cluster we need to store the requested
% function runs for later execution.
if any(strcmp(mode, {'cluster', 'clusterPar'}))
funNum = 1;
jobContainerCount = 1;
JobContainer = generateJobContainer(jobsPerContainer, jobContainerCount, ...
scheduleFolder, mode);
else
assert(strcmp(mode, 'local'))
end
for iModel = 1 : length(Settings)
TheseSettings = Settings(iModel);
for iPtpnt = 1 : length(DSet.P)
% Store the settings used for modelling below
DSet.P(iPtpnt).Models(prevModels + iModel).Settings = TheseSettings;
BoundaryVals = mT_setUpParamVals(TheseSettings);
BoundaryVals = rmfield(BoundaryVals, 'InitialVals');
DSet.P(iPtpnt).Models(prevModels + iModel).Settings.ParamBounds ...
= BoundaryVals;
% If requested in 'TheseSettings', run the minimisation several times from
% different start points.
for iStartPoint = 1 : TheseSettings.NumStartPoints
% Are we using the end points from previous fits as start points, or
% drawing new ones?
if ~TheseSettings.PresetStartPoints
SetupValsFun = @(Settings) mT_setUpParamVals(Settings);
elseif TheseSettings.PresetStartPoints
SetupVals = mT_setUpParamVals(TheseSettings);
SetupVals.InitialVals ...
= DSet.P(iPtpnt).Models(iModel).Fits(iStartPoint).Params;
SetupValsFun = @(Settings) SetupVals;
end
DSetSpec = DSet.Spec;
if strcmp(mode, 'local')
% If we are on the local machine we wont have saved the participant
% data for later loading, instead we need to find it now.
PtpntData = DSet.P(iPtpnt).Data;
[DSet.P(iPtpnt).Models(prevModels + iModel).Fits(iStartPoint), ...
~] = mT_findMaximumLikelihood(PtpntData, DSetSpec, ...
TheseSettings, SetupValsFun, '--');
elseif any(strcmp(mode, {'cluster', 'clusterPar'}))
JobContainer.JobSubID(funNum) = funNum;
JobContainer.FunName{funNum} = 'mT_findMaximumLikelihood';
% Save the filenames of relevant files as strings
PtpntDataSaveDir{iPtpnt} = convertCharsToStrings(PtpntDataSaveDir{iPtpnt});
[~, PtpntDataSaveFile, ~] = fileparts(PtpntDataSaveDir{iPtpnt});
JobContainer.PtpntData{funNum} = PtpntDataSaveFile;
JobContainer.DSetSpec{funNum} = DSetSpec;
JobContainer.Settings{funNum} = TheseSettings;
JobContainer.SetupValFuns{funNum} = SetupValsFun;
% Store the ID number in the corresponding location in DSet
DSet.P(iPtpnt).Models(prevModels + iModel).Fits(iStartPoint).JobContainerID ...
= JobContainer.ID;
DSet.P(iPtpnt).Models(prevModels + iModel).Fits(iStartPoint).JobSubID ...
= JobContainer.JobSubID(funNum);
funNum = funNum +1;
% Have we filled the job container?
if funNum > jobsPerContainer
% Save the JobContainer ready for execution later. First
% remove function handles in places we don't need them.
saveWithoutSomeHandles(JobContainer)
% Set up a new job container
funNum = 1;
jobContainerCount = jobContainerCount +1;
JobContainer = generateJobContainer(jobsPerContainer, ...
jobContainerCount, ...
scheduleFolder, ...
mode);
end
else
error('Bug')
end
end
disp('One participant, one model, complete')
end
end
% Save the final job container
if any(strcmp(mode, {'cluster', 'clusterPar'})) && ~(funNum == 1)
saveWithoutSomeHandles(JobContainer)
end
% If we are running in local mode then during the execution of this script
% we will have already fit all models, so we can do some extra analysis
% with these results, and find the best fit resulting from any start point.
if strcmp(mode, 'local')
DSet = mT_findBestFit(DSet);
end
% If we are running in cluster mode save DSet, as this now contains job IDs
% which link to the scheduled jobs.
if any(strcmp(mode, {'cluster', 'clusterPar'}))
now = string(datetime);
now = now{1};
now([3, 7, 15, 18]) = [];
tic
DSet = mT_removeFunctionHandles(DSet, {'FindSampleSize', 'FindIncludedTrials'});
save([scheduleFolder '/_' now '_DataStruct'], 'DSet', '-v7.3')
diff = toc;
% If saving the files has been particularly quick, wait 3 seconds to ensure
% that if this function is called again immediately, no JobContainers will
% be given the same name as an existing one (these are based on the time in
% seconds).
if diff < 3; pause(3); end
end
end
function JobContainer = generateJobContainer(jobsPerContainer, ...
jobContainerCount, scheduleFolder, mode)
% Generate a strcuture to store requested jobs
% Generate a job ID number
now = string(datetime);
now = now{1};
now([3, 7, 15, 18]) = [];
now(10)='_';
jobContainerID = [now '_' num2str(jobContainerCount)];
JobContainer.Count = jobContainerCount;
JobContainer.ID = jobContainerID;
% Where will we save the job container later?
JobContainer.SaveDir = [scheduleFolder '/' JobContainer.ID '_job.mat'];
% Check this file doesn't already exist
if isfile(JobContainer.SaveDir)
error('bug')
end
JobContainer.JobSubID = NaN(jobsPerContainer, 1);
if strcmp(mode, 'cluster')
JobContainer.UseParfor = false;
elseif strcmp(mode, 'clusterPar')
JobContainer.UseParfor = true;
else
error('Bug')
end
end
function saveWithoutSomeHandles(JobContainer)
ContainerWithHandles = JobContainer;
JobContainer = mT_removeFunctionHandles(JobContainer, ...
{'FindSampleSize', 'FindIncludedTrials'});
JobContainer.Settings = ContainerWithHandles.Settings;
JobContainer.SetupValFuns = ContainerWithHandles.SetupValFuns;
save(JobContainer.SaveDir, 'JobContainer')
end