Skip to content

Commit ebe7b57

Browse files
authored
Merge pull request #25 from cpp-lln-lab/fix_metadata
[FIX] various bug fixes in metadata creation
2 parents 7c7e6b1 + c4e06e0 commit ebe7b57

File tree

7 files changed

+101
-26
lines changed

7 files changed

+101
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ lib/JSONio
1212
coverage_html
1313
coverage.xml
1414
test_report.log
15+
mapping.md
1516

1617
## MATLAB / OCTAVE gitignore template
1718

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

src/spm_2_bids.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,20 @@
148148

149149
json = bids.derivatives_json(bf.filename);
150150

151+
if strcmp(bf.suffix, 'probseg')
152+
json.content.Manual = false;
153+
end
154+
151155
content = json.content;
156+
152157
content.RawSources = identify_rawsources(file, verbose);
153-
content.Sources = identify_sources(file, map, verbose);
158+
159+
Sources = identify_sources(file, map, verbose);
160+
if ~isempty(Sources)
161+
content.Sources = Sources;
162+
elseif isfield(content, 'Sources')
163+
content = rmfield(content, 'Sources');
164+
end
154165

155166
if isfield(bf.entities, 'space') && strcmp(bf.entities.space, 'IXI549Space')
156167
content.SpatialReference = struct('IXI549Space', ...

src/utils/identify_rawsources.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
derivatives = strrep(derivatives, '_uw.mat', '.nii');
4040
end
4141

42-
bf = bids.File(derivatives, 'verbose', verbose);
42+
bf = bids.File(derivatives, 'verbose', verbose, 'use_schema', false);
4343

4444
bf.prefix = '';
4545

src/utils/identify_sources.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,23 @@
102102
else
103103
% remove the prefix of the last step
104104

105-
if startsWith(bf.prefix, 's') || startsWith(bf.prefix, 'u')
105+
if startsWith(bf.prefix, 's')
106+
107+
% in case the prefix includes a number to denotate the FXHM used
108+
% for smoothing
109+
starts_with_fwhm = regexp(bf.prefix, '^s[0-9]*', 'match');
110+
if ~isempty(starts_with_fwhm)
111+
bf.prefix = bf.prefix(length(starts_with_fwhm{1}) + 1:end);
112+
else
113+
bf.prefix = bf.prefix(2:end);
114+
end
115+
116+
elseif ismember(bf.prefix(1:2), {'c1', 'c2', 'c3', 'c4', 'c5'})
117+
% TODO bias corrected image
118+
sources = 'TODO';
119+
return
120+
121+
elseif startsWith(bf.prefix, 'u')
106122
bf.prefix = bf.prefix(2:end);
107123

108124
elseif startsWith(bf.prefix, 'w')
@@ -120,10 +136,12 @@
120136
% 'meanu'
121137
% 'meanua'
122138
% };
139+
sources = 'TODO';
123140
return
124141

125142
else
126143
% no idea
144+
sources = 'TODO';
127145
return
128146

129147
end
@@ -136,6 +154,7 @@
136154

137155
sources{1, 1} = fullfile(bf.bids_path, new_filename);
138156

157+
% for normalized images
139158
if add_deformation_field
140159

141160
% for anatomical data we assume that

tests/test_identify_sources.m

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@
88
initTestSuite;
99
end
1010

11+
function test_identify_sources_anat()
12+
13+
anat_file = 'sub-01_T1w.nii';
14+
15+
prefix_output = {'wm', 'sub-01/sub-01_space-individual_desc-biascor_T1w.nii'
16+
'wc1', 'sub-01/sub-01_space-individual_label-GM_probseg.nii'
17+
'wc2', 'sub-01/sub-01_space-individual_label-WM_probseg.nii'
18+
'wc3', 'sub-01/sub-01_space-individual_label-CSF_probseg.nii'
19+
'wc1', 'sub-01/sub-01_space-individual_label-GM_probseg.nii'
20+
};
21+
22+
map = default_mapping();
23+
24+
for i = 1:size(prefix_output, 1)
25+
26+
sources = identify_sources([prefix_output{i, 1} anat_file], map, false);
27+
28+
assertEqual(sources{1}, prefix_output{i, 2});
29+
30+
end
31+
32+
end
33+
1134
function test_identify_sources_func()
1235

1336
func_file = 'sub-01_task-auditory_bold.nii';
@@ -60,28 +83,6 @@ function test_identify_sources_mean()
6083

6184
end
6285

63-
function test_identify_sources_anat()
64-
65-
anat_file = 'sub-01_T1w.nii';
66-
67-
prefix_output = {'wm', 'sub-01/sub-01_space-individual_desc-biascor_T1w.nii'
68-
'wc1', 'sub-01/sub-01_space-individual_label-GM_probseg.nii'
69-
'wc2', 'sub-01/sub-01_space-individual_label-WM_probseg.nii'
70-
'wc3', 'sub-01/sub-01_space-individual_label-CSF_probseg.nii'
71-
};
72-
73-
map = default_mapping();
74-
75-
for i = 1:size(prefix_output, 1)
76-
77-
sources = identify_sources([prefix_output{i, 1} anat_file], map, false);
78-
79-
assertEqual(sources{1}, prefix_output{i, 2});
80-
81-
end
82-
83-
end
84-
8586
function test_identify_sources_suffix()
8687

8788
input_output = {'msub-01_T1w_seg8.mat', ...

tests/test_spm_2_bids_metadata.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,49 @@
88
initTestSuite;
99
end
1010

11+
function test_spm_2_bids_metadata_probseg()
12+
13+
file = 'c1sub-01_T1w.nii';
14+
15+
[~, ~, json] = spm_2_bids(file, [], false);
16+
17+
assertEqual(fieldnames(json), {'filename'; 'content'});
18+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii');
19+
assertEqual(json.content.Manual, false);
20+
21+
bids.util.jsonencode(json.filename, json.content);
22+
23+
end
24+
25+
function test_spm_2_bids_metadata_smoothed_data()
26+
27+
file = 's6wusub-01_task-auditory_bold.nii';
28+
29+
[~, ~, json] = spm_2_bids(file, [], false);
30+
31+
assertEqual(fieldnames(json), {'filename'; 'content'});
32+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_task-auditory_bold.nii');
33+
assertEqual(json.content.Sources{1}, ...
34+
'sub-01/sub-01_task-auditory_space-IXI549Space_desc-preproc_bold.nii');
35+
36+
bids.util.jsonencode(json.filename, json.content);
37+
38+
end
39+
40+
function test_spm_2_bids_metadata_source_must_be_empty()
41+
42+
file = 'msub-01_T1w.nii';
43+
44+
[~, ~, json] = spm_2_bids(file, [], false);
45+
46+
assertEqual(fieldnames(json), {'filename'; 'content'});
47+
assertEqual(fieldnames(json.content), {'Description'; 'RawSources'; 'SpatialReference'});
48+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii');
49+
50+
bids.util.jsonencode(json.filename, json.content);
51+
52+
end
53+
1154
function test_spm_2_bids_metadata_anat()
1255

1356
file = 'wmsub-01_T1w.nii';

0 commit comments

Comments
 (0)