Skip to content

Commit 3859e60

Browse files
TomasLencRemi-Gau
authored andcommitted
Small changes
Changes based on Remi-Gau's comments.
1 parent 69a2816 commit 3859e60

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed

src/createDataDictionary.m

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ function createDataDictionary(cfg, logFile)
2727

2828
function jsonContent = setJsonContent(fullFilename, logFile)
2929

30-
if logFile.isStim == 1
30+
% add default _event file fields to the json content
31+
if ~logFile.isStim
32+
33+
jsonContent = logFile.columns;
3134

35+
% write json fields if this is a _stim file
36+
elseif logFile.isStim
37+
3238
samplingFrequency = nan;
3339
startTime = nan;
3440

@@ -43,14 +49,8 @@ function createDataDictionary(cfg, logFile)
4349
'SamplingFrequency', samplingFrequency, ...
4450
'StartTime', startTime, ...
4551
'Columns', []);
46-
47-
elseif logFile.isStim == 1
48-
49-
% add holy trininty columns to the json content
50-
jsonContent = logFile.columns;
51-
5252
end
53-
53+
5454
% transfer content of extra fields to json content
5555
namesExtraColumns = returnNamesExtraColumns(logFile);
5656

@@ -62,10 +62,8 @@ function createDataDictionary(cfg, logFile)
6262

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

65-
if ismember('_stim', fullFilename)
66-
65+
if logFile.isStim
6766
jsonContent.Columns{end + 1} = headerName;
68-
6967
end
7068

7169
jsonContent.(headerName) = ...

src/saveEventsFile.m

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,9 @@
7272
%
7373
%
7474

75-
if nargin < 1
76-
error('Missing action input');
77-
end
78-
7975
if nargin < 2
80-
cfg = struct();
76+
error(['Missing arguments. Please specify <action input> ',...
77+
'and <cfg file> as the first two arguments']);
8178
end
8279

8380
if nargin < 3 || isempty(logFile)
@@ -89,7 +86,7 @@
8986
case 'init'
9087

9188
% flag to indicate that this will be an _events file
92-
logFile.isStim = 0;
89+
logFile(1).isStim = 0;
9390

9491
logFile(1).filename = cfg.fileName.events;
9592

@@ -98,7 +95,7 @@
9895
case 'init_stim'
9996

10097
% flag to indicate that this will be an _stim file
101-
logFile.isStim = 1;
98+
logFile(1).isStim = 1;
10299

103100
logFile(1).filename = cfg.fileName.stim;
104101

@@ -214,7 +211,7 @@
214211
logFile.filename), ...
215212
'w');
216213

217-
if logFile(1,1).isStim == 0
214+
if logFile(1).isStim == 0
218215
% print the basic BIDS columns
219216
fprintf(logFile(1).fileID, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
220217
fprintf(1, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
@@ -225,7 +222,7 @@
225222
fprintf(logFile(1).fileID, '\n');
226223
fprintf(1, '\n');
227224

228-
elseif logFile(1,1).isStim == 1
225+
elseif logFile(1).isStim == 1
229226
% don't print column headers for _stim.tsv
230227

231228
end
@@ -343,9 +340,12 @@ function printHeaderExtraColumns(logFile)
343340

344341
logFile = checklLogFile('fields', logFile, iEvent, cfg);
345342

343+
% check if this event should be skipped
344+
skipEvent = false;
345+
346346
% if this is _events file, we skip events with onset or duration
347347
% that are empty, nan or char.
348-
if logFile(1,1).isStim==0
348+
if logFile(1).isStim==0
349349

350350
onset = logFile(iEvent).onset;
351351
duration = logFile(iEvent).duration;
@@ -355,41 +355,54 @@ function printHeaderExtraColumns(logFile)
355355
any(cellfun(@ischar, {onset duration})) || ...
356356
any(isempty({onset duration}))
357357

358-
warning('saveEventsFile:emptyEvent', ...
359-
'\nSkipping saving this event.\n onset: %s \n duration: %s\n', ...
358+
skipEvent = true;
359+
360+
warningMessage = sprintf(['saveEventsFile:emptyEvent', ...
361+
'\nSkipping saving this event.\n onset: %s \n duration: %s\n'], ...
360362
onset, ...
361363
duration);
362-
else
363-
printData(logFile(1).fileID, onset, cfg);
364-
printData(logFile(1).fileID, duration, cfg);
365-
printData(logFile(1).fileID, trial_type, cfg);
366-
367-
printExtraColumns(logFile, iEvent, cfg);
368-
369-
fprintf(logFile(1).fileID, '\n');
370-
fprintf(1, '\n');
371-
372364
end
373365

374366
% if this is _stim file, we skip missing events (i.e. events where
375367
% all extra columns have NO values)
376-
elseif logFile(1,1).isStim==1
368+
elseif logFile(1).isStim==1
377369

378-
skipEvent = false;
379370
namesExtraColumns = returnNamesExtraColumns(logFile);
371+
isValid = ones(1,numel(namesExtraColumns))
380372
for iExtraColumn = 1:numel(namesExtraColumns)
381373
data = logFile(iEvent).(namesExtraColumns{iExtraColumn});
382374
if isempty(data) || isnan(data) || ( ischar(data) && strcmp(data,'n/a') )
383-
skipEvent = true;
375+
isValid(iExtraColumn) = 0;
384376
end
385377
end
386-
if ~skipEvent
387-
printExtraColumns(logFile, iEvent, cfg);
378+
if ~any(isValid)
379+
skipEvent = true;
380+
381+
warningMessage = sprintf(['saveEventsFile:emptyEvent', ...
382+
'\nSkipping saving this event.\n No values defined. \n']);
383+
end
384+
end
385+
386+
% now save the event to log file (if not skipping)
387+
if skipEvent
388+
389+
warning(warningMessage);
390+
391+
else
392+
393+
if logFile(1).isStim==0
394+
395+
printData(logFile(1).fileID, onset, cfg);
396+
printData(logFile(1).fileID, duration, cfg);
397+
printData(logFile(1).fileID, trial_type, cfg);
388398

389-
fprintf(logFile(1).fileID, '\n');
390-
fprintf(1, '\n');
391399
end
392400

401+
printExtraColumns(logFile, iEvent, cfg);
402+
403+
fprintf(logFile(1).fileID, '\n');
404+
fprintf(1, '\n');
405+
393406
end
394407

395408
end
@@ -441,9 +454,8 @@ function printData(output, data, cfg)
441454

442455
logFile(2:end) = [];
443456

444-
if logFile(1,1).isStim == 0
445-
namesColumns = {'onset', 'duration', 'trial_type'};
446-
elseif logFile(1,1).isStim == 1
457+
namesColumns = {'onset', 'duration', 'trial_type'};
458+
if logFile(1).isStim == 1
447459
namesColumns = {};
448460
end
449461
namesExtraColumns = returnNamesExtraColumns(logFile);

0 commit comments

Comments
 (0)