Skip to content

Commit

Permalink
[ENH] allow to query directly for tsv content (#555)
Browse files Browse the repository at this point in the history
* silence some warnings

* test error
  • Loading branch information
Remi-Gau authored Apr 28, 2023
1 parent f423ea6 commit 786b562
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
4 changes: 3 additions & 1 deletion +bids/File.m
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,9 @@ function validate_string(obj, str, type, pattern)
if ~isempty(str)
res = regexp(str, pattern, 'once');
if isempty(res)
msg = sprintf('%s do not satisfy pattern %s', str, pattern);
msg = sprintf('%s do not satisfy pattern %s', ...
str, ...
strrep(pattern, '\', '\\'));
obj.bids_file_error(['Invalid' type], msg);
end
end
Expand Down
31 changes: 27 additions & 4 deletions +bids/query.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
% - ``'dependencies'``
% - ``'extensions'``
% - ``'prefixes'``
% - ``'tsv_content'``
%
% And any of the BIDS entities:
%
Expand Down Expand Up @@ -163,7 +164,8 @@
'metafiles', ...
'dependencies', ...
'extensions', ...
'prefixes'}, ...
'prefixes', ...
'tsv_content'}, ...
valid_entity_queries());

if ~any(strcmp(query, VALID_QUERIES))
Expand All @@ -173,8 +175,6 @@
bids.internal.error_handling(mfilename(), 'unknownQuery', msg, false, true);
end

% bids_entities = schema_entities();

BIDS = bids.layout(BIDS);

options = parse_query(varargin);
Expand All @@ -196,7 +196,11 @@
% Get optional target option for metadata query
[target, options] = get_target(query, options);

result = perform_query(BIDS, query, options, subjects, modalities, target);
if strcmp(query, 'tsv_content')
result = perform_query(BIDS, 'data', options, subjects, modalities, target);
else
result = perform_query(BIDS, query, options, subjects, modalities, target);
end

%% Postprocessing output variable
switch query
Expand All @@ -223,6 +227,25 @@
case cat(2, {'suffixes', 'suffixes', 'extensions', 'prefixes'}, valid_entity_queries())
result = unique(result);
result(cellfun('isempty', result)) = [];

case 'tsv_content'
if isempty(result)
return
end
extensions = bids.internal.file_utils(result, 'ext');
if numel(unique(extensions)) > 1 || ~strcmp(unique(extensions), 'tsv')
msg = sprintf(['Queries for ''tsv_content'' must be done only on tsv files.\n', ...
'Your query returned: %s'], ...
bids.internal.create_unordered_list(result));
bids.internal.error_handling(mfilename(), 'notJustTsvFiles', msg, false);
return
end
tmp = {};
for i_tsv_file = 1:numel(result)
tmp{i_tsv_file} = bids.util.tsvread(result{i_tsv_file});
end
result = tmp;

end

end
Expand Down
33 changes: 33 additions & 0 deletions tests/tests_query/test_bids_query.m
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,39 @@ function test_query_modalities()

end

function test_query_tsv_content()

pth_bids_example = get_test_data_dir();

BIDS = bids.layout(fullfile(pth_bids_example, 'eeg_ds003645s_hed_inheritance'));

tsv_content = bids.query(BIDS, 'tsv_content', 'suffix', 'events');

assertEqual(numel(tsv_content), 3);
assertEqual(fieldnames(tsv_content{1}), ...
{'onset'; ...
'duration'; ...
'sample'; ...
'event_type'; ...
'face_type'; ...
'rep_status'; ...
'trial'; ...
'rep_lag'; ...
'value'; ...
'stim_file'});

end

function test_query_tsv_content_error()

pth_bids_example = get_test_data_dir();

BIDS = bids.layout(fullfile(pth_bids_example, 'qmri_tb1tfl'));
assertExceptionThrown(@()bids.query(BIDS, 'tsv_content', 'extension', '.nii.gz'), ...
'query:notJustTsvFiles');

end

function test_query_sessions()

pth_bids_example = get_test_data_dir();
Expand Down
2 changes: 2 additions & 0 deletions tests/tests_query/test_bids_query_eeg.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function test_bids_query_eeg_basic()
suffixes = {'channels', 'eeg', 'electrodes', 'events'};
assertEqual(bids.query(BIDS, 'suffixes'), suffixes);

extension = bids.query(BIDS, 'data', 'extension', '.tsv');

%%
BIDS = bids.layout(fullfile(pth_bids_example, 'eeg_ds000117'));

Expand Down

0 comments on commit 786b562

Please sign in to comment.