Skip to content

Commit 147aaf9

Browse files
committed
refactor saveEvents before adding more tests
1 parent 4d0f841 commit 147aaf9

File tree

5 files changed

+73
-61
lines changed

5 files changed

+73
-61
lines changed

src/saveEventsFile.m

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ function printHeaderExtraColumns(logFile)
285285
namesExtraColumns{iExtraColumn});
286286

287287
end
288-
warningSaveEventsFile(cfg, 'missingData', warningMessage);
288+
throwWarning(cfg, 'saveEventsFile:missingData', warningMessage);
289289

290290
if cfg.verbose > 1
291291
disp(logFile(iEvent));
@@ -297,50 +297,6 @@ function printHeaderExtraColumns(logFile)
297297

298298
end
299299

300-
function data = checkInput(data)
301-
% check the data to write
302-
% default will be 'n/a' for chars and NaN for numeric data
303-
% for numeric data that don't have the expected length, it will be padded with NaNs
304-
if islogical(data) && data
305-
data = 'true';
306-
elseif islogical(data) && ~data
307-
data = 'false';
308-
end
309-
310-
if ischar(data) && isempty(data) || strcmp(data, ' ')
311-
data = 'n/a';
312-
elseif isempty(data)
313-
% important to not set this to n/a as we still need to check if this
314-
% numeric valur has the right length and needs to be nan padded
315-
data = nan;
316-
end
317-
318-
end
319-
320-
function data = nanPadding(cfg, data, expectedLength)
321-
322-
if nargin < 2
323-
expectedLength = [];
324-
end
325-
326-
if ~isempty(expectedLength) && isnumeric(data)
327-
328-
if max(size(data)) < expectedLength
329-
padding = expectedLength - max(size(data));
330-
data(end + 1:end + padding) = nan(1, padding);
331-
332-
elseif max(size(data)) > expectedLength
333-
warningMessage = ['A field for this event is longer than expected.', ...
334-
'Truncating extra values.'];
335-
warningSaveEventsFile(cfg, 'arrayTooLong', warningMessage);
336-
337-
data = data(1:expectedLength);
338-
339-
end
340-
end
341-
342-
end
343-
344300
function logFile = saveToLogFile(logFile, cfg)
345301

346302
% appends to the logfile all the data stored in the structure
@@ -367,7 +323,7 @@ function printHeaderExtraColumns(logFile)
367323

368324
skipEvent = true;
369325

370-
warningMessageID = 'emptyEvent';
326+
warningMessageID = 'saveEventsFile:emptyEvent';
371327
warningMessage = sprintf(['Skipping saving this event. \n '...
372328
'onset: %s \n duration: %s \n'], ...
373329
onset, ...
@@ -391,21 +347,21 @@ function printHeaderExtraColumns(logFile)
391347
if all(~isValid)
392348
skipEvent = true;
393349

394-
warningMessageID = 'emptyEvent';
350+
warningMessageID = 'saveEventsFile:emptyEvent';
395351
warningMessage = sprintf(['Skipping saving this event. \n', ...
396352
'No values defined. \n']);
397353

398354
elseif any(~isValid)
399355
skipEvent = false;
400356

401-
warningMessageID = 'missingData';
357+
warningMessageID = 'saveEventsFile:missingData';
402358
warningMessage = sprintf('Missing some %s data for this event. \n', ...
403359
namesExtraColumns{find(isValid)});
404360
end
405361
end
406362

407363
% now save the event to log file (if not skipping)
408-
warningSaveEventsFile(cfg, warningMessageID, warningMessage);
364+
throwWarning(cfg, warningMessageID, warningMessage);
409365

410366
printToFile(cfg, logFile, skipEvent, iEvent);
411367

@@ -504,14 +460,3 @@ function errorSaveEventsFile(identifier)
504460
error(errorStruct);
505461

506462
end
507-
508-
function warningSaveEventsFile(cfg, identifier, warningMessage)
509-
if cfg.verbose > 0 && ...
510-
nargin == 3 && ...
511-
~isempty(identifier) && ...
512-
~isempty(warningMessage)
513-
514-
warningMessageID = ['saveEventsFile:' identifier];
515-
warning(warningMessageID, warningMessage);
516-
end
517-
end

src/utils/checkInput.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function data = checkInput(data)
4+
%
5+
% Check the data to write and convert to the proper format if needed.
6+
%
7+
% Default will be 'n/a' for chars and NaN for numeric data.
8+
%
9+
% USAGE::
10+
%
11+
% data = checkInput(data)
12+
%
13+
14+
if islogical(data) && data
15+
data = 'true';
16+
elseif islogical(data) && ~data
17+
data = 'false';
18+
end
19+
20+
if ischar(data) && isempty(data) || strcmp(data, ' ')
21+
data = 'n/a';
22+
elseif isempty(data)
23+
% Important to not set this to n/a as we might still need to check if this
24+
% numeric value has the right length and needs to be nan padded
25+
data = nan;
26+
end
27+
28+
end

src/utils/nanPadding.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function data = nanPadding(cfg, data, expectedLength)
4+
5+
% For numeric data that don't have the expected length, it will be padded
6+
% with NaNs. If the vector is too long it will be truncated
7+
8+
if nargin < 2
9+
expectedLength = [];
10+
end
11+
12+
if ~isempty(expectedLength) && isnumeric(data)
13+
14+
if max(size(data)) < expectedLength
15+
padding = expectedLength - max(size(data));
16+
data(end + 1:end + padding) = nan(1, padding);
17+
18+
elseif max(size(data)) > expectedLength
19+
warningMessage = ['A field for this event is longer than expected.', ...
20+
'Truncating extra values.'];
21+
throwWarning(cfg, 'nanPadding:arrayTooLong', warningMessage);
22+
23+
data = data(1:expectedLength);
24+
25+
end
26+
end
27+
28+
end

src/utils/throwWarning.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function throwWarning(cfg, identifier, warningMessage)
4+
if cfg.verbose > 0 && ...
5+
nargin == 3 && ...
6+
~isempty(identifier) && ...
7+
~isempty(warningMessage)
8+
9+
warning(identifier, warningMessage);
10+
end
11+
end

tests/test_saveEventsFileSave.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function test_saveEventsFileSaveArraySize()
165165
logFile(end, 1).duration = 3;
166166
logFile(end, 1).LHL24 = rand(1, 15);
167167

168-
assertWarning(@()saveEventsFile('save', cfg, logFile), 'saveEventsFile:arrayTooLong');
168+
assertWarning(@()saveEventsFile('save', cfg, logFile), 'nanPadding:arrayTooLong');
169169

170170
saveEventsFile('save', cfg, logFile);
171171

0 commit comments

Comments
 (0)