-
Notifications
You must be signed in to change notification settings - Fork 12
/
Experiment.m
564 lines (489 loc) · 22.5 KB
/
Experiment.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
classdef Experiment < handle
%EXPERIMENT Abstract class for defining experiments
% Detailed explanation goes here
properties (SetAccess = protected, GetAccess = public)
ExperimentParams
end
properties (SetAccess = private, GetAccess = public)
CaseFolder
ExperimentName
GlobalDataFolder = 'data/';
NTrials
ResultsFolder
end
properties (Access = private)
RandStreamTrials
RandStreamExperiment
tStruct = struct('confoundingVariables',[],'treatmentVariables',[],'outputs',[]); % Model for trial structure; could be another class
completedTrialsCacheIsDirty = true
completedTrials % Cache completed trials
end
properties (Abstract, SetAccess = protected, GetAccess = public)
Metrics
end
properties (Dependent)
CompletedTrials % completed trials, read cached or load from disk.
end
methods
% Constructor: experimentName and globalDataFolder as path to data
% directory are required. A caseName is optional to specify a
% particular case of this experiment.
function obj = Experiment(experimentName,args)
if (nargin < 2)
args = struct;
end
obj.ExperimentName = experimentName;
% Set up directory structure
if ~isfield(args,'caseName')
obj.ResultsFolder = sprintf('%sexperiments/outputs/%s/',obj.GlobalDataFolder,obj.ExperimentName);
obj.CaseFolder = sprintf('%sexperiments/inputs/%s/',obj.GlobalDataFolder,obj.ExperimentName);
else
caseName = args.caseName;
obj.ResultsFolder = sprintf('%sexperiments/outputs/%s/%s/',obj.GlobalDataFolder,obj.ExperimentName,caseName);
obj.CaseFolder = sprintf('%sexperiments/inputs/%s/%s/',obj.GlobalDataFolder,obj.ExperimentName,caseName);
end
if ~exist(obj.ResultsFolder,'dir')
mkdir(obj.ResultsFolder);
end
if ~exist(sprintf('%strials/',obj.ResultsFolder),'dir')
mkdir(sprintf('%strials/',obj.ResultsFolder));
end
% Get the number of trials to run
p = readKeyValue(sprintf('%skey_values.csv',obj.CaseFolder));
obj.NTrials = p.N_trials;
% Set up the random number generation and streams for each
% trial
seed = p.seed;
streams = RandStream.create('mrg32k3a','Seed',seed,'NumStreams',obj.NTrials+1,'CellOutput',true);
obj.RandStreamExperiment = streams{1}; % First one is for experiment setup
obj.RandStreamTrials = streams(2:end); % Second is for each trial
% Remove those properties and set the rest as experiment
% parameters
p = rmfield(p,'N_trials');
p = rmfield(p,'seed');
obj.ExperimentParams = p;
% Seed rng for setting up additional parameters
RandStream.setGlobalStream(obj.RandStreamExperiment);
if (isfield(args,'setupArgs'))
setupArgs = args.setupArgs;
else
setupArgs = struct;
end
obj.setupAdditionalParameters(setupArgs);
end
% Run the experiment either with trials in parallel or series
function trials = runExperiment(obj,order)
if nargin < 2
order = 'par';
end
switch lower(order)
case 'par'
trials = obj.runTrialsPar(true);
case 'ser'
trials = obj.runTrialsSerFrom();
otherwise
error('Unrecognized execution order');
end
end
function trials = runTrialsSerFrom(obj,startTrial,endTrial)
if nargin < 2 || isempty(startTrial)
startTrial = 1;
end
if nargin < 3 || isempty(endTrial)
endTrial = obj.NTrials;
end
fprintf('Running %s in series starting from trial %i\r',obj.ExperimentName,startTrial);
if (startTrial > 1)
% Get trials that were previously saved
end
for i = startTrial:endTrial
% try
t = obj.runTrial(i);
if (isempty(t.outputs))
warning('A completed trial should assign outputs')
end
obj.saveTrial(i,t);
% catch ME
% warning('An error was encountered running trial %i. Skipping it. Error message was %s',i,ME.message);
% end
end
trials = obj.loadAllTrials();
end
function trials = runTrialsPar(obj,overwrite)
% Get an array of trials struct
fprintf('Running %s in parallel with %i trials. Overwriting any saved: %s\r',obj.ExperimentName,obj.NTrials,mat2str(overwrite));
% Model for trial structure
tStruct = obj.tStruct;
% Functions to call within par loop
loadTrialFun = @obj.loadTrial;
saveTrialFun = @obj.saveTrial;
runTrialFun = @obj.runTrial;
% Run trials
parfor i = 1:obj.NTrials
if (overwrite)
% Create a fresh one
trial = tStruct;
else
% Try and load
trial = feval(loadTrialFun,i);
if isempty(trial)
% Couldn't load a saved one so create fresh
trial = tStruct;
end
end
if (isempty(trial.outputs)) % Will be true for fresh trials
try
t = feval(runTrialFun,i);
if (isempty(t.outputs))
warning('A completed trial should assign outputs')
end
trial = t;
feval(saveTrialFun,i,trial);
catch ME
warning('An error was encountered running trial %i. Skipping it. Error message was %s',i,ME.message);
end
else
fprintf('Trial %i already completed\n',i);
end
end
fprintf('Completed all trials. Loading results...');
trials = obj.loadAllTrials();
fprintf('...done\n');
end
function trial = runTrial(obj,i)
fprintf('Starting trial %i...\n',i);
% Requires that obj.loadExperimentParameters has been called
% first
% Set the stream for this trial
RandStream.setGlobalStream(obj.RandStreamTrials{i});
tTrial = tic; % Time the trial
trial = struct;
fprintf('Generating confounding variables for trial %i...\n',i);
tDataProcess = tic;
trial.confoundingVariables = obj.generateConfoundingVariables(i); % Returns struct
fprintf('...confounding variables generated for trial %i. Data processing took %g seconds\n',i,toc(tDataProcess));
trial.treatmentVariables = obj.generateTreatmentVariables(trial.confoundingVariables); % Returns struct array
stream = RandStream.getGlobalStream;
streamState = stream.State;
for j = 1:length(trial.treatmentVariables)
stream.State = streamState; % Ensures same RNG for each trial
fprintf('Simulating treatment %i of %i for trial %i...\n',j,length(trial.treatmentVariables),i);
% Simulation
tSim = tic; % Time the simulation with the treatment
trial.outputs(j) = obj.simulateTreatment(trial.confoundingVariables,trial.treatmentVariables(j));
fprintf('...done simulating treatment %i of %i for trial %i. Elapsed time: %g seconds\n',j,length(trial.treatmentVariables),i,toc(tSim));
end
fprintf('...trial %i completed. Elapsed time for trial: %g seconds\n',i,toc(tTrial));
end
function trial = loadTrial(obj,i)
try
t = load(sprintf('%strials/%i',obj.ResultsFolder,i),'trial');
trial = t.trial;
catch
trial = [];
end
end
function [trials,indCompleted] = loadAllTrials(obj)
indCompleted = false(1,obj.NTrials);
for i = 1:obj.NTrials
t = obj.loadTrial(i);
if (~isempty(t) && ~isempty(t.outputs))
trials(i) = t;
indCompleted(i) = true;
else
trials(i) = obj.tStruct;
end
end
end
function [trials] = loadCompletedTrials(obj)
[trials,indCompleted] = obj.loadAllTrials();
trials = trials(indCompleted);
obj.completedTrials = trials;
obj.completedTrialsCacheIsDirty = false;
end
function saveTrial(obj,i,trial)
save(sprintf('%strials/%i',obj.ResultsFolder,i),'trial');
obj.completedTrialsCacheIsDirty = true;
end
function resultsDistStruct = computeResultsDistributionMetricMultiple(obj,metricNames)
if nargin >= 2
% Find indices of metricNames
metricInds = [];
for m = 1:length(metricNames)
metricInds = [metricInds find(strcmp(metricNames{m},obj.Metrics(:,1)),1)];
end
else
% Do them all
metricInds = 1:size(obj.Metrics,1);
end
trials = obj.CompletedTrials;
% Infer the number of trials and treatments in each trial
Ntrials = length(trials); % Number of trials
if ~Ntrials
% resultsDist = []; % Think this can be deleted unless
% function needs to return an output. TODO: verify
return;
end
Ntreats = length(trials(1).treatmentVariables); % Number of treatments; assumes all trials have the same treatments
% Compute each metric
for m = metricInds
metricFun = obj.Metrics{m,2};
% Preallocate a matrix for the results for each trial and
% treatment. It will be Ntrials x Ntreats and the i,j element
% will be a performance metric for that combination
resultsDist = nan(Ntrials,Ntreats);
% Compute the performance metric for each outcome
for i = 1:Ntrials
if (length(trials(i).outputs) < Ntreats)
% Not all outputs were computed for each treatment for this trial
continue
end
for j = 1:Ntreats
% Compute the metric for that treatment
resultsDist(i,j) = metricFun(trials(i).outputs(j),obj.ExperimentParams,trials(i));
end
end
resultsDistStruct.(obj.Metrics{m,1}) = resultsDist;
end
end
function resultsDist = computeResultsDistributionMetricSingle(obj,metricFun,passTrial)
if (nargin < 3)
passTrial = false; % Optional flag to request that the whole trial info (i.e. confounding and experiment params) be passed to compute the metric.
end
trials = obj.CompletedTrials;
% Infer the number of trials and treatments in each trial
Ntrials = length(trials); % Number of trials
if ~Ntrials
resultsDist = [];
return;
end
Ntreats = length(trials(1).treatmentVariables); % Number of treatments; assumes all trials have the same treatments
% Preallocate a matrix for the results for each trial and
% treatment. It will be Ntrials x Ntreats and the i,j element
% will be a performance metric for that combination
resultsDist = nan(Ntrials,Ntreats);
% Compute the performance metric for each outcome
for i = 1:Ntrials
if (length(trials(i).outputs) < Ntreats)
% Not all outputs were computed for each treatment for this trial
continue
end
for j = 1:Ntreats
% Compute the metric for that treatment
if (passTrial)
resultsDist(i,j) = metricFun(trials(i).outputs(j),obj.ExperimentParams,trials(i));
else
resultsDist(i,j) = metricFun(trials(i).outputs(j),obj.ExperimentParams);
end
end
end
end
function plotMetricConvergence(obj,metricName,params)
% Plot the evolution of the coefficient of variation for each
% treatment as the number of trials increase
resultsDistStruct = computeResultsDistributionMetricMultiple(obj,{metricName});
results = resultsDistStruct.(metricName);
if (isfield(params,'treatmentInds'))
treatmentInds = params.treatmentInds;
else
treatmentInds = 1:size(results,2);
end
if (isfield(params,'treatmentLabels'))
treatmentLabels = params.treatmentLabels;
else
treatmentLabels = arrayfun(@(i) num2str(i),treatmentInds,'UniformOutput',false);
end
if (isfield(params,'metricLabel'))
metricLabel = params.metricLabel;
else
metricLabel = metricName;
end
cov = nan(size(results,1)-1,length(treatmentInds));
for i = 2:size(results,1)
cov(i-1,:) = std(results(1:i,treatmentInds))./mean(results(1:i,treatmentInds));
end
plot(2:size(results,1),cov);
if (length(treatmentLabels) > 1)
legend(treatmentLabels{:});
end
ylabel('CoV');
xlabel('Trials');
title(sprintf('Metric variation: %s',metricLabel));
end
function [axs,f] = plotResultsDistributionTreatment(obj,metricName,treatmentLabels)
% metricName: string defining the metric. Must be in
% obj.Metrics.
% treatmentLabels: (optional) cell array mapping the index of
% the treatment to a label to be displayed in the subplot
% title. If defined, must be of length Ntreatements.
resultsDistStruct = computeResultsDistributionMetricMultiple(obj,{metricName});
results = resultsDistStruct.(metricName);
N = size(results,2); % Number of treatments
treatmentLabelsDefined = nargin > 2 && length(treatmentLabels) == N;
if (N > 12)
warning('Only showing max of 12 treatments');
N = 12;
end
if ~treatmentLabelsDefined
treatmentLabels = arrayfun(@(i) sprintf('Treatment %i',i),1:N,'UniformOutput',false);
end
% Get number of rows
if (N < 1)
return;
elseif (N <= 3)
nRows = 1;
elseif (N <= 8)
nRows = 2;
else
nRows = 3;
end
nCols = ceil(N/nRows);
% Use the same bin width for all
if size(results,3) >= 60
nBins = 20;
else
nBins = ceil(size(results,1)/3);
end
edges = linspace(min(results(:)),max(results(:)),nBins+1);
f = figure('Name',metricName);
yMax = 0;
for j = 1:N
axs(j) = subplot(nRows,nCols,j);
histogram(results(:,j),edges,'Normalization','probability');
title(treatmentLabels{j})
t = get(axs(j),'YLim');
yMax = max(yMax,t(2));
end
try
sgtitle(sprintf('Distribution of %s for each treatment',metricName));
end
set(axs,'YLim',[0 yMax]); % Give all the same y axis
f = figure('Name',sprintf('%s (overlay)',metricName));
for j = 1:N
histogram(results(:,j),edges,'Normalization','pdf');
hold on
end
legend(treatmentLabels);
end
function plotResultsDistributionMetrics(obj,params)
%function [ax,f] = plotRelativeResultsDistributionMetrics(obj,metricNames,baseInd,treatmentLabels,metricLabels)
% Makes a stacked bar chart. Each group of bars corresponds to
% a metric. Each bar in the group is the percent change in the
% metric for that treatment relative to the baseline treatment.
if (nargin < 2)
params = struct;
end
if (isfield(params,'metricNames'))
metricNames = params.metricNames;
resultsDistStruct = computeResultsDistributionMetricMultiple(obj,metricNames);
if length(fieldnames(resultsDistStruct)) < length(metricNames)
error('At least one of the metric names is not defined');
end
else
% Use all metrics (infer from results field names)
resultsDistStruct = computeResultsDistributionMetricMultiple(obj);
metricNames = fieldnames(resultsDistStruct);
if (length(metricNames)) < 1
warning('No metrics defined for experiment');
return;
end
end
% N metric names
N = length(metricNames);
% Infer metric labels if not explicitly defined
if (~isfield(params,'metricLabels') || length(params.metricLabels) ~= N)
metricLabels = metricNames;
else
metricLabels = params.metricLabels;
end
% Infer treatments, assumes the same for all trials and metrics
% M treatments
if (isfield(params,'treatmentInd'))
tInd = params.treatmentInd;
M = length(tInd);
else
M = size(resultsDistStruct.(metricNames{1}),2);
tInd = 1:M;
end
if (isfield(params,'treatmentLabels') && length(params.treatmentLabels) == M)
treatmentLabels = params.treatmentLabels;
treatmentsNumeric = isnumeric(treatmentLabels);
else
treatmentLabels = [];
treatmentsNumeric = false;
end
if (treatmentsNumeric)
% Treatments have a numeric interpretation. Sort them
[treatmentLabels, tInd] = sort(treatmentLabels);
end
% Set normalization, default 'absolute'
if (isfield(params,'normalization'))
normalization = params.normalization;
else
normalization = 'absolute';
end
data = cell(1,N);
switch lower(normalization)
case 'absolute'
for i = 1:N
t = resultsDistStruct.(metricNames{i});
data{i} = t(:,tInd);
end
ylabelText = 'Metric value';
case 'percentchangebytrial'
if (isfield(params,'baseIndex'))
baseInd = params.baseIndex;
else
baseInd = 1;
end
baseInd2 = tInd == baseInd;
% Set the y label to indicate percent change
ylabelText = sprintf('%% change relative to treatment: %s',treatmentLabels{baseInd2});
% Remove the baseInd
tInd(baseInd2) = [];
treatmentLabels(baseInd2) = [];
% Function to compute relative change of each treatment to the
% reference.
relativeChange = @(x) (x-repmat(x(:,baseInd),1,size(x,2)))./abs(repmat(x(:,baseInd)+1e-10,1,size(x,2)))*100;
for i = 1:N
t = relativeChange(resultsDistStruct.(metricNames{i}));
data{i} = t(:,tInd);
end
otherwise
error('Unrecognized normalization: %s',normalization);
end
if (treatmentsNumeric)
% Make a line graph of the data
plotMetricLine(data,treatmentLabels,metricLabels);
else
% Treatments are labeled categorical
if (isfield(params,'barOpts'))
barOpts = params.barOpts;
else
barOpts = struct;
end
plotGroupedBar(data,metricLabels,treatmentLabels,barOpts);
end
title(gca,'Metrics');
ylabel(gca,ylabelText);
xlabel(gca,'Treatment');
end
function y = get.CompletedTrials(self)
if (self.completedTrialsCacheIsDirty)
y = self.loadCompletedTrials();
else
y = self.completedTrials;
end
end
function setupAdditionalParameters(obj,setupArgs)
end
end
methods (Abstract)
% Generate confounding variables
generateConfoundingVariables(obj,trialInd)
% Generate treatment variables
generateTreatmentVariables(obj,confoundingVariables)
% Simulate a trial
simulateTreatment(obj,confoundingVariables,treatmentVariables)
end
end