forked from BUNPC/DataTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTreeClass.m
More file actions
651 lines (522 loc) · 23.7 KB
/
DataTreeClass.m
File metadata and controls
651 lines (522 loc) · 23.7 KB
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
classdef DataTreeClass < handle
properties
files
filesErr
groups
dirnameGroups
currElem
reg
config
logger
warningflag
dataStorageScheme
end
methods
% ---------------------------------------------------------------
function obj = DataTreeClass(groupDirs, fmt, procStreamCfgFile, options)
global logger
obj.groups = GroupClass().empty();
obj.currElem = TreeNodeClass().empty();
obj.reg = RegistriesClass().empty();
obj.config = ConfigFileClass().empty();
obj.dirnameGroups = {};
obj.logger = InitLogger(logger, 'DataTree');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parse args
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Arg 1: get folder of the group being loaded
if ~exist('groupDirs','var') || isempty(groupDirs)
groupDirs{1} = pwd;
elseif ~isa(groupDirs, 'DataTreeClass') && ~iscell(groupDirs)
groupDirs = {groupDirs};
end
% Arg 2: get the file format of the data files
if ~exist('fmt','var')
fmt = '';
end
% Arg 3: Get the processing stream config files name
if ~exist('procStreamCfgFile','var')
procStreamCfgFile = '';
end
% Arg 4: Force distrubited file saving scheme
if ~exist('options','var')
options = '';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now that we have all the arguments, ready to start
% condtructing object
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isa(groupDirs, 'DataTreeClass')
obj.Copy(groupDirs);
return;
elseif strcmp(options, 'empty')
return;
end
cfg = ConfigFileClass();
obj.dataStorageScheme = cfg.GetValue('Data Storage Scheme');
% Estimate amount of memory required and set the data storage scheme
obj.SetDataStorageScheme();
obj.FindAndLoadGroups(groupDirs, fmt, procStreamCfgFile, options);
if obj.IsEmpty()
return;
end
% Change current folder to last loaded group; even though we
% handle multiple groups and use absolute paths we still have
% group as the basic data unit and context. So we want to
% change the current folder to whatever is the current working
% group.
cd(obj.groups(end).path);
% Load user function registry
obj.reg = RegistriesClass();
if ~isempty(obj.reg.GetSavedRegistryPath())
obj.logger.Write(sprintf('Loaded saved registry %s\n', obj.reg.GetSavedRegistryPath()));
end
% Initialize the current processing element within the group
obj.SetCurrElem(1,1,1);
obj.warningflag = 0;
end
% --------------------------------------------------------------
function delete(obj)
if isa(obj.logger, 'Logger')
obj.logger.Close('DataTree');
end
end
% --------------------------------------------------------------
function Copy(obj, obj2)
idx = obj2.currElem.GetIndexID();
iG = idx(1);
iS = idx(2);
iR = idx(3);
if isempty(obj.groups)
obj.groups = GroupClass(obj2.groups(iG));
else
obj.groups(iG).Copy(obj.groups(iG))
end
obj.SetCurrElem(iG, iS, iR);
obj.groups(iG).SetConditions();
end
% --------------------------------------------------------------
function CopyStims(obj, obj2)
idx = obj2.currElem.GetIndexID();
iG = idx(1);
obj.groups(iG).CopyStims(obj2.groups(iG));
end
% --------------------------------------------------------------
function status = FoundDataFilesInOtherFormat(obj, dataInit, kk)
global supportedFormats
status = false;
k = [];
format0 = dataInit.type;
% Find index of another file format to try
for ii = 1:length(supportedFormats)
if ~isempty(findstr(dataInit.type, supportedFormats{ii,1})) %#ok<FSTR>
k = ii;
break;
end
end
if isempty(k)
return;
end
if k<length(supportedFormats) && k>1
k = k-1;
elseif k<length(supportedFormats)
k = k+1;
else
k = [];
end
if ~isempty(k)
dataInit = FindFiles(obj.dirnameGroups{kk}, supportedFormats{k});
if isempty(dataInit) || dataInit.isempty()
return;
end
else
dataInit = [];
end
if ~isempty(dataInit)
msg{1} = sprintf('Could not load any of the .%s files in the group folder but did find .%s files. ', format0, dataInit.type);
msg{3} = sprintf('Do you want to rename the .%s files to names with a .old extension, delete them or cancel? ', format0);
msg{2} = sprintf('NOTE: Renaming or deleting the .%s files will allow Homer3 to regenerate them from .%s file later.', ...
format0, dataInit.type);
q = MenuBox(msg, {'Rename (Recommended)','Delete','CANCEL'}, [], 90);
if q==1
DeleteDataFiles(obj.dirnameGroups, format0, 'move')
status = true;
elseif q==2
DeleteDataFiles(obj.dirnameGroups, format0)
status = true;
end
end
end
% --------------------------------------------------------------
function status = SelectOptionsWhenLoadFails(obj)
status = -1;
% msg{1} = sprintf('Could not load any of the requested files in the group folder %s. ', obj.dirnameGroups);
% msg{2} = sprintf('Do you want to select another group folder?');
% q = MenuBox(msg, {'YES','NO'});
msg{1} = sprintf('Could not load any of the requested files in the group folder %s. ', obj.dirnameGroups);
msg{2} = sprintf('Do you want to select another group folder?');
q = MenuBox(msg, {'YES','NO'}, [], 110);
if q==2
obj.logger.Write(sprintf('Skipping group folder %s...\n', obj.dirnameGroups));
obj.dirnameGroups = 0;
return;
end
obj.dirnameGroups = uigetdir(pwd, 'Please select another group folder ...');
if obj.dirnameGroups==0
obj.logger.Write(sprintf('Skipping group folder %s...\n', obj.dirnameGroups));
return;
end
status = 0;
end
% --------------------------------------------------------------
function FindAndLoadGroups(obj, groupDirs, fmt, procStreamCfgFile, options)
tic;
for kk = 1:length(groupDirs)
obj.dirnameGroups{kk} = filesepStandard(groupDirs{kk},'full');
iGnew = length(obj.groups)+1;
% Get file names and load them into DataTree
while length(obj.groups) < iGnew
% Find group folder and it's acqiosition files
obj.files = FileClass().empty();
obj.filesErr = FileClass().empty();
dataInit = DataFilesClass();
dataInitPrev = DataFilesClass();
iter = 1;
while dataInit.GetError() < 0
dataInit = FindFiles(obj.dirnameGroups{kk}, fmt, options);
if isempty(dataInit) || dataInit.isempty()
return;
end
dataInitPrev(iter) = dataInit;
obj.dirnameGroups{kk} = dataInit.pathnm;
iter = iter+1;
end
obj.files = dataInit.files;
% Print file and folder numbers stats
nfolders = length(dataInit.files)-dataInit.nfiles;
if nfolders==0
nfolders = 1;
end
obj.logger.Write(sprintf('DataTreeClass.FindAndLoadGroups: Found %d data files in %d folders\n', ...
dataInit.nfiles, nfolders));
% Now load group files to data tree
obj.LoadGroup(iGnew, procStreamCfgFile, options);
if length(obj.groups) < iGnew
if obj.FoundDataFilesInOtherFormat(dataInit, kk)
continue;
elseif obj.SelectOptionsWhenLoadFails()<0
break;
end
end
% Clean up any obsolete files in output folder if names
% of files or folder was changed
obj.groups(iGnew).CleanUpOutput(dataInitPrev);
end
end
obj.logger.Write(sprintf('Loaded data set in %0.1f seconds\n', toc));
end
% ---------------------------------------------------------------
function SetDataStorageScheme(obj)
% If there is no config option that was used to set
% dataStorageScheme then try to determine from saved data
% what the storage scheme is. Main user of this is AtlasViewer
if isempty(obj.dataStorageScheme)
obj.AutoSetDataStorageScheme();
end
% Estimate memory requirement based on number of acquired files and their
% average size
% obj.logger.Write(sprintf('Memory required for data tree: %0.1f MB\n', obj.MemoryRequired() / 1e6));
if strcmpi(obj.dataStorageScheme, 'files') || strcmpi(obj.dataStorageScheme, 'disk')
onoff = true;
elseif strcmpi(obj.dataStorageScheme, 'memory') || strcmpi(obj.dataStorageScheme, 'ram')
onoff = false;
else
onoff = false;
end
obj.groups.SaveMemorySpace(onoff);
end
% ---------------------------------------------------------------
function AutoSetDataStorageScheme(obj)
if isempty(obj.dataStorageScheme)
obj.dataStorageScheme = 'files';
end
end
% ---------------------------------------------------------------
function LoadGroup(obj, iG, procStreamCfgFile, options)
if ~exist('procStreamCfgFile','var')
procStreamCfgFile = '';
end
if ~exist('options','var')
options = '';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load acquisition data from the data files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
obj.AcqData2Group(iG);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Remove file entries from files array for data files
% which didn't load correctly because of format incompatibility
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
obj.ErrorCheckLoadedFiles();
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load derived or post-acquisition data from a file if it
% exists
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
obj.groups(iG).Load('init');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize procStream for all tree nodes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~optionExists(options, 'noloadconfig')
obj.groups(iG).InitProcStream(procStreamCfgFile);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generate the stimulus conditions for the group tree
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
obj.groups(iG).SetConditions();
end
% ----------------------------------------------------------
function AcqData2Group(obj, iG)
if isempty(obj.files)
return;
end
if ~exist('iG','var')
iG = 1;
end
groupCurr = GroupClass().empty();
subjCurr = SubjClass().empty();
runCurr = RunClass().empty();
tic;
obj.logger.Write(sprintf('\n'));
for iF = 1:length(obj.files)
% Extract group, subj, and run names from file struct
[groupName, subjName, runName] = obj.files(iF).ExtractNames();
% Create current TreeNode objects corresponding to the current files entry
if ~isempty(groupName) && ~strcmp(groupName, groupCurr.GetName)
groupCurr = GroupClass(obj.files(iF), iG, 'noprint');
end
if ~isempty(subjName) && ~strcmp(subjName, subjCurr.GetName)
subjCurr = SubjClass(obj.files(iF));
end
if ~isempty(runName) && ~strcmp(runName, runCurr.GetName)
runCurr = RunClass(obj.files(iF));
end
% If current run has successfully loaded acquired data from data file, then add
% current group, subject and run to dataTree. Then reset current run to empty.
% (We do not reset current subject or group because they can contain multiple
% nodes and they cannot be empty once they've been initialized once whereas run
% can be if it fails to load a data file.
if ~runCurr.Error()
obj.Add(groupCurr, subjCurr, runCurr);
runCurr = RunClass().empty();
end
end
obj.logger.Write(sprintf('\n'));
obj.logger.Write(sprintf('Loaded group %s acquisition data in %0.1f seconds\n', obj.groups(iG).name, toc));
obj.logger.Write(sprintf(' Derived data output folder : %s%s\n', obj.groups(iG).path, obj.groups(iG).outputDirname));
obj.logger.Write(sprintf(' Derived data output file : %s\n\n', obj.groups(iG).outputFilename));
end
% ----------------------------------------------------------
function Add(obj, group, subj, run)
if nargin<2
return;
end
% Add group to this dataTree
jj=0;
for ii=1:length(obj.groups)
if strcmp(obj.groups(ii).GetName, group.GetName())
jj=ii;
break;
end
end
if jj==0
jj = length(obj.groups)+1;
group.SetIndexID(jj);
obj.groups(jj) = group;
obj.groups(jj).SetPath(obj.dirnameGroups{jj})
obj.logger.Write(sprintf('Added group %s to dataTree.\n', obj.groups(jj).GetName));
end
%v Add subj and run to group
obj.groups(jj).Add(subj, run);
end
% ----------------------------------------------------------
function ErrorCheckLoadedFiles(obj)
for iF=length(obj.files):-1:1
if ~obj.files(iF).Loadable() && obj.files(iF).IsFile()
obj.filesErr(end+1) = obj.files(iF).copy;
obj.files(iF) = [];
end
end
end
% ----------------------------------------------------------------------------------
function list = DepthFirstTraversalList(obj)
list = {};
for ii=1:length(obj.groups)
list = [list; obj.groups(ii).DepthFirstTraversalList()];
end
end
% ----------------------------------------------------------
function err = LoadCurrElem(obj)
err = 0;
if isempty(obj.groups)
return;
end
err = obj.currElem.Load();
end
% ----------------------------------------------------------
function SetCurrElem(obj, iGroup, iSubj, iRun)
if isempty(obj.groups)
return;
end
if nargin==1
iGroup = 0;
iSubj = 0;
iRun = 0;
elseif nargin==2
iSubj = 0;
iRun = 0;
elseif nargin==3
iRun = 0;
end
if obj.currElem.IsSame(iGroup, iSubj, iRun)
return;
end
% Free up memory of current element before reassigning it to
% another node.
obj.currElem.FreeMemory();
if iSubj==0 && iRun==0
obj.currElem = obj.groups(iGroup);
elseif iSubj>0 && iRun==0
obj.currElem = obj.groups(iGroup).subjs(iSubj);
elseif iSubj>0 && iRun>0
obj.currElem = obj.groups(iGroup).subjs(iSubj).runs(iRun);
end
end
% ----------------------------------------------------------
function procElem = GetCurrElem(obj)
procElem = obj.currElem;
end
% ----------------------------------------------------------
function [iGroup, iSubj, iRun] = GetCurrElemIndexID(obj)
iGroup = obj.currElem.iGroup;
iSubj = obj.currElem.iSubj;
iRun = obj.currElem.iRun;
end
% ----------------------------------------------------------
function CopyCurrElem(obj, obj2, options)
if isempty(obj)
return;
end
if isempty(obj2)
return;
end
if ~exist('options', 'var')
options = 'reference';
end
if optionExists(options, 'reference')
obj3 = obj2;
elseif optionExists(options, 'value')
obj3 = obj;
end
idx = obj2.currElem.GetIndexID();
iGroup = idx(1);
iSubj = idx(2);
iRun = idx(3);
if iSubj==0 && iRun==0
obj.currElem = obj3.groups(iGroup);
elseif iSubj>0 && iRun==0
obj.currElem = obj3.groups(iGroup).subjs(iSubj);
elseif iSubj>0 && iRun>0
obj.currElem = obj3.groups(iGroup).subjs(iSubj).runs(iRun);
end
end
% ----------------------------------------------------------------------------------
function nbytes = MemoryRequired(obj)
nbytes = 0;
if isempty(obj)
return;
end
for ii = 1:length(obj.groups)
nbytes = nbytes + obj.groups(ii).MemoryRequired();
end
end
% ----------------------------------------------------------------------------------
function diskspaceToSpare = CheckAvailableDiskSpace(obj, hwait)
diskspaceToSpare = getFreeDiskSpace(); % Disk space to spare in megabytes
%diskspaceToSpare = (getFreeDiskSpace() - obj.MemoryRequired()); % Disk space to spare in megabytes
end
% ----------------------------------------------------------
function Save(obj, hwait)
if ~exist('hwait','var')
hwait = [];
end
% Check that there is anough disk space. NOTE: for now we
% assume that all groups are on the same drive. This should be
% changed but for now we simplify.
if obj.CheckAvailableDiskSpace(hwait) <= 0
return;
end
t_local = tic;
for ii = 1:length(obj.groups)
obj.logger.Write(sprintf('Saving group %d in %s\n', ii, [obj.groups(ii).pathOutputAlt, obj.groups(ii).GetFilename()]));
obj.groups(ii).Save(hwait);
end
obj.logger.Write(sprintf('Completed saving processing results for all groups in %0.3f seconds.\n', toc(t_local)));
end
% ----------------------------------------------------------
function CalcCurrElem(obj)
obj.currElem.Calc();
end
% ----------------------------------------------------------
function ResetCurrElem(obj)
obj.currElem.Reset();
idx = obj.currElem.GetIndexID();
if isa(obj.currElem, 'SubjClass')
obj.groups(idx(1)).Reset('up')
elseif isa(obj.currElem, 'RunClass')
obj.groups(idx(1)).Reset('up')
obj.groups(idx(1)).subjs(idx(2)).Reset('up')
end
end
% ----------------------------------------------------------
function ResetAllGroups(obj)
for ii = 1:length(obj.groups)
obj.groups(ii).Reset()
end
end
% ----------------------------------------------------------
function b = IsEmpty(obj)
b = true;
if isempty(obj)
return
end
if isempty(obj.files)
return;
end
if isempty(obj.groups)
return;
end
b = false;
end
% ----------------------------------------------------------
function b = IsEmptyOutput(obj)
b = true;
if obj.IsEmpty()
return;
end
for ii = 1:length(obj.groups)
if ~obj.groups(ii).IsEmptyOutput
b = false;
break;
end
end
end
% ----------------------------------------------------------
function b = IsFlatFileDir(obj)
if obj.files(1).isdir
b = false;
else
b = true;
end
end
end
end