Skip to content

Commit d78eb09

Browse files
committed
update creation and saving of stim files
1 parent 2c89605 commit d78eb09

File tree

7 files changed

+66
-41
lines changed

7 files changed

+66
-41
lines changed

src/createDataDictionary.m

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,43 @@ function createDataDictionary(cfg, logFile)
77
% will create empty field that you can then fill in manually in the JSON
88
% file
99

10+
fileName = strrep(logFile(1).filename, '.tsv', '.json');
11+
fullFilename = getFullFilename(fileName, cfg);
12+
13+
jsonContent = setJsonContent(fullFilename, logFile);
14+
1015
opts.Indent = ' ';
16+
bids.util.jsonencode(fullFilename, jsonContent, opts);
1117

12-
fileName = strrep(logFile(1).filename, '.tsv', '.json');
18+
end
1319

14-
fileName = fullfile( ...
15-
cfg.dir.outputSubject, ...
16-
cfg.fileName.modality, ...
17-
fileName);
20+
function jsonContent = setJsonContent(fullFilename, logFile)
1821

22+
% transfer content of extra fields to json content
23+
namesExtraColumns = returnNamesExtraColumns(logFile);
24+
25+
% default content for events file that will be overriddent if we are dealing
26+
% with a stim file
1927
jsonContent = struct( ...
2028
'onset', struct( ...
2129
'Description', 'time elapsed since experiment start', ...
22-
'Unit', 's'), ...
30+
'Units', 's'), ...
2331
'trial_type', struct( ...
2432
'Description', 'types of trial', ...
2533
'Levels', ''), ...
2634
'duration', struct( ...
2735
'Description', 'duration of the event or the block', ...
28-
'Unit', 's') ...
36+
'Units', 's') ...
2937
);
3038

31-
% transfer content of extra fields to json content
32-
namesExtraColumns = returnNamesExtraColumns(logFile);
39+
if contains(fullFilename, '_stim')
40+
41+
jsonContent = struct( ...
42+
'SamplingFrequency', nan, ...
43+
'StartTime', nan, ...
44+
'Columns', []);
45+
46+
end
3347

3448
for iExtraColumn = 1:numel(namesExtraColumns)
3549

@@ -39,13 +53,17 @@ function createDataDictionary(cfg, logFile)
3953

4054
headerName = returnHeaderName(namesExtraColumns{iExtraColumn}, nbCol, iCol);
4155

56+
if contains(fullFilename, '_stim')
57+
58+
jsonContent.Columns{end + 1} = headerName;
59+
60+
end
61+
4262
jsonContent.(headerName) = ...
4363
logFile(1).extraColumns.(namesExtraColumns{iExtraColumn}).bids;
4464

4565
end
4666

4767
end
4868

49-
bids.util.jsonencode(fileName, jsonContent, opts);
50-
5169
end

src/createJson.m

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ function createJson(varargin)
3737
end
3838

3939
fileName = strrep(fileName, '.tsv', '.json');
40-
41-
fileName = fullfile( ...
42-
cfg.dir.outputSubject, ...
43-
modality, ...
44-
fileName);
40+
fullFilename = getFullFilename(fileName, cfg);
4541

4642
%% add content of extraInfo to the JSON content
4743

@@ -52,7 +48,7 @@ function createJson(varargin)
5248

5349
%% save
5450
opts.Indent = ' ';
55-
bids.util.jsonencode(fileName, jsonContent, opts);
51+
bids.util.jsonencode(fullFilename, jsonContent, opts);
5652

5753
end
5854

src/saveEventsFile.m

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282

8383
logFile(1).filename = cfg.fileName.stim;
8484

85-
logFile = initializeFile(cfg, logFile);
85+
logFile = initializeStimFile(cfg, logFile);
8686

8787
case 'save'
8888

@@ -147,6 +147,22 @@
147147

148148
function logFile = initializeFile(cfg, logFile)
149149

150+
logFile = initializeStimFile(cfg, logFile);
151+
152+
% print the basic BIDS columns
153+
fprintf(logFile(1).fileID, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
154+
fprintf(1, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
155+
156+
printHeaderExtraColumns(logFile);
157+
158+
% next line so we start printing at the right place
159+
fprintf(logFile(1).fileID, '\n');
160+
fprintf(1, '\n');
161+
162+
end
163+
164+
function logFile = initializeStimFile(cfg, logFile)
165+
150166
logFile = initializeExtraColumns(logFile);
151167

152168
createDataDictionary(cfg, logFile);
@@ -160,16 +176,6 @@
160176
logFile.filename), ...
161177
'w');
162178

163-
% print the basic BIDS columns
164-
fprintf(logFile(1).fileID, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
165-
fprintf(1, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
166-
167-
printHeaderExtraColumns(logFile);
168-
169-
% next line so we start printing at the right place
170-
fprintf(logFile(1).fileID, '\n');
171-
fprintf(1, '\n');
172-
173179
end
174180

175181
function printHeaderExtraColumns(logFile)

src/subfun/getFullFilename.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function fullFilename = getFullFilename(fileName, cfg)
4+
5+
fullFilename = fullfile( ...
6+
cfg.dir.outputSubject, ...
7+
cfg.fileName.modality, ...
8+
fileName);
9+
10+
end

src/subfun/initializeExtraColumns.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
fieldsToSet.bids.Description = '';
1313
fieldsToSet.bids.Levels = '';
1414
fieldsToSet.bids.TermURL = '';
15+
fieldsToSet.bids.Units = '';
1516

1617
% convert the cell of column name into a structure
1718
if iscell(logFile(1).extraColumns)

tests/test_saveEventsFileInit.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function test_saveEventsFileInitExtraColumns()
4444
expectedStrcut(1).extraColumns.Speed.bids.Description = '';
4545
expectedStrcut(1).extraColumns.Speed.bids.Levels = '';
4646
expectedStrcut(1).extraColumns.Speed.bids.TermURL = '';
47+
expectedStrcut(1).extraColumns.Speed.bids.Units = '';
4748

4849
%% test
4950
assertEqual(expectedStrcut, logFile);
@@ -69,11 +70,13 @@ function test_saveEventsFileInitExtraColumnsArray()
6970
expectedStrcut(1).extraColumns.Speed.bids.Description = '';
7071
expectedStrcut(1).extraColumns.Speed.bids.Levels = '';
7172
expectedStrcut(1).extraColumns.Speed.bids.TermURL = '';
73+
expectedStrcut(1).extraColumns.Speed.bids.Units = '';
7274
expectedStrcut(1).extraColumns.LHL24.length = 3;
7375
expectedStrcut(1).extraColumns.LHL24.bids.LongName = '';
7476
expectedStrcut(1).extraColumns.LHL24.bids.Description = '';
7577
expectedStrcut(1).extraColumns.LHL24.bids.Levels = '';
7678
expectedStrcut(1).extraColumns.LHL24.bids.TermURL = '';
79+
expectedStrcut(1).extraColumns.LHL24.bids.Units = '';
7780

7881
%% test
7982
assertEqual(expectedStrcut, logFile);

tests/test_saveEventsFileOpen.m

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,12 @@ function test_saveEventsFileOpenStimfile()
7878

7979
%% data to test against
8080
funcDir = fullfile(outputDir, 'source', 'sub-001', 'ses-001', 'func');
81-
eventFilename = ['sub-001_ses-001_task-testtask_run-001_stim_date-' ...
82-
cfg.fileName.date '.tsv'];
81+
stimFilename = ['sub-001_ses-001_task-testtask_run-001_stim_date-' ...
82+
cfg.fileName.date '.tsv'];
8383

8484
% check that the file has the right path and name
85-
assert(exist(fullfile(funcDir, eventFilename), 'file') == 2);
86-
assert(exist(fullfile(funcDir, strrep(eventFilename, '.tsv', '.json')), 'file') == 2);
87-
88-
FID = fopen(fullfile(funcDir, eventFilename), 'r');
89-
C = textscan(FID, repmat('%s', 1, 3), 'Delimiter', '\t', 'EndOfLine', '\n');
90-
91-
%% test
92-
% check the extra columns of the header
93-
assertEqual(C{1}{1}, 'onset');
94-
assertEqual(C{2}{1}, 'duration');
95-
assertEqual(C{3}{1}, 'trial_type');
85+
assert(exist(fullfile(funcDir, stimFilename), 'file') == 2);
86+
assert(exist(fullfile(funcDir, strrep(stimFilename, '.tsv', '.json')), 'file') == 2);
9687

9788
end
9889

0 commit comments

Comments
 (0)