-
Notifications
You must be signed in to change notification settings - Fork 17
/
pop_importbids.m
1077 lines (975 loc) · 51.6 KB
/
pop_importbids.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% POP_IMPORTBIDS - Import BIDS format folder structure into an EEGLAB
% study.
% Usage:
% >> [STUDY ALLEEG] = pop_importbids(bidsfolder);
% >> [STUDY ALLEEG] = pop_importbids(bidsfolder, 'key', value);
%
% Inputs:
% bidsfolder - a loaded epoched EEG dataset structure.
% options are 'bidsevent', 'bidschanloc' of be turned 'on' (default) or 'off'
% 'outputdir' default is bidsfolder/derivatives/eeglab
% 'studyName' default is eeg
%
% Optional inputs:
% 'studyName' - [string] name of the STUDY
% 'subjects' - [integer array] indices of subjects to import
% 'sessions' - [cell array] session numbers or names to import
% 'runs' - [integer array] run numbers to import
% 'bidsevent' - ['on'|'off'] import events from BIDS .tsv file and
% ignore events in raw binary EEG files.
% 'bidschanloc' - ['on'|'off'] import channel location from BIDS .tsv file
% and ignore locations (if any) in raw binary EEG files.
% 'outputdir' - [string] output folder (default is to use the BIDS
% folders).
% 'eventtype' - [string] BIDS event column to use for EEGLAB event types.
% common choices are usually 'trial_type' or 'value'.
% Default is 'value'.
% 'bidstask' - [string] value of a key task- allowing to analyze some
% tasks only
% 'metadata' - ['on'|'off'] only import metadata. Default 'off'.
% 'ctffunc' - ['fileio'|'ctfimport'] function to use to import CTF data
% Default 'fileio'.
%
% Outputs:
% STUDY - EEGLAB STUDY structure
% ALLEEG - EEGLAB ALLEEG structure
% bids - BIDS structure (same as ALLEEG(i).BIDS
% stats - BIDS metadata statistics structure
%
% Authors: Arnaud Delorme, SCCN, INC, UCSD, January, 2019
% Cyril Pernet, University of Edinburgh
%
% Example:
% pop_importbids('/data/matlab/bids_matlab/rishikesh_study/BIDS_EEG_meditation_experiment');
% Copyright (C) Arnaud Delorme, 2018
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
function [STUDY, ALLEEG, bids, stats, commands] = pop_importbids(bidsFolder, varargin)
STUDY = [];
ALLEEG = [];
bids = [];
stats = [];
commands = '';
if nargin < 1
bidsFolder = uigetdir('Pick a BIDS folder');
if isequal(bidsFolder,0), return; end
cb_select = [ 'tmpfolder = uigetdir;' ...
'if ~isequal(tmpfolder, 0)' ...
' set(findobj(gcbf, ''tag'', ''folder''), ''string'', tmpfolder);' ...
'end;' ...
'clear tmpfolder;' ];
type_fields = { 'value' 'trial_type' 'event_kind' 'event_type' };
disp('Scanning folders...');
% scan if multiple tasks are present
[tasklist,sessions,runs] = bids_getinfofromfolder(bidsFolder);
% scan for event fields
type_fields = bids_geteventfieldsfromfolder(bidsFolder);
indVal = strmatch('value', type_fields);
if ~isempty(indVal)
type_fields(indVal) = [];
type_fields = {'value' type_fields{:} };
end
bids_event_toggle = ~isempty(type_fields);
if isempty(type_fields) type_fields = { 'n/a' }; end
if isempty(tasklist) tasklist = { 'n/a' }; end
cb_event = 'set(findobj(gcbf, ''userdata'', ''bidstype''), ''enable'', fastif(get(gcbo, ''value''), ''on'', ''off''));';
cb_task = 'set(findobj(gcbf, ''userdata'', ''task'' ), ''enable'', fastif(get(gcbo, ''value''), ''on'', ''off''));';
cb_sess = 'set(findobj(gcbf, ''userdata'', ''sessions''), ''enable'', fastif(get(gcbo, ''value''), ''on'', ''off''));';
cb_run = 'set(findobj(gcbf, ''userdata'', ''runs'' ), ''enable'', fastif(get(gcbo, ''value''), ''on'', ''off''));';
promptstr = { ...
{ 'style' 'text' 'string' 'Enter study name (default is BIDS folder name)' } ...
{ 'style' 'edit' 'string' '' 'tag' 'studyName' } ...
{} ...
{ 'style' 'checkbox' 'string' 'Use BIDS electrode.tsv files (when present) for channel locations; off: look up locations using channel labels' 'tag' 'chanlocs' 'value' 1 } ...
{ 'style' 'checkbox' 'string' 'Use BIDS event.tsv files for events and use the following BIDS field for event type' 'tag' 'events' 'value' bids_event_toggle 'callback' cb_event } ...
{ 'style' 'popupmenu' 'string' type_fields 'tag' 'typefield' 'value' 1 'userdata' 'bidstype' 'enable' fastif(bids_event_toggle, 'on', 'off') } ...
{ 'style' 'checkbox' 'string' 'Import only the following BIDS tasks' 'tag' 'bidstask' 'value' 0 'callback' cb_task } ...
{ 'style' 'popupmenu' 'string' tasklist 'tag' 'bidstaskstr' 'value' 1 'userdata' 'task' 'enable' 'off' } {} ...
{ 'style' 'checkbox' 'string' 'Import only the following sessions' 'tag' 'bidssessions' 'value' 0 'callback' cb_sess } ...
{ 'style' 'listbox' 'string' sessions 'tag' 'bidsessionstr' 'max' 2 'value' [] 'userdata' 'sessions' 'enable' 'off' } {} ...
{ 'style' 'checkbox' 'string' 'Import only the following runs' 'tag' 'bidsruns' 'value' 0 'callback' cb_run } ...
{ 'style' 'listbox' 'string' runs 'tag' 'bidsrunsstr' 'max' 2 'value' [] 'userdata' 'runs' 'enable' 'off' } {} ...
{} ...
{ 'style' 'text' 'string' 'Study output folder' } ...
{ 'style' 'edit' 'string' fullfile(bidsFolder, 'derivatives', 'eeglab') 'tag' 'folder' 'HorizontalAlignment' 'left' } ...
{ 'style' 'pushbutton' 'string' '...' 'callback' cb_select } ...
};
geometry = {[2 1.5], 1, 1,[1 0.35],[0.6 0.35 0.5],[0.6 0.35 0.5],[0.6 0.35 0.5],1,[1 2 0.5]};
geomvert = [1 0.5, 1 1 1 1.5 1.5 0.5 1];
if isempty(runs)
promptstr(13:15) = [];
geometry(7) = [];
geomvert(7) = [];
end
if isempty(sessions)
promptstr(10:12) = [];
geometry(6) = [];
geomvert(6) = [];
end
[~,~,~,res] = inputgui( 'geometry', geometry, 'geomvert', geomvert, 'uilist', promptstr, 'helpcom', 'pophelp(''pop_importbids'')', 'title', 'Import BIDS data -- pop_importbids()');
if isempty(res), return; end
if ~isempty(type_fields) && ~strcmpi(type_fields{res.typefield}, 'n/a'), options = { 'eventtype' type_fields{res.typefield} }; else options = {}; end
if res.events, options = { options{:} 'bidsevent' 'on' }; else options = { options{:} 'bidsevent' 'off' }; end
if res.chanlocs, options = { options{:} 'bidschanloc' 'on' }; else options = { options{:} 'bidschanloc' 'off' }; end
if ~isempty(res.folder), options = { options{:} 'outputdir' res.folder }; end
if ~isempty(res.studyName), options = { options{:} 'studyName' res.studyName }; end
if res.bidstask && ~strcmpi(tasklist{res.bidstaskstr}, 'n/a'), options = { options{:} 'bidstask' tasklist{res.bidstaskstr} }; end
if isfield(res, 'bidssessions')
if res.bidssessions && ~isempty(res.bidsessionstr), options = { options{:} 'sessions' sessions(res.bidsessionstr) }; end
end
if isfield(res, 'bidsruns')
if res.bidsruns && ~isempty(res.bidsruns), options = { options{:} 'runs' str2double(runs(res.bidsrunsstr)) }; end
end
else
options = varargin;
end
[~,defaultStudyName] = fileparts(bidsFolder);
opt = finputcheck(options, { ...
'bidsevent' 'string' { 'on' 'off' } 'on'; ...
'bidschanloc' 'string' { 'on' 'off' } 'on'; ...
'bidscoord' 'string' { 'on' 'off' } 'on'; ...
'bidstask' {'string' 'cell'} {'',{}} ''; ...
'subjects' 'integer' {} []; ...
'sessions' 'cell' {} {}; ...
'runs' 'integer' {} []; ...
'metadata' 'string' { 'on' 'off' } 'off'; ...
'ctffunc' 'string' { 'fileio' 'ctfimport' } 'fileio'; ...
'eventtype' 'string' { } 'value'; ...
'outputdir' 'string' { } fullfile(bidsFolder, 'derivatives', 'eeglab'); ...
'studyName' 'string' { } defaultStudyName ...
}, 'pop_importbids');
if isstr(opt), error(opt); end
if ~exist('jsondecode.m','file')
addpath([fileparts(which('pop_importbids.m')) filesep 'JSONio'])
end
% Options:
% - copy folder
% - use channel location and event
% load change file
changesFile = fullfile(bidsFolder, 'CHANGES');
bids.CHANGES = '';
if exist(changesFile,'File')
bids.CHANGES = bids_loadfile( changesFile, [], true );
end
% load Readme file
readmeFile = fullfile(bidsFolder, 'README');
bids.README = '';
if exist(readmeFile,'File')
bids.README = bids_loadfile( readmeFile, [], true );
end
% load dataset description file
dataset_descriptionFile = fullfile(bidsFolder, 'dataset_description.json');
bids.dataset_description = '';
if exist(dataset_descriptionFile,'File')
bids.dataset_description = bids_loadfile( dataset_descriptionFile );
end
% load participant file
participantsFile = fullfile(bidsFolder, 'participants.tsv');
bids.participants = '';
pInd = 1;
if exist(participantsFile,'File')
bids.participants = bids_loadfile( participantsFile );
if ~isempty(bids.participants) && ~isequal(bids.participants{1}, 'participant_id')
pInd = find(cellfun(@(x)contains(x, 'participant_id'), bids.participants(1,:))); % sometime special chars
if isempty(pInd)
error('Cannot find participant_id column')
end
end
end
% if no participants.tsv, use subjects folder names as their IDs
if isempty(bids.participants)
participantFolders = dir(fullfile(bidsFolder, 'sub-*'));
bids.participants = {'participant_id' participantFolders.name }';
end
bids.participants(strcmp(bids.participants, 'sub-emptyroom'),:) = [];
% load participant sidecar file
participantsJSONFile = fullfile(bidsFolder, 'participants.json');
bids.participantsJSON = '';
if exist(participantsJSONFile,'File')
bids.participantsJSON = bids_loadfile( participantsJSONFile );
end
% scan participants
count = 1;
commands = {};
task = [ 'task-' bidsFolder ];
bids.data = [];
bids.eventInfo = [];
bids.data.eventdesc = [];
bids.data.eventinfo = [];
inconsistentChannels = 0;
inconsistentEvents = 0;
faileddatasets = [];
if isempty(opt.subjects)
opt.subjects = 2:size(bids.participants,1); % indices into the participants.tsv file, ignoring first header row
else
opt.subjects = opt.subjects+1;
end
for iSubject = opt.subjects
parentSubjectFolder = fullfile(bidsFolder , bids.participants{iSubject,pInd});
outputSubjectFolder = fullfile(opt.outputdir, bids.participants{iSubject,pInd});
iteration = 0;
while ~exist(parentSubjectFolder, 'dir') && iteration < 3
fprintf(2, 'Folder %s does not exist\n', parentSubjectFolder);
dashpos = find(bids.participants{iSubject,1} == '-');
if ~isempty(dashpos)
bids.participants{iSubject,1} = [ bids.participants{iSubject,1}(1:dashpos) '0' bids.participants{iSubject,1}(dashpos+1:end) ];
parentSubjectFolder = fullfile(bidsFolder , bids.participants{iSubject,pInd});
outputSubjectFolder = fullfile(opt.outputdir, bids.participants{iSubject,pInd});
end
iteration = iteration + 1;
end
% find folder containing eeg
subFolders = dir(fullfile(parentSubjectFolder, 'ses*'));
if ~isempty(subFolders)
subFolders = { subFolders.name };
else
subFolders = {''};
end
subjectFolder = {};
subjectFolderOut = {};
if ~isempty(opt.sessions)
subFolders = intersect(subFolders, opt.sessions);
end
for iFold = 1:length(subFolders)
subjectFolder{ iFold} = fullfile(parentSubjectFolder, subFolders{iFold}, 'eeg');
subjectFolderOut{iFold} = fullfile(outputSubjectFolder, subFolders{iFold}, 'eeg');
if ~exist(subjectFolder{iFold},'dir')
subjectFolder{ iFold} = fullfile(parentSubjectFolder, subFolders{iFold}, 'meg');
subjectFolderOut{iFold} = fullfile(outputSubjectFolder, subFolders{iFold}, 'meg');
if ~exist(subjectFolder{iFold},'dir')
subjectFolder{ iFold} = fullfile(parentSubjectFolder, subFolders{iFold}, 'ieeg');
subjectFolderOut{iFold} = fullfile(outputSubjectFolder, subFolders{iFold}, 'ieeg');
end
end
end
% import data
for iFold = 1:length(subjectFolder) % scan sessions
if ~exist(subjectFolder{iFold},'dir')
fprintf(2, 'No EEG data folder for subject %s session %s\n', bids.participants{iSubject,pInd}, subFolders{iFold});
else
% scans.tsv for time synch information
%-------------------------------------
try
try
scansFile = searchparent(fileparts(subjectFolder{iFold}), '*_scans.tsv');
catch
% for some reason parent search not working - a quick workaround
scansFile = searchparent(fileparts(fileparts(subjectFolder{iFold})), '*_scans.tsv');
end
catch
end
if exist('scansFile', 'var') && ~isempty(scansFile)
useScans = true;
scans = bids_loadfile( scansFile.name, scansFile);
bids.data = setallfields(bids.data, [iSubject-1,iFold,1], struct('scans', {scans}));
else
useScans = false;
end
% MEG, EEG, iEEG, Motion, Physio, or BEH
% which raw data - with folder inheritance
eegFile = searchparent(subjectFolder{iFold}, '*eeg.*');
if isempty(eegFile)
eegFile = searchparent(subjectFolder{iFold}, '*_meg.*');
end
if isempty(eegFile)
eegFile = searchparent(subjectFolder{iFold}, '*_ieeg.*');
end
infoFile = searchparent(subjectFolder{iFold}, '*_eeg.json');
channelFile = searchparent(subjectFolder{iFold}, '*_channels.tsv');
elecFile = searchparent(subjectFolder{iFold}, '*_electrodes.tsv');
eventFile = searchparent(subjectFolder{iFold}, '*_events.tsv');
eventDescFile = searchparent(subjectFolder{iFold}, '*_events.json');
coordFile = searchparent(subjectFolder{iFold}, '*_coordsystem.json');
behFile = searchparent(fullfile(subjectFolder{iFold}, '..', 'beh'), '*_beh.tsv');
motionFile = searchparent(fullfile(subjectFolder{iFold}, '..', 'motion'), '*_motion.tsv');
physioFile = searchparent(fullfile(subjectFolder{iFold}, '..', 'physio'), '*_physio.tsv');
% remove BEH files which have runs (treated separately)
if ~isempty(behFile) && (contains(behFile(1).name, 'run') || contains(behFile(1).name, 'task'))
behFile = {};
end
% check the task
if ~isempty(opt.bidstask)
eegFile = filterFiles(eegFile , opt.bidstask);
infoFile = filterFiles(infoFile , opt.bidstask);
channelFile = filterFiles(channelFile , opt.bidstask);
elecFile = filterFiles(elecFile , opt.bidstask);
eventFile = filterFiles(eventFile , opt.bidstask);
eventDescFile = filterFiles(eventDescFile, opt.bidstask);
coordFile = filterFiles(coordFile , opt.bidstask);
behFile = filterFiles(behFile , opt.bidstask);
end
% check the task
if ~isempty(opt.runs)
eegFile = filterFilesRun(eegFile , opt.runs);
infoFile = filterFilesRun(infoFile , opt.runs);
channelFile = filterFilesRun(channelFile , opt.runs);
elecFile = filterFilesRun(elecFile , opt.runs);
eventFile = filterFilesRun(eventFile , opt.runs);
eventDescFile = filterFilesRun(eventDescFile, opt.runs);
% no runs for BEH or coordsystem
end
% raw data
allFiles = { eegFile.name };
ind = strmatch( 'json', cellfun(@(x)x(end-3:end), allFiles, 'uniformoutput', false) );
if ~isempty(ind)
eegFileJSON = allFiles(ind);
allFiles(ind) = [];
end
ind = strmatch( '.set', cellfun(@(x)x(end-3:end), allFiles, 'uniformoutput', false) ); % EEGLAB
if ~isempty(ind)
eegFileRawAll = allFiles(ind);
elseif length(allFiles) == 1
eegFileRawAll = allFiles;
else
ind = strmatch( '.eeg', cellfun(@(x)x(end-3:end), allFiles, 'uniformoutput', false) ); % BVA
if isempty(ind)
ind = strmatch( '.edf', cellfun(@(x)x(end-3:end), allFiles, 'uniformoutput', false) ); % EDF
if isempty(ind)
ind = strmatch( '.bdf', cellfun(@(x)x(end-3:end), allFiles, 'uniformoutput', false) ); % BDF
if isempty(ind)
ind = strmatch( '.fif', cellfun(@(x)x(end-3:end), allFiles, 'uniformoutput', false) ); % FIF
if isempty(ind)
ind = strmatch( '.gz', cellfun(@(x)x(end-2:end), allFiles, 'uniformoutput', false) ); % FIF
if isempty(ind) && ~isempty(allFiles)
ind = strmatch( '.ds', cellfun(@(x)x(end-2:end), allFiles, 'uniformoutput', false) ); % DS
if isempty(ind) && ~isempty(allFiles)
ind = strmatch( '.mefd', cellfun(@(x)x(end-4:end), allFiles, 'uniformoutput', false) ); % MEFD
if isempty(ind) && ~isempty(allFiles)
fprintf(2, 'No EEG/MEG file found for subject %s\n', bids.participants{iSubject,pInd});
end
end
end
end
ind2 = cellfun(@(x)~isempty(strfind(x, 'acq-crosstalk')), allFiles(ind));
ind(ind2) = [];
end
end
end
eegFileRawAll = allFiles(ind);
end
% identify non-EEG data files
%--------------------------------------------------------------
if ~isempty(behFile) % should be a single file
if length(behFile) > 1
fprintf(2, 'More than 1 BEH file for a given subject, do not know what to do with it\n');
end
behData = readtable(fullfile(behFile(1).folder, behFile(1).name),'FileType','text');
end
otherModality = {};
motionData = {}; % can be multiple files (tracksys, runs)
if ~isempty(motionFile)
for iMotion = 1:numel(motionFile)
motionData{iMotion} = readtable(fullfile(motionFile(iMotion).folder, motionFile(iMotion).name),'FileType','text');
motionFile(iMotion).tracksys = extractBetween(motionFile(iMotion).name,'tracksys-','_');
motionFile(iMotion).run = extractBetween(motionFile(iMotion).name,'run-','_');
end
otherModality{end+1} = 'motion';
end
physioData = {}; % can be multiple files (runs)
if ~isempty(physioFile)
for iPhys = 1:numel(physioFile)
physioData{iPhys} = readtable(fullfile(physioFile(iPhys).folder, physioFile(iPhys).name),'FileType','text');
physioFile(iPhys).run = extractBetween(physioFile(iPhys).name,'run-','_');
end
otherModality{end+1} = 'physio';
end
% skip most import if set file with no need for modication
for iFile = 1:length(eegFileRawAll)
try
eegFileName = eegFileRawAll{iFile};
[~,tmpFileName,fileExt] = fileparts(eegFileName);
eegFileRaw = fullfile(subjectFolder{ iFold}, eegFileName);
eegFileNameOut = fullfile(subjectFolderOut{iFold}, [ tmpFileName '.set' ]);
% what is the run
iRun = 1;
ind = strfind(eegFileRaw, '_run-');
if ~isempty(ind)
tmpEegFileRaw = eegFileRaw(ind(1)+5:end);
indUnder = find(tmpEegFileRaw == '_');
iRun = str2double(tmpEegFileRaw(1:indUnder(1)-1));
if isnan(iRun)
iRun = str2double(tmpEegFileRaw(1:indUnder(1)-2)); % rare case run 5H in ds003190/sub-01/ses-01/eeg/sub-01_ses-01_task-ctos_run-5H_eeg.eeg
if isnan(iRun)
error('Problem converting run information');
end
end
% check for BEH file
filePathTmp = fileparts(eegFileRaw);
behFileTmp = fullfile(filePathTmp,'..', 'beh', [eegFileRaw(1:ind(1)-1) '_beh.tsv' ]);
if exist(behFileTmp, 'file')
behData = readtable(behFileTmp,'FileType','text');
else
behData = [];
end
else
% check for BEH file
[filePathTmp, fileBaseTmp ] = fileparts(eegFileRaw);
behFileTmp = fullfile(filePathTmp, '..', 'beh', [fileBaseTmp(1:end-4) '_beh.tsv' ]);
if exist(behFileTmp, 'file')
try
behData = readtable(behFileTmp,'FileType','text');
catch
disp('Warning: could not load BEH file');
end
else
behData = [];
end
end
% extract task name and modality
underScores = find(tmpFileName == '_');
if ~strcmpi(tmpFileName(underScores(end)+1:end), 'ieeg')
if ~strcmpi(tmpFileName(underScores(end)+1:end), 'eeg')
if ~strcmpi(tmpFileName(underScores(end)+1:end), 'meg.fif')
if ~strcmpi(tmpFileName(underScores(end)+1:end), 'meg')
error('Data file name does not contain eeg, ieeg, or meg'); % theoretically impossible
else
modality = 'meg';
end
else
modality = 'meg';
end
else
modality = 'eeg';
end
else
modality = 'ieeg';
end
% JSON information file
infoData = bids_importjson([ eegFileRaw(1:end-8) '_' modality '.json' ], ['_' modality '.json']); % bids_loadfile([ eegFileRaw(1:end-8) '_eeg.json' ], infoFile);
bids.data = setallfields(bids.data, [iSubject-1,iFold,iFile], infoData);
if contains(tmpFileName,'task')
tStart = strfind(tmpFileName,'_task')+1;
tEnd = underScores - tStart;
tEnd = min(tEnd(tEnd>0)) + tStart - 1;
task = tmpFileName(tStart:tEnd);
end
if ~strcmpi(fileExt, '.set') || strcmpi(opt.bidsevent, 'on') || strcmpi(opt.bidschanloc, 'on') || ~strcmpi(opt.outputdir, bidsFolder)
fprintf('Importing file: %s\n', eegFileRaw);
switch lower(fileExt)
case '.set' % do nothing
if strcmpi(opt.metadata, 'on')
EEG = pop_loadset( 'filename', eegFileRaw, 'loadmode', 'info' );
else
EEG = pop_loadset( 'filename', eegFileName, 'filepath', subjectFolder{iFold});
end
case {'.bdf','.edf'}
EEG = pop_biosig( eegFileRaw ); % no way to read meta data only (because events in channel)
case '.eeg'
[tmpPath,tmpFileName,~] = fileparts(eegFileRaw);
if exist(fullfile(tmpPath, [tmpFileName '.vhdr']), 'file')
ext = '.vhdr';
elseif exist(fullfile(tmpPath, [tmpFileName '.VHDR']), 'file'),
ext = '.VHDR';
else
fprintf(2, 'Warning: eeg file found without BVA header file\n');
break;
end
if strcmpi(opt.metadata, 'on')
EEG = pop_loadbv( tmpPath, [tmpFileName ext], [], [], true );
else
EEG = pop_loadbv( tmpPath, [tmpFileName ext] );
end
case '.fif'
EEG = pop_fileio(eegFileRaw); % fif folder
case '.gz'
gunzip(eegFileRaw);
EEG = pop_fileio(eegFileRaw(1:end-3)); % fif folder
case '.ds'
if strcmpi(opt.ctffunc, 'fileio')
EEG = pop_fileio(eegFileRaw);
else
EEG = pop_ctf_read(eegFileRaw);
end
case '.mefd'
if ~exist('pop_MEF3', 'file')
error('MEF plugin not present, please install the MEF3 plugin first')
end
EEG = pop_MEF3(eegFileRaw); % MEF folder
otherwise
error('No EEG data found for subject/session %s', subjectFolder{iFold});
end
EEG = eeg_checkset(EEG);
% check for group information: get from participants
% file if doesn't exist
if isempty(EEG.group) && sum(ismember(lower(bids.participants(1,:)),'group'))
igroup = bids.participants{iSubject,ismember(lower(bids.participants(1,:)),'group')};
if ~isempty(igroup)
EEG.group = igroup;
end
end
EEGnodata = EEG;
EEGnodata.data = [];
bids.data = setallfields(bids.data, [iSubject-1,iFold,iFile], struct('EEG', EEGnodata));
% channel location data
% ---------------------
selected_chanfile = bids_get_file(eegFileRaw(1:end-8), '_channels.tsv', channelFile);
selected_elecfile = bids_get_file(eegFileRaw(1:end-8), '_electrodes.tsv', elecFile);
if strcmpi(opt.bidschanloc, 'on')
[EEG, channelData, elecData] = bids_importchanlocs(EEG, selected_chanfile, selected_elecfile);
if isempty(EEG.chanlocs) || ~isfield(EEG.chanlocs, 'theta') || all(cellfun(@isempty, { EEG.chanlocs.theta }))
EEG = bids_chan_lookup(EEG, infoData);
end
else
channelData = bids_loadfile(selected_chanfile);
elecData = bids_loadfile(selected_elecfile);
if ~isfield(EEG.chanlocs, 'theta') || all(cellfun(@isempty, { EEG.chanlocs.theta }))
EEG = bids_chan_lookup(EEG, infoData);
else
disp('The EEG file has channel locations associated with it, we are keeping them');
end
end
% look up EEG channel type
disp('Looking up/checking channel type from channel labels');
EEG = eeg_getchantype(EEG);
bids.data = setallfields(bids.data, [iSubject-1,iFold,iFile], struct('chaninfo', { channelData }));
bids.data = setallfields(bids.data, [iSubject-1,iFold,iFile], struct('elecinfo', { elecData }));
% event data
% ----------
if strcmpi(opt.bidsevent, 'on')
eventfile = bids_get_file(eegFileRaw(1:end-8), '_events.tsv', eventFile);
selected_eventdescfile = bids_get_file(eegFileRaw(1:end-8), '_events.json', eventDescFile);
if ~isempty(eventfile)
[EEG, bids, eventData, eventDesc] = bids_importeventfile(EEG, eventfile, 'bids', bids, 'eventDescFile', selected_eventdescfile, 'eventtype', opt.eventtype);
if isempty(eventData), error('bidsevent on but events.tsv has no data'); end
bids.data = setallfields(bids.data, [iSubject-1,iFold,iFile], struct('eventinfo', {eventData}));
bids.data = setallfields(bids.data, [iSubject-1,iFold,iFile], struct('eventdesc', {eventDesc}));
end
end
% coordsystem file
% ----------------
if strcmpi(opt.bidscoord, 'on')
coordFile = bids_get_file(eegFileRaw(1:end-8), '_coordsystem.json', coordFile);
[EEG, bids] = bids_importcoordsystemfile(EEG, coordFile, 'bids', bids);
end
% copy information inside dataset
EEG.subject = bids.participants{iSubject,pInd};
EEG.session = iFold;
EEG.run = iRun;
EEG.task = task(6:end); % task is currently of format "task-<Task name>"
% build `EEG.BIDS` from `bids`
BIDS.gInfo = bids.dataset_description;
BIDS.gInfo.README = bids.README;
BIDS.gInfo.CHANGES = bids.CHANGES;
BIDS.pInfo = [bids.participants(1,:); bids.participants(iSubject,:)]; % header -> iSubject info
BIDS.pInfoDesc = bids.participantsJSON;
BIDS.eInfo = bids.eventInfo;
BIDS.eInfoDesc = bids.data.eventdesc;
BIDS.tInfo = infoData;
BIDS.bidsstats = stats;
if ~isempty(elecData)
BIDS.scannedElectrodes = true;
end
if ~isempty(behData)
behData = table2struct(behData);
end
BIDS.behavioral = behData;
EEG.BIDS = BIDS;
if strcmpi(opt.metadata, 'off')
if exist(subjectFolderOut{iFold},'dir') ~= 7
mkdir(subjectFolderOut{iFold});
end
EEG = pop_saveset( EEG, eegFileNameOut);
end
end
% building study command
commands = [ commands { 'index' count 'load' eegFileNameOut 'subject' bids.participants{iSubject,pInd} 'session' iFold 'task' task(6:end) 'run' iRun } ];
% custom numerical fields
for iCol = 2:size(bids.participants,2)
commands = [ commands { bids.participants{1,iCol} bids.participants{iSubject,iCol} } ];
end
if isstruct(behData) && ~isempty(behData)
behFields = fieldnames(behData);
if length(behData) > 1
warning('Behavioral data length larger than 1, only retaining the first element');
end
for iFieldBeh = 1:length(behFields)
commands = [ commands { behFields{iFieldBeh} [behData(1).(behFields{iFieldBeh})] } ];
end
end
count = count+1;
% check dataset consistency
bData = bids.data(iSubject-1,iFold,iFile);
if ~isempty(bData.chaninfo)
if size(bData.chaninfo,1)-1 ~= bData.EEG.nbchan
warning('Warning: inconsistency detected, %d channels in BIDS file vs %d in EEG file for %s\n', size(bData.chaninfo,1)-1, bData.EEG.nbchan, [tmpFileName,fileExt]);
inconsistentChannels = inconsistentChannels+1;
end
end
%{
if ~isempty(bData.eventinfo)
if size(bData.eventinfo,1)-1 ~= length(bData.EEG.event)
fprintf(2, 'Warning: inconsistency detected, %d events in BIDS file vs %d in EEG file for %s\n', size(bData.eventinfo,1)-1, length(bData.EEG.event), [tmpFileName,fileExt]);
inconsistentEvents = inconsistentEvents+1;
end
end
%}
catch ME
faileddatasets = [faileddatasets eegFileRaw];
end
end % end for eegFileRawAll
% import data of other tpyes than EEG, MEG, iEEG
for iMod = 1:numel(otherModality)
try
dataType = otherModality{iMod};
dataFile = eval([dataType 'File']);
dataRaw = eval([dataType 'Data']); % cell array containing tables
subjectID = bids.participants{iSubject,pInd};
subjectDataFolder = subjectFolder{iFold}(1:end-3);
subjectDataFolderOut = subjectFolderOut{iFold}(1:end-3);
if isfield(bids.data(iSubject-1,iFold,1), 'scans')
scansRaw = bids.data(iSubject-1,iFold,1).scans;
else
scansRaw = [];
end
for iDat = 1:numel(dataFile)
[DATA, dataFileOut] = import_noneeg(dataType, dataFile(iDat), dataRaw{iDat}, subjectID, scansRaw, iFold, strcmpi(opt.metadata, 'on'), strcmpi(opt.bidschanloc, 'on'), useScans, subjectDataFolder, subjectDataFolderOut);
if strcmpi(opt.metadata, 'off')
if exist([subjectFolderOut{iFold}(1:end-3), dataType],'dir') ~= 7
mkdir(subjectFolderOut{iFold}(1:end-3), dataType);
end
pop_saveset(DATA, dataFileOut);
end
end
catch exception
fprintf('Error importing non i/M/EEG modality %s. Skipped\n', dataType);
fprintf(getReport(exception));
end
end
fclose all;
end
end
end
% update statistics
% -----------------
stats = bids_metadata_stats(bids, inconsistentChannels);
% study name and study creation
% -----------------------------
if strcmpi(opt.metadata, 'off')
if isempty(commands)
error('No dataset were found');
end
studyName = fullfile(opt.outputdir, [opt.studyName '.study']);
if exist('tasklist','var') && length(tasklist)~=1 && isempty(opt.bidstask)
[STUDY, ALLEEG] = std_editset([], [], 'commands', commands, 'filename', studyName, 'task', 'task-mixed');
else
[STUDY, ALLEEG] = std_editset([], [], 'commands', commands, 'filename', studyName, 'task', task);
end
if ~isempty(options)
commands = sprintf('[STUDY, ALLEEG] = pop_importbids(''%s'', %s);', bidsFolder, vararg2str(options));
else
commands = sprintf('[STUDY, ALLEEG] = pop_importbids(''%s'');', bidsFolder);
end
end
% track failed datasets
STUDY.etc.bidsimportinfo = [];
STUDY.etc.bidsimportinfo.totaldatasetcount = numel(eegFileRawAll);
STUDY.etc.bidsimportinfo.faileddatasets = faileddatasets;
% import HED tags if exists in top level events.json
% -----------------------------
% scan for top level events.json
top_level_eventsjson = dir(fullfile(bidsFolder, '*_events.json'));
if ~isempty(top_level_eventsjson) && numel(top_level_eventsjson) == 1
top_level_eventsjson = fullfile(top_level_eventsjson.folder, top_level_eventsjson.name);
if plugin_status('HEDTools')
try
fMap = fieldMap.createfMapFromJson(top_level_eventsjson);
if fMap.hasAnnotation()
STUDY.etc.tags = fMap.getStruct();
end
catch ME
warning('Found top-level events.json file and tried importing HED tags but failed');
end
end
end
% search parent folders (outward search) for the file of given fileName
% ---------------------
function outFile = searchparent(folder, fileName)
% search nestedly outward
% only get exact match and filter out hidden file
outFile = '';
parent = folder;
count = 4;
while ~any(arrayfun(@(x) strcmp(lower(x.name),'dataset_description.json'), dir(parent))) && isempty(outFile) && count > 0 % dataset_description indicates root BIDS folder
outFile = filterHiddenFile(folder, dir(fullfile(parent, fileName)));
parent = fileparts(parent);
count = count-1;
end
if isempty(outFile)
outFile = filterHiddenFile(parent, dir(fullfile(parent, fileName)));
end
function fileList = filterHiddenFile(folder, fileList)
isGoodFile = true(1,numel(fileList));
% loop to identify hidden files
for iFile = 1:numel(fileList) %'# loop only non-dirs
% on OSX, hidden files start with a dot
isGoodFile(iFile) = logical(~strcmp(fileList(iFile).name(1),'.'));
if isGoodFile(iFile) && ispc
% check for hidden Windows files - only works on Windows
[~,stats] = fileattrib(fullfile(folder,fileList(iFile).name));
if stats.hidden
isGoodFile(iFile) = false;
end
end
end
% remove bad files
fileList = fileList(isGoodFile);
% Filter files
% ------------
function fileList = filterFiles(fileList, taskList)
keepInd = zeros(1,length(fileList));
for iFile = 1:length(fileList)
if contains(fileList(iFile).name, taskList)
keepInd(iFile) = 1;
end
end
fileList = fileList(logical(keepInd));
% Filter file runs
% ----------------
function fileList = filterFilesRun(fileList, runs)
keepInd = zeros(1,length(fileList));
for iFile = 1:length(fileList)
runInd = strfind(fileList(iFile).name, '_run-');
if ~isempty(runInd)
strTmp = fileList(iFile).name(runInd+5:end);
underScore = find(strTmp == '_');
if any(runs == str2double(strTmp(1:underScore(1)-1)))
keepInd(iFile) = 1;
end
end
end
fileList = fileList(logical(keepInd));
% set structure
% -------------
function sdata = setallfields(sdata, indices, newdata)
if isempty(newdata), return; end
if ~isstruct(newdata), error('Can only assign structures'); end
if length(indices) < 3, error('Must have 3 indices'); end
allFields = fieldnames(newdata);
for iField = 1:length(allFields)
sdata(indices(1), indices(2), indices(3)).(allFields{iField}) = newdata.(allFields{iField});
end
% get BIDS file
function filestr = bids_get_file(baseName, ext, alternateFile)
filestr = '';
if exist([ baseName ext ], 'file')
filestr = [ baseName ext ];
else
if ~isempty(alternateFile) && isfield(alternateFile, 'folder') && isfield(alternateFile, 'name')
tmpFile = fullfile(alternateFile(1).folder, alternateFile(1).name);
if exist(tmpFile, 'file')
filestr = tmpFile;
end
end
end
% format other data types than EEG, MEG, iEEG
%--------------------------------------------
function [DATA, dataFileOut] = import_noneeg(dataType, dataFile, dataRaw, subject, scansData, session, onlyMetadata, useChanlocs, useScans, subjectFolder, subjectDataFolderOut)
disp(['Processing ' dataType ' data'])
% replace extension tsv with set
[~,fileName,fileExt] = fileparts(dataFile.name);
dataFileOut = fullfile(subjectDataFolderOut, dataType, [fileName '.set']);
% check for json file that might be applicable to the current file in current directory using rule 2.b and 2.c
% of https://bids-specification.readthedocs.io/en/stable/common-principles.html#the-inheritance-principle
bids_entity = strsplit(fileName, '_');
bids_entity = bids_entity{end};
dataFileJSON = fullfile([subjectFolder, dataType], ['*_' bids_entity '.json']);
% resolve wildcard if applicable
dataFileJSONDir = dir(dataFileJSON);
if ~isempty(dataFileJSONDir)
for u=1:numel(dataFileJSONDir)
dataFileJSONName_parts = strsplit(dataFileJSONDir(u).name, '_');
dataFileJSONName_parts = dataFileJSONName_parts(1:end-1); % only consider the suffices
if all(cellfun(@(x) contains(fileName, x), dataFileJSONName_parts))
dataFileJSON = fullfile(dataFileJSONDir(u).folder, dataFileJSONDir(u).name);
break
end
end
end
infoData = bids_importjson(dataFileJSON);
% check or construct needed channel files according to the data type
switch dataType
case 'motion'
% look for associated *_channels.tsv
% since *_motion.json share the same principle and already
% looked up above, we only change the postfix
channelFileMotion = [dataFileJSON(1:end-numel('motion.json')) 'channels.tsv']; % replace 'motion.json' with 'channels.tsv'
% channelFile = searchparent([subjectFolder, dataType], channelFileMotion);
channelData = bids_loadfile(channelFileMotion); %channelFile(1).name, channelFile);
case 'physio'
% channel file (for physio data, hidden in json file as 'columns')
channelData = {'name', 'type', 'units'};
for Ci = 1:numel(infoData.Columns)
channelData{end + 1,1} = [char(infoData.Columns{Ci})] ;
end
end
switch lower(fileExt)
case '.set' % do nothing
if onlyMetadata
DATA = pop_loadset( 'filename', dataFile.name, 'loadmode', 'info' );
else
DATA = pop_loadset( 'filename', dataFile.name );
end
case '.tsv'
DATA = eeg_emptyset;
DATA.data = table2array(dataRaw)';
if strcmp(dataType,'motion')
if isfield(infoData, 'SamplingFrequencyEffective')
% 'SamplingFrequencyEffective' can be used if nominal
% https://bids-specification.readthedocs.io/en/stable/modality-specific-files/motion.html#motion-specific-fields
DATA.srate = infoData.SamplingFrequencyEffective;
if isfield(infoData, 'SamplingFrequency')
DATA.etc.nominal_srate = infoData.SamplingFrequency;
end
else
DATA.srate = infoData.SamplingFrequency;
end
else
try
DATA.srate = infoData.SamplingFrequencyEffective; % Actual sampling rate used in motion data. Note that the unit of the time must be in second.
catch
DATA.srate = infoData.SamplingFrequency; % Generic physio data
end
end
% find latency channel
headers = dataRaw.Properties.VariableNames;
useLatency = 0;
if strcmp(dataType,'motion')
latencyInd = find(strcmpi(channelData(:,strcmp(channelData(1,:),'type')), 'latency'));
useLatency = ~isempty(latencyInd);
if useLatency
latencyHeader = channelData{latencyInd,strcmp(channelData(1,:),'name')};
latencyRowInData = find(strcmp(headers, latencyHeader));
end
elseif strcmp(dataType,'physio')
% check if the tracking system comes with latency
latencyInd = find(contains(channelData(:,strcmp(channelData(1,:),'name')), 'latency'));
useLatency = ~isempty(latencyInd);
if useLatency
latencyHeader = channelData{latencyInd,strcmp(channelData(1,:),'name')};
latencyRowInData = find(strcmp(headers, latencyHeader));
end
end
% reconstruct time : use scans.tsv for synching
% it computes offset between motion and eeg data
if useScans
for Coli = 1:size(scansData, 2)
if strcmp(scansData{1,Coli}, 'acq_time')
acqTimeColi = Coli;
elseif strcmp(scansData{1,Coli}, 'filename')
fNameColi = Coli;
end
end
for Rowi = 1:size(scansData, 1)
sesString = '';
taskString = '';
runString = '';
trackSysString = '';
if exist('tracksys', 'var') && ~isempty(tracksys)
trackSysString = tracksys;
end
splitName = regexp(dataFileOut,'_','split');
for SNi = 1:numel(splitName)
if contains(splitName{SNi}, 'ses-')
sesString = splitName{SNi}(5:end);
elseif contains(splitName{SNi}, 'task-')
taskString = splitName{SNi}(6:end);
elseif contains(splitName{SNi}, 'run-')
runString = splitName{SNi}(5:end);
end
end
% find files that matches in session, task, tracking system (in case it is motion data), and run
if contains(scansData{Rowi,fNameColi}, 'eeg.') &&...
contains(scansData(Rowi,fNameColi), sesString) && contains(scansData(Rowi,fNameColi), taskString) &&...
contains(scansData(Rowi,fNameColi), runString)
eegAcqTime = scansData(Rowi,acqTimeColi);
elseif contains(scansData(Rowi,fNameColi), sesString) && contains(scansData(Rowi,fNameColi), taskString) &&...
contains(scansData(Rowi,fNameColi), runString) && contains(scansData(Rowi,fNameColi), trackSysString) &&...
contains(scansData(Rowi,fNameColi), dataType)
otherAcqTime = scansData(Rowi,acqTimeColi);
end
end
startTime = seconds(datetime(otherAcqTime{1}, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss.SSS') - datetime(eegAcqTime{1}, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss.SSS'));
else
if isfield(infoData, 'StartTime')
if isnumeric(infoData.StartTime)
startTime = infoData.StartTime;