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] add zero padding to entity labels / indices when passed as numbers #680

Merged
merged 4 commits into from
Mar 24, 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
25 changes: 25 additions & 0 deletions +bids/File.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@

verbose = false

padding = 2

end

properties (SetAccess = private)
Expand All @@ -188,11 +190,13 @@
args.addParameter('use_schema', false, @islogical);
args.addParameter('tolerant', obj.tolerant, @islogical);
args.addParameter('verbose', obj.verbose, @islogical);
args.addParameter('padding', obj.padding, @isnumeric);

args.parse(varargin{:});

obj.tolerant = args.Results.tolerant;
obj.verbose = args.Results.verbose;
obj.padding = args.Results.padding;

if args.Results.use_schema
obj.schema = bids.Schema();
Expand Down Expand Up @@ -224,6 +228,11 @@
obj.bids_file_error('emptySuffix', 'no suffix specified');
end

if isfield(f_struct, 'entities')
f_struct.entities = obj.pad_indices(f_struct.entities);
obj.entities = f_struct.entities;
end

if isfield(f_struct, 'modality')
obj.modality = f_struct.modality;
end
Expand All @@ -241,6 +250,21 @@
obj = obj.update();
end

function structure = pad_indices(obj, structure)
fields = fieldnames(structure);
pattern = ['%0', num2str(obj.padding), '.0f'];
if obj.padding <= 0
pattern = '%0.0f';
end
for i = 1:numel(fields)
if isnumeric(structure.(fields{i}))
structure.(fields{i}) = sprintf(pattern, ...
structure.(fields{i}));
end
end

end

%% Getters
function value = get.bids_path(obj)
if obj.changed
Expand Down Expand Up @@ -577,6 +601,7 @@
obj.extension = spec.ext;
end
if isfield(spec, 'entities')
spec.entities = obj.pad_indices(spec.entities);
spec.entities = obj.normalize_entities(spec.entities);
entities = fieldnames(spec.entities); %#ok<*PROPLC>
for i = 1:numel(entities)
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"fileMatch": ["model-*_smdl.json"],
"url": "https://raw.githubusercontent.com/bids-standard/stats-models/gh-pages/BIDSStatsModel.json"
}
]
],
"esbonio.sphinx.confDir": ""
}
61 changes: 61 additions & 0 deletions docs/source/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!--
### Added

### Changed

### Deprecated

### Removed

### Fixed

### Security
-->

## [Unreleased]

### Added

* [ENH] Add zero padding when numbers are passed for indices to `bids.File` or `bids.File.rename` #680 by @Remi-Gau

### Changed

### Deprecated

### Removed

### Fixed

### Security


## [v0.2.0]

### Changed

### Deprecated

### Removed

### Fixed

### Security

## [v0.1.0]

### Changed

### Deprecated

### Removed

### Fixed

### Security
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ To see how to install BIDS-Matlab, please check
schema
performance
dev_doc
changelog


Indices and tables
Expand Down
36 changes: 36 additions & 0 deletions tests/test_bids_file.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

function test_no_entity_warning

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

assertWarning(@()bids.File('TStatistic.nii', 'verbose', true), ...
'File:noEntity');

Expand Down Expand Up @@ -391,6 +393,40 @@ function test_change()

end

function test_zero_padding

entities = struct('sub', 1, ...
'task', 'faceRecognition', ...
'ses', 3, ...
'run', 2);
filename.suffix = 'bold';
filename.ext = '.nii';
filename.entities = entities;
file = bids.File(filename, 'use_schema', true);
assertEqual(file.filename, 'sub-01_ses-03_task-faceRecognition_run-02_bold.nii');

file = bids.File(filename, 'use_schema', true, 'padding', 3);
assertEqual(file.filename, 'sub-001_ses-003_task-faceRecognition_run-002_bold.nii');

file = bids.File(filename, 'use_schema', true, 'padding', 0);
assertEqual(file.filename, 'sub-1_ses-3_task-faceRecognition_run-2_bold.nii');

end

function test_zero_padding_spec

input_filename = 'sub-01_task-faceRecognition_bold.nii';
file = bids.File(input_filename, 'use_schema', false);

spec.entities.res = 2;

file = file.rename('spec', spec);

output_filename = 'sub-01_task-faceRecognition_res-02_bold.nii';
assertEqual(file.filename, output_filename);

end

function test_bids_file_remove_entities()

% GIVEN
Expand Down