|
34 | 34 | mapping = map.mapping; |
35 | 35 | cfg = map.cfg; |
36 | 36 |
|
37 | | - use_suffix_as_label = false; |
38 | | - |
39 | 37 | % deal with suffixes modified by SPM |
40 | 38 | % turns them into prefixes that can be handled by the default mapping |
41 | | - if strfind(file, '_uw.mat') %#ok<*STRIFCND> |
42 | | - file = bids.internal.file_utils(file, 'prefix', 'unwarpparam_'); |
43 | | - file = strrep(file, '_uw.mat', '.mat'); |
44 | | - use_suffix_as_label = true; |
45 | | - end |
46 | | - if strfind(file, '_seg8.mat') %#ok<*STRIFCND> |
47 | | - file = bids.internal.file_utils(file, 'prefix', 'segparam_'); |
48 | | - file = strrep(file, '_seg8.mat', '.mat'); |
| 39 | + use_suffix_as_label = false; |
| 40 | + [file, status(1)] = turn_spm_suffix_in_prefix(file, '_uw.mat', 'unwarpparam_'); |
| 41 | + [file, status(2)] = turn_spm_suffix_in_prefix(file, '_seg8.mat', 'segparam_'); |
| 42 | + if any(status) |
49 | 43 | use_suffix_as_label = true; |
50 | 44 | end |
51 | 45 |
|
52 | | - pth = fileparts(file); |
53 | | - new_filename = bids.internal.file_utils(file, 'filename'); |
54 | | - json = []; |
| 46 | + bf = bids.File(file, 'use_schema', false); |
| 47 | + pth = bf.bids_path; |
| 48 | + new_filename = bf.filename; |
55 | 49 |
|
56 | | - p = bids.internal.parse_filename(file); |
| 50 | + json = []; |
57 | 51 |
|
58 | 52 | % TO DO allow renaming even if there is no prefix ? |
59 | | - if isempty(p.prefix) |
| 53 | + if isempty(bf.prefix) |
60 | 54 | return |
61 | 55 | end |
62 | 56 |
|
|
66 | 60 | % the right mapping |
67 | 61 |
|
68 | 62 | % look for the right prefix in the mapping |
69 | | - prefix_match = map.find_mapping('prefix', p.prefix); |
| 63 | + prefix_match = map.find_mapping('prefix', bf.prefix); |
70 | 64 |
|
71 | 65 | % TODO implement methods in Mapping to filter by suffix / extention / |
72 | 66 | % entities |
|
75 | 69 | % if none is mentioned anywhere in the mapping then anything goes |
76 | 70 | suffix_match = true(size(mapping)); |
77 | 71 | if ~all(cellfun('isempty', {mapping.suffix}')) |
78 | | - suffix_match = any([strcmp({mapping.suffix}', p.suffix), ... |
| 72 | + suffix_match = any([strcmp({mapping.suffix}', bf.suffix), ... |
79 | 73 | strcmp({mapping.suffix}', '*')], 2); |
80 | 74 | end |
81 | 75 | ext_match = true(size(mapping)); |
82 | 76 | if ~all(cellfun('isempty', {mapping.ext}')) |
83 | | - ext_match = any([strcmp({mapping.ext}', p.ext), ... |
| 77 | + ext_match = any([strcmp({mapping.ext}', bf.extension), ... |
84 | 78 | strcmp({mapping.ext}', '*')], 2); |
85 | 79 | end |
86 | 80 |
|
|
96 | 90 |
|
97 | 91 | idx = find(needs_entity_check); |
98 | 92 | for i = 1:numel(idx) |
99 | | - status = check_field_content(p.entities, mapping(idx(i)).entities); |
| 93 | + status = check_field_content(bf.entities, mapping(idx(i)).entities); |
100 | 94 | entitiy_match(idx(i)) = status; |
101 | 95 | end |
102 | 96 | end |
|
119 | 113 |
|
120 | 114 | if isempty(spec) |
121 | 115 | % TODO this warning should probably go in the find_mapping methods |
122 | | - msg = sprintf('Unknown prefix: %s', p.prefix); |
| 116 | + msg = sprintf('Unknown prefix: %s', bf.prefix); |
123 | 117 | warning('spm_2_bids:unknownPrefix', msg); %#ok<SPWRN> |
124 | 118 | return |
125 | 119 | end |
126 | 120 |
|
127 | 121 | spec = add_fwhm_to_smooth_label(spec, cfg); |
128 | 122 |
|
129 | | - spec = adapt_from_label_to_input(spec, p); |
| 123 | + spec = adapt_from_label_to_input(spec, bf); |
130 | 124 |
|
131 | 125 | if use_suffix_as_label |
132 | | - spec.entities.label = p.suffix; |
| 126 | + spec.entities.label = bf.suffix; |
133 | 127 | end |
134 | 128 |
|
135 | 129 | spec = use_config_spec(spec, cfg); |
136 | 130 |
|
137 | | - overwrite = true; |
138 | | - spec.prefix = ''; |
139 | | - use_schema = false; |
140 | | - p = set_missing_fields(p, spec, overwrite); |
| 131 | + bf = update_filename(bf, spec, cfg); |
| 132 | + |
| 133 | + % arg out |
| 134 | + pth = bf.bids_path; |
| 135 | + |
| 136 | + new_filename = bf.filename; |
141 | 137 |
|
142 | | - p = reorder_entities(p, cfg); |
| 138 | + json = bids.derivatives_json(bf.filename); |
| 139 | + json.content.RawSources{1} = strrep(bf.filename, bf.prefix, ''); |
143 | 140 |
|
144 | | - bidsFile = bids.File(file, use_schema, p); |
145 | | - bidsFile = bidsFile.reorder_entities(p.entity_order); |
146 | | - bidsFile = bidsFile.create_filename; |
| 141 | +end |
| 142 | + |
| 143 | +function bf = update_filename(bf, spec, cfg) |
147 | 144 |
|
148 | | - new_filename = bidsFile.filename; |
149 | | - pth = bidsFile.pth; |
150 | | - json = bids.derivatives_json(new_filename); |
| 145 | + bf.prefix = ''; |
| 146 | + if isfield(spec, 'suffix') |
| 147 | + bf.suffix = spec.suffix; |
| 148 | + end |
| 149 | + if isfield(spec, 'ext') |
| 150 | + bf.extension = spec.ext; |
| 151 | + end |
| 152 | + if isfield(spec, 'entities') |
| 153 | + entities = fieldnames(spec.entities); |
| 154 | + for i = 1:numel(entities) |
| 155 | + bf = bf.set_entity(entities{i}, spec.entities.(entities{i})); |
| 156 | + end |
| 157 | + end |
151 | 158 |
|
152 | | - % TODO update json content |
153 | | - p = bids.internal.parse_filename(file); |
154 | | - json.content.RawSources{1} = strrep(p.filename, p.prefix, ''); |
| 159 | + bf = reorder_entities(bf, cfg); |
| 160 | + bf = bf.update; |
155 | 161 |
|
156 | 162 | end |
157 | 163 |
|
158 | | -function spec = add_fwhm_to_smooth_label(spec, cfg) |
| 164 | +function bf = add_fwhm_to_smooth_label(bf, cfg) |
159 | 165 | % |
160 | 166 | % adds the FWHM to the description label for smoothing |
161 | 167 | % |
162 | 168 |
|
163 | | - if isfield(spec, 'entities') && ... |
164 | | - isfield(spec.entities, 'desc') && ... |
165 | | - strcmp(spec.entities.desc, 'smth') && ... |
| 169 | + if isfield(bf, 'entities') && ... |
| 170 | + isfield(bf.entities, 'desc') && ... |
| 171 | + strcmp(bf.entities.desc, 'smth') && ... |
166 | 172 | ~isempty(cfg.fwhm) |
167 | | - spec.entities.desc = sprintf('smth%i', cfg.fwhm); |
| 173 | + bf.entities.desc = sprintf('smth%i', cfg.fwhm); |
168 | 174 | end |
169 | 175 |
|
170 | 176 | end |
171 | 177 |
|
172 | | -function spec = adapt_from_label_to_input(spec, p) |
| 178 | +function spec = adapt_from_label_to_input(spec, bf) |
173 | 179 | % |
174 | 180 | % for deformation fields |
175 | 181 | % |
176 | 182 |
|
177 | | - if strcmp(p.prefix, 'y_') |
178 | | - spec.entities.from = p.suffix; |
| 183 | + if strcmp(bf.prefix, 'y_') |
| 184 | + spec.entities.from = bf.suffix; |
179 | 185 | spec.entities = orderfields(spec.entities, {'from', 'to', 'mode'}); |
180 | | - elseif strcmp(p.prefix, 'iy_') |
181 | | - spec.entities.to = p.suffix; |
| 186 | + elseif strcmp(bf.prefix, 'iy_') |
| 187 | + spec.entities.to = bf.suffix; |
182 | 188 | spec.entities = orderfields(spec.entities, {'from', 'to', 'mode'}); |
183 | 189 | end |
184 | 190 |
|
|
204 | 210 |
|
205 | 211 | end |
206 | 212 |
|
207 | | -function p = reorder_entities(p, cfg) |
| 213 | +function bf = reorder_entities(bf, cfg) |
208 | 214 | % |
209 | 215 | % put entity from raw bids before those of derivatives |
210 | 216 | % and make sure that derivatives entities are in the right order |
211 | 217 | % |
212 | 218 | % |
213 | 219 |
|
214 | | - entities = fieldnames(p.entities); |
| 220 | + entities = fieldnames(bf.entities); |
215 | 221 |
|
216 | 222 | is_raw_entity = ~ismember(entities, cfg.entity_order); |
217 | 223 |
|
|
221 | 227 | entities(~is_raw_entity)); |
222 | 228 | derivative_entities = cfg.entity_order(derivative_entities_present); |
223 | 229 |
|
224 | | - p.entity_order = cat(1, raw_entities, derivative_entities); |
| 230 | + bf = bf.reorder_entities(cat(1, raw_entities, derivative_entities)); |
| 231 | + |
| 232 | +end |
225 | 233 |
|
| 234 | +function [filename, status] = turn_spm_suffix_in_prefix(filename, pattern, string) |
| 235 | + status = false; |
| 236 | + |
| 237 | + if strfind(filename, pattern) %#ok<*STRIFCND> |
| 238 | + filename = bids.internal.file_utils(filename, 'prefix', string); |
| 239 | + filename = strrep(filename, ... |
| 240 | + pattern, ... |
| 241 | + ['.' bids.internal.file_utils(filename, 'ext')]); |
| 242 | + status = true; |
| 243 | + end |
226 | 244 | end |
0 commit comments