Skip to content

Commit 46817f1

Browse files
authored
Merge pull request #27 from cpp-lln-lab/meta
[ENH] improve Sources and RawSource metadata creation
2 parents d86818d + 594c847 commit 46817f1

File tree

10 files changed

+170
-58
lines changed

10 files changed

+170
-58
lines changed

docs/source/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
author = "Rémi Gau"
2323

2424
# The full version, including alpha/beta/rc tags
25-
release = "v0.2.0dev"
25+
with open('../../version.txt', encoding='utf-8') as version_file:
26+
release = version_file.read()
2627

2728

2829
# -- General configuration ---------------------------------------------------

run_tests.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
if isdir(fullfile(thisDir, 'lib', 'bids-matlab'))
66
addpath(fullfile(thisDir, 'lib', 'bids-matlab'));
7+
end
8+
if isdir(fullfile(thisDir, 'lib', 'JSONio'))
79
addpath(fullfile(thisDir, 'lib', 'JSONio'));
810
end
911

1012
folderToCover = fullfile(thisDir, 'src');
1113
testFolder = fullfile(thisDir, 'tests');
1214

15+
addpath(fullfile(testFolder, 'utils'));
16+
1317
success = moxunit_runtests(testFolder, ...
1418
'-verbose', '-recursive', '-with_coverage', ...
1519
'-cover', folderToCover, ...

src/Mapping.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
%
2929
% (C) Copyright 2021 spm_2_bids developers
3030

31-
% TODO add a print_mapping method to allow easy visualization
32-
% of the input --> output relationship
33-
3431
properties
3532

3633
mapping

src/defaults/check_cfg.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
% just to keep track of all the BIDS entities in the cfg
7878
bidsFile = bids.File('sub-01_T1.nii', 'use_schema', false);
7979
bidsFile = bidsFile.reorder_entities;
80+
fields_to_set.schema = bids.Schema;
8081
fields_to_set.all_entity_order = bidsFile.entity_order;
8182

8283
fields_to_set.entity_order = {'hemi'; ...

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: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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
55
%
6+
% Assumption: the raw file was zipped
7+
%
68
% USAGE::
79
%
810
% rawsource = identify_rawsources(derivatives)
@@ -41,10 +43,34 @@
4143
derivatives = strrep(derivatives, '_uw.mat', '.nii');
4244
end
4345

46+
% - only deal with files with official BIDS suffixes
47+
% - remove prefix
48+
% - remove eventual derivatives entities
49+
% - change surface extension to a volume one
50+
% - use only .nii.gz
4451
bf = bids.File(derivatives, 'verbose', verbose, 'use_schema', false);
4552

53+
if ~ismember(bf.suffix, fieldnames(map.cfg.schema.content.objects.suffixes))
54+
rawsource{1} = 'TODO';
55+
return
56+
end
57+
4658
bf.prefix = '';
4759

60+
entities = fieldnames(bf.entities);
61+
idx = find(ismember(entities, map.cfg.entity_order));
62+
for i = 1:numel(idx)
63+
bf.entities.(entities{idx(i)}) = '';
64+
end
65+
66+
if strcmp(bf.extension, '.surf.gii')
67+
bf.extension = '.nii';
68+
end
69+
70+
if strcmp(bf.extension, '.nii')
71+
bf.extension = '.nii.gz';
72+
end
73+
4874
rawsource{1} = fullfile(bf.bids_path, bf.filename);
4975

5076
end

src/utils/identify_sources.m

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,6 @@
3939
add_deformation_field = false;
4040
deformation_field = 'TODO: add deformation field';
4141

42-
% TODO? grab all the anat suffixes from BIDS schema?
43-
covered_suffixes = {'T1w', ...
44-
'T2w', ...
45-
'PDw', ...
46-
'T2starw', ...
47-
'inplaneT1', ...
48-
'inplaneT2', ...
49-
'PD', ...
50-
'PDT2', ...
51-
'T2star', ...
52-
'FLASH', ...
53-
'T1map', ...
54-
'T2map', ...
55-
'T2starmap', ...
56-
'R1map', ...
57-
'R2map', ...
58-
'R2starmap', ...
59-
'PDmap', ...
60-
'UNIT1'};
61-
6242
args = inputParser;
6343

6444
addOptional(args, 'derivatives', pwd, @ischar);
@@ -75,6 +55,7 @@
7555
return
7656
end
7757

58+
% deal with SPM's funky suffixes
7859
if endsWith(derivatives, '_seg8.mat')
7960

8061
prefix_based = false;
@@ -91,6 +72,18 @@
9172

9273
bf = bids.File(derivatives, 'verbose', verbose, 'use_schema', false);
9374

75+
if ~ismember(bf.suffix, fieldnames(map.cfg.schema.content.objects.suffixes))
76+
sources{1} = 'TODO';
77+
return
78+
end
79+
80+
% deal with surface data
81+
if strcmp(bf.extension, '.surf.gii')
82+
bf.extension = '.nii';
83+
prefix_based = false;
84+
end
85+
86+
% anything prefix based
9487
if prefix_based
9588

9689
if numel(bf.prefix) < 2
@@ -164,7 +157,7 @@
164157
% for anatomical data we assume that
165158
% the deformation field comes from the anatomical file itself
166159
if (~isempty(bf.modality) && ismember(bf.modality, {'anat'})) || ...
167-
(~isempty(bf.suffix) && ismember(bf.suffix, covered_suffixes))
160+
(~isempty(bf.suffix) && ~isempty(map.cfg.schema.find_suffix_group('anat', bf.suffix)))
168161

169162
bf.prefix = 'y_';
170163
bf = bf.update;

tests/test_identify_rawsources.m

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

11+
function test_identify_rawsources_skip_unknown_suffix()
12+
13+
input_output = {'wsub-01_label-brain_mask.nii', ''};
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, {'TODO'});
24+
25+
end
26+
27+
end
28+
29+
function test_identify_rawsources_surface()
30+
31+
input_output = {'c1sub-01_T1w.surf.gii', 'sub-01_T1w.nii.gz'};
32+
33+
verbose = false;
34+
35+
map = default_mapping();
36+
37+
for i = 1:size(input_output, 1)
38+
39+
rawsource = identify_rawsources(input_output{i, 1}, map, verbose);
40+
41+
assertEqual(rawsource, {['sub-01/' input_output{i, 2}]});
42+
43+
end
44+
45+
end
46+
47+
function test_identify_rawsources_when_der_entities()
48+
49+
input_output = {'sub-01_desc-skullstripped_T1w.nii', 'sub-01_T1w.nii.gz'};
50+
51+
verbose = false;
52+
53+
map = default_mapping();
54+
55+
for i = 1:size(input_output, 1)
56+
57+
rawsource = identify_rawsources(input_output{i, 1}, map, verbose);
58+
59+
assertEqual(rawsource, {['sub-01/' input_output{i, 2}]});
60+
61+
end
62+
63+
end
64+
1165
function test_identify_rawsources_suffix()
1266

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'};
67+
input_output = {'sub-01_T1w_seg8.mat', 'sub-01_T1w.nii.gz'
68+
'sub-01_task-foo_bold_uw.mat', 'sub-01_task-foo_bold.nii.gz'};
1569

1670
verbose = false;
1771

72+
map = default_mapping();
73+
1874
for i = 1:size(input_output, 1)
1975

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

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

@@ -44,13 +100,15 @@ function test_identify_rawsources_anat()
44100

45101
verbose = false;
46102

103+
map = default_mapping();
104+
47105
for i = 1:numel(prefixes)
48106

49107
file = [prefixes{i} anat_file];
50108

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

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

55113
end
56114

@@ -83,13 +141,15 @@ function test_identify_rawsources_func()
83141

84142
verbose = false;
85143

144+
map = default_mapping();
145+
86146
for i = 1:numel(prefixes)
87147

88148
file = [prefixes{i} func_file];
89149

90-
rawsource = identify_rawsources(file, verbose);
150+
rawsource = identify_rawsources(file, map, verbose);
91151

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

94154
end
95155

tests/test_identify_sources.m

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

11+
function test_identify_sources_surface()
12+
13+
anat_file = 'sub-01_T1w.surf.gii';
14+
15+
prefix_output = {'wm', 'sub-01/sub-01_space-IXI549Space_desc-preproc_T1w.nii'};
16+
17+
map = default_mapping();
18+
19+
for i = 1:size(prefix_output, 1)
20+
21+
sources = identify_sources([prefix_output{i, 1} anat_file], map, false);
22+
23+
assertEqual(sources{1}, prefix_output{i, 2});
24+
25+
end
26+
27+
end
28+
1129
function test_identify_sources_anat()
1230

1331
anat_file = 'sub-01_T1w.nii';

0 commit comments

Comments
 (0)