Skip to content

Commit

Permalink
create valid participants and sessions tsv during dataset init
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Apr 7, 2024
1 parent 6e2a3fb commit 67d014f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 52 deletions.
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
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
11 changes: 9 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,17 @@

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);

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 +29,9 @@ 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);

output_filename = bids.util.create_participants_tsv(bids_path);

Expand Down
16 changes: 6 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,9 @@

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);

validate_dataset(bids_path);

Expand All @@ -27,7 +29,9 @@ 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);

validate_dataset(bids_path);

Expand All @@ -41,12 +45,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

0 comments on commit 67d014f

Please sign in to comment.