forked from sccn/EEG-BIDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_tobids.m
209 lines (182 loc) · 9.76 KB
/
std_tobids.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
function STUDY = std_tobids(varargin)
% From a STUDY rename files if needed, sort folders, and export metadata
% following the BIDS specification
%
% FORMAT STUDY = std_tobids(STUDY,options)
% options are: 'export_dir' followed by the path
% 'keep_files' being 'yes' (default) or 'no'
% 'update_study' being 'yes' or 'no'
%
% Author: Cyril Pernet - LIMO Team, University of Edinurgh
% Arnaud Delorme, EEGLAB, SCCN, 2018
%
%% options
if nargin == 1
export_dir = STUDY.filepath; % where to make the bids directory
keep_files = 'no'; % copy files to move and rename
update_study = 'yes'; % return study with updated names and paths
else
for in = 2:nargin
if strcmpi(varargin{in},'export_dir') || strcmpi(varargin{in},'exportdir')
export_dir = varargin{in+1};
elseif strcmpi(varargin{in},'keep_files') || strcmpi(varargin{in},'keepfiles')
keep_files = varargin{in+1};
elseif strcmpi(varargin{in},'update_study') || strcmpi(varargin{in},'updatestudy')
update_study = varargin{in+1};
end
end
end
%% get subjects info
N = length(STUDY.subject);
if isempty(STUDY.task)
STUDY.task = inputdlg2('input task name','task info missing'); % @Arno inputdlg2 fails
end
% task name with no space
task = lower(STUDY.task);
task(1) = upper(task(1));
task(find(isspace(STUDY.task))+1) = upper(task(find(isspace(STUDY.task))+1));
task(isspace(task)) = [];
% let's get the index of which files belong to the same subject
datasetindex = cell(N,length(STUDY.session));
if length(STUDY.session) > 1
for subject =1:N
file_index = 1;
for files = 1:length(STUDY.datasetinfo)
if strcmp(STUDY.subject{subject},STUDY.datasetinfo(files).subject)
datasetindex{subject,file_index} = files;
file_index = file_index+1;
end
end
end
else
for subject =1:N
datasetindex{subject} = STUDY.subject{subject};
end
end
% % check if subjects are all together or in different folders
% for files = 1:length(STUDY.datasetinfo)
% paths{files} = STUDY.datasetinfo(files).filepath;
% end
% for each subject, move/rename and export metadata
for subject =1:N
if strncmp(STUDY.subject{subject},'sub-',4)
newname = STUDY.subject{subject};
else
newname = ['sub-' STUDY.subject{subject}];
end
% create sub- folder
if strcmp(export_dir(length(export_dir)),filesep)
export_dir = export_dir(1:end-1);
end
subj_dir = [export_dir filesep newname filesep 'eeg'];
subj_derivatives_dir = [export_dir filesep 'derivatives' filesep newname filesep 'eeg'];
mkdir(subj_dir)
% rename/move files
for run = 1:length(STUDY.session)
if ~isempty(datasetindex{subject,run})
if ischar(datasetindex{subject,run})
tmp = STUDY.datasetinfo(str2num(datasetindex{subject,run}));
else
tmp = STUDY.datasetinfo(datasetindex{subject,run});
end
[~,name,ext]=fileparts(tmp.filename);
if strcmpi(keep_files,'no')
movefile([tmp.filepath filesep tmp.filename],[subj_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_eeg' ext]);
movefile([tmp.filepath filesep name '.fdt'], [subj_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_eeg.fdt']);
elseif strcmpi(keep_files,'yes')
copyfile([tmp.filepath filesep tmp.filename],[subj_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_eeg' ext]);
copyfile([tmp.filepath filesep name '.fdt'], [subj_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_eeg.fdt']);
end
EEG = pop_loadset([subj_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_eeg' ext]);
EEG.setname = [subj_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_eeg'];
EEG.datfile = [newname '_task-' task '_sess-1_run-' num2str(run) '_eeg.fdt'];
% metadata
SF(subject,run) = EEG.srate;
CC(subject,run) = size(EEG.chanlocs,2);
Ref{subject,run} = EEG.ref;
% check derivatives and update .set
if exist([tmp.filepath filesep STUDY.subject{subject} '.daterp'],'file') || ...
exist([tmp.filepath filesep STUDY.subject{subject} '.daterpim'],'file') || ...
exist([tmp.filepath filesep STUDY.subject{subject} '.datspec'],'file') || ...
exist([tmp.filepath filesep STUDY.subject{subject} '.dattimef'],'file')
name = STUDY.subject{subject}; % otherwise comes from STUDY.datasetinfo, depends if computed from study or not
end
if exist([tmp.filepath filesep name '.daterp'],'file')
if ~exist(subj_derivatives_dir,'dir'); mkdir(subj_derivatives_dir); end
if strcmpi(keep_files,'no')
movefile([tmp.filepath filesep name '.daterp'], [subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-erp_eeg.daterp']);
elseif strcmpi(keep_files,'yes')
copyfile([tmp.filepath filesep name '.daterp'], [subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-erp_eeg.daterp']);
end
EEG.etc.datafiles.daterp = [subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-erp_eeg.daterp'];
end
if exist([tmp.filepath filesep name '.daterpim'],'file')
if ~exist(subj_derivatives_dir,'dir'); mkdir(subj_derivatives_dir); end
if strcmpi(keep_files,'no')
movefile([tmp.filepath filesep name '.daterpim'],[subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-erpimg_eeg.daterpim']);
elseif strcmpi(keep_files,'yes')
copyfile([tmp.filepath filesep name '.daterpim'],[subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-erpimg_eeg.daterpim']);
end
EEG.etc.datafiles.daterpim = [subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-erpimg_eeg.daterpim'];
end
if exist([tmp.filepath filesep name '.datspec'],'file')
if ~exist(subj_derivatives_dir,'dir'); mkdir(subj_derivatives_dir); end
if strcmpi(keep_files,'no')
movefile([tmp.filepath filesep name '.datspec'],[subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-spectrum_eeg.datspec']);
elseif strcmpi(keep_files,'yes')
copyfile([tmp.filepath filesep name '.datspec'],[subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-spectrum_eeg.datspec']);
EEG.etc.datafiles.datspec = [subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-spectrum_eeg.datspec'];
end
end
if exist([tmp.filepath filesep name '.dattimef'],'file')
if ~exist(subj_derivatives_dir,'dir'); mkdir(subj_derivatives_dir); end
if strcmpi(keep_files,'no')
movefile([tmp.filepath filesep name '.dattimef'],[subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-timefrequency_eeg.dattimef']);
elseif strcmpi(keep_files,'yes')
copyfile([tmp.filepath filesep name '.dattimef'],[subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-timefrequency_eeg.dattimef']);
EEG.etc.datafiles.dattimef = [subj_derivatives_dir filesep newname '_task-' task '_sess-1_run-' num2str(run) '_desc-timefrequency_eeg.dattimef'];
end
end
% resave the .set with updated info
pop_saveset(EEG,'savemode','resave');
% export metadata
if run == 1
electrodes_to_tsv(EEG);
end
channelloc_to_tsv(EEG);
events_to_tsv(EEG);
end
if strcmpi(update_study,'yes')
STUDY.datasetinfo(subject).filepath = subj_dir;
STUDY.datasetinfo(subject).filename = [newname '_task-' task '_sess-1_run-' num2str(run) '_eeg' ext];
STUDY.datasetinfo(subject).subject = newname;
STUDY.subject{subject} = newname;
end
end
end
% update task name
if strcmpi(update_study,'yes')
STUDY.task = task;
STUDY = pop_savestudy(STUDY,'savemode','resave');
end
% finish of with root metadata
% ----------------------------
% make *_eeg.json
PLF = str2num(cell2mat(inputdlg('What was the Power Line Frequency?','BIDS spec requirement')));
if size(unique(Ref),1) > 1
warning('difference referencing scheme found of this dataset')
Ref = cell2mat(inputdlg('What was the reference?','BIDS spec requirement'));
end
json = struct('TaskName',task, ...
'SamplingFrequency', mean(SF(:)), ...
'EEGChannelCount', max(CC(:)), ...
'EEGReference', unique(Ref), ...
'PowerLineFrequency', PLF, ...
'SoftwareFilters', ' ');
jsonwrite([export_dir filesep task '_eeg.json'],json,struct('indent',' '))
% make a participants table and save
age = zeros(N,1);
sex = repmat(' ',[N 1]);
t = table(STUDY.subject',age,sex,'VariableNames',{'participant_id','age','sex'});
writetable(t,[export_dir filesep 'Participants.tsv'],'FileType','text','Delimiter','\t');
warndlg('Job done, but metadata created need editing','BIDS spec','modal')