Skip to content

Commit 02429c1

Browse files
committed
refactor default mapping
1 parent 77c7017 commit 02429c1

File tree

4 files changed

+105
-23
lines changed

4 files changed

+105
-23
lines changed

src/defaults/Mapping.m

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,55 @@
159159

160160
end
161161

162+
function print_mapping(obj)
163+
164+
obj = flatten_mapping(obj);
165+
166+
fprintf(1, '\n');
167+
168+
for i = 1:size(obj.mapping, 1)
169+
170+
input = obj.mapping(i);
171+
172+
if isfield(input, 'suffix') && isempty(input.suffix) || ...
173+
~isfield(input, 'suffix')
174+
input.suffix = '*';
175+
end
176+
177+
if isfield(input, 'ext') && ~isempty(input.ext)
178+
input.extension = '.*';
179+
end
180+
if ~isfield(input, 'extension') || isempty(input.extension)
181+
input.extension = '.*';
182+
end
183+
184+
input = bids.File(input);
185+
186+
output = obj.mapping(i).name_spec;
187+
188+
if isfield(output, 'suffix') && isempty(output.suffix) || ...
189+
~isfield(output, 'suffix')
190+
output.suffix = '*';
191+
end
192+
193+
if isfield(output, 'ext') && ~isempty(output.ext)
194+
output.extension = '.*';
195+
end
196+
if ~isfield(output, 'extension') || isempty(output.extension)
197+
output.extension = '.*';
198+
end
199+
200+
output = bids.File(output);
201+
output.re;
202+
203+
fprintf(1, [input.filename ' --> ' output.filename '\n']);
204+
205+
end
206+
207+
fprintf(1, '\n');
208+
209+
end
210+
162211
function obj = default(obj)
163212
%
164213
% Load into the the mapping objects the default map for SPM --> bids derivatives
@@ -168,8 +217,7 @@
168217
% map = map.default;
169218
%
170219

171-
spec = {
172-
{ obj.bias_cor }, obj.cfg.segment.bias_corrected; ...
220+
spec = {{ obj.bias_cor }, obj.cfg.segment.bias_corrected; ...
173221
{ 'c1' }, obj.cfg.segment.gm; ...
174222
{ 'c2' }, obj.cfg.segment.wm; ...
175223
{ 'c3' }, obj.cfg.segment.csf; ...
@@ -184,17 +232,18 @@
184232
{ 'rp_', ...
185233
['rp_', obj.stc], ...
186234
['rp_', obj.stc, obj.unwarp] }, obj.cfg.real_param; ...
187-
{ 'mean', ...
188-
['mean' obj.unwarp], ...
189-
['mean' obj.unwarp, obj.stc], ...
190-
['mean' obj.stc, obj.unwarp] }, obj.cfg.mean; ...
191-
{[obj.norm, 'mean', obj.unwarp], ...
192-
[obj.norm, 'mean', obj.unwarp, obj.stc], ...
193-
[obj.norm, 'mean', obj.stc, obj.unwarp]}, obj.cfg.normalized_mean; ...
194235
{[obj.norm, 'c1'] }, obj.cfg.segment.gm_norm; ...
195236
{[obj.norm, 'c2'] }, obj.cfg.segment.wm_norm; ...
196-
{[obj.norm, 'c3'] }, obj.cfg.segment.csf_norm
197-
};
237+
{[obj.norm, 'c3'] }, obj.cfg.segment.csf_norm};
238+
239+
spec_mean = {{ 'mean', ...
240+
['mean' obj.unwarp], ...
241+
['mean' obj.unwarp, obj.stc], ...
242+
['mean' obj.stc, obj.unwarp] }, obj.cfg.mean};
243+
244+
spec_norm_mean = {{[obj.norm, 'mean', obj.unwarp], ...
245+
[obj.norm, 'mean', obj.unwarp, obj.stc], ...
246+
[obj.norm, 'mean', obj.stc, obj.unwarp]}, obj.cfg.normalized_mean};
198247

199248
spec_smooth = {{ obj.smooth, ...
200249
[obj.smooth, obj.unwarp, obj.stc], ...
@@ -218,7 +267,12 @@
218267
[obj.norm, obj.unwarp], ...
219268
[obj.norm, obj.realign] }, obj.cfg.preproc_norm};
220269

221-
spec = cat(1, spec, spec_smooth, spec_smooth_norm, spec_preproc_norm);
270+
spec = cat(1, spec, ...
271+
spec_smooth, ...
272+
spec_smooth_norm, ...
273+
spec_preproc_norm, ...
274+
spec_mean, ...
275+
spec_norm_mean);
222276

223277
for i_map = 1:size(spec, 1)
224278
obj = obj.add_mapping('prefix', spec{i_map, 1}, ...

tests/test_identify_sources.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ function test_identify_sources_func()
2626
'su', 'sub-01_task-auditory_space-individual_desc-realignUnwarp_bold.nii'
2727
};
2828

29+
map = default_mapping();
30+
2931
for i = 1:size(prefix_output, 1)
3032

31-
sources = identify_sources([prefix_output{i, 1} func_file], [], false);
33+
sources = identify_sources([prefix_output{i, 1} func_file], map, false);
3234

3335
assertEqual(sources{1}, fullfile('sub-01', prefix_output{i, 2}));
3436

@@ -46,9 +48,11 @@ function test_identify_sources_mean()
4648
'wmeanau', 'sub-01_task-auditory_space-individual_desc-mean_bold.nii'
4749
};
4850

51+
map = default_mapping();
52+
4953
for i = 1:size(prefix_output, 1)
5054

51-
sources = identify_sources([prefix_output{i, 1} func_file], [], false);
55+
sources = identify_sources([prefix_output{i, 1} func_file], map, false);
5256

5357
assertEqual(sources{1}, fullfile('sub-01', prefix_output{i, 2}));
5458

@@ -66,9 +70,11 @@ function test_identify_sources_anat()
6670
'wc3', 'sub-01/sub-01_space-individual_label-CSF_probseg.nii'
6771
};
6872

73+
map = default_mapping();
74+
6975
for i = 1:size(prefix_output, 1)
7076

71-
sources = identify_sources([prefix_output{i, 1} anat_file], [], false);
77+
sources = identify_sources([prefix_output{i, 1} anat_file], map, false);
7278

7379
assertEqual(sources{1}, prefix_output{i, 2});
7480

@@ -83,9 +89,11 @@ function test_identify_sources_suffix()
8389
'asub-01_task-foo_bold_uw.mat', ...
8490
'sub-01_task-foo_space-individual_desc-stc_bold.nii'};
8591

92+
map = default_mapping();
93+
8694
for i = 1:size(input_output, 1)
8795

88-
sources = identify_sources(input_output{i, 1}, [], false);
96+
sources = identify_sources(input_output{i, 1}, map, false);
8997

9098
assertEqual(sources{1}, ['sub-01/' input_output{i, 2}]);
9199

tests/test_spm_2_bids.m

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@
1111
function test_spm_2_bids_order_entities()
1212

1313
file = 'wmsub-01_desc-skullstripped_T1w.nii';
14-
new_filename = spm_2_bids(file, [], false);
14+
new_filename = spm_2_bids(file, default_mapping(), false);
1515
assertEqual(new_filename, 'sub-01_space-IXI549Space_desc-preproc_T1w.nii');
1616

1717
end
1818

1919
function test_spm_2_bids_suffix()
2020

21+
map = default_mapping();
22+
2123
input_output = {'sub-01_T1w_seg8.mat', 'sub-01_label-T1w_segparam.mat'
2224
'sub-01_task-foo_bold_uw.mat', 'sub-01_task-foo_label-bold_unwarpparam.mat'};
2325

2426
for i = 1:numel(size(input_output, 1))
2527

2628
print_here('%s\n', input_output{i, 1});
2729

28-
filename = spm_2_bids(input_output{i, 1}, [], false);
30+
filename = spm_2_bids(input_output{i, 1}, map, false);
2931

3032
expected = input_output{i, 2};
3133
assertEqual(filename, expected);
@@ -82,7 +84,7 @@ function test_spm_2_bids_new_mapping()
8284
function test_spm_2_bids_no_prefix()
8385

8486
file = 'sub-01_ses-02_T1w.nii';
85-
new_filename = spm_2_bids(file, [], false);
87+
new_filename = spm_2_bids(file, default_mapping(), false);
8688
assertEqual(new_filename, file);
8789

8890
end
@@ -103,7 +105,7 @@ function test_spm_2_bids_unknown_prefix()
103105
function test_spm_2_bids_json()
104106

105107
file = 'c1sub-01_ses-02_T1w.nii';
106-
[new_filename, pth, json] = spm_2_bids(file, [], false);
108+
[new_filename, pth, json] = spm_2_bids(file, default_mapping(), false);
107109

108110
end
109111

@@ -125,6 +127,8 @@ function test_spm_2_bids_defor_field()
125127
'sub-01_from-IXI549Space_to-T2w_mode-image_xfm.nii' ...
126128
};
127129

130+
map = default_mapping();
131+
128132
for i = 1:size(prefix_input_output, 1)
129133

130134
prefixes = get_prefixes(prefix_input_output, i);
@@ -135,7 +139,7 @@ function test_spm_2_bids_defor_field()
135139

136140
print_here('%s\n', file);
137141

138-
filename = spm_2_bids(file, [], false);
142+
filename = spm_2_bids(file, map, false);
139143

140144
expected = prefix_input_output{i, 3};
141145
assertEqual(filename, expected);
@@ -198,6 +202,8 @@ function test_spm_2_bids_anat()
198202
'wc3', 'sub-01_space-IXI549Space_label-CSF_probseg.nii' ...
199203
};
200204

205+
map = default_mapping();
206+
201207
for i = 1:size(prefix_and_output, 1)
202208

203209
prefixes = get_prefixes(prefix_and_output, i);
@@ -208,7 +214,7 @@ function test_spm_2_bids_anat()
208214

209215
print_here('%s\n', file);
210216

211-
filename = spm_2_bids(file, [], false);
217+
filename = spm_2_bids(file, map, false);
212218

213219
expected = prefix_and_output{i, 2};
214220
assertEqual(filename, expected);
@@ -243,6 +249,8 @@ function test_spm_2_bids_func()
243249
'sub-01_task-auditory_space-individual_desc-smth_bold.nii' ...
244250
};
245251

252+
map = default_mapping();
253+
246254
for i = 1:size(prefix_and_output, 1)
247255

248256
prefixes = get_prefixes(prefix_and_output, i);
@@ -253,7 +261,7 @@ function test_spm_2_bids_func()
253261

254262
print_here('%s\n', file);
255263

256-
filename = spm_2_bids(file, [], false);
264+
filename = spm_2_bids(file, map, false);
257265

258266
expected = prefix_and_output{i, 2};
259267
assertEqual(filename, expected);
@@ -288,3 +296,8 @@ function print_here(string, file)
288296
fprintf(1, string, file);
289297
end
290298
end
299+
300+
function map = default_mapping()
301+
map = Mapping();
302+
map = map.default();
303+
end

tests/utils/default_mapping.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function map = default_mapping()
2+
%
3+
% (C) Copyright 2021 spm_2_bids developers
4+
5+
map = Mapping();
6+
map = map.default();
7+
end

0 commit comments

Comments
 (0)