Skip to content

Commit 9e8cb6e

Browse files
committed
make createJson be able to handle EEG MEG iEEG BEH
1 parent 1596457 commit 9e8cb6e

File tree

4 files changed

+266
-78
lines changed

4 files changed

+266
-78
lines changed

src/createBoldJson.m

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/createJson.m

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
function createJson(varargin)
2+
% createBoldJson(cfg, varargin)
3+
%
4+
% Creates the side car JSON file for a run.
5+
% This will only contain the minimum BIDS requirement and will likey be less
6+
% complete than the info you could from a proper conversion.
7+
%
8+
% USAGE
9+
%
10+
% createJson(cfg, modality)
11+
%
12+
% modality is string ('beh', 'func', 'eeg', 'ieeg', 'meg') to specify which
13+
% JSON to save
14+
%
15+
%
16+
% createJson(cfg, extraInfo)
17+
%
18+
% extraInfo is a strcuture to add axtra content can be added to the JSON file
19+
%
20+
%
21+
% createJson(cfg, modality, extraInfo)
22+
%
23+
% both are possible
24+
%
25+
%
26+
27+
[cfg, modality, extraInfo] = checkInput(varargin);
28+
29+
if strcmp(modality, 'func')
30+
fileName = strrep(cfg.fileName.events, '_events', '_bold');
31+
jsonContent = cfg.bids.mri;
32+
else
33+
fileName = strrep(cfg.fileName.events, '_events', ['_' modality]);
34+
jsonContent = cfg.bids.(modality);
35+
end
36+
37+
fileName = strrep(fileName, '.tsv', '.json');
38+
39+
fileName = fullfile( ...
40+
cfg.dir.outputSubject, ...
41+
modality, ...
42+
fileName);
43+
44+
%% add content of extraInfo to the JSON content
45+
46+
fieldList = fieldnames(extraInfo);
47+
for iField = 1:numel(fieldList)
48+
jsonContent.(fieldList{iField}) = extraInfo.(fieldList{iField});
49+
end
50+
51+
%% save
52+
opts.Indent = ' ';
53+
bids.util.jsonencode(fileName, jsonContent, opts);
54+
55+
end
56+
57+
function [cfg, modality, extraInfo] = checkInput(varargin)
58+
59+
% trying to parse inputs
60+
% modality is either given as a string by user or guessed from cfg.fileName
61+
% extraInfo must be a structure
62+
63+
input = varargin{1};
64+
65+
modality = '';
66+
extraInfo = struct();
67+
68+
switch numel(input)
69+
70+
case 0
71+
errorCreateJson('notEnoughInput');
72+
73+
case 1
74+
cfg = input{1};
75+
if isfield(cfg.fileName, 'modality')
76+
modality = cfg.fileName.modality;
77+
end
78+
79+
case 2
80+
cfg = input{1};
81+
if ischar(input{2})
82+
modality = varargin{2};
83+
elseif isstruct(input{2})
84+
extraInfo = varargin{2};
85+
else
86+
errorCreateJson('wrongInputType');
87+
end
88+
89+
otherwise
90+
cfg = input{1};
91+
modality = varargin{2};
92+
extraInfo = varargin{3};
93+
94+
end
95+
96+
if ~ischar(modality) || ...
97+
all(~strcmpi(modality, {'beh', 'func', 'eeg', 'ieeg', 'meg'}))
98+
errorCreateJson('wrongModalityInput', modality);
99+
end
100+
101+
if ~isstruct(extraInfo)
102+
warning('Additional info added to a json file must be a structure.');
103+
end
104+
105+
end
106+
107+
function errorCreateJson(identifier, varargin)
108+
109+
switch identifier
110+
case 'notEnoughInput'
111+
errorStruct.message = 'First input must be a valid cfg structure.';
112+
113+
case 'wrongInputType'
114+
errorStruct.message = ['The second input must be a string (modality)' ...
115+
'or a structure (extraInfo)'];
116+
case 'wrongModalityInput'
117+
errorStruct.message = sprintf(['The given modality is not valid: %s.\n', ...
118+
'Only the following string are accepted: beh, func, eeg, ieeg, meg'], ...
119+
varargin{1});
120+
121+
end
122+
123+
errorStruct.identifier = ['createJson:' identifier];
124+
125+
error(errorStruct);
126+
end

tests/test_createBoldJson.m

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/test_createJson.m

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
function test_suite = test_createJson %#ok<*STOUT>
2+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
3+
test_functions = localfunctions(); %#ok<*NASGU>
4+
catch % no problem; early Matlab versions can use initTestSuite fine
5+
end
6+
initTestSuite;
7+
end
8+
9+
function test_createJsonFunc()
10+
11+
%% set up
12+
cfg = setUp();
13+
14+
cfg.testingDevice = 'mri';
15+
16+
cfg = createFilename(cfg);
17+
18+
logFile = saveEventsFile('init', cfg); %#ok<*NASGU>
19+
20+
createJson(cfg);
21+
22+
%% data to test against
23+
funcDir = fullfile(cfg.dir.output, 'source', 'sub-001', 'ses-001', 'func');
24+
25+
eventFilename = ['sub-001_ses-001_task-testtask_run-001_bold_date-' ...
26+
cfg.fileName.date '.json'];
27+
28+
%% test
29+
assertTrue(exist(fullfile(funcDir, eventFilename), 'file') == 2);
30+
31+
end
32+
33+
function test_createJsonBeh()
34+
35+
%% set up
36+
cfg = setUp();
37+
38+
cfg.testingDevice = 'pc';
39+
40+
cfg = createFilename(cfg);
41+
42+
logFile = saveEventsFile('init', cfg); %#ok<*NASGU>
43+
44+
createJson(cfg);
45+
46+
%% data to test against
47+
funcDir = fullfile(cfg.dir.output, 'source', 'sub-001', 'ses-001', 'beh');
48+
49+
eventFilename = ['sub-001_ses-001_task-testtask_run-001_beh_date-' ...
50+
cfg.fileName.date '.json'];
51+
52+
%% test
53+
assertTrue(exist(fullfile(funcDir, eventFilename), 'file') == 2);
54+
55+
end
56+
57+
function test_createJsonEeg()
58+
59+
%% set up
60+
cfg = setUp();
61+
62+
cfg.testingDevice = 'eeg';
63+
64+
cfg = createFilename(cfg);
65+
66+
logFile = saveEventsFile('init', cfg); %#ok<*NASGU>
67+
68+
createJson(cfg);
69+
70+
%% data to test against
71+
funcDir = fullfile(cfg.dir.output, 'source', 'sub-001', 'ses-001', 'eeg');
72+
73+
eventFilename = ['sub-001_ses-001_task-testtask_run-001_eeg_date-' ...
74+
cfg.fileName.date '.json'];
75+
76+
%% test
77+
assertTrue(exist(fullfile(funcDir, eventFilename), 'file') == 2);
78+
79+
end
80+
81+
function test_createJsonMeg()
82+
83+
%% set up
84+
cfg = setUp();
85+
86+
cfg.testingDevice = 'meg';
87+
88+
cfg = createFilename(cfg);
89+
90+
logFile = saveEventsFile('init', cfg); %#ok<*NASGU>
91+
92+
createJson(cfg);
93+
94+
%% data to test against
95+
funcDir = fullfile(cfg.dir.output, 'source', 'sub-001', 'ses-001', 'meg');
96+
97+
eventFilename = ['sub-001_ses-001_task-testtask_run-001_meg_date-' ...
98+
cfg.fileName.date '.json'];
99+
100+
%% test
101+
assertTrue(exist(fullfile(funcDir, eventFilename), 'file') == 2);
102+
103+
end
104+
105+
function test_createJsonIeeg()
106+
107+
%% set up
108+
cfg = setUp();
109+
110+
cfg.testingDevice = 'ieeg';
111+
112+
cfg = createFilename(cfg);
113+
114+
logFile = saveEventsFile('init', cfg); %#ok<*NASGU>
115+
116+
createJson(cfg);
117+
118+
%% data to test against
119+
funcDir = fullfile(cfg.dir.output, 'source', 'sub-001', 'ses-001', 'ieeg');
120+
121+
eventFilename = ['sub-001_ses-001_task-testtask_run-001_ieeg_date-' ...
122+
cfg.fileName.date '.json'];
123+
124+
%% test
125+
assertTrue(exist(fullfile(funcDir, eventFilename), 'file') == 2);
126+
127+
end
128+
129+
function cfg = setUp()
130+
131+
cfg.verbose = false;
132+
133+
cfg.subject.subjectNb = 1;
134+
cfg.subject.runNb = 1;
135+
136+
cfg.task.name = 'testtask';
137+
138+
cfg.dir.output = fullfile(fileparts(mfilename('fullpath')), '..', 'output');
139+
140+
end

0 commit comments

Comments
 (0)