Skip to content

Commit

Permalink
[FIX] failing to return file index during dependencies indexing thorw…
Browse files Browse the repository at this point in the history
… warning and not error (#546)

* no file index throws warning and not error

* silence octave warning
  • Loading branch information
Remi-Gau authored Apr 27, 2023
1 parent 258ff7a commit bd992f0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
30 changes: 17 additions & 13 deletions +bids/+internal/return_file_index.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function file_idx = return_file_index(BIDS, modality, filename)
%
% For a given filename and modality, it returns the file index
% in the subject sub-structure of the BIDS structure
% in the subject sub-structure of the BIDS structure.
%
% UISAGE::
%
Expand All @@ -13,18 +13,22 @@
sub_idx = bids.internal.return_subject_index(BIDS, filename);
try
file_idx = strcmp(filename, {BIDS.subjects(sub_idx).(modality).filename}');
catch ME
warning(['An error occurred when processing', ...
'\n\t- dataset: %s', ...
'\n\t- subject: %s', ...
'\n\t- modality: %s', ...
'\n\t- file: %s', ...
'\nThis may happen if your dataset is not valid.'], ...
BIDS.pth, ...
BIDS.subjects(sub_idx).name, ...
modality, ...
filename);
rethrow(ME);
catch
msg = sprintf(['An error occurred when processing', ...
'\n\t- dataset: %s', ...
'\n\t- subject: %s', ...
'\n\t- modality: %s', ...
'\n\t- file: %s', ...
'\nThis may happen if your dataset is not valid.'], ...
BIDS.pth, ...
BIDS.subjects(sub_idx).name, ...
modality, ...
filename);
tolerant = true;
verbose = true;
bids.internal.error_handling(mfilename(), 'noFileIndex', msg, tolerant, verbose);
file_idx = [];
return
end
file_idx = find(file_idx);

Expand Down
7 changes: 4 additions & 3 deletions +bids/+internal/return_file_info.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
end

file_info.modality = bids.internal.file_utils(file_info.path, 'filename');
file_info.file_idx = bids.internal.return_file_index(BIDS, ...
file_info.modality, ...
file_info.filename);
file_idx = bids.internal.return_file_index(BIDS, ...
file_info.modality, ...
file_info.filename);
file_info.file_idx = file_idx;

end
3 changes: 2 additions & 1 deletion +bids/copy_to_derivative.m
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ function copy_file(BIDS, derivatives_folder, data_file, unzip_files, force, skip

info = bids.internal.return_file_info(BIDS, data_file);

if ~isfield(info, 'sub_idx') || ~isfield(info, 'modality')
if ~isfield(info, 'sub_idx') || ~isfield(info, 'modality') || ...
isempty(info.sub_idx) || isempty(info.file_idx)
% TODO: for we do not copy files in the root directory that have been indexed.
return
end
Expand Down
2 changes: 1 addition & 1 deletion +bids/layout.m
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@

info_src = bids.internal.return_file_info(BIDS, file_list{iFile});
% skip files in the root folder with no sub entity
if isempty(info_src.sub_idx)
if isempty(info_src.sub_idx) || isempty(info_src.file_idx)
continue
end
file = BIDS.subjects(info_src.sub_idx).(info_src.modality)(info_src.file_idx);
Expand Down
20 changes: 20 additions & 0 deletions tests/tests_private/test_return_file_index.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,23 @@ function test_return_file_index_basic()
assertEqual(file_idx, 2);

end

function test_return_file_index_warning()

if bids.internal.is_octave
return
end

pth_bids_example = get_test_data_dir();

bids_dir = fullfile(pth_bids_example, 'qmri_tb1tfl');

unindexable_file = fullfile(bids_dir, 'sub-01_task-foo_events.tsv');
content = struct('onset', 1, 'duration', 1, 'trial_type', 'bar');
bids.util.tsvwrite(unindexable_file, content);

assertWarning(@()bids.layout(bids_dir), 'return_file_index:noFileIndex');

delete(unindexable_file);

end

0 comments on commit bd992f0

Please sign in to comment.