Skip to content

Commit 16afef7

Browse files
committed
add isBinaryMask function
1 parent 42ebd0f commit 16afef7

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/utils/isBinaryMask.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function status = isBinaryMask(image)
2+
%
3+
% (C) Copyright 2022 CPP ROI developers
4+
5+
status = true;
6+
7+
if exist(image, 'file') == 0
8+
err.identifier = 'isBinaryMask:notAFile';
9+
err.message = sprintf(['the image:', ...
10+
'\n\t%s\n'...
11+
'does not exist.'], ...
12+
image);
13+
error(err);
14+
end
15+
16+
hdr = spm_vol(image);
17+
18+
if numel(hdr) > 1
19+
err.identifier = 'isBinaryMask:notBinaryImage';
20+
err.message = sprintf(['the image:', ...
21+
'\n\t%s\n', ...
22+
'must be a 3D image. It seems to be 4D image with %i volume.'], ...
23+
image);
24+
error(err);
25+
end
26+
27+
vol = spm_read_vols(hdr);
28+
29+
if numel(unique(vol)) > 2 || ~all(unique(vol) == [0; 1])
30+
err.identifier = 'isBinaryMask:notBinaryImage';
31+
err.message = sprintf(['the image:', ...
32+
'\n\t%s\n', ...
33+
'must be a binary image. It seems to more than ones and zeros.'], ...
34+
image);
35+
error(err);
36+
end
37+
38+
end

tests/test_isBinaryMask.m

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function test_suite = test_isBinaryMask %#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_isBinaryMask_true()
11+
12+
roiFilename = prepareRoiAndVolumeDefiningImage();
13+
isBinaryMask(roiFilename);
14+
15+
end
16+
17+
function test_isBinaryMask_false()
18+
19+
[~, zMap] = prepareRoiAndVolumeDefiningImage();
20+
assertExceptionThrown(@()isBinaryMask(zMap), 'isBinaryMask:notBinaryImage');
21+
22+
end
23+
24+
function value = thisDir()
25+
value = fullfile(fileparts(mfilename('fullpath')));
26+
end
27+
28+
function value = demoDir()
29+
30+
value = fullfile(thisDir(), '..', 'demos', 'roi', 'inputs');
31+
32+
if exist(fullfile(value, 'TStatistic.nii'), 'file') == 0
33+
gunzip(fullfile(value, '*.gz'));
34+
end
35+
36+
end
37+
38+
function [roiFilename, zMap] = prepareRoiAndVolumeDefiningImage()
39+
40+
zMap = fullfile(demoDir(), 'space-MNI_atlas-neurosynth_label-visualMotion_probseg.nii');
41+
42+
roiFilename = fullfile(demoDir(), ...
43+
'space-MNI_atlas-neurosynth_label-visualMotion_desc-p10pt00_mask.nii');
44+
45+
if exist(roiFilename, 'file') == 2
46+
47+
else
48+
49+
zMap = renameNeuroSynth(zMap);
50+
threshold = 10;
51+
roiFilename = thresholdToMask(zMap, threshold);
52+
53+
end
54+
55+
end

0 commit comments

Comments
 (0)