Skip to content

Commit f740246

Browse files
committed
add function to identifyu source data
1 parent be3fdc2 commit f740246

File tree

4 files changed

+181
-2
lines changed

4 files changed

+181
-2
lines changed

src/spm_2_bids.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
spec = [];
5858

59-
% TODO se if some of the bids-query machinery cannot be kept for identifying
59+
% TODO see if some of the bids-query machinery cannot be kept for identifying
6060
% the right mapping
6161

6262
% look for the right prefix in the mapping
@@ -136,7 +136,7 @@
136136
new_filename = bf.filename;
137137

138138
json = bids.derivatives_json(bf.filename);
139-
json.content.RawSources{1} = strrep(bf.filename, bf.prefix, '');
139+
json.content.RawSources{1} = identify_rawsources(file, false);
140140

141141
end
142142

src/utils/identify_sources.m

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
function sources = identify_sources(derivatives)
2+
%
3+
% finds the most likely files in the detrivatives that was used to create
4+
% this file
5+
%
6+
% USAGE::
7+
%
8+
% sources = identify_sources(derivatives, verbose)
9+
%
10+
% :param file: SPM preprocessed filename (can be fullpath);
11+
% for example ``wmsub-01_ses-01_T1w.nii``
12+
% :type file: string
13+
%
14+
%
15+
% (C) Copyright 2021 spm_2_bids developers
16+
17+
% "r" could mean realigned or resliced...
18+
% 'sr'
19+
% 'sra'
20+
% 'wr'
21+
% 'wra'
22+
23+
% TODO mean may involve several files from the source (across runs
24+
% and sessions
25+
% prefixes = {
26+
% 'mean'
27+
% 'meanu'
28+
% 'meanua'
29+
% 'wmeanu'
30+
% };
31+
32+
sources = '';
33+
34+
prefix_based = true;
35+
36+
if nargin < 1 || isempty(derivatives)
37+
return
38+
end
39+
40+
if endsWith(derivatives, '_seg8.mat')
41+
42+
prefix_based = false;
43+
44+
derivatives = strrep(derivatives, '_seg8.mat', '.nii');
45+
46+
elseif endsWith(derivatives, '_uw.mat')
47+
48+
prefix_based = false;
49+
50+
derivatives = strrep(derivatives, '_uw.mat', '.nii');
51+
52+
end
53+
54+
bf = bids.File(derivatives);
55+
56+
if prefix_based
57+
if numel(bf.prefix) < 2
58+
% needs at least 2 characters for this file to have some provenance in the
59+
% derivatives
60+
return
61+
62+
else
63+
% remove the prefix of the last step
64+
65+
if startsWith(bf.prefix, 's') || startsWith(bf.prefix, 'w')
66+
bf.prefix = bf.prefix(2:end);
67+
68+
elseif startsWith(bf.prefix, 'rp_a')
69+
bf.prefix = bf.prefix(4:end);
70+
71+
else
72+
% no idea
73+
return
74+
75+
end
76+
77+
end
78+
end
79+
80+
% call spm_2_bids what is the filename from the previous step
81+
[new_filename] = spm_2_bids(bf.filename);
82+
83+
sources = fullfile(bf.bids_path, new_filename);
84+
85+
end

tests/test_identify_sources.m

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
% (C) Copyright 2022 spm_2_bids developers
2+
3+
function test_suite = test_identify_sources %#ok<*STOUT>
4+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
5+
test_functions = localfunctions(); %#ok<*NASGU>
6+
catch % no problem; early Matlab versions can use initTestSuite fine
7+
end
8+
initTestSuite;
9+
end
10+
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+
};
20+
21+
for i = 1:size(prefix_output, 1)
22+
23+
sources = identify_sources([prefix_output{i, 1} anat_file]);
24+
25+
assertEqual(sources, prefix_output{i, 2});
26+
27+
end
28+
29+
end
30+
31+
function test_identify_sources_func()
32+
33+
func_file = 'sub-01_task-auditory_bold.nii';
34+
35+
prefix_output = {
36+
'rp_a', 'sub-01_task-auditory_space-individual_desc-stc_bold.nii'
37+
'wua', 'sub-01_task-auditory_space-individual_desc-realignUnwarp_bold.nii'
38+
'wu', 'sub-01_task-auditory_space-individual_desc-realignUnwarp_bold.nii'
39+
'sw', 'sub-01_task-auditory_space-IXI549Space_desc-preproc_bold.nii'
40+
'swua', 'sub-01_task-auditory_space-IXI549Space_desc-preproc_bold.nii'
41+
'swu', 'sub-01_task-auditory_space-IXI549Space_desc-preproc_bold.nii'
42+
'swr', 'sub-01_task-auditory_space-IXI549Space_desc-preproc_bold.nii'
43+
'swra', 'sub-01_task-auditory_space-IXI549Space_desc-preproc_bold.nii'
44+
'sua', 'sub-01_task-auditory_space-individual_desc-realignUnwarp_bold.nii'
45+
'su', 'sub-01_task-auditory_space-individual_desc-realignUnwarp_bold.nii'
46+
};
47+
48+
for i = 1:size(prefix_output, 1)
49+
50+
sources = identify_sources([prefix_output{i, 1} func_file]);
51+
52+
assertEqual(sources, fullfile('sub-01', prefix_output{i, 2}));
53+
54+
end
55+
56+
end
57+
58+
function test_identify_sources_suffix()
59+
60+
input_output = {'msub-01_T1w_seg8.mat', ...
61+
'sub-01_space-individual_desc-biascor_T1w.nii'
62+
'asub-01_task-foo_bold_uw.mat', ...
63+
'sub-01_task-foo_space-individual_desc-stc_bold.nii'};
64+
65+
for i = 1:size(input_output, 1)
66+
67+
sources = identify_sources(input_output{i, 1});
68+
69+
assertEqual(sources, ['sub-01/' input_output{i, 2}]);
70+
71+
end
72+
73+
end

tests/test_spm_2_bids_metadata.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
% (C) Copyright 2021 spm_2_bids developers
2+
3+
function test_suite = test_spm_2_bids_metadata %#ok<*STOUT>
4+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
5+
test_functions = localfunctions(); %#ok<*NASGU>
6+
catch % no problem; early Matlab versions can use initTestSuite fine
7+
end
8+
initTestSuite;
9+
end
10+
11+
function test_spm_2_bids_metadata_basic()
12+
13+
file = 'wmsub-01_T1w.nii';
14+
15+
[~, ~, json] = spm_2_bids(file);
16+
17+
assertEqual(fieldnames(json), {'filename'; 'content'});
18+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii');
19+
assertEqual(json.content.Sources{1}, {'sub-01_desc-biasCorected_T1w.nii'});
20+
21+
end

0 commit comments

Comments
 (0)