Skip to content

Commit 53d0ab9

Browse files
committed
allow createBoldJson to add extra content to a json file
1 parent b52ab2a commit 53d0ab9

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Instructions": "",
3+
"RepetitionTime": [],
4+
"SliceTiming": [],
5+
"TaskDescription": "",
6+
"TaskName": "testtask",
7+
"extraInfo": {
8+
"nestedExtraInfo": "something extra"
9+
}
10+
}

manualTests/test_createBoldJson.m

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function test_suite = test_createBoldJson %#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_createBoldJsonExtra()
10+
11+
outputDir = fullfile(fileparts(mfilename('fullpath')), 'output');
12+
13+
%% set up
14+
15+
cfg.verbose = false;
16+
17+
cfg.subject.subjectNb = 1;
18+
cfg.subject.runNb = 1;
19+
20+
cfg.task.name = 'testtask';
21+
22+
cfg.dir.output = outputDir;
23+
24+
cfg.testingDevice = 'mri';
25+
26+
cfg = createFilename(cfg);
27+
28+
logFile = saveEventsFile('init', cfg); %#ok<*NASGU>
29+
30+
extraInfo = struct('extraInfo', struct('nestedExtraInfo', 'something extra'));
31+
32+
createBoldJson(cfg, extraInfo);
33+
34+
%% check content
35+
fileName = strrep(cfg.fileName.events, '_events', '_bold');
36+
fileName = strrep(fileName, '.tsv', '.json');
37+
38+
actualStruct = bids.util.jsondecode(fullfile( ...
39+
cfg.dir.outputSubject, ...
40+
cfg.fileName.modality, ...
41+
fileName));
42+
43+
% data to test against
44+
expectedStruct = bids.util.jsondecode( ...
45+
fullfile(pwd, 'testData', 'extra_bold.json'));
46+
47+
% test
48+
assertEqual(expectedStruct, actualStruct);
49+
50+
end

manualTests/test_makeRawDataset.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ function test_makeRawDataset()
2929

3030
cfg = createFilename(cfg);
3131

32-
createBoldJson(cfg);
32+
extraInfo = struct('extraInfo', struct('nestedExtraInfo', 'something extra'));
33+
createBoldJson(cfg, extraInfo);
3334

3435
createDatasetDescription(cfg);
3536

src/createBoldJson.m

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
function createBoldJson(cfg)
2-
% createBoldJson(cfg)
1+
function createBoldJson(cfg, extraInfo)
2+
% createBoldJson(cfg, extraInfo)
33
%
44
% Creates the side car JSON file for a BOLD functional run.
55
% This will only contain the minimum BIDS requirement and will likey be less
66
% complete than the info you could from DICOM conversion.
7+
%
8+
% Extra content can be added to the JSON file
79

810
opts.Indent = ' ';
11+
12+
if nargin<2
13+
extraInfo = struct();
14+
end
915

1016
fileName = strrep(cfg.fileName.events, '_events', '_bold');
1117
fileName = strrep(fileName, '.tsv', '.json');
@@ -16,6 +22,15 @@ function createBoldJson(cfg)
1622
fileName);
1723

1824
jsonContent = cfg.bids.mri;
25+
26+
% add content of extraInfo to the JSON content
27+
if ~isstruct(extraInfo)
28+
warning('additional info added to a json file must be a structure')
29+
end
30+
fieldList = fieldnames(extraInfo);
31+
for iField = 1:numel(fieldList)
32+
jsonContent.(fieldList{iField}) = extraInfo.(fieldList{iField});
33+
end
1934

2035
bids.util.jsonencode(fileName, jsonContent, opts);
2136

0 commit comments

Comments
 (0)