|
1 | | -function [new_filename, pth, json] = spm_2_bids(file, map, verbose) |
| 1 | +function [new_filename, pth, json] = spm_2_bids(varargin) |
2 | 2 | % |
3 | 3 | % Provides a bids derivatives name for a file preprocessed with SPM |
4 | 4 | % |
5 | 5 | % USAGE:: |
6 | 6 | % |
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]) |
9 | 8 | % |
10 | 9 | % :param file: SPM preprocessed filename (can be fullpath); |
11 | 10 | % for example ``wmsub-01_ses-01_T1w.nii`` |
12 | 11 | % :type file: string |
| 12 | + % |
13 | 13 | % :param map: optional spm_2_bids map to overwrite the default |
14 | 14 | % map (see Mapping) |
15 | 15 | % :param map: Mapping object |
16 | 16 | % |
| 17 | + % :param verbose: |
| 18 | + % :param verbose: boolean |
| 19 | + % |
17 | 20 | % :returns: - :new_filename: (string) BIDS compatible filename |
18 | 21 | % for example ``sub-01_ses-01_space-IXI549Space_desc-preproc_T1w.nii``; |
19 | 22 | % - :pth: (string) relative BIDS path |
|
27 | 30 |
|
28 | 31 | % (C) Copyright 2021 spm_2_bids developers |
29 | 32 |
|
30 | | - if nargin < 2 || isempty(map) |
| 33 | + args = inputParser; |
| 34 | + |
| 35 | + addRequired(args, 'file', @ischar); |
| 36 | + addOptional(args, 'map', []); |
| 37 | + addOptional(args, 'verbose', true, @islogical); |
| 38 | + |
| 39 | + parse(args, varargin{:}); |
| 40 | + |
| 41 | + file = args.Results.file; |
| 42 | + map = args.Results.map; |
| 43 | + verbose = args.Results.verbose; |
| 44 | + |
| 45 | + if isempty(map) |
31 | 46 | map = Mapping(); |
32 | 47 | map = map.default(); |
33 | 48 | end |
34 | 49 |
|
35 | | - if nargin < 3 |
36 | | - verbose = true; |
37 | | - end |
38 | | - |
39 | 50 | mapping = map.mapping; |
40 | 51 | cfg = map.cfg; |
41 | 52 |
|
|
83 | 94 | strcmp({mapping.ext}', '*')], 2); |
84 | 95 | end |
85 | 96 |
|
86 | | - % we compare the entities-label pairs present in the file |
87 | | - % to those required in the mapping (if any) |
88 | | - % if no entity requirement anywhere in the mapping then anything goes |
89 | | - entitiy_match = true(size(mapping)); |
90 | | - |
91 | | - needs_entity_check = ~cellfun('isempty', {mapping.entities}'); |
92 | | - if any(needs_entity_check) |
93 | | - |
94 | | - entitiy_match = false(size(mapping)); |
95 | | - |
96 | | - idx = find(needs_entity_check); |
97 | | - for i = 1:numel(idx) |
98 | | - status = check_field_content(bf.entities, mapping(idx(i)).entities); |
99 | | - entitiy_match(idx(i)) = status; |
100 | | - end |
101 | | - end |
| 97 | + entitiy_match = get_entity_match(mapping, bf); |
102 | 98 |
|
103 | 99 | this_mapping = [prefix_match, suffix_match, entitiy_match, ext_match]; |
104 | 100 |
|
|
117 | 113 | end |
118 | 114 |
|
119 | 115 | if isempty(spec) && verbose |
120 | | - % TODO this warning should probably go in the find_mapping methods |
121 | | - msg = sprintf('Unknown prefix: %s', bf.prefix); |
122 | | - warning('spm_2_bids:unknownPrefix', msg); %#ok<SPWRN> |
| 116 | + if isnan(str2double(bf.prefix(1))) && ... |
| 117 | + ~bids.internal.starts_with(bf.prefix, 'c4') && ... % ALI toolbox prefixes |
| 118 | + ~bids.internal.starts_with(bf.prefix, 'td_') % spmup toolbox truncated prefixes |
| 119 | + |
| 120 | + % TODO this warning should probably go in the find_mapping methods |
| 121 | + msg = sprintf('Unknown prefix: %s', bf.prefix); |
| 122 | + warning('spm_2_bids:unknownPrefix', msg); %#ok<SPWRN> |
| 123 | + |
| 124 | + end |
123 | 125 | return |
124 | 126 | end |
125 | 127 |
|
|
145 | 147 |
|
146 | 148 | end |
147 | 149 |
|
| 150 | +function entitiy_match = get_entity_match(mapping, bf) |
| 151 | + % we compare the entities-label pairs present in the file |
| 152 | + % to those required in the mapping (if any) |
| 153 | + % if no entity requirement anywhere in the mapping then anything goes |
| 154 | + entitiy_match = true(size(mapping)); |
| 155 | + |
| 156 | + needs_entity_check = ~cellfun('isempty', {mapping.entities}'); |
| 157 | + if any(needs_entity_check) |
| 158 | + |
| 159 | + entitiy_match = false(size(mapping)); |
| 160 | + |
| 161 | + idx = find(needs_entity_check); |
| 162 | + for i = 1:numel(idx) |
| 163 | + status = check_field_content(bf.entities, mapping(idx(i)).entities); |
| 164 | + entitiy_match(idx(i)) = status; |
| 165 | + end |
| 166 | + end |
| 167 | +end |
| 168 | + |
148 | 169 | function json = set_metadata(file, map, verbose, bf) |
149 | 170 |
|
150 | 171 | json = bids.derivatives_json(bf.filename); |
|
0 commit comments