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

[ENH] sanitize entities in rename spec #679

Merged
merged 1 commit into from
Mar 15, 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
31 changes: 30 additions & 1 deletion +bids/File.m
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@
% file = file.rename('spec', spec, 'dry_run', true, 'verbose', [], 'force', false);
%
% :param spec: structure specifying what entities, suffix, extension... to apply
% If one of the entities in the ``spec`` contains a `'.'`
% it will be replaced by `pt`.
% :type spec: structure
%
% :param dry_run: If ``true`` no file is actually renamed.
Expand Down Expand Up @@ -574,11 +576,13 @@
obj.extension = spec.ext;
end
if isfield(spec, 'entities')
spec.entities = obj.normalize_entities(spec.entities);
entities = fieldnames(spec.entities); %#ok<*PROPLC>
for i = 1:numel(entities)
obj = obj.set_entity(entities{i}, ...
bids.internal.camel_case(spec.entities.(entities{i})));
end

end
if isfield(spec, 'entity_order')
obj = obj.reorder_entities(spec.entity_order);
Expand Down Expand Up @@ -614,6 +618,31 @@

end

function entities = normalize_entities(~, entities, replace)
% Clean up entities
%
% Replaces "." in entity label with "pt".
%
%
% USAGE::
%
% entities = file.normalize_entities(entities);
%
REPLACE(1) = struct('in', '.', 'out', 'pt');
if nargin < 3
replace = REPLACE;
end

entities_names = fieldnames(entities); %#ok<*PROPLC>
for j = 1:numel(replace)
for i = 1:numel(entities_names)
entities.(entities_names{i}) = strrep(entities.(entities_names{i}), ...
REPLACE(j).in, ...
REPLACE(j).out);
end
end
end

%% schema related methods
function obj = use_schema(obj)
%
Expand Down Expand Up @@ -884,7 +913,7 @@ function bids_file_error(obj, id, msg)
function validate_string(obj, str, type, pattern)

if ~ischar(str)
obj.bids_file_error(['Invalid' type], 'not chararray');
obj.bids_file_error(['Invalid' type], sprintf('"%s" is not char array', str));
end

if size(str, 1) > 1
Expand Down
19 changes: 19 additions & 0 deletions tests/test_bids_file.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@ function test_rename_with_spec()

end

function test_rename_spec_with_pt()

input_filename = 'wuasub-01_task-faceRecognition_bold.nii';
output_filename = 'sub-01_task-faceRecognition_label-GM_res-1pt5_desc-bold_dseg.json';
file = bids.File(input_filename, 'use_schema', false);

spec.prefix = '';
spec.entities.desc = 'bold';
spec.entities.res = '1.5';
spec.entities.label = 'GM';
spec.suffix = 'dseg';
spec.ext = '.json';
spec.entity_order = {'sub', 'task', 'label', 'res', 'desc'};

file = file.rename('spec', spec);
assertEqual(file.filename, output_filename);

end

function test_rename_force()

input_filename = 'wuasub-01_ses-test_task-faceRecognition_run-02_bold.nii';
Expand Down