-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBidsBuildStudyFiles.m
133 lines (124 loc) · 4.72 KB
/
BidsBuildStudyFiles.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
function BidsInfo = BidsBuildStudyFiles(BidsFolder, BidsInfo, Overwrite, SaveFiles, RemoveEmptyFields, isEvents)
% Create BIDS metadata files for a dataset.
% Satisfies BIDS version indicated at the start of the code below.
%
% INPUT: BidsFolder: Path to the root study folder
%
% Authors: Elizabeth Bock, Marc Lalancette, 2017 - 2024-10-11
BIDSVersion = '1.10.0';
if nargin < 5 || isempty(RemoveEmptyFields)
RemoveEmptyFields = false;
end
if nargin < 4 || isempty(SaveFiles)
SaveFiles = true;
end
if nargin < 3 || isempty(Overwrite)
Overwrite = false;
end
if nargin < 2 || isempty(BidsInfo)
BidsInfo = struct();
end
if nargin < 6 || isempty(isEvents)
if isfield(BidsInfo, 'Events')
isEvents = true;
else
isEvents = false;
end
end
if nargout
Output = true;
else
Output = false;
end
if ~SaveFiles && ~Output
% Nothing to do.
warning('Selected options leave nothing to do.');
return;
end
% Dependencies
BidsSetPath;
DataDescripFile = fullfile(BidsFolder, 'dataset_description.json');
if ~exist(DataDescripFile, 'file') || Overwrite || Output
% Mostly optional fields, see if they're in BidsInfo.
J.DatasetType = 'raw';
for Field = {'Name', 'BIDSVersion', 'DatasetType', 'HEDVersion', 'License', 'Authors', 'Acknowledgements', ...
'HowToAcknowledge', 'Funding', 'EthicsApprovals', 'ReferencesAndLinks', 'DatasetDOI'}
Field = Field{1}; %#ok<FXSET>
if isfield(BidsInfo, Field)
J.(Field) = BidsInfo.(Field);
if Output
BidsInfo = rmfield(BidsInfo, Field);
end
elseif isfield(BidsInfo, 'Dataset') && isfield(BidsInfo.Dataset, Field)
J.(Field) = BidsInfo.Dataset.(Field);
end
end
if RemoveEmptyFields
J = StructTrim(J);
end
% Required fields
if ~isfield(J, 'Name') || isempty(J.Name)
if isfield(BidsInfo, 'Study') && ~isempty(BidsInfo.Study)
J.Name = BidsInfo.Study;
if Output
BidsInfo = rmfield(BidsInfo, 'Study');
end
else
[~, J.Name] = fileparts(BidsFolder); % Often not really appropriate name, but keep for now as it is a required field.
end
end
if ~isfield(J, 'BIDSVersion') || isempty(J.BIDSVersion)
J.BIDSVersion = BIDSVersion;
end
if SaveFiles
WriteJson(DataDescripFile, J);
end
if Output
BidsInfo.Dataset = J;
BidsInfo.Files.Dataset = DataDescripFile;
BidsInfo.BidsFolder = BidsFolder;
end
end
if isfield(BidsInfo, 'Ignore') && ~isempty(BidsInfo.Ignore)
% create .bidsignore files
% ignore .log files inside the meg folders
IgnoreFile = fullfile(BidsFolder, '.bidsignore');
if (~exist(IgnoreFile, 'file') || Overwrite) && SaveFiles
Fid = fopen(IgnoreFile, 'w');
for i = 1:numel(BidsInfo.Ignore)
fprintf(Fid, '%s\n', BidsInfo.Ignore{i});
end
fclose(Fid);
end
if Output
BidsInfo.Files.Ignore = IgnoreFile;
% BidsInfo.Ignore = BidsInfo.Ignore;
end
end
if isEvents
EventsJsonFile = fullfile(BidsFolder, 'events.json');
if ~exist(EventsJsonFile, 'file') || Overwrite || Output
clear J
% Column description
%struct('LongName', '', 'Description', '', 'Levels', struct(), 'Units', '', 'TermURL', '');
J.onset = struct('Description', 'Event onset time, assuming CTF dataset trials recorded continuously', 'Units', 's');
%J.duration = struct('Description', 'Duration of the event.');
J.ds_trial = struct('Description', 'Dataset trial (epoch) in which the event is located in time. First trial is 1.');
J.sample = struct('Description', 'Sample within the dataset trial (epoch) corresponding to the event onset. First sample is 1.');
J.value = struct('Description', 'Name of the event.');
if isfield(BidsInfo, 'Events')
J = StructOverwrite(J, BidsInfo.Events);
end
if RemoveEmptyFields
J = StructTrim(J);
end
if SaveFiles
WriteJson(EventsJsonFile, J);
end
if Output
BidsInfo.Events = J;
BidsInfo.Files.Events = EventsJsonFile;
end
end
end
end