-
Notifications
You must be signed in to change notification settings - Fork 4
/
SubjClass.m
637 lines (516 loc) · 23.9 KB
/
SubjClass.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
classdef SubjClass < TreeNodeClass
properties % (Access = private)
sess;
end
methods
% ----------------------------------------------------------------------------------
function obj = SubjClass(varargin)
obj@TreeNodeClass(varargin);
obj.type = 'subj';
obj.sess = SessClass().empty;
if nargin==0
obj.name = '';
return;
end
if nargin<3
if isa(varargin{1}, 'SubjClass')
if nargin==1
obj.Copy(varargin{1});
elseif nargin==2
obj.Copy(varargin{1}, varargin{2});
end
return;
elseif isa(varargin{1}, 'FileClass')
[~, obj.name] = varargin{1}.ExtractNames();
else
obj.name = varargin{1};
end
elseif nargin==3
if ~isa(varargin{1}, 'FileClass')
[~, obj.name] = varargin{1}.ExtractNames();
else
obj.name = varargin{1};
end
obj.iGroup = varargin{2};
obj.iSubj = varargin{3};
end
end
% ----------------------------------------------------------------------------------
function nbytes = MemoryRequired(obj)
nbytes = 0;
for ii = 1:length(obj.sess)
nbytes = nbytes + obj.sess(ii).MemoryRequired();
end
nbytes = nbytes + obj.procStream.MemoryRequired();
end
% ----------------------------------------------------------------------------------
% Copy processing params (procInut and procResult) from
% S to obj if obj and S are equivalent nodes
% ----------------------------------------------------------------------------------
function Copy(obj, obj2, conditional)
% Copy SubjClass object obj2 to SubjClass object obj. Conditional option applies
% only to all the sessions under this group. If == 'conditional' ONLY derived data,
% that is, only from procStream but NOT from acquired data is copied for all the sessions.
%
% Conversly unconditional copy copies all properties in the sessions under this subject
if nargin==3 && strcmp(conditional, 'conditional')
if obj.Mismatch(obj2)
return
end
for i = 1:length(obj.sess)
j = obj.existRun(i,obj2);
if (j>0)
obj.sess(i).Copy(obj2.sess(j), 'conditional');
elseif i<=length(obj2.sess)
obj.sess(i).Copy(obj2.sess(i), 'conditional');
else
obj.Mismatch();
end
end
obj.Copy@TreeNodeClass(obj2, 'conditional');
else
if nargin<3
conditional = '';
end
for i = 1:length(obj2.sess)
obj.sess(i) = SessClass(obj2.sess(i), conditional);
end
obj.Copy@TreeNodeClass(obj2);
end
end
% --------------------------------------------------------------
function CopyStims(obj, obj2)
obj.CondNames = obj2.CondNames;
for ii = 1:length(obj.sess)
obj.sess(ii).CopyStims(obj2.sess(ii));
end
end
% ----------------------------------------------------------------------------------
% Check whether session R exists in this subject and return
% its index if it does exist. Else return 0.
% ----------------------------------------------------------------------------------
function j = existRun(obj, k, S)
j=0;
for i=1:length(S.sess)
[~,rname1] = fileparts(obj.sess(k).name);
[~,rname2] = fileparts(S.sess(i).name);
if strcmp(rname1,rname2)
j=i;
break;
end
end
end
% ----------------------------------------------------------------------------------
% Subjects obj1 and obj2 are considered equivalent if their names
% are equivalent and their sets of sessions are equivalent.
% ----------------------------------------------------------------------------------
function B = equivalent(obj1, obj2)
B=1;
if ~strcmp(obj1.name, obj2.name)
B=0;
return;
end
for i = 1:length(obj1.sess)
j = existRun(obj1, i, obj2);
if j==0
B=0;
return;
end
end
for i = 1:length(obj2.sess)
j = existRun(obj2, i, obj1);
if j==0
B=0;
return;
end
end
end
% ----------------------------------------------------------------------------------
function b = CheckForNameConflict(obj, sess)
b = false;
[~, sessname] = fileparts(sess.GetName());
if strcmp(sessname, obj.name)
b = true;
end
end
% ----------------------------------------------------------------------------------
function Add(obj, sess, run)
[~,f,e] = fileparts(sess.GetName());
if strcmp(f, obj.name)
msg{1} = sprintf('WARNING: The session being added (%s) has the same name as the subject (%s) containing it. ', [f,e], obj.name);
msg{2} = sprintf('The session names should not have the same name as the subject, otherwise ');
msg{3} = sprintf('it may cause incorrect results in processing. Do you want to change this session''s name?');
obj.logger.Write('%s\n', [msg{:}]);
% Deconflict name of subject with name of session if there is no
% subject folder for this subject
if ~ispathvalid(['./',obj.name], 'dir')
obj.name = [obj.name, '_sub'];
obj.logger.Write('Renaming subject to %s\n', obj.name);
end
end
% Add session to this subject
jj=0;
for ii = 1:length(obj.sess)
if strcmp(obj.sess(ii).GetName, sess.GetName())
jj=ii;
break;
end
end
if jj==0
jj = length(obj.sess)+1;
sess.SetIndexID(obj.iGroup, obj.iSubj, jj);
sess.SetPath(obj.path); % Inherit root path from subject
obj.sess(jj) = sess;
obj.logger.Write(' Added session "%s" to subject "%s" .\n', obj.sess(jj).GetFileName, obj.GetFileName);
end
% Add sess to subj
obj.sess(jj).Add(run);
obj.children = obj.sess;
end
% ----------------------------------------------------------------------------------
function list = DepthFirstTraversalList(obj)
list{1} = obj;
for ii = 1:length(obj.sess)
list = [list; obj.sess(ii).DepthFirstTraversalList()];
end
end
% ----------------------------------------------------------------------------------
% Deletes derived data in procResult
% ----------------------------------------------------------------------------------
function Reset(obj, option)
if ~exist('option','var')
option = 'down';
end
if strcmp(option, 'down')
for jj = 1:length(obj.sess)
obj.sess(jj).Reset();
end
end
Reset@TreeNodeClass(obj);
end
% ----------------------------------------------------------------------------------
function err = LoadSubBranch(obj)
err = -1;
if isempty(obj)
return;
end
err1 = obj.procStream.Load([obj.path, obj.GetOutputFilename()]);
err2 = obj.sess(1).Load();
if err1==0 && err2==0
err = 0;
end
end
% ----------------------------------------------------------------------------------
function FreeMemorySubBranch(obj)
if isempty(obj)
return;
end
obj.sess(1).FreeMemory()
end
% ----------------------------------------------------------------------------------
function FreeMemoryRecursive(obj)
if isempty(obj)
return
end
for ii = 1:length(obj.sess)
obj.sess(ii).FreeMemory();
end
obj.FreeMemory();
end
% ----------------------------------------------------------------------------------
function LoadRecursive(obj)
if isempty(obj)
return
end
for ii = 1:length(obj.sess)
obj.sess(ii).Load();
end
obj.Load();
end
% ----------------------------------------------------------------------------------
function CreateOutputDir(obj)
if ispathvalid([obj.pathOutputAlt, obj.outputDirname, obj.name])
return;
end
if ~ispathvalid([obj.pathOutputAlt, obj.name])
return;
end
mkdir([obj.pathOutputAlt, obj.outputDirname, obj.name]);
end
% ----------------------------------------------------------------------------------
function LoadInputVars(obj, tHRF_common)
obj.inputVars.nTrialsSess = [];
for iSess = 1:length(obj.sess)
% Set common tHRF: make sure size of tHRF, dcAvg and dcAvg is same for
% all sessions. Use smallest tHRF as the common one.
obj.sess(iSess).procStream.output.SettHRFCommon(tHRF_common, obj.sess(iSess).name, obj.sess(iSess).type);
obj.inputVars.dodAvgSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('dodAvg');
obj.inputVars.dodAvgStdSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('dodAvgStd');
obj.inputVars.dodSum2Sess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('dodSum2');
obj.inputVars.dcAvgSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('dcAvg');
obj.inputVars.dcAvgStdSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('dcAvgStd');
obj.inputVars.dcSum2Sess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('dcSum2');
obj.inputVars.tHRFSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetTHRF();
obj.inputVars.mlActSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('mlActAuto');
if isempty(obj.inputVars.nTrialsSess)
obj.inputVars.nTrialsSess = obj.sess(iSess).procStream.output.GetVar('nTrials');
else
nTrialsSess = obj.sess(iSess).procStream.output.GetVar('nTrials');
for iBlk = 1:length(obj.inputVars.nTrialsSess)
obj.inputVars.nTrialsSess{iBlk} = obj.inputVars.nTrialsSess{iBlk} + nTrialsSess{iBlk};
end
end
if ~isempty(obj.sess(iSess).procStream.output.GetVar('misc'))
if isfield(obj.sess(iSess).procStream.output.misc, 'stim') == 1
obj.inputVars.stimSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.misc.stim;
else
obj.inputVars.stimSess{obj.sess(iSess).iSess} = obj.sess(iSess).GetVar('stim');
end
else
obj.inputVars.stimSess{obj.sess(iSess).iSess} = obj.sess(iSess).GetVar('stim');
end
obj.inputVars.dcSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('dc');
obj.inputVars.AauxSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('Aaux');
obj.inputVars.tIncAutoSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('tIncAuto');
obj.inputVars.rcMapSess{obj.sess(iSess).iSess} = obj.sess(iSess).procStream.output.GetVar('rcMap');
% a) Find all variables needed by proc stream
args = obj.procStream.GetInputArgs();
% b) Find these variables in this session
for ii = 1:length(args)
if ~eval( sprintf('isproperty(obj.inputVars, ''%s'')', args{ii}) )
eval( sprintf('obj.inputVars.%s = obj.GetVar(args{ii});', args{ii}) );
end
end
% Free session memory
obj.sess(iSess).FreeMemory()
end
end
% ----------------------------------------------------------------------------------
function Calc(obj, options)
if ~exist('options','var') || isempty(options)
options = 'overwrite';
end
if strcmpi(options, 'overwrite')
% Recalculating result means deleting old results, if
% option == 'overwrite'
obj.procStream.output.Flush();
end
if obj.DEBUG
obj.logger.Write(sprintf('Calculating processing stream for group %d, subject %d\n', obj.iGroup, obj.iSubj));
end
% Calculate all sessions in this session and generate common tHRF
tHRF_common = {};
for iSess = 1:length(obj.sess)
obj.sess(iSess).Calc();
% Find smallest tHRF among the subjects and make this the common one.
tHRF_common = obj.sess(iSess).procStream.output.GeneratetHRFCommon(tHRF_common);
end
% Update call application GUI using it's generic Update function
if ~isempty(obj.updateParentGui)
obj.updateParentGui('DataTreeClass', [obj.iGroup, obj.iSubj, obj.iSess, obj.iRun]);
end
% Load all the variables that might be needed by procStream.Calc() to calculate proc stream for this subject
obj.LoadInputVars(tHRF_common);
Calc@TreeNodeClass(obj);
if obj.DEBUG
fprintf('Completed processing stream for group %d, subject %d\n', obj.iGroup, obj.iSubj);
fprintf('\n');
end
end
% ----------------------------------------------------------------------------------
function Print(obj, indent)
if ~exist('indent', 'var')
indent = 4;
else
indent = indent+4;
end
Print@TreeNodeClass(obj, indent);
for ii = 1:length(obj.sess)
obj.sess(ii).Print(indent);
end
end
% ---------------------------------------------------------------
function PrintProcStream(obj)
fcalls = obj.procStream.GetFuncCallChain();
obj.logger.Write('Subject processing stream:\n');
for ii = 1:length(fcalls)
obj.logger.Write('%s\n', fcalls{ii});
end
obj.logger.Write('\n');
obj.sess(1).PrintProcStream();
end
% ----------------------------------------------------------------------------------
function b = IsEmpty(obj)
b = true;
if isempty(obj)
return;
end
for ii = 1:length(obj.sess)
if ~obj.sess(ii).IsEmpty()
b = false;
break;
end
end
end
% ----------------------------------------------------------------------------------
function b = IsEmptyOutput(obj)
b = true;
if isempty(obj)
return;
end
for ii = 1:length(obj.sess)
if ~obj.sess(ii).IsEmptyOutput()
b = false;
break;
end
end
end
% ----------------------------------------------------------------------------------
function SaveAcquiredData(obj)
for ii = 1:length(obj.sess)
obj.sess(ii).SaveAcquiredData();
end
end
% ----------------------------------------------------------------------------------
function b = AcquiredDataModified(obj)
b = false;
for ii = 1:length(obj.sess)
if obj.sess(ii).AcquiredDataModified()
b = true;
end
end
end
% ----------------------------------------------------------------------------------
function varval = GetVar(obj, varname)
% First call the common code for all levels
varval = obj.GetVar@TreeNodeClass(varname);
% Now call the subject specific part
if isempty(varval)
varval = obj.sess(1).GetVar(varname);
end
end
% --------------------------------------------------------------------------
function ApplyParamEditsToAllSessions(obj, iFcall, iParam, val)
for jj = 1:length(obj.sess)
obj.sess(jj).procStream.EditParam(iFcall, iParam, val);
end
end
% --------------------------------------------------------------------------
function ApplyParamEditsToAllRuns(obj, iFcall, iParam, val)
for jj = 1:length(obj.sess)
obj.sess(jj).ApplyParamEditsToAllRuns(iFcall, iParam, val);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Public Set/Get methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
% ----------------------------------------------------------------------------------
function SetSDG(obj,option)
if exist('option','var')
obj.SD = obj.sess(1).GetSDG(option);
else
obj.SD = obj.sess(1).GetSDG();
end
end
% ----------------------------------------------------------------------------------
function SD = GetSDG(obj,option)
if exist('option','var')
SD = obj.sess(1).GetSDG(option);
else
SD = obj.sess(1).GetSDG();
end
end
% ----------------------------------------------------------------------------------
function bbox = GetSdgBbox(obj)
bbox = obj.sess(1).GetSdgBbox();
end
% ----------------------------------------------------------------------------------
function probe = GetProbe(obj)
probe = obj.sess(1).GetProbe();
end
% ----------------------------------------------------------------------------------
function wls = GetWls(obj)
wls = obj.sess(1).GetWls();
end
% ----------------------------------------------------------------------------------
function [iDataBlks, iCh] = GetDataBlocksIdxs(obj, iCh)
if nargin<2
iCh = [];
end
[iDataBlks, iCh] = obj.sess(1).GetDataBlocksIdxs(iCh);
end
% ----------------------------------------------------------------------------------
function n = GetDataBlocksNum(obj)
n = obj.sess(1).GetDataBlocksNum();
end
% ----------------------------------------------------------------------------------
function SetConditions(obj, CondNames)
if nargin==1
CondNames = {};
for ii = 1:length(obj.sess)
obj.sess(ii).SetConditions();
CondNames = [CondNames, obj.sess(ii).GetConditions()];
end
elseif nargin==2
for ii = 1:length(obj.sess)
obj.sess(ii).SetConditions(CondNames);
end
end
obj.CondNames = unique(CondNames);
end
% ----------------------------------------------------------------------------------
function CondNames = GetConditionsActive(obj)
CondNames = obj.CondNames;
for ii = 1:length(obj.sess)
CondNamesRun = obj.sess(ii).GetConditionsActive();
for jj=1:length(CondNames)
k = find(strcmp(['-- ', CondNames{jj}], CondNamesRun));
if ~isempty(k)
CondNames{jj} = ['-- ', CondNames{jj}];
end
end
end
end
% ----------------------------------------------------------------------------------
function aux = GetAuxiliary(obj)
aux = [];
end
% ----------------------------------------------------------------------------------
function [fn_error, missing_args, prereqs] = CheckProcStreamOrder(obj)
missing_args = {};
fn_error = 0;
prereqs = '';
for i = 1:length(obj.sess)
[fn_error, missing_args, prereqs] = obj.sess(i).CheckProcStreamOrder;
if ~isempty(missing_args)
return
end
end
end
% ----------------------------------------------------------------------------------
function r = ListOutputFilenames(obj, options)
if ~exist('options','var')
options = '';
end
r = obj.GetOutputFilename(options);
fprintf(' %s %s\n', obj.path, r);
for ii = 1:length(obj.sess)
obj.sess(ii).ListOutputFilenames(options);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Private methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods (Access = public)
% ----------------------------------------------------------------------------------
function BackwardCompatability(obj)
obj.BackwardCompatability@TreeNodeClass();
for ii = 1:length(obj.sess)
obj.sess(ii).BackwardCompatability();
end
end
end % Private methods
end