Skip to content

Commit 91136a2

Browse files
committed
adapt to new bids.File function and refactor
1 parent 06de48b commit 91136a2

File tree

2 files changed

+68
-50
lines changed

2 files changed

+68
-50
lines changed

miss_hit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ copyright_entity: "spm_2_bids developers"
1717
# metric for code quality
1818
metric "cnest": limit 5
1919
metric "file_length": limit 500
20-
metric "cyc": limit 15
20+
metric "cyc": limit 13
2121
metric "parameters": limit 5

src/spm_2_bids.m

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,23 @@
3434
mapping = map.mapping;
3535
cfg = map.cfg;
3636

37-
use_suffix_as_label = false;
38-
3937
% deal with suffixes modified by SPM
4038
% 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)
4943
use_suffix_as_label = true;
5044
end
5145

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;
5549

56-
p = bids.internal.parse_filename(file);
50+
json = [];
5751

5852
% TO DO allow renaming even if there is no prefix ?
59-
if isempty(p.prefix)
53+
if isempty(bf.prefix)
6054
return
6155
end
6256

@@ -66,7 +60,7 @@
6660
% the right mapping
6761

6862
% 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);
7064

7165
% TODO implement methods in Mapping to filter by suffix / extention /
7266
% entities
@@ -75,12 +69,12 @@
7569
% if none is mentioned anywhere in the mapping then anything goes
7670
suffix_match = true(size(mapping));
7771
if ~all(cellfun('isempty', {mapping.suffix}'))
78-
suffix_match = any([strcmp({mapping.suffix}', p.suffix), ...
72+
suffix_match = any([strcmp({mapping.suffix}', bf.suffix), ...
7973
strcmp({mapping.suffix}', '*')], 2);
8074
end
8175
ext_match = true(size(mapping));
8276
if ~all(cellfun('isempty', {mapping.ext}'))
83-
ext_match = any([strcmp({mapping.ext}', p.ext), ...
77+
ext_match = any([strcmp({mapping.ext}', bf.extension), ...
8478
strcmp({mapping.ext}', '*')], 2);
8579
end
8680

@@ -96,7 +90,7 @@
9690

9791
idx = find(needs_entity_check);
9892
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);
10094
entitiy_match(idx(i)) = status;
10195
end
10296
end
@@ -119,66 +113,78 @@
119113

120114
if isempty(spec)
121115
% 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);
123117
warning('spm_2_bids:unknownPrefix', msg); %#ok<SPWRN>
124118
return
125119
end
126120

127121
spec = add_fwhm_to_smooth_label(spec, cfg);
128122

129-
spec = adapt_from_label_to_input(spec, p);
123+
spec = adapt_from_label_to_input(spec, bf);
130124

131125
if use_suffix_as_label
132-
spec.entities.label = p.suffix;
126+
spec.entities.label = bf.suffix;
133127
end
134128

135129
spec = use_config_spec(spec, cfg);
136130

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;
141137

142-
p = reorder_entities(p, cfg);
138+
json = bids.derivatives_json(bf.filename);
139+
json.content.RawSources{1} = strrep(bf.filename, bf.prefix, '');
143140

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)
147144

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
151158

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;
155161

156162
end
157163

158-
function spec = add_fwhm_to_smooth_label(spec, cfg)
164+
function bf = add_fwhm_to_smooth_label(bf, cfg)
159165
%
160166
% adds the FWHM to the description label for smoothing
161167
%
162168

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') && ...
166172
~isempty(cfg.fwhm)
167-
spec.entities.desc = sprintf('smth%i', cfg.fwhm);
173+
bf.entities.desc = sprintf('smth%i', cfg.fwhm);
168174
end
169175

170176
end
171177

172-
function spec = adapt_from_label_to_input(spec, p)
178+
function spec = adapt_from_label_to_input(spec, bf)
173179
%
174180
% for deformation fields
175181
%
176182

177-
if strcmp(p.prefix, 'y_')
178-
spec.entities.from = p.suffix;
183+
if strcmp(bf.prefix, 'y_')
184+
spec.entities.from = bf.suffix;
179185
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;
182188
spec.entities = orderfields(spec.entities, {'from', 'to', 'mode'});
183189
end
184190

@@ -204,14 +210,14 @@
204210

205211
end
206212

207-
function p = reorder_entities(p, cfg)
213+
function bf = reorder_entities(bf, cfg)
208214
%
209215
% put entity from raw bids before those of derivatives
210216
% and make sure that derivatives entities are in the right order
211217
%
212218
%
213219

214-
entities = fieldnames(p.entities);
220+
entities = fieldnames(bf.entities);
215221

216222
is_raw_entity = ~ismember(entities, cfg.entity_order);
217223

@@ -221,6 +227,18 @@
221227
entities(~is_raw_entity));
222228
derivative_entities = cfg.entity_order(derivative_entities_present);
223229

224-
p.entity_order = cat(1, raw_entities, derivative_entities);
230+
bf = bf.reorder_entities(cat(1, raw_entities, derivative_entities));
231+
232+
end
225233

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
226244
end

0 commit comments

Comments
 (0)