Skip to content

Commit 0de33e5

Browse files
committed
refactor spm_2_bids
1 parent 20d0062 commit 0de33e5

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

src/Mapping.m

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,26 @@
104104
% filter.ext
105105
%
106106

107-
p = inputParser;
107+
args = inputParser;
108108

109-
addParameter(p, 'prefix', obj.default_value);
110-
addParameter(p, 'suffix', obj.default_value);
111-
addParameter(p, 'entities', obj.default_value);
112-
addParameter(p, 'ext', obj.default_value);
113-
addParameter(p, 'name_spec', obj.default_value);
109+
addParameter(args, 'prefix', obj.default_value);
110+
addParameter(args, 'suffix', obj.default_value);
111+
addParameter(args, 'entities', obj.default_value);
112+
addParameter(args, 'ext', obj.default_value);
113+
addParameter(args, 'name_spec', obj.default_value);
114114

115-
parse(p, varargin{:});
115+
parse(args, varargin{:});
116116

117-
prefix = p.Results.prefix;
117+
prefix = args.Results.prefix;
118118
if ~iscell(prefix)
119119
prefix = {prefix};
120120
end
121121

122122
obj.mapping(end + 1, 1).prefix = prefix;
123-
obj.mapping(end, 1).suffix = p.Results.suffix;
124-
obj.mapping(end, 1).entities = p.Results.entities;
125-
obj.mapping(end, 1).ext = p.Results.ext;
126-
obj.mapping(end, 1).name_spec = p.Results.name_spec;
123+
obj.mapping(end, 1).suffix = args.Results.suffix;
124+
obj.mapping(end, 1).entities = args.Results.entities;
125+
obj.mapping(end, 1).ext = args.Results.ext;
126+
obj.mapping(end, 1).name_spec = args.Results.name_spec;
127127

128128
end
129129

@@ -351,15 +351,15 @@ function print_mapping(obj, filename)
351351
% idx = obj.find_mapping('prefix', str)
352352
%
353353

354-
p = inputParser;
354+
args = inputParser;
355355

356-
addParameter(p, 'prefix', @ischar);
356+
addParameter(args, 'prefix', @ischar);
357357

358-
parse(p, varargin{:});
358+
parse(args, varargin{:});
359359

360360
available_mapped_prefixes = {obj.mapping.prefix}';
361361

362-
idx = strcmp(p.Results.prefix, available_mapped_prefixes);
362+
idx = strcmp(args.Results.prefix, available_mapped_prefixes);
363363

364364
end
365365

src/spm_2_bids.m

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
function [new_filename, pth, json] = spm_2_bids(file, map, verbose)
1+
function [new_filename, pth, json] = spm_2_bids(varargin)
22
%
33
% Provides a bids derivatives name for a file preprocessed with SPM
44
%
55
% USAGE::
66
%
7-
% [new_filename, pth, json] = spm_2_bids(file)
8-
% [new_filename, pth, json] = spm_2_bids(file, map)
7+
% [new_filename, pth, json] = spm_2_bids(file [, map][, verbose])
98
%
109
% :param file: SPM preprocessed filename (can be fullpath);
1110
% for example ``wmsub-01_ses-01_T1w.nii``
1211
% :type file: string
12+
%
1313
% :param map: optional spm_2_bids map to overwrite the default
1414
% map (see Mapping)
1515
% :param map: Mapping object
1616
%
17+
% :param verbose:
18+
% :param verbose: boolean
19+
%
1720
% :returns: - :new_filename: (string) BIDS compatible filename
1821
% for example ``sub-01_ses-01_space-IXI549Space_desc-preproc_T1w.nii``;
1922
% - :pth: (string) relative BIDS path
@@ -26,15 +29,23 @@
2629
%
2730
% (C) Copyright 2021 spm_2_bids developers
2831

29-
if nargin < 2 || isempty(map)
32+
args = inputParser;
33+
34+
addRequired(args, 'file', @ischar);
35+
addOptional(args, 'map', []);
36+
addOptional(args, 'verbose', true, @islogical);
37+
38+
parse(args, varargin{:});
39+
40+
file = args.Results.file;
41+
map = args.Results.map;
42+
verbose = args.Results.verbose;
43+
44+
if isempty(map)
3045
map = Mapping();
3146
map = map.default();
3247
end
3348

34-
if nargin < 3
35-
verbose = true;
36-
end
37-
3849
mapping = map.mapping;
3950
cfg = map.cfg;
4051

@@ -82,22 +93,7 @@
8293
strcmp({mapping.ext}', '*')], 2);
8394
end
8495

85-
% we compare the entities-label pairs present in the file
86-
% to those required in the mapping (if any)
87-
% if no entity requirement anywhere in the mapping then anything goes
88-
entitiy_match = true(size(mapping));
89-
90-
needs_entity_check = ~cellfun('isempty', {mapping.entities}');
91-
if any(needs_entity_check)
92-
93-
entitiy_match = false(size(mapping));
94-
95-
idx = find(needs_entity_check);
96-
for i = 1:numel(idx)
97-
status = check_field_content(bf.entities, mapping(idx(i)).entities);
98-
entitiy_match(idx(i)) = status;
99-
end
100-
end
96+
entitiy_match = get_entity_match(mapping);
10197

10298
this_mapping = [prefix_match, suffix_match, entitiy_match, ext_match];
10399

@@ -150,6 +146,25 @@
150146

151147
end
152148

149+
function entitiy_match = get_entity_match(mapping)
150+
% we compare the entities-label pairs present in the file
151+
% to those required in the mapping (if any)
152+
% if no entity requirement anywhere in the mapping then anything goes
153+
entitiy_match = true(size(mapping));
154+
155+
needs_entity_check = ~cellfun('isempty', {mapping.entities}');
156+
if any(needs_entity_check)
157+
158+
entitiy_match = false(size(mapping));
159+
160+
idx = find(needs_entity_check);
161+
for i = 1:numel(idx)
162+
status = check_field_content(bf.entities, mapping(idx(i)).entities);
163+
entitiy_match(idx(i)) = status;
164+
end
165+
end
166+
end
167+
153168
function json = set_metadata(file, map, verbose, bf)
154169

155170
json = bids.derivatives_json(bf.filename);

0 commit comments

Comments
 (0)