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

[FIX] index modality at same level as sessions #681

Merged
merged 3 commits into from
Mar 25, 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
4 changes: 3 additions & 1 deletion +bids/+internal/return_subject_index.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

sub_idx = strcmp(['sub-' sub], {BIDS.subjects.name}');

ses_idx = true(size(sub_idx));
if isfield(parsed_file.entities, 'ses')
ses = parsed_file.entities.ses;
ses_idx = strcmp(['ses-' ses], {BIDS.subjects.session}');
else
% in case we have modality on same level as sessions
ses_idx = cellfun('isempty', {BIDS.subjects.session})';
end

sub_idx = find(all([sub_idx, ses_idx], 2));
Expand Down
9 changes: 9 additions & 0 deletions +bids/layout.m
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@

end

if numel(sessions) > 1
new_subject = parse_subject(BIDS.pth, subjects{iSub}, '', ...
schema, filter, tolerant, verbose);
[BIDS.subjects, new_subject] = bids.internal.match_structure_fields(BIDS.subjects, ...
new_subject);
% TODO: this can be added to "match_structure_fields"
BIDS.subjects(end + 1) = new_subject;
end

if verbose
fprintf(1, ']\n');
end
Expand Down
45 changes: 45 additions & 0 deletions tests/tests_layout/test_layout_derivatives.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,51 @@
initTestSuite;
end

function test_layout_modality_same_level_sessions()

pth_bids_example = fullfile(get_test_data_dir(), ...
'synthetic', ...
'derivatives', ...
'fmriprep');
tear_down(pth_bids_example);

BIDS_flat = bids.layout(pth_bids_example, ...
'use_schema', false, ...
'filter', struct('sub', {{'01', '02', '03'}}), ...
'index_dependencies', true);

% the name won't be accurate
% because the filenames will still contain the ses entity
for i = 1:3
copyfile(fullfile(pth_bids_example, sprintf('sub-%02.0f', i), 'ses-01', 'func'), ...
fullfile(pth_bids_example, sprintf('sub-%02.0f', i), 'func'));
end

BIDS_nested = bids.layout(pth_bids_example, ...
'use_schema', false, ...
'filter', struct('sub', {{'01', '02', '03'}}), ...
'index_dependencies', true);

assert(numel(bids.query(BIDS_flat, 'data')) < numel(bids.query(BIDS_nested, 'data')));
assertEqual(numel(bids.query(BIDS_flat, 'data', 'modality', 'anat')), ...
numel(bids.query(BIDS_nested, 'data', 'modality', 'anat')));
assertEqual(numel(bids.query(BIDS_flat, 'sessions')), ...
numel(bids.query(BIDS_nested, 'sessions')));
assertEqual(numel(bids.query(BIDS_flat, 'subjects')), ...
numel(bids.query(BIDS_nested, 'subjects')));

tear_down(pth_bids_example);

function tear_down(pth_bids_example)
for j = 1:3
if exist(fullfile(pth_bids_example, sprintf('sub-%02.0f', j), 'func'), 'dir')
rmdir(fullfile(pth_bids_example, sprintf('sub-%02.0f', j), 'func'), 's');
end
end
end

end

function test_layout_nested()

pth_bids_example = get_test_data_dir();
Expand Down