Skip to content

Commit ae6cd09

Browse files
committed
add test for createRoi sphere and refactor
1 parent a839556 commit ae6cd09

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

src/roi/createRoi.m

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [mask, outputFile] = createRoi(type, specification, volumeDefiningImage, outputDir, saveImg)
1+
function [mask, outputFile] = createRoi(varargin)
22
%
33
% Returns a mask to be used as a ROI by ``spm_summarize``.
44
% Can also save the ROI as binary image.
@@ -20,18 +20,23 @@
2020
%
2121
% :param type: ``'mask'``, ``'sphere'``, ``'intersection'``, ``'expand'``
2222
% :type type: string
23+
%
2324
% :param volumeDefiningImage: fullpath of the image that will define the space
2425
% (resolution, ...) if the ROI is to be saved.
2526
% :type volumeDefiningImage: string
27+
%
2628
% :param saveImg: Will save the resulting image as binary mask if set to
2729
% ``true``
2830
% :type saveImg: boolean
31+
%
2932
% :param specification: depending on the chosen ``type`` this can be:
3033
%
3134
% :roiImage: - :string: fullpath of the roi image for ``'mask'``
35+
%
3236
% :sphere: - :structure: defines the charateristic for ``'sphere'``
3337
% - ``sphere.location``: X Y Z coordinates in millimeters
3438
% - ``spehere.radius``: radius in millimeters
39+
%
3540
% :specification: - :structure: defines the charateristic for ``'intersection'`` and ``'expand'``
3641
% - ``sphere.location``: X Y Z coordinates in millimeters
3742
% - ``sphere.radius``: radius in millimeters
@@ -64,17 +69,23 @@
6469
%
6570
% (C) Copyright 2021 CPP ROI developers
6671

67-
if nargin < 5
68-
saveImg = false;
69-
end
72+
args = inputParser;
7073

71-
if nargin < 4
72-
outputDir = pwd;
73-
end
74+
allowedTypes = @(x) ismember(x, {'mask', 'sphere', 'intersection', 'expand'});
7475

75-
if nargin < 3
76-
volumeDefiningImage = '';
77-
end
76+
args.addRequired('type', allowedTypes);
77+
args.addRequired('specification');
78+
args.addOptional('volumeDefiningImage', '', @ischar);
79+
args.addOptional('outputDir', pwd, @isdir);
80+
args.addOptional('saveImg', false, @islogical);
81+
82+
args.parse(varargin{:});
83+
84+
type = args.Results.type;
85+
specification = args.Results.specification;
86+
volumeDefiningImage = args.Results.volumeDefiningImage;
87+
outputDir = args.Results.outputDir;
88+
saveImg = args.Results.saveImg;
7889

7990
switch type
8091

tests/test_createRoi.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function test_suite = test_createRoi %#ok<*STOUT>
2+
% (C) Copyright 2022 CPP ROI developers
3+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
4+
test_functions = localfunctions(); %#ok<*NASGU>
5+
catch % no problem; early Matlab versions can use initTestSuite fine
6+
end
7+
initTestSuite;
8+
end
9+
10+
function test_createRoi_sphere()
11+
12+
volumeDefiningImage = fullfile(demoDir(), 'TStatistic.nii');
13+
14+
sphere.location = [44 -67 0];
15+
sphere.radius = 5;
16+
17+
saveImg = true;
18+
outputDir = thisDir();
19+
20+
mask = createRoi('sphere', sphere, volumeDefiningImage, outputDir, saveImg);
21+
22+
assertEqual(exist(fullfile(thisDir(), 'label-sphere5x44yMinus67z0_mask.nii'), 'file'), 2);
23+
assertEqual(exist(fullfile(thisDir(), 'label-sphere5x44yMinus67z0_mask.json'), 'file'), 2);
24+
25+
delete(fullfile(thisDir(), '*.nii'));
26+
delete(fullfile(thisDir(), '*.json'));
27+
28+
mask = createRoi('sphere', sphere, volumeDefiningImage, outputDir, false);
29+
30+
assertEqual(exist(fullfile(thisDir(), 'label-sphere5x44yMinus67z0_mask.nii'), 'file'), 0);
31+
assertEqual(exist(fullfile(thisDir(), 'label-sphere5x44yMinus67z0_mask.json'), 'file'), 0);
32+
33+
end
34+
35+
function value = thisDir()
36+
value = fullfile(fileparts(mfilename('fullpath')));
37+
end
38+
39+
function value = demoDir()
40+
value = fullfile(thisDir(), '..', 'demos', 'roi', 'inputs');
41+
gunzip(fullfile(value, '*.gz'));
42+
end

0 commit comments

Comments
 (0)