Skip to content

Commit 849dfa9

Browse files
committed
update tests and lint
1 parent 814a9a7 commit 849dfa9

12 files changed

+98
-88
lines changed

+bids/+internal/append_to_layout.m

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@
5050
end
5151

5252
if isempty(subject.(modality))
53-
subject.(modality) = p;
53+
subject.(modality) = p;
5454
else
55-
subject.(modality)(end+1,1) = p;
55+
subject.(modality)(end + 1, 1) = p;
5656
end
57-
5857

5958
end
6059

+bids/+internal/parse_filename.m

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
function p = parse_filename(filename, fields)
2+
%
23
% Split a filename into its building constituents
3-
% FORMAT p = bids.internal.parse_filename(filename, fields)
4+
%
5+
% USAGE::
6+
%
7+
% p = bids.internal.parse_filename(filename, fields)
8+
%
9+
% :param filename: fielname to parse that follows the pattern
10+
% ``sub-label[_entity-label]*_suffix.extension``
11+
% :type filename: string
12+
% :param fields: cell of strings of the entities to use for parsing
13+
% :type fields: cell
414
%
515
% Example:
616
%
7-
% >> filename = '../sub-16/anat/sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz';
8-
% >> bids.internal.parse_filename(filename)
17+
% filename = '../sub-16/anat/sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz';
18+
%
19+
% bids.internal.parse_filename(filename)
920
%
10-
% ans =
21+
% ans =
1122
%
1223
% struct with fields:
1324
%
14-
% filename: 'sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz'
15-
% suffix: 'T1w'
16-
% ext: '.nii.gz'
17-
% sub: '16'
18-
% ses: 'mri'
19-
% run: '1'
20-
% acq: 'hd'
25+
% 'filename', 'sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz', ...
26+
% 'suffix', 'T1w', ...
27+
% 'ext', '.nii.gz', ...
28+
% 'entities', struct('sub', '16', ...
29+
% 'ses', 'mri', ...
30+
% 'run', '1', ...
31+
% 'acq', 'hd');
32+
%
2133
% __________________________________________________________________________
2234

2335
% Copyright (C) 2016-2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging
@@ -26,12 +38,10 @@
2638
filename = bids.internal.file_utils(filename, 'filename');
2739

2840
% -Identify all the BIDS entity-label pairs present in the filename (delimited by "_")
29-
% https://bids-specification.readthedocs.io/en/stable/99-appendices/04-entity-table.html
3041
[parts, dummy] = regexp(filename, '(?:_)+', 'split', 'match'); %#ok<ASGLU>
3142
p.filename = filename;
3243

3344
% -Identify the suffix and extension of this file
34-
% https://bids-specification.readthedocs.io/en/stable/02-common-principles.html#file-name-structure
3545
[p.suffix, p.ext] = strtok(parts{end}, '.');
3646

3747
% -Separate the entity from the label for each pair identified above

+bids/layout.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@
110110
BIDS.subjects = parse_subject(BIDS.dir, subjects{iSub}, sessions{iSess}, schema);
111111
else
112112
new_subject = parse_subject(BIDS.dir, subjects{iSub}, sessions{iSess}, schema);
113-
113+
114114
[BIDS.subjects, new_subject] = bids.internal.match_structure_fields(BIDS.subjects, ...
115115
new_subject);
116116
% TODO: this can be added to "match_structure_fields"
117-
BIDS.subjects(end + 1) = new_subject;
117+
BIDS.subjects(end + 1) = new_subject;
118118

119119
end
120120
end

tests/test_append_to_layout.m

+25-24
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function test_append_to_layout_basic()
2929
'rec', '', ...
3030
'part', ''));
3131

32-
assertEqual(subject.anat, expected.anat);
32+
assertEqual(subject, expected);
3333

3434
end
3535

@@ -46,29 +46,30 @@ function test_append_to_structure_basic_test()
4646
file = '../sub-16/anat/sub-16_ses-mri_run-1_T1map.nii.gz';
4747
subject = bids.internal.append_to_layout(file, subject, modality, schema);
4848

49-
expected(1).anat = struct( ...
50-
'filename', 'sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz', ...
51-
'suffix', 'T1w', ...
52-
'ext', '.nii.gz', ...
53-
'entities', struct('sub', '16', ...
54-
'ses', 'mri', ...
55-
'run', '1', ...
56-
'acq', 'hd', ...
57-
'ce', '', ...
58-
'rec', '', ...
59-
'part', ''));
60-
61-
expected(2).anat = struct( ...
62-
'filename', 'sub-16_ses-mri_run-1_T1map.nii.gz', ...
63-
'suffix', 'T1map', ...
64-
'ext', '.nii.gz', ...
65-
'entities', struct('sub', '16', ...
66-
'ses', 'mri', ...
67-
'run', '1', ...
68-
'acq', '', ...
69-
'ce', '', ...
70-
'rec', '', ...
71-
'part', '')); %#ok<*STRNU>
49+
expected.anat(1, 1) = struct( ...
50+
'filename', 'sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz', ...
51+
'suffix', 'T1w', ...
52+
'ext', '.nii.gz', ...
53+
'entities', struct('sub', '16', ...
54+
'ses', 'mri', ...
55+
'run', '1', ...
56+
'acq', 'hd', ...
57+
'ce', '', ...
58+
'rec', '', ...
59+
'part', ''));
60+
61+
expected.anat(2, 1) = struct( ...
62+
'filename', 'sub-16_ses-mri_run-1_T1map.nii.gz', ...
63+
'suffix', 'T1map', ...
64+
'ext', '.nii.gz', ...
65+
'entities', struct('sub', '16', ...
66+
'ses', 'mri', ...
67+
'run', '1', ...
68+
'acq', '', ...
69+
'ce', '', ...
70+
'rec', '')); %#ok<*STRNU>
71+
72+
assertEqual(subject, expected);
7273

7374
end
7475

tests/test_bids_query.m

+13-13
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ function test_bids_query_modalities()
7070
BIDS = bids.layout(fullfile(pth_bids_example, 'ds007'));
7171

7272
modalities = {'anat', 'func'};
73-
assert(isequal(bids.query(BIDS, 'modalities'), modalities));
74-
assert(isequal(bids.query(BIDS, 'modalities', 'sub', '01'), modalities));
73+
assertEqual(bids.query(BIDS, 'modalities'), modalities);
74+
assertEqual(bids.query(BIDS, 'modalities', 'sub', '01'), modalities);
7575

7676
BIDS = bids.layout(fullfile(pth_bids_example, '7t_trt'));
7777

7878
modalities = {'anat', 'fmap', 'func'};
7979

80-
assert(isequal(bids.query(BIDS, 'modalities'), modalities));
81-
assert(isequal(bids.query(BIDS, 'modalities', 'sub', '01'), modalities));
82-
assert(isequal(bids.query(BIDS, 'modalities', 'sub', '01', 'ses', '1'), modalities));
80+
assertEqual(bids.query(BIDS, 'modalities'), modalities);
81+
assertEqual(bids.query(BIDS, 'modalities', 'sub', '01'), modalities);
82+
assertEqual(bids.query(BIDS, 'modalities', 'sub', '01', 'ses', '1'), modalities);
8383

8484
% this now fails on octave 4.2.2 but not on Matlab
8585
%
@@ -94,7 +94,7 @@ function test_bids_query_modalities()
9494
%
9595
% when it should return
9696

97-
% assert(isequal(bids.query(BIDS, 'modalities', 'sub', '01', 'ses', '2'), mods(2:3)));
97+
% assertEqual(bids.query(BIDS, 'modalities', 'sub', '01', 'ses', '2'), mods(2:3)));
9898

9999
end
100100

@@ -108,13 +108,13 @@ function test_bids_query_basic()
108108
'stopsignalwithletternaming', ...
109109
'stopsignalwithmanualresponse', ...
110110
'stopsignalwithpseudowordnaming'};
111-
assert(isequal(bids.query(BIDS, 'tasks'), tasks));
111+
assertEqual(bids.query(BIDS, 'tasks'), tasks);
112112

113113
assert(isempty(bids.query(BIDS, 'runs', 'suffix', 'T1w')));
114114

115115
runs = {'01', '02'};
116-
assert(isequal(bids.query(BIDS, 'runs'), runs));
117-
assert(isequal(bids.query(BIDS, 'runs', 'suffix', 'bold'), runs));
116+
assertEqual(bids.query(BIDS, 'runs'), runs);
117+
assertEqual(bids.query(BIDS, 'runs', 'suffix', 'bold'), runs);
118118

119119
end
120120

@@ -125,7 +125,7 @@ function test_bids_query_subjects()
125125
BIDS = bids.layout(fullfile(pth_bids_example, 'ds007'));
126126

127127
subjs = arrayfun(@(x) sprintf('%02d', x), 1:20, 'UniformOutput', false);
128-
assert(isequal(bids.query(BIDS, 'subjects'), subjs));
128+
assertEqual(bids.query(BIDS, 'subjects'), subjs);
129129

130130
end
131131

@@ -135,8 +135,8 @@ function test_bids_query_sessions()
135135

136136
BIDS = bids.layout(fullfile(pth_bids_example, 'synthetic'));
137137
sessions = {'01', '02'};
138-
assert(isequal(bids.query(BIDS, 'sessions'), sessions));
139-
assert(isequal(bids.query(BIDS, 'sessions', 'sub', '02'), sessions));
138+
assertEqual(bids.query(BIDS, 'sessions'), sessions);
139+
assertEqual(bids.query(BIDS, 'sessions', 'sub', '02'), sessions);
140140

141141
BIDS = bids.layout(fullfile(pth_bids_example, 'ds007'));
142142

@@ -151,6 +151,6 @@ function test_bids_query_suffixes()
151151
BIDS = bids.layout(fullfile(pth_bids_example, 'ds007'));
152152

153153
suffixes = {'T1w', 'bold', 'events', 'inplaneT2'};
154-
assert(isequal(bids.query(BIDS, 'suffixes'), suffixes));
154+
assertEqual(bids.query(BIDS, 'suffixes'), suffixes);
155155

156156
end

tests/test_file_utils.m

+19-19
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,49 @@ function test_file_utils_basic()
1414
str = fullfile('folder', 'filename.extension');
1515

1616
p = bids.internal.file_utils(str, 'path');
17-
assert(isequal(p, 'folder'));
17+
assertEqual(p, 'folder');
1818

1919
p = bids.internal.file_utils(str, 'fpath');
20-
assert(isequal(p, fullfile(pwd, 'folder')));
20+
assertEqual(p, fullfile(pwd, 'folder'));
2121

2222
filename = bids.internal.file_utils(str, 'basename');
23-
assert(isequal(filename, 'filename'));
23+
assertEqual(filename, 'filename');
2424

2525
ext = bids.internal.file_utils(str, 'ext');
26-
assert(isequal(ext, 'extension'));
26+
assertEqual(ext, 'extension');
2727

2828
str = fullfile('folder', 'subfolder', '..', 'filename.extension');
2929
cpath = bids.internal.file_utils(str, 'cpath');
30-
assert(isequal(cpath, ...
31-
fullfile(pwd, 'folder', 'filename.extension')));
30+
assertEqual(cpath, ...
31+
fullfile(pwd, 'folder', 'filename.extension'));
3232

3333
%% test to set certain part of a filename
3434
% {'path', 'basename', 'ext', 'filename', 'prefix', 'suffix'}
3535

3636
str = fullfile('folder', 'filename.extension');
3737

3838
new_str = bids.internal.file_utils(str, 'ext', 'newext');
39-
assert(isequal(new_str, fullfile('folder', 'filename.newext')));
39+
assertEqual(new_str, fullfile('folder', 'filename.newext'));
4040

4141
new_str = bids.internal.file_utils(str, 'basename', 'new_name');
42-
assert(isequal(new_str, fullfile('folder', 'new_name.extension')));
42+
assertEqual(new_str, fullfile('folder', 'new_name.extension'));
4343

4444
new_str = bids.internal.file_utils(str, 'filename', 'new_name.newext');
45-
assert(isequal(new_str, fullfile('folder', 'new_name.newext')));
45+
assertEqual(new_str, fullfile('folder', 'new_name.newext'));
4646

4747
new_str = bids.internal.file_utils(str, 'prefix', 'pre_');
48-
assert(isequal(new_str, fullfile('folder', 'pre_filename.extension')));
48+
assertEqual(new_str, fullfile('folder', 'pre_filename.extension'));
4949

5050
new_str = bids.internal.file_utils(str, 'suffix', '_suffix');
51-
assert(isequal(new_str, fullfile('folder', 'filename_suffix.extension')));
51+
assertEqual(new_str, fullfile('folder', 'filename_suffix.extension'));
5252

5353
new_str = bids.internal.file_utils(str, 'path', fullfile(pwd, 'new_folder'));
54-
assert(isequal(new_str, fullfile(pwd, 'new_folder', 'filename.extension')));
54+
assertEqual(new_str, fullfile(pwd, 'new_folder', 'filename.extension'));
5555

5656
new_str = bids.internal.file_utils(str, ...
5757
'prefix', 'pre_', ...
5858
'suffix', '_suffix');
59-
assert(isequal(new_str, fullfile('folder', 'pre_filename_suffix.extension')));
59+
assertEqual(new_str, fullfile('folder', 'pre_filename_suffix.extension'));
6060

6161
%% test to list files
6262

@@ -65,29 +65,29 @@ function test_file_utils_basic()
6565
file = bids.internal.file_utils('List', ...
6666
test_directory, ...
6767
'^test_file_utils.m$');
68-
assert(isequal(file, 'test_file_utils.m'));
68+
assertEqual(file, 'test_file_utils.m');
6969

7070
file = bids.internal.file_utils('List', ...
7171
test_directory, ...
7272
'^.*.md$');
73-
assert(isequal(file, 'README.md'));
73+
assertEqual(file, 'README.md');
7474

7575
directory = bids.internal.file_utils('List', ...
7676
test_directory, ...
7777
'dir', ...
7878
'^data$');
79-
assert(isequal(directory, 'data'));
79+
assertEqual(directory, 'data');
8080

8181
fp_file = bids.internal.file_utils('FPList', ...
8282
test_directory, ...
8383
'^test_file_utils.m$');
84-
assert(isequal(fp_file, [mfilename('fullpath') '.m']));
84+
assertEqual(fp_file, [mfilename('fullpath') '.m']);
8585

8686
fp_directory = bids.internal.file_utils('FPList', ...
8787
test_directory, ...
8888
'dir', ...
8989
'^data$');
90-
assert(isequal(fp_directory, ...
91-
fullfile(test_directory, 'data')));
90+
assertEqual(fp_directory, ...
91+
fullfile(test_directory, 'data'));
9292

9393
end

tests/test_get_metadata.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function test_get_metadata_basic()
5252

5353
%% test anat metadata subject 01
5454
metadata = bids.query(BIDS, 'metadata', 'sub', '01', 'suffix', 'T1w');
55-
assert(metadata.FlipAngle == anat_sub_01.FlipAngle);
56-
assert(strcmp(metadata.Manufacturer, anat_sub_01.Manufacturer));
55+
assertEqual(metadata.FlipAngle, anat_sub_01.FlipAngle);
56+
assertEqual(metadata.Manufacturer, anat_sub_01.Manufacturer);
5757

5858
end

tests/test_return_modality_entities.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
expected_output = {'sub', 'ses', 'task', 'acq', 'ce', 'rec', 'dir', 'run', 'echo', 'part'};
1616

17-
assert(isequal(entities, expected_output));
17+
assertEqual(entities, expected_output);
1818

1919
end

tests/test_return_modality_regular_expression.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
'_(T1w|T2w|PDw|T2starw|FLAIR|inplaneT1|inplaneT2|PDT2|angio){1}', ...
1717
'(.nii.gz|.nii){1}$'];
1818

19-
assert(isequal(regular_expression, expected_expression));
19+
assertEqual(regular_expression, expected_expression);
2020

2121
data_dir = fullfile(fileparts(mfilename('fullpath')), 'data', 'MoAEpilot', 'sub-01', 'anat');
2222
subject_name = 'sub-01';
2323
file = bids.internal.file_utils('List', data_dir, sprintf(expected_expression, subject_name));
2424

25-
assert(isequal(file, 'sub-01_T1w.nii.gz'));
25+
assertEqual(file, 'sub-01_T1w.nii.gz');
2626

2727
end

tests/test_return_modality_suffixes.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
schema = bids.schema.load_schema();
1212

1313
suffixes = bids.internal.return_modality_suffixes(schema.datatypes.func(1));
14-
assert(isequal(suffixes, '_(bold|cbv|sbref){1}'));
14+
assertEqual(suffixes, '_(bold|cbv|sbref){1}');
1515

1616
end

tests/test_tsvread.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ function test_tsvread_basic()
3030
%% test tsvread on tsv file
3131
tsv_file = fullfile(pth, 'sub-01', 'func', 'sub-01_task-auditory_events.tsv');
3232
output = bids.util.tsvread(tsv_file);
33-
assert(isequal(output.onset', events.onset));
33+
assertEqual(output.onset', events.onset);
3434

3535
%% test tsvread on zipped tsv file
3636
output = bids.util.tsvread(fullfile( ...
3737
fileparts(mfilename('fullpath')), ....
3838
'data', ...
3939
'sub-01_task-auditory_events.tsv.gz'));
40-
assert(isequal(output.onset', events.onset));
40+
assertEqual(output.onset', events.onset);
4141

4242
end

0 commit comments

Comments
 (0)