Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added modality retrieval from path #656

Merged
merged 9 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions +bids/File.m
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
obj.path = args.Results.input;
end
f_struct = bids.internal.parse_filename(args.Results.input);
obj.modality = obj.get_modality(f_struct.entities);
elseif isstruct(args.Results.input)
f_struct = args.Results.input;
end
Expand All @@ -223,14 +224,14 @@
obj.bids_file_error('emptySuffix', 'no suffix specified');
end

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

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

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

if args.Results.use_schema
obj = obj.use_schema();
end
Expand Down Expand Up @@ -950,6 +951,39 @@ function validate_prefix(obj, prefix)
end
end

function modality = get_modality(obj, entities)
% Retrieves modality out of the path
%
% Only works if ses and sub entities match those found in the path
modality = '';

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

if ~isfield(entities, 'sub')
return
end

path = fileparts(obj.path);
[path, candidate] = 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 = candidate;
end

end

end

end
71 changes: 53 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,69 @@ 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()

bf = bids.File(struct('modality', 'anat'), 'use_schema', false);
assertEqual(bf.modality, 'anat');

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