From be82fe6de76e2138954a32f9b0ef22870ce4b030 Mon Sep 17 00:00:00 2001 From: Remi Gau Date: Sun, 24 Mar 2024 11:55:04 -0400 Subject: [PATCH] [ENH] add zero padding to entity labels / indices when passed as numbers (#680) * pad indices when passed as numbers * update changelog * skip octave test --- +bids/File.m | 25 ++++++++++++++++ .vscode/settings.json | 3 +- docs/source/changelog.md | 61 ++++++++++++++++++++++++++++++++++++++++ docs/source/index.rst | 1 + tests/test_bids_file.m | 36 ++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 docs/source/changelog.md diff --git a/+bids/File.m b/+bids/File.m index c88fa2e1..223e086c 100644 --- a/+bids/File.m +++ b/+bids/File.m @@ -172,6 +172,8 @@ verbose = false + padding = 2 + end properties (SetAccess = private) @@ -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(); @@ -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 @@ -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 @@ -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) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9e80844a..b3e8da67 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,6 @@ "fileMatch": ["model-*_smdl.json"], "url": "https://raw.githubusercontent.com/bids-standard/stats-models/gh-pages/BIDSStatsModel.json" } - ] + ], + "esbonio.sphinx.confDir": "" } diff --git a/docs/source/changelog.md b/docs/source/changelog.md new file mode 100644 index 00000000..49529ab9 --- /dev/null +++ b/docs/source/changelog.md @@ -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). + + + +## [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 diff --git a/docs/source/index.rst b/docs/source/index.rst index 228951c6..0aad4f3d 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -26,6 +26,7 @@ To see how to install BIDS-Matlab, please check schema performance dev_doc + changelog Indices and tables diff --git a/tests/test_bids_file.m b/tests/test_bids_file.m index ac70d0c2..7e57b5a0 100644 --- a/tests/test_bids_file.m +++ b/tests/test_bids_file.m @@ -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'); @@ -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