Skip to content

Commit 4216007

Browse files
committed
add tests extractRoiFromAtlas
1 parent 1c50a30 commit 4216007

File tree

4 files changed

+91
-20
lines changed

4 files changed

+91
-20
lines changed

.github/workflows/run_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ name: tests and coverage
1717
on:
1818
push:
1919
branches:
20-
- dev
20+
- '*'
2121
pull_request:
2222
branches: '*'
2323
schedule:

src/atlas/extractRoiFromAtlas.m

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,78 @@
1-
function roiImage = extractRoiFromAtlas(roiDir, atlas, roiName, hemisphere)
1+
function roiImage = extractRoiFromAtlas(outputDir, atlasName, roiName, hemisphere)
2+
%
3+
% Outputs a ROI image and side car json for a given atlas, roi name (as
4+
% defined in the look up table of that atlas) and a hemisphere
5+
%
6+
% USAGE::
7+
%
8+
% roiImage = extractRoiFromAtlas(outputDir, atlasName, roiName, hemisphere)
9+
%
10+
% :param outputDir:
11+
% :param atlasName: ``wang``, ``neuromorphometrics``
12+
% :param roiName: run ``getLookUpTable(atlasName)`` to get a list of ROI names to choose from
13+
% :param hemisphere: ``L`` or ``R``
14+
% :type outputDir: string
15+
% :type atlasName: string
16+
% :type roiName: string
17+
% :type hemisphere: string
218
%
319
% (C) Copyright 2021 CPP ROI developers
420

5-
if strcmp(atlas, 'wang')
21+
[atlasFile, lut] = getAtlasAndLut(atlasName);
622

7-
[maxProbaFiles, roiLabels] = getRetinoProbaAtlas();
23+
if strcmp(atlasName, 'wang')
824

925
if strcmp(hemisphere, 'L')
10-
sourceImage = maxProbaFiles(1, :);
26+
atlasFile = atlasFile(1, :);
1127
else
12-
sourceImage = maxProbaFiles(2, :);
28+
atlasFile = atlasFile(2, :);
1329
end
1430

15-
elseif strcmp(atlas, 'neuromorphometrics')
31+
roiIdx = strcmp(roiName, lut.ROI);
1632

17-
sourceImage = fullfile(returnAtlasDir(), 'space-IXI549Space_desc-neuromorphometrics_dseg.nii');
33+
elseif strcmp(atlasName, 'neuromorphometrics')
1834

19-
roiLabels = getRoiLabelLookUpTable(atlas);
35+
roiName = regexprep(roiName, '(Left )|(Right )', '');
36+
37+
if strcmp(hemisphere, 'L')
38+
prefix = 'Left ';
39+
elseif strcmp(hemisphere, 'L')
40+
prefix = 'Right ';
41+
end
42+
43+
roiIdx = strcmp([prefix roiName], lut.ROI);
2044

2145
end
2246

23-
roiIdx = strcmp(roiName, roiLabels.ROI);
24-
label = roiLabels.label(roiIdx);
47+
% create ROI
48+
if isempty(roiIdx) || ~any(roiIdx)
49+
disp(lut.ROI);
50+
error('No ROI named %s for atlas %s. See list of available ROIs above', ...
51+
roiName, atlasName);
52+
end
53+
label = lut.label(roiIdx);
2554

26-
labelStruct = struct('ROI', [hemisphere roiName], ...
55+
labelStruct = struct('ROI', roiName, ...
2756
'label', label);
2857

29-
roiImage = extractRoiByLabel(sourceImage, labelStruct);
58+
roiImage = extractRoiByLabel(atlasFile, labelStruct);
3059

60+
% rename file
3161
entities = struct('space', 'MNI', ...
3262
'hemi', hemisphere, ...
3363
'label', roiName, ...
34-
'desc', atlas);
64+
'desc', atlasName);
3565
nameStructure = struct('entities', entities, ...
3666
'suffix', 'mask', ...
3767
'ext', '.nii');
38-
3968
nameStructure.use_schema = false;
40-
4169
newName = bids.create_filename(nameStructure);
4270

43-
movefile(roiImage, fullfile(roiDir, newName));
44-
45-
roiImage = fullfile(roiDir, newName);
71+
movefile(roiImage, fullfile(outputDir, newName));
4672

73+
% create side car json
74+
roiImage = fullfile(outputDir, newName);
4775
json = bids.derivatives_json(roiImage);
4876
bids.util.jsonencode(json.filename, json.content);
77+
4978
end

tests/test_extractRoiByLabel.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
initTestSuite;
99
end
1010

11-
function test_lut_wang()
11+
function test_lut_neuromorphometrics()
1212

1313
[atlasFile, lut] = getAtlasAndLut('neuromorphometrics');
1414

tests/test_extractRoiFromAtlas.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
% (C) Copyright 2020 CPP ROI developers
2+
3+
function test_suite = test_extractRoiFromAtlas() %#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_extractRoiFromAtlas_wang()
12+
13+
roiImage = extractRoiFromAtlas(pwd, 'wang', 'V1v', 'L');
14+
15+
assertEqual(exist(fullfile(pwd, 'space-MNI_hemi-L_label-V1v_desc-wang_mask.nii'), ...
16+
'file'), ...
17+
2);
18+
19+
vol = spm_read_vols(spm_vol(roiImage));
20+
assertEqual(sum(vol(:) == 1), 3605); % check the ROI has the right number of voxel
21+
22+
delete(fullfile(pwd, '*.nii'));
23+
delete(fullfile(pwd, '*.json'));
24+
25+
end
26+
27+
function test_extractRoiFromAtlas_neuromorphometrics()
28+
29+
roiImage = extractRoiFromAtlas(pwd, 'neuromorphometrics', 'Amygdala', 'L');
30+
31+
assertEqual(exist(fullfile(pwd, ...
32+
'space-MNI_hemi-L_label-Amygdala_desc-neuromorphometrics_mask.nii'), ...
33+
'file'), ...
34+
2);
35+
36+
vol = spm_read_vols(spm_vol(roiImage));
37+
assertEqual(sum(vol(:) == 1), 375); % check the ROI has the right number of voxel
38+
39+
delete(fullfile(pwd, '*.nii'));
40+
delete(fullfile(pwd, '*.json'));
41+
42+
end

0 commit comments

Comments
 (0)