Skip to content

Commit 3683fa2

Browse files
committed
refactor identify sources
1 parent 37b4b46 commit 3683fa2

File tree

4 files changed

+90
-67
lines changed

4 files changed

+90
-67
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 20
20+
metric "cyc": limit 16
2121
metric "parameters": limit 6

src/utils/identify_rawsources.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
% 'wmeanu'
2828
% };
2929

30+
% TODO adapt in case prefixes have been changed from SPM defaults
31+
3032
rawsource = '';
3133

3234
if nargin < 1 || isempty(derivatives)

src/utils/identify_sources.m

Lines changed: 83 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@
2525
% 'wra'
2626
% those will throw warnings
2727

28+
% TODO adapt in case prefixes have been changed from SPM defaults
29+
2830
% TODO
2931
% functional to anatomical coregistration
3032
% anatomical to functional coregistration
3133

3234
default_map = Mapping();
3335
default_map = default_map.default();
3436

35-
sources = '';
37+
sources = {};
3638

3739
prefix_based = true;
3840

39-
add_deformation_field = false;
40-
deformation_field = 'TODO: add deformation field';
41-
4241
args = inputParser;
4342

4443
addOptional(args, 'derivatives', pwd, @ischar);
@@ -72,8 +71,9 @@
7271

7372
bf = bids.File(derivatives, 'verbose', verbose, 'use_schema', false);
7473

74+
% unknown suffix
7575
if ~ismember(bf.suffix, fieldnames(map.cfg.schema.content.objects.suffixes))
76-
sources{1} = 'TODO';
76+
sources{1, 1} = 'TODO';
7777
return
7878
end
7979

@@ -92,88 +92,109 @@
9292
return
9393
end
9494

95+
sources = add_deformation_field(bf, sources, map, verbose);
96+
9597
% anything prefix based
9698
if prefix_based
9799

98-
if length(bf.prefix) < 2
100+
[status, bf] = update_prefix(bf);
101+
102+
if status == 0
103+
return
99104

100-
% TODO: files that have been realigned but not resliced have no
101-
% "prefix" so we may miss some transformation
105+
elseif status == 1
106+
sources = 'TODO';
102107
return
103108

104109
end
105110

106-
% remove the prefix of the last step
107-
if startsWith(bf.prefix, 's')
111+
end
108112

109-
% in case the prefix includes a number to denotate the FXHM used
110-
% for smoothing
111-
starts_with_fwhm = regexp(bf.prefix, '^s[0-9]*', 'match');
112-
if ~isempty(starts_with_fwhm)
113-
bf = shorten_prefix(bf, length(starts_with_fwhm{1}));
114-
else
115-
bf = shorten_prefix(bf, 1);
116-
end
113+
% call spm_2_bids what is the filename from the previous step
114+
new_filename = spm_2_bids(bf.filename, map, verbose);
117115

118-
elseif startsWith(bf.prefix, 'u')
119-
bf = shorten_prefix(bf, 1);
116+
sources{end + 1, 1} = fullfile(bf.bids_path, new_filename);
120117

121-
elseif startsWith(bf.prefix, 'w')
122-
bf = shorten_prefix(bf, 1);
123-
add_deformation_field = true;
124-
125-
elseif startsWith(bf.prefix, 'rp_a')
126-
bf = shorten_prefix(bf, 3);
127-
128-
elseif startsWith(bf.prefix, 'mean')
129-
% TODO mean may involve several files from the source (across runs
130-
% and sessions
131-
% prefixes = {
132-
% 'mean'
133-
% 'meanu'
134-
% 'meanua'
135-
% };
136-
sources = 'TODO';
137-
return
118+
end
138119

139-
elseif ismember(bf.prefix(1:2), {'c1', 'c2', 'c3', 'c4', 'c5'})
140-
% bias corrected image
141-
sources = 'TODO';
142-
return
120+
function sources = add_deformation_field(bf, sources, map, verbose)
143121

144-
else
145-
% no idea
146-
sources = 'TODO';
147-
return
122+
if ~startsWith(bf.prefix, 'w')
123+
return
124+
end
148125

149-
end
126+
% for anatomical data we assume that
127+
% the deformation field comes from the anatomical file itself
128+
if (~isempty(bf.modality) && ismember(bf.modality, {'anat'})) || ...
129+
(~isempty(bf.suffix) && ~isempty(map.cfg.schema.find_suffix_group('anat', bf.suffix)))
130+
131+
bf.prefix = 'y_';
132+
bf = bf.update;
133+
new_filename = spm_2_bids(bf.filename, map, verbose);
134+
deformation_field = fullfile(bf.bids_path, new_filename);
135+
136+
% otherwise we can't guess it just from the file name
137+
else
138+
deformation_field = 'TODO: add deformation field';
150139

151140
end
152141

153-
% call spm_2_bids what is the filename from the previous step
154-
new_filename = spm_2_bids(bf.filename, map, verbose);
142+
sources{end + 1, 1} = deformation_field;
155143

156-
sources{1, 1} = fullfile(bf.bids_path, new_filename);
144+
end
157145

158-
% for normalized images
159-
if add_deformation_field
146+
function [status, bf] = update_prefix(bf)
160147

161-
% for anatomical data we assume that
162-
% the deformation field comes from the anatomical file itself
163-
if (~isempty(bf.modality) && ismember(bf.modality, {'anat'})) || ...
164-
(~isempty(bf.suffix) && ~isempty(map.cfg.schema.find_suffix_group('anat', bf.suffix)))
148+
status = 2;
165149

166-
bf.prefix = 'y_';
167-
bf = bf.update;
168-
new_filename = spm_2_bids(bf.filename, map, verbose);
169-
deformation_field = fullfile(bf.bids_path, new_filename);
150+
if length(bf.prefix) < 2
151+
% TODO: files that have been realigned but not resliced have no
152+
% "prefix" so we may miss some transformation
153+
status = 0;
154+
return
155+
end
170156

171-
% otherwise we can't guess it just from the file name
172-
else
157+
% remove the prefix of the last step
158+
if startsWith(bf.prefix, 's')
173159

160+
% in case the prefix includes a number to denotate the FXHM used
161+
% for smoothing
162+
starts_with_fwhm = regexp(bf.prefix, '^s[0-9]*', 'match');
163+
if ~isempty(starts_with_fwhm)
164+
bf = shorten_prefix(bf, length(starts_with_fwhm{1}));
165+
else
166+
bf = shorten_prefix(bf, 1);
174167
end
175168

176-
sources{2, 1} = deformation_field;
169+
elseif startsWith(bf.prefix, 'u')
170+
bf = shorten_prefix(bf, 1);
171+
172+
elseif startsWith(bf.prefix, 'w')
173+
bf = shorten_prefix(bf, 1);
174+
175+
elseif startsWith(bf.prefix, 'rp_a')
176+
bf = shorten_prefix(bf, 3);
177+
178+
elseif startsWith(bf.prefix, 'mean')
179+
% TODO mean may involve several files from the source (across runs
180+
% and sessions
181+
% prefixes = {
182+
% 'mean'
183+
% 'meanu'
184+
% 'meanua'
185+
% };
186+
status = 1;
187+
return
188+
189+
elseif ismember(bf.prefix(1:2), {'c1', 'c2', 'c3', 'c4', 'c5'})
190+
% bias corrected image
191+
status = 1;
192+
return
193+
194+
else
195+
% no idea
196+
status = 1;
197+
return
177198

178199
end
179200

tests/test_identify_sources.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function test_identify_sources_surface()
3838

3939
sources = identify_sources([prefix_output{i, 1} anat_file], map, false);
4040

41-
assertEqual(sources{1}, prefix_output{i, 2});
41+
assertEqual(sources{end}, prefix_output{i, 2});
4242

4343
end
4444

@@ -61,7 +61,7 @@ function test_identify_sources_anat()
6161

6262
sources = identify_sources([prefix_output{i, 1} anat_file], map, false);
6363

64-
assertEqual(sources{1}, prefix_output{i, 2});
64+
assertEqual(sources{end}, prefix_output{i, 2});
6565

6666
end
6767

@@ -91,7 +91,7 @@ function test_identify_sources_func()
9191

9292
sources = identify_sources([prefix_output{i, 1} func_file], map, false);
9393

94-
assertEqual(sources{1}, fullfile('sub-01', prefix_output{i, 2}));
94+
assertEqual(sources{end}, fullfile('sub-01', prefix_output{i, 2}));
9595

9696
end
9797

@@ -113,7 +113,7 @@ function test_identify_sources_mean()
113113

114114
sources = identify_sources([prefix_output{i, 1} func_file], map, false);
115115

116-
assertEqual(sources{1}, fullfile('sub-01', prefix_output{i, 2}));
116+
assertEqual(sources{end}, fullfile('sub-01', prefix_output{i, 2}));
117117

118118
end
119119

0 commit comments

Comments
 (0)