Skip to content

Commit

Permalink
add tests and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Mar 15, 2024
1 parent 3e1e627 commit 555ceb5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 34 deletions.
5 changes: 5 additions & 0 deletions +bids/+internal/parse_filename.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@

p = parse_entity_label_pairs(p, basename, tolerant, verbose);

if ~isempty(p)
p.modality = '';
end

% Extra fields can be added to the structure and ordered specifically.
if ~isempty(fields)
for i = 1:numel(fields)
Expand All @@ -114,6 +118,7 @@
p = struct([]);
end
end

end

function p = parse_entity_label_pairs(p, basename, tolerant, verbose)
Expand Down
36 changes: 20 additions & 16 deletions +bids/File.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,6 @@
obj.entities = f_struct.entities;
end

if isfield(f_struct, 'modality')
obj.modality = f_struct.modality;
end

if args.Results.use_schema
obj = obj.use_schema();
end
Expand Down Expand Up @@ -952,24 +948,32 @@ function validate_prefix(obj, prefix)
end

function modality = get_modality(obj, entities)
% Retrieves modsality out of the path by checking if
% Retrieves modality out of the path by checking if
% 2-level up folder is same as ses or sub entities
modality = '';
path = fileparts(obj.path);
[path, cand, ext] = fileparts(path);
cand = [cand, ext];
[~, ent_path, ext] = fileparts(path);
if ~isempty(ext)

if isempty(obj.path) || isempty(fileparts(obj.path))
return
end

comp_path = '';
if isfield(entities, 'ses') && ~isempty(entities.ses)
comp_path = ['ses-' entities.ses];
elseif isfield(entities, 'sub')
comp_path = ['sub-' entities.sub];
if ~isfield(entities, 'sub')
return
end
if ~isempty(comp_path) && strcmp(ent_path, comp_path)

path = fileparts(obj.path);
[path, cand] = fileparts(path);

has_ses = isfield(entities, 'ses') && ~isempty(entities.ses);
ses_ok = true;
if has_ses
[path, ses] = fileparts(path);
ses_ok = strcmp(ses, ['ses-' entities.ses]);
end

[~, sub] = fileparts(path);
sub_ok = strcmp(sub, ['sub-' entities.sub]);

if all([sub_ok, ses_ok])
modality = cand;
end

Expand Down
68 changes: 50 additions & 18 deletions tests/test_bids_file.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

bf = bids.File(file);

assertEqual(bf.modality, '');

end

function test_get_metadata_suffixes_basic()
Expand Down Expand Up @@ -297,36 +299,66 @@ function test_forbidden_entity()
function test_parsing()

filename = 'wuasub-01_ses-test_task-faceRecognition_run-02_bold.nii';
file = bids.File(filename, 'use_schema', false);
bf = bids.File(filename, 'use_schema', false);

entities = struct('sub', '01', 'ses', 'test', ...
'task', 'faceRecognition', 'run', '02');

assertEqual(file.prefix, 'wua');
assertEqual(file.suffix, 'bold');
assertEqual(file.extension, '.nii');
assertEqual(file.entities, entities);
assertEqual(file.filename, 'wuasub-01_ses-test_task-faceRecognition_run-02_bold.nii');
assertEqual(file.json_filename, 'wuasub-01_ses-test_task-faceRecognition_run-02_bold.json');
assertEqual(file.bids_path, fullfile('sub-01', 'ses-test'));
assertEqual(bf.modality, '');
assertEqual(bf.prefix, 'wua');
assertEqual(bf.suffix, 'bold');
assertEqual(bf.extension, '.nii');
assertEqual(bf.entities, entities);
assertEqual(bf.filename, 'wuasub-01_ses-test_task-faceRecognition_run-02_bold.nii');
assertEqual(bf.json_filename, 'wuasub-01_ses-test_task-faceRecognition_run-02_bold.json');
assertEqual(bf.bids_path, fullfile('sub-01', 'ses-test'));

filename = [];
filename.prefix = 'wua';
filename.suffix = 'bold';
filename.ext = '.nii';
filename.entities = entities;
file = bids.File(filename, 'use_schema', false);
assertEqual(file.filename, 'wuasub-01_ses-test_task-faceRecognition_run-02_bold.nii');
bf = bids.File(filename, 'use_schema', false);
assertEqual(bf.filename, 'wuasub-01_ses-test_task-faceRecognition_run-02_bold.nii');

end

function test_modality_parsing()

filename_expected = { ...
'', ''; ...
'sub-01_task-faceRecognition_bold.nii', ''; ...
fullfile('foo', 'participants.tsv'), ''; ...
fullfile('sub-01', 'sub-01_scans.tsv'), ...
''; ... % no modality folder
fullfile('sub-01', 'anat', 'sub-01_t1w.nii'), 'anat'; ...
fullfile('anat', 'sub-01_t1w.nii'), ...
''; ... % no sub folder
fullfile('sub-01', 'ses-01', 'anat', 'sub-01_ses-01_t1w.nii'), 'anat'; ...
fullfile('ses-01', 'anat', 'sub-01_ses-01_t1w.nii'), ''; ... % no sub folder
fullfile('sub-01', 'ses-01', 'anat', 'sub-01_t1w.nii'), ...
''; ... % no ses in filename
fullfile('sub-01', 'anat', 'sub-01_ses-01_t1w.nii'), '' ... % no ses folder
};

for i = 1:size(filename_expected)
bf = bids.File(filename_expected{i, 1}, 'use_schema', false);
assertEqual(bf.modality, filename_expected{i, 2});
end

end

function test_parsing_empty_filename()

bf = bids.File('');

% testing empty file name
file = bids.File('');
file.suffix = 'bold';
file.extension = '.nii';
assertEqual(file.filename, 'bold.nii');
bf.suffix = 'bold';
bf.extension = '.nii';
assertEqual(bf.filename, 'bold.nii');

file = file.set_entity('sub', 'abc');
file.suffix = '';
assertEqual(file.filename, 'sub-abc.nii');
bf = bf.set_entity('sub', 'abc');
bf.suffix = '';
assertEqual(bf.filename, 'sub-abc.nii');

end

Expand Down
9 changes: 9 additions & 0 deletions tests/tests_private/test_parse_filename.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function test_parse_filename_participants()
'suffix', 'participants', ...
'entities', struct(), ...
'ext', '.tsv', ...
'modality', '', ...
'prefix', '');

assertEqual(output, expected);
Expand All @@ -65,6 +66,7 @@ function test_parse_filename_prefix()
'suffix', 'bold', ...
'prefix', 'a', ...
'ext', '.nii', ...
'modality', '', ...
'entities', struct('sub', '16', ...
'task', 'rest', ...
'run', '1'));
Expand All @@ -83,6 +85,7 @@ function test_parse_filename_prefix()
'suffix', 'bold', ...
'prefix', 'a', ...
'ext', '.nii', ...
'modality', '', ...
'entities', struct('sub', '16', ...
'task', 'rest', ...
'wsub', '1'));
Expand All @@ -93,6 +96,9 @@ function test_parse_filename_prefix()
entities = fieldnames(output.entities);
assertEqual(entities, expectedEntities);

end

function test_parse_filename_sub_later_in_filename
% sub containing entity later in the filename
% NOT SURE THIS IS THE EXPECTED BEHAVIOR
filename = 'group-ctrl_wsub-1_bold.nii';
Expand All @@ -103,6 +109,7 @@ function test_parse_filename_prefix()
'prefix', 'group-ctrl_w', ...
'ext', '.nii', ...
'entities', struct('sub', '1'));
expected.modality = '';

assertEqual(output, expected);

Expand All @@ -120,6 +127,7 @@ function test_parse_filename_basic()
expected = struct('filename', 'sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz', ...
'suffix', 'T1w', ...
'ext', '.nii.gz', ...
'modality', '', ...
'entities', struct('sub', '16', ...
'ses', 'mri', ...
'run', '1', ...
Expand All @@ -143,6 +151,7 @@ function test_parse_filename_fields()
expected = struct('filename', 'sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz', ...
'suffix', 'T1w', ...
'ext', '.nii.gz', ...
'modality', '', ...
'entities', struct('sub', '16', ...
'ses', 'mri', ...
'run', '1', ...
Expand Down

0 comments on commit 555ceb5

Please sign in to comment.