Skip to content

Commit 98d8ccf

Browse files
TomasLencRemi-Gau
authored andcommitted
updated tests and small changes
All unit tests are working with the updated functions. Also, applied some small changes requested in the review process for the PR.
1 parent 3859e60 commit 98d8ccf

File tree

9 files changed

+167
-62
lines changed

9 files changed

+167
-62
lines changed

src/createDataDictionary.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ function createDataDictionary(cfg, logFile)
1717
fileName = strrep(logFile(1).filename, '.tsv', '.json');
1818
fullFilename = getFullFilename(fileName, cfg);
1919

20-
jsonContent = setJsonContent(fullFilename, logFile);
20+
jsonContent = setJsonContent(logFile);
2121

2222
opts.Indent = ' ';
2323

2424
bids.util.jsonencode(fullFilename, jsonContent, opts);
2525

2626
end
2727

28-
function jsonContent = setJsonContent(fullFilename, logFile)
28+
function jsonContent = setJsonContent(logFile)
2929

30-
% add default _event file fields to the json content
31-
if ~logFile.isStim
30+
% regular _events file: add default _event file fields to the json content
31+
if ~isfield(logFile,'isStim') || isempty(logFile.isStim) || ~logFile.isStim
3232

3333
jsonContent = logFile.columns;
3434

35-
% write json fields if this is a _stim file
35+
% _stim file: write stim-specific fields to the json content
3636
elseif logFile.isStim
3737

3838
samplingFrequency = nan;
@@ -62,7 +62,7 @@ function createDataDictionary(cfg, logFile)
6262

6363
headerName = returnHeaderName(namesExtraColumns{iExtraColumn}, nbCol, iCol);
6464

65-
if logFile.isStim
65+
if isfield(logFile,'isStim') && ~isempty(logFile.isStim) && logFile.isStim
6666
jsonContent.Columns{end + 1} = headerName;
6767
end
6868

src/saveEventsFile.m

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,30 @@
8686
case 'init'
8787

8888
% flag to indicate that this will be an _events file
89-
logFile(1).isStim = 0;
89+
logFile(1).isStim = false;
9090

91-
logFile(1).filename = cfg.fileName.events;
92-
93-
logFile = initializeFile(cfg, logFile);
91+
if isfield(cfg,'fileName') && ...
92+
isfield(cfg.fileName,'events') && ...
93+
~isempty(cfg.fileName.events)
94+
logFile(1).filename = cfg.fileName.events;
95+
else
96+
logFile(1).filename = '';
97+
end
98+
logFile = initializeFile(logFile);
9499

95100
case 'init_stim'
96101

97102
% flag to indicate that this will be an _stim file
98-
logFile(1).isStim = 1;
103+
logFile(1).isStim = true;
99104

100-
logFile(1).filename = cfg.fileName.stim;
101-
102-
logFile = initializeStimFile(cfg, logFile);
105+
if isfield(cfg,'fileName') && ...
106+
isfield(cfg.fileName,'stim') && ...
107+
~isempty(cfg.fileName.stim)
108+
logFile(1).filename = cfg.fileName.stim;
109+
else
110+
logFile(1).filename = '';
111+
end
112+
logFile = initializeStimFile(logFile);
103113

104114
case 'open'
105115

@@ -167,7 +177,7 @@
167177

168178
end
169179

170-
function logFile = initializeFile(cfg, logFile)
180+
function logFile = initializeFile(logFile)
171181
% This function creates the bids field structure for json files for the
172182
% three basic bids event columns, and for all requested extra columns.
173183
%
@@ -178,21 +188,21 @@
178188
logFile(1).columns = struct( ...
179189
'onset', struct( ...
180190
'Description', 'time elapsed since experiment start', ...
181-
'Unit', 's'), ...
191+
'Units', 's'), ...
182192
'trial_type', struct( ...
183193
'Description', 'types of trial', ...
184194
'Levels', ''), ...
185195
'duration', struct( ...
186196
'Description', 'duration of the event or the block', ...
187-
'Unit', 's') ...
197+
'Units', 's') ...
188198
);
189199

190200

191201
logFile = initializeExtraColumns(logFile);
192202

193203
end
194204

195-
function logFile = initializeStimFile(cfg, logFile)
205+
function logFile = initializeStimFile(logFile)
196206

197207
logFile = initializeExtraColumns(logFile);
198208

@@ -211,7 +221,7 @@
211221
logFile.filename), ...
212222
'w');
213223

214-
if logFile(1).isStim == 0
224+
if ~logFile(1).isStim
215225
% print the basic BIDS columns
216226
fprintf(logFile(1).fileID, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
217227
fprintf(1, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
@@ -222,7 +232,7 @@
222232
fprintf(logFile(1).fileID, '\n');
223233
fprintf(1, '\n');
224234

225-
elseif logFile(1).isStim == 1
235+
elseif logFile(1).isStim
226236
% don't print column headers for _stim.tsv
227237

228238
end
@@ -334,6 +344,7 @@ function printHeaderExtraColumns(logFile)
334344

335345
function logFile = saveToLogFile(logFile, cfg)
336346

347+
337348
% appends to the logfile all the data stored in the structure
338349
% first with the standard BIDS data and then any extra things
339350
for iEvent = 1:size(logFile, 1)
@@ -345,7 +356,7 @@ function printHeaderExtraColumns(logFile)
345356

346357
% if this is _events file, we skip events with onset or duration
347358
% that are empty, nan or char.
348-
if logFile(1).isStim==0
359+
if ~logFile(1).isStim
349360

350361
onset = logFile(iEvent).onset;
351362
duration = logFile(iEvent).duration;
@@ -357,40 +368,48 @@ function printHeaderExtraColumns(logFile)
357368

358369
skipEvent = true;
359370

360-
warningMessage = sprintf(['saveEventsFile:emptyEvent', ...
361-
'\nSkipping saving this event.\n onset: %s \n duration: %s\n'], ...
362-
onset, ...
363-
duration);
371+
warningMessageID = 'saveEventsFile:emptyEvent';
372+
warningMessage = sprintf(['Skipping saving this event. \n '...
373+
'onset: %s \n duration: %s \n'], ...
374+
onset, ...
375+
duration);
364376
end
365377

366378
% if this is _stim file, we skip missing events (i.e. events where
367379
% all extra columns have NO values)
368-
elseif logFile(1).isStim==1
380+
elseif logFile(1).isStim
369381

370382
namesExtraColumns = returnNamesExtraColumns(logFile);
371-
isValid = ones(1,numel(namesExtraColumns))
383+
isValid = ones(1,numel(namesExtraColumns));
372384
for iExtraColumn = 1:numel(namesExtraColumns)
373385
data = logFile(iEvent).(namesExtraColumns{iExtraColumn});
374386
if isempty(data) || isnan(data) || ( ischar(data) && strcmp(data,'n/a') )
375387
isValid(iExtraColumn) = 0;
376388
end
377389
end
378-
if ~any(isValid)
390+
if all(~isValid)
379391
skipEvent = true;
380392

381-
warningMessage = sprintf(['saveEventsFile:emptyEvent', ...
382-
'\nSkipping saving this event.\n No values defined. \n']);
393+
warningMessageID = 'saveEventsFile:emptyEvent';
394+
warningMessage = sprintf(['Skipping saving this event. \n', ...
395+
'No values defined. \n']);
396+
elseif any(~isValid)
397+
skipEvent = false;
398+
399+
warningMessageID = 'saveEventsFile:missingData';
400+
warningMessage = sprintf('Missing some %s data for this event. \n', ...
401+
namesExtraColumns{find(isValid)});
383402
end
384403
end
385404

386405
% now save the event to log file (if not skipping)
387406
if skipEvent
388407

389-
warning(warningMessage);
408+
warning(warningMessageID, warningMessage);
390409

391410
else
392411

393-
if logFile(1).isStim==0
412+
if ~logFile(1).isStim
394413

395414
printData(logFile(1).fileID, onset, cfg);
396415
printData(logFile(1).fileID, duration, cfg);
@@ -455,7 +474,7 @@ function printData(output, data, cfg)
455474
logFile(2:end) = [];
456475

457476
namesColumns = {'onset', 'duration', 'trial_type'};
458-
if logFile(1).isStim == 1
477+
if logFile(1).isStim
459478
namesColumns = {};
460479
end
461480
namesExtraColumns = returnNamesExtraColumns(logFile);

src/utils/utilsForTests/setUp.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
cfg.task.name = 'testtask';
1111

1212
cfg.testingDevice = 'mri';
13-
13+
1414
cfg = createFilename(cfg);
1515

16+
logFile.extraColumns = {'Speed','LHL24','is_Fixation'};
17+
18+
logFile = saveEventsFile('init', cfg, logFile);
19+
1620
logFile.extraColumns.Speed.length = 1;
1721
logFile.extraColumns.LHL24.length = 12;
1822
logFile.extraColumns.is_Fixation.length = 1;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"SamplingFrequency": 100,
3+
"StartTime": 0,
4+
"Columns": [
5+
"Speed",
6+
"LHL24_01",
7+
"LHL24_02",
8+
"LHL24_03",
9+
"is_Fixation"
10+
],
11+
"Speed": {
12+
"Description": "",
13+
"Levels": "",
14+
"LongName": "",
15+
"TermURL": "",
16+
"Units": ""
17+
},
18+
"LHL24_01": {
19+
"Description": "",
20+
"Levels": "",
21+
"LongName": "",
22+
"TermURL": "",
23+
"Units": ""
24+
},
25+
"LHL24_02": {
26+
"Description": "",
27+
"Levels": "",
28+
"LongName": "",
29+
"TermURL": "",
30+
"Units": ""
31+
},
32+
"LHL24_03": {
33+
"Description": "",
34+
"Levels": "",
35+
"LongName": "",
36+
"TermURL": "",
37+
"Units": ""
38+
},
39+
"is_Fixation": {
40+
"Description": "",
41+
"Levels": "",
42+
"LongName": "",
43+
"TermURL": "",
44+
"Units": ""
45+
}
46+
}

tests/test_createDataDictionary.m

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ function test_createDataDictionaryBasic()
2525

2626
cfg = createFilename(cfg);
2727

28+
logFile.extraColumns = {'Speed','LHL24'};
29+
30+
logFile = saveEventsFile('init', cfg, logFile);
31+
2832
logFile.extraColumns.Speed.length = 1;
2933
logFile.extraColumns.LHL24.length = 3;
30-
logFile = saveEventsFile('init', cfg, logFile);
3134

3235
logFile = saveEventsFile('open', cfg, logFile);
3336

34-
createDataDictionary(cfg, logFile);
35-
3637
%% check that the file has the right path and name
3738

3839
% data to test against
@@ -53,16 +54,17 @@ function test_createDataDictionaryBasic()
5354
% test_createDataDictionary>test_createDataDictionaryBasic:48
5455
% (/github/workspace/tests/test_createDataDictionary.m)
5556

56-
% actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
57-
%
58-
% % data to test against
59-
% expectedStruct = bids.util.jsondecode( ...
60-
% fullfile(pwd, ...
61-
% 'testData', ...
62-
% 'eventsDataDictionary.json'));
63-
%
64-
% % test
65-
% assertTrue(isequal(expectedStruct, actualStruct));
57+
actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
58+
59+
% data to test against
60+
expectedStruct = bids.util.jsondecode( ...
61+
fullfile( ...
62+
pwd, ...
63+
'testData', ...
64+
'eventsDataDictionary.json'));
65+
66+
% test
67+
assertTrue(isequal(expectedStruct, actualStruct));
6668

6769
end
6870

@@ -85,18 +87,18 @@ function test_createDataDictionaryStim()
8587

8688
cfg = createFilename(cfg);
8789

90+
stimLogFile.extraColumns = {'Speed','LHL24','is_Fixation'};
91+
92+
stimLogFile = saveEventsFile('init_stim', cfg, stimLogFile);
93+
8894
stimLogFile.extraColumns.Speed.length = 1;
8995
stimLogFile.extraColumns.LHL24.length = 3;
9096
stimLogFile.extraColumns.is_Fixation.length = 1;
9197

9298
stimLogFile.SamplingFrequency = 100;
9399
stimLogFile.StartTime = 0;
94100

95-
stimLogFile = saveEventsFile('init', cfg, stimLogFile);
96-
97-
stimLogFile = saveEventsFile('open_stim', cfg, stimLogFile);
98-
99-
createDataDictionary(cfg, stimLogFile);
101+
stimLogFile = saveEventsFile('open', cfg, stimLogFile);
100102

101103
%% check that the file has the right path and name
102104

@@ -117,5 +119,17 @@ function test_createDataDictionaryStim()
117119
% jsondecode:27 (/github/workspace/lib/bids-matlab/+bids/+util/jsondecode.m)
118120
% test_createDataDictionary>test_createDataDictionaryBasic:48
119121
% (/github/workspace/tests/test_createDataDictionary.m)
122+
123+
actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
124+
125+
% data to test against
126+
expectedStruct = bids.util.jsondecode( ...
127+
fullfile( ...
128+
pwd, ...
129+
'testData', ...
130+
'stimDataDictionary.json'));
131+
132+
% test
133+
assertTrue(isequal(expectedStruct, actualStruct));
120134

121135
end

tests/test_readAndFilterLogfile.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
function test_readAndFilterLogfileBasic()
1010

1111
%% set up
12-
cfg.dir.output = fullfile(fileparts(mfilename('fullpath')), '..', 'output');
1312
[cfg, logFile] = setUp();
13+
cfg.dir.output = fullfile(fileparts(mfilename('fullpath')), '..', 'output');
1414

1515
% create the events file and header
1616
logFile = saveEventsFile('open', cfg, logFile);

0 commit comments

Comments
 (0)