Skip to content

Commit e8fd1a7

Browse files
committed
create test for read and filter log
1 parent b3af2bd commit e8fd1a7

File tree

7 files changed

+136
-65
lines changed

7 files changed

+136
-65
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
# exclude content of logfiles folders
99
*output*
10-
*.tsv
1110
*.mat
1211

1312
# exclude temp files from tests and coverage
1413
*test_code_report.txt
1514
*coverage*
1615

16+
*filteredBy*
17+
1718
tests/*.nii*
1819
tests/*.json*
1920
tests/*.tsv*

lib/bids-matlab

src/readAndFilterLogfile.m

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
function outputFiltered = readAndFilterLogfile(columnName, filterBy, saveOutputTsv, varargin)
2-
% outputFiltered = readOutputFilter(filterHeader, filterContent, varargin)
2+
% outputFiltered = readAndFilterLogfile(columnName, filterBy, saveOutputTsv, varargin)
33
%
44
% It will display in the command window the content of the `output.tsv' filtered by one element
55
% of a target column.
66
%
7-
% DEPENDENCIES:
8-
% - bids_matlab (from CPP_BIDS)
9-
%
107
% INPUT:
118
%
129
% - columnName: string, the header of the column where the content of insterest is stored
@@ -21,66 +18,59 @@
2118
%
2219
% - outputFiltered: dataset with only the specified content, to see it in the command window
2320
% use display(outputFiltered)
24-
21+
2522
% Create tag to add to output file in case you want to save it
2623
outputFilterTag = ['_filteredBy-' columnName '_' filterBy '.tsv'];
27-
24+
2825
% Checke if input is cfg or the file path and assign the output filename for later saving
2926
if ischar(varargin{1})
30-
27+
3128
tsvFile = varargin{1};
32-
33-
% Divide path and file name
34-
fileSepPos = find(tsvFile == filesep, 1, 'last');
35-
path1 = tsvFile(1:fileSepPos - 1);
36-
path2 = tsvFile(fileSepPos + 1:end - 4);
37-
38-
% Create output file name
39-
outputFileName = fullfile(path1, ...
40-
[path2 ...
41-
outputFilterTag]);
42-
29+
4330
elseif isstruct(varargin{1})
44-
31+
4532
tsvFile = fullfile(varargin{1}.dir.outputSubject, ...
46-
varargin{1}.fileName.modality, ...
47-
varargin{1}.fileName.events);
48-
49-
% Create output file name
50-
outputFileName = fullfile(varargin{1}.dir.outputSubject, ...
51-
varargin{1}.fileName.modality, ...
52-
[varargin{1}.fileName.events(1:end - 4) ...
53-
outputFilterTag]);
33+
varargin{1}.fileName.modality, ...
34+
varargin{1}.fileName.events);
35+
5436
end
55-
37+
38+
% Create output file name
39+
outputFileName = strrep(tsvFile, '.tsv', outputFilterTag);
40+
5641
% Check if the file exists
5742
if ~exist(tsvFile, 'file')
58-
error([newline 'Input file does not exist']);
43+
error([newline 'Input file does not exist: %s'], tsvFile);
5944
end
60-
45+
6146
try
6247
% Read the the tsv file and store each column in a field of `output` structure
6348
output = bids.util.tsvread(tsvFile);
6449
catch
6550
% Add the 'bids-matlab' in case is not in the path
66-
addpath(genpath(fullfile(pwd, '../lib')));
51+
addpath(genpath(fullfile(pwd, '..', 'lib')));
6752
% Read the the tsv file and store each column in a field of `output` structure
6853
output = bids.util.tsvread(tsvFile);
6954
end
70-
55+
7156
% Get the index of the target contentent to filter and display
72-
filterIdx = find(strncmp(output.(columnName), filterBy, length(filterBy)));
73-
57+
filterIdx = strncmp(output.(columnName), filterBy, length(filterBy));
58+
59+
% apply the filter
60+
listFields = fieldnames(output);
61+
for iField = 1:numel(listFields)
62+
output.(listFields{iField})(~filterIdx) = [];
63+
end
64+
7465
% Convert the structure to dataset
75-
outputDataset = struct2dataset(output);
76-
77-
% Get the dataset with the content of intereset
78-
outputFiltered = outputDataset(filterIdx, :);
79-
66+
outputFiltered = struct2dataset(output);
67+
8068
if saveOutputTsv
8169

8270
bids.util.tsvwrite(outputFileName, output);
83-
71+
8472
end
85-
73+
8674
end
75+
76+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
onset duration trial_type
2+
2 15 VisMot
3+
25 15 VisStat

tests/setUp.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function [cfg, logFile] = setUp()
2+
3+
outputDir = fullfile(fileparts(mfilename('fullpath')), 'output');
4+
5+
cfg.verbose = true;
6+
7+
cfg.subject.subjectNb = 1;
8+
cfg.subject.runNb = 1;
9+
10+
cfg.task.name = 'testtask';
11+
12+
cfg.dir.output = outputDir;
13+
14+
cfg.testingDevice = 'mri';
15+
16+
cfg = createFilename(cfg);
17+
18+
logFile.extraColumns.Speed.length = 1;
19+
logFile.extraColumns.LHL24.length = 12;
20+
logFile.extraColumns.is_Fixation.length = 1;
21+
22+
end

tests/test_readAndFilterLogfile.m

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
function test_suite = test_readAndFilterLogfile %#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_readAndFilterLogfileBasic()
10+
11+
%% set up
12+
13+
[cfg, logFile] = setUp();
14+
15+
% create the events file and header
16+
logFile = saveEventsFile('open', cfg, logFile);
17+
18+
% ROW 2: normal events : all info is there
19+
logFile(1, 1).onset = 2;
20+
logFile(end, 1).trial_type = 'motion_up';
21+
logFile(end, 1).duration = 3;
22+
logFile(end, 1).Speed = 2;
23+
logFile(end, 1).is_Fixation = true;
24+
logFile(end, 1).LHL24 = 1:12;
25+
26+
logFile(2, 1).onset = 2;
27+
logFile(end, 1).trial_type = 'motion_down';
28+
logFile(end, 1).duration = 3;
29+
logFile(end, 1).Speed = 2;
30+
logFile(end, 1).is_Fixation = true;
31+
logFile(end, 1).LHL24 = 2:13;
32+
33+
logFile = saveEventsFile('save', cfg, logFile);
34+
35+
% close the file
36+
saveEventsFile('close', cfg, logFile);
37+
38+
% filter file
39+
outputFiltered = readAndFilterLogfile('trial_type', 'motion_down', true, cfg);
40+
41+
%% test
42+
expectedFilename = strrep(cfg.fileName.events, '.tsv', ...
43+
['_filteredBy-' 'trial_type' '_' 'motion_down' '.tsv']);
44+
expectedFile = fullfile(cfg.dir.outputSubject, ...
45+
cfg.fileName.modality, ...
46+
expectedFilename);
47+
48+
assertEqual(exist(expectedFile, 'file'), 2)
49+
50+
content = bids.util.tsvread(expectedFile);
51+
52+
assertEqual(content.trial_type{1}, 'motion_down')
53+
54+
end
55+
56+
57+
function test_readAndFilterLogfileFromFile()
58+
59+
%% set up
60+
61+
inputFile = fullfile(fileparts(mfilename('fullpath')), 'dummyData', ...
62+
'sub-blind01_ses-01_task-vislocalizer_events.tsv');
63+
64+
% filter file
65+
outputFiltered = readAndFilterLogfile('trial_type', 'VisStat', true, inputFile);
66+
67+
%% test
68+
expectedFile = strrep(inputFile, '.tsv', ...
69+
['_filteredBy-' 'trial_type' '_' 'VisStat' '.tsv']);
70+
71+
assertEqual(exist(expectedFile, 'file'), 2)
72+
73+
content = bids.util.tsvread(expectedFile);
74+
75+
assertEqual(content.trial_type{1}, 'VisStat')
76+
77+
end

tests/test_saveEventsFileSave.m

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -207,28 +207,6 @@ function test_saveEventsFileSaveErrors()
207207

208208
end
209209

210-
function [cfg, logFile] = setUp()
211-
212-
outputDir = fullfile(fileparts(mfilename('fullpath')), '..', 'output');
213-
214-
cfg.verbose = true;
215-
216-
cfg.subject.subjectNb = 1;
217-
cfg.subject.runNb = 1;
218-
219-
cfg.task.name = 'testtask';
220-
221-
cfg.dir.output = outputDir;
222-
223-
cfg.testingDevice = 'mri';
224-
225-
cfg = createFilename(cfg);
226-
227-
logFile.extraColumns.Speed.length = 1;
228-
logFile.extraColumns.LHL24.length = 12;
229-
logFile.extraColumns.is_Fixation.length = 1;
230-
231-
end
232210

233211
function content = getFileContent(cfg, logFile)
234212

0 commit comments

Comments
 (0)