|
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 |
|
26 | 29 | % |
27 | 30 | % (C) Copyright 2021 spm_2_bids developers |
28 | 31 |
|
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) |
30 | 45 | map = Mapping(); |
31 | 46 | map = map.default(); |
32 | 47 | end |
33 | 48 |
|
34 | | - if nargin < 3 |
35 | | - verbose = true; |
36 | | - end |
37 | | - |
38 | 49 | mapping = map.mapping; |
39 | 50 | cfg = map.cfg; |
40 | 51 |
|
|
82 | 93 | strcmp({mapping.ext}', '*')], 2); |
83 | 94 | end |
84 | 95 |
|
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, bf); |
101 | 97 |
|
102 | 98 | this_mapping = [prefix_match, suffix_match, entitiy_match, ext_match]; |
103 | 99 |
|
|
116 | 112 | end |
117 | 113 |
|
118 | 114 | if isempty(spec) && verbose |
119 | | - % TODO this warning should probably go in the find_mapping methods |
120 | | - msg = sprintf('Unknown prefix: %s', bf.prefix); |
121 | | - warning('spm_2_bids:unknownPrefix', msg); %#ok<SPWRN> |
| 115 | + if isnan(str2double(bf.prefix(1))) && ... |
| 116 | + ~bids.internal.starts_with(bf.prefix, 'c4') && ... % ALI toolbox prefixes |
| 117 | + ~bids.internal.starts_with(bf.prefix, 'td_') % spmup toolbox truncated prefixes |
| 118 | + |
| 119 | + % TODO this warning should probably go in the find_mapping methods |
| 120 | + msg = sprintf('Unknown prefix: %s', bf.prefix); |
| 121 | + warning('spm_2_bids:unknownPrefix', msg); %#ok<SPWRN> |
| 122 | + |
| 123 | + end |
122 | 124 | return |
123 | 125 | end |
124 | 126 |
|
|
144 | 146 |
|
145 | 147 | end |
146 | 148 |
|
| 149 | +function entitiy_match = get_entity_match(mapping, bf) |
| 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 | + |
147 | 168 | function json = set_metadata(file, map, verbose, bf) |
148 | 169 |
|
149 | 170 | json = bids.derivatives_json(bf.filename); |
|
0 commit comments