Skip to content

Commit 8f328c9

Browse files
committed
remove derivative entities for RawSources and assume they were gunzipped
1 parent d86818d commit 8f328c9

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

src/spm_2_bids.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154

155155
content = json.content;
156156

157-
content.RawSources = identify_rawsources(file, verbose);
157+
content.RawSources = identify_rawsources(file, map, verbose);
158158

159159
Sources = identify_sources(file, map, verbose);
160160
if ~isempty(Sources)

src/utils/identify_rawsources.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function rawsource = identify_rawsources(derivatives, verbose)
1+
function rawsource = identify_rawsources(derivatives, map, verbose)
22
%
33
% find the most likely files in the raw dataset
44
% that was used to create this derivatives
@@ -41,9 +41,21 @@
4141
derivatives = strrep(derivatives, '_uw.mat', '.nii');
4242
end
4343

44+
% - remove prefix
45+
% - remove eventual derivatives entities
46+
% - use only .nii.gz
4447
bf = bids.File(derivatives, 'verbose', verbose, 'use_schema', false);
4548

4649
bf.prefix = '';
50+
51+
entities = fieldnames(bf.entities);
52+
idx = find(ismember(entities, map.cfg.entity_order));
53+
for i = 1:numel(idx)
54+
bf.entities.(entities{idx(i)}) = '';
55+
end
56+
if strcmp(bf.extension, '.nii')
57+
bf.extension = '.nii.gz';
58+
end
4759

4860
rawsource{1} = fullfile(bf.bids_path, bf.filename);
4961

tests/test_identify_rawsources.m

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,36 @@
88
initTestSuite;
99
end
1010

11+
function test_identify_rawsources_when_der_entities()
12+
13+
input_output = {'sub-01_desc-skullstripped_T1w.nii', 'sub-01_T1w.nii.gz'};
14+
15+
verbose = false;
16+
17+
map = default_mapping();
18+
19+
for i = 1:size(input_output, 1)
20+
21+
rawsource = identify_rawsources(input_output{i, 1}, map, verbose);
22+
23+
assertEqual(rawsource, {['sub-01/' input_output{i, 2}]});
24+
25+
end
26+
27+
end
28+
1129
function test_identify_rawsources_suffix()
1230

13-
input_output = {'sub-01_T1w_seg8.mat', 'sub-01_T1w.nii'
14-
'sub-01_task-foo_bold_uw.mat', 'sub-01_task-foo_bold.nii'};
31+
input_output = {'sub-01_T1w_seg8.mat', 'sub-01_T1w.nii.gz'
32+
'sub-01_task-foo_bold_uw.mat', 'sub-01_task-foo_bold.nii.gz'};
1533

1634
verbose = false;
35+
36+
map = default_mapping();
1737

1838
for i = 1:size(input_output, 1)
1939

20-
rawsource = identify_rawsources(input_output{i, 1}, verbose);
40+
rawsource = identify_rawsources(input_output{i, 1}, map, verbose);
2141

2242
assertEqual(rawsource, {['sub-01/' input_output{i, 2}]});
2343

@@ -43,14 +63,16 @@ function test_identify_rawsources_anat()
4363
};
4464

4565
verbose = false;
66+
67+
map = default_mapping();
4668

4769
for i = 1:numel(prefixes)
4870

4971
file = [prefixes{i} anat_file];
5072

51-
rawsource = identify_rawsources(fullfile(pwd, 'sub-01', file), verbose);
73+
rawsource = identify_rawsources(fullfile(pwd, 'sub-01', file), map, verbose);
5274

53-
assertEqual(rawsource, {'sub-01/sub-01_T1w.nii'});
75+
assertEqual(rawsource, {'sub-01/sub-01_T1w.nii.gz'});
5476

5577
end
5678

@@ -82,14 +104,16 @@ function test_identify_rawsources_func()
82104
};
83105

84106
verbose = false;
107+
108+
map = default_mapping();
85109

86110
for i = 1:numel(prefixes)
87111

88112
file = [prefixes{i} func_file];
89113

90-
rawsource = identify_rawsources(file, verbose);
114+
rawsource = identify_rawsources(file, map, verbose);
91115

92-
assertEqual(rawsource, {'sub-01/ses-02/sub-01_ses-02_task-foo_bold.nii'});
116+
assertEqual(rawsource, {'sub-01/ses-02/sub-01_ses-02_task-foo_bold.nii.gz'});
93117

94118
end
95119

tests/test_spm_2_bids_metadata.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function test_spm_2_bids_metadata_probseg()
1515
[~, ~, json] = spm_2_bids(file, [], false);
1616

1717
assertEqual(fieldnames(json), {'filename'; 'content'});
18-
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii');
18+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii.gz');
1919
assertEqual(json.content.Manual, false);
2020

2121
bids.util.jsonencode(json.filename, json.content);
@@ -29,7 +29,7 @@ function test_spm_2_bids_metadata_smoothed_data()
2929
[~, ~, json] = spm_2_bids(file, [], false);
3030

3131
assertEqual(fieldnames(json), {'filename'; 'content'});
32-
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_task-auditory_bold.nii');
32+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_task-auditory_bold.nii.gz');
3333
assertEqual(json.content.Sources{1}, ...
3434
'sub-01/sub-01_task-auditory_space-IXI549Space_desc-preproc_bold.nii');
3535

@@ -45,7 +45,7 @@ function test_spm_2_bids_metadata_source_must_be_empty()
4545

4646
assertEqual(fieldnames(json), {'filename'; 'content'});
4747
assertEqual(fieldnames(json.content), {'Description'; 'RawSources'; 'SpatialReference'});
48-
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii');
48+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii.gz');
4949

5050
bids.util.jsonencode(json.filename, json.content);
5151

@@ -58,7 +58,7 @@ function test_spm_2_bids_metadata_anat()
5858
[~, ~, json] = spm_2_bids(file, [], false);
5959

6060
assertEqual(fieldnames(json), {'filename'; 'content'});
61-
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii');
61+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_T1w.nii.gz');
6262
assertEqual(json.content.Sources{1}, ...
6363
'sub-01/sub-01_space-individual_desc-biascor_T1w.nii');
6464
assertEqual(json.content.Sources{2}, ...
@@ -75,7 +75,7 @@ function test_spm_2_bids_metadata_func()
7575
[~, ~, json] = spm_2_bids(file, [], false);
7676

7777
assertEqual(fieldnames(json), {'filename'; 'content'});
78-
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_task-foo_bold.nii');
78+
assertEqual(json.content.RawSources{1}, 'sub-01/sub-01_task-foo_bold.nii.gz');
7979
assertEqual(json.content.Sources{1}, ...
8080
'sub-01/sub-01_task-foo_space-individual_desc-realignUnwarp_bold.nii');
8181
assertEqual(json.content.Sources{2}, 'TODO: add deformation field');

0 commit comments

Comments
 (0)