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] create valid participants and sessions tsv during dataset init #688

Merged
merged 3 commits into from
Apr 7, 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
8 changes: 4 additions & 4 deletions +bids/+util/create_participants_tsv.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@
% in case the query returns empty in case no file was indexed
if isempty(subjects_list) && ~use_schema
subjects_list = cellstr(bids.internal.file_utils('List', layout.pth, 'dir', '^sub-.*$'));
output_structure = struct('participant_id', {subjects_list});
else
subjects_list = [repmat('sub-', numel(subjects_list), 1), char(subjects_list')];
output_structure = struct('participant_id', subjects_list);
end

subjects_list = [repmat('sub-', numel(subjects_list), 1), char(subjects_list')];

output_structure = struct('participant_id', subjects_list);

output_filename = fullfile(layout.pth, 'participants.tsv');

bids.util.tsvwrite(fullfile(layout.pth, 'participants.tsv'), output_structure);
Expand Down
2 changes: 1 addition & 1 deletion +bids/+util/create_sessions_tsv.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
fullfile(layout.pth, ...
['sub-' subjects_list{i_sub}]), ...
'dir', ...
'^sub-.*$'));
'^ses-.*$'));
sessions_list = regexprep(sessions_list, 'ses-', '');
end

Expand Down
2 changes: 2 additions & 0 deletions docs/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [FIX] Create valid participants and sessions tsv during dataset init #688 by @Remi-Gau

### Security


Expand Down
83 changes: 48 additions & 35 deletions tests/test_bids_init.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ function test_basic()

set_test_cfg();

dataset_description = fullfile(pwd, 'dummy_ds', 'dataset_description.json');
output_dir = fullfile(temp_dir(), 'dummy_ds');

bids.init('dummy_ds');
assertEqual(exist(fullfile(pwd, 'dummy_ds'), 'dir'), 7);
bids.init(output_dir);
assertEqual(exist(output_dir, 'dir'), 7);

dataset_description = fullfile(output_dir, 'dataset_description.json');
assertEqual(exist(dataset_description, 'file'), 2);
ds_metadata = bids.util.jsondecode(dataset_description);
assertEqual(ds_metadata.DatasetType, 'raw');

clean_up();

end

function test_no_folder_smoke_test()
Expand All @@ -30,102 +29,116 @@ function test_no_folder_smoke_test()

bids.init('dummy_ds', 'folders', struct(), 'is_derivative', true);

clean_up();
end

function test_valid_tsv()
set_test_cfg();

output_dir = fullfile(temp_dir(), 'dummy_ds');

folders.subjects = 'a';
folders.sessions = '1';
folders.modalities = 'beh';

bids.init(output_dir, 'folders', folders);
assertEqual(exist(fullfile(output_dir, 'sub-a', 'ses-1', 'beh'), 'dir'), 7);
assertEqual(exist(fullfile(output_dir, 'sub-a', 'sub-a_sessions.tsv'), 'file'), 2);

participants = bids.util.tsvread(fullfile(output_dir, 'participants.tsv'));
assertEqual(participants.participant_id, {'sub-a'});

sessions = bids.util.tsvread(fullfile(output_dir, 'sub-a', 'sub-a_sessions.tsv'));
assertEqual(sessions.session_id, {'ses-1'});
end

function test_folders()

set_test_cfg();

output_dir = fullfile(temp_dir(), 'dummy_ds');

folders.subjects = {'01', '02'};
folders.sessions = {'test', 'retest', ''};
folders.modalities = {'anat', 'func', 'fizz', ''};

bids.init('dummy_ds', 'folders', folders);
assertEqual(exist(fullfile(pwd, 'dummy_ds', 'sub-02', 'ses-retest', 'func'), 'dir'), 7);
assertEqual(exist(fullfile(pwd, 'dummy_ds', 'sub-02', 'sub-02_sessions.tsv'), 'file'), 2);
bids.init(output_dir, 'folders', folders);
assertEqual(exist(fullfile(output_dir, 'sub-02', 'ses-retest', 'func'), 'dir'), 7);
assertEqual(exist(fullfile(output_dir, 'sub-02', 'sub-02_sessions.tsv'), 'file'), 2);

clean_up();
sessions = bids.util.tsvread(fullfile(output_dir, 'sub-01', 'sub-01_sessions.tsv'));
assertEqual(sessions.session_id, {'ses-retest'; 'ses-test'});

end

function test_folders_no_session()

set_test_cfg();

output_dir = fullfile(temp_dir(), 'dummy_ds');

folders.subjects = {'01', '02'};
folders.modalities = {'anat', 'func'};

bids.init('dummy_ds', 'folders', folders);
assertEqual(exist(fullfile(pwd, 'dummy_ds', 'sub-02', 'func'), 'dir'), 7);
assertEqual(exist(fullfile(pwd, 'dummy_ds', 'sub-02', 'sub-02_sessions.tsv'), 'file'), 0);
bids.init(output_dir, 'folders', folders);
assertEqual(exist(fullfile(output_dir, 'sub-02', 'func'), 'dir'), 7);
assertEqual(exist(fullfile(output_dir, 'sub-02', 'sub-02_sessions.tsv'), 'file'), 0);

clean_up();
participants = bids.util.tsvread(fullfile(output_dir, 'participants.tsv'));
assertEqual(participants.participant_id, {'sub-01'; 'sub-02'});

end

function test_validate()

set_test_cfg();

output_dir = fullfile(temp_dir(), 'dummy_ds');

folders.subjects = {'01-bla', '02_foo'};
folders.sessions = {'te-st', 'ret$est'};
folders.modalities = {'a#nat', 'fu*nc', '45^['};

assertExceptionThrown(@() bids.init('dummy_ds', 'folders', folders), ...
assertExceptionThrown(@() bids.init(output_dir, 'folders', folders), ...
'init:nonAlphaNumFodler');

folders.subjects = {'01', '02'};
folders.sessions = {'te-st', 'ret$est'};
folders.modalities = {'a#nat', 'fu*nc', '45^['};

assertExceptionThrown(@() bids.init('dummy_ds', 'folders', folders), ...
assertExceptionThrown(@() bids.init(output_dir, 'folders', folders), ...
'init:nonAlphaNumFodler');

folders.subjects = {'01', '02'};
folders.sessions = {'test', 'retest'};
folders.modalities = {'a#nat', 'fu*nc', '45^['};

assertExceptionThrown(@() bids.init('dummy_ds', 'folders', folders), ...
assertExceptionThrown(@() bids.init(output_dir, 'folders', folders), ...
'init:nonAlphaNumFodler');

clean_up();

end

function test_derivatives()

set_test_cfg();

output_dir = fullfile(temp_dir(), 'dummy_ds');

folders.subjects = {'01', '02'};
folders.sessions = {'test', 'retest'};
folders.modalities = {'anat', 'func'};

dataset_description = fullfile(pwd, 'dummy_ds', 'dataset_description.json');
dataset_description = fullfile(output_dir, 'dataset_description.json');

bids.init('dummy_ds', 'folders', folders, 'is_derivative', true);
assertEqual(exist(fullfile(pwd, 'dummy_ds', 'sub-02', 'ses-retest', 'func'), 'dir'), 7);
bids.init(output_dir, 'folders', folders, 'is_derivative', true);
assertEqual(exist(fullfile(output_dir, 'sub-02', 'ses-retest', 'func'), 'dir'), 7);

ds_metadata = bids.util.jsondecode(dataset_description);
assertEqual(ds_metadata.DatasetType, 'derivative');

% smoke test
bids.init('dummy_ds', ...
bids.init(output_dir, ...
'folders', folders, ...
'is_derivative', true, ...
'is_datalad_ds', true);

clean_up();

end

function clean_up()

pause(0.5);

if isdir(fullfile(pwd, 'dummy_ds'))
rmdir(fullfile(pwd, 'dummy_ds'), 's');
end

end
19 changes: 17 additions & 2 deletions tests/tests_utils/test_create_participants_tsv.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@

function test_create_participants_tsv_basic()

bids_path = fullfile(get_test_data_dir(), 'asl001');
bids_path = temp_dir();

copyfile(fullfile(get_test_data_dir(), 'asl001'), bids_path);

if bids.internal.is_octave
bids_path = fullfile(bids_path, 'asl001');
end

validate_dataset(bids_path);

output_filename = bids.util.create_participants_tsv(bids_path, 'verbose', false);

participants = bids.util.tsvread(output_filename);
assertEqual(participants.participant_id, {'sub-Sub103'});

validate_dataset(bids_path);

delete(output_filename);
Expand All @@ -24,7 +33,13 @@ function test_create_participants_tsv_already_exist()

skip_if_octave('mixed-string-concat warning thrown');

bids_path = fullfile(get_test_data_dir(), 'ds210');
bids_path = temp_dir();

copyfile(fullfile(get_test_data_dir(), 'ds210'), bids_path);

if bids.internal.is_octave
bids_path = fullfile(bids_path, 'ds210');
end

output_filename = bids.util.create_participants_tsv(bids_path);

Expand Down
24 changes: 14 additions & 10 deletions tests/tests_utils/test_create_sessions_tsv.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

function test_create_sessions_tsv_no_session()

bids_path = fullfile(get_test_data_dir(), 'ds210');
bids_path = temp_dir();

copyfile(fullfile(get_test_data_dir(), 'ds210'), bids_path);

if bids.internal.is_octave
bids_path = fullfile(bids_path, 'ds210');
end

validate_dataset(bids_path);

Expand All @@ -27,7 +33,13 @@ function test_create_sessions_tsv_no_session()

function test_create_sessions_tsv_basic()

bids_path = fullfile(get_test_data_dir(), 'ieeg_epilepsy');
bids_path = temp_dir();

copyfile(fullfile(get_test_data_dir(), 'ieeg_epilepsy'), bids_path);

if bids.internal.is_octave
bids_path = fullfile(bids_path, 'ieeg_epilepsy');
end

validate_dataset(bids_path);

Expand All @@ -41,12 +53,4 @@ function test_create_sessions_tsv_basic()

validate_dataset(bids_path);

teardown(bids_path, output_filenames);

end

function teardown(pth, filelist)
for i = 1:numel(filelist)
delete(fullfile(pth, filelist{i}));
end
end