Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label geom test #784

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ants/label/label_geometry_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def label_geometry_measures(label_image, intensity_image=None):
>>> seg = ants.kmeans_segmentation( fi, 3 )['segmentation']
>>> geom = ants.label_geometry_measures(seg,fi)
"""
import numpy as np
if intensity_image is None:
intensity_image = label_image.clone()

Expand All @@ -49,9 +48,11 @@ def label_geometry_measures(label_image, intensity_image=None):
libfn = get_lib_fn('LabelGeometryMeasures')
pp = libfn(veccer_processed)
pp = pd.read_csv(outcsv)
import numpy as np
# pp['Label'] = np.sort(np.unique(label_image[label_image>0])).astype('int')
if 'VolumeInVoxels' in pp.columns and not 'VolumeInMillimeters' in pp.columns:
spc = np.prod(label_image.spacing)
pp['VolumeInMillimeters'] = pp['VolumeInVoxels'] * spc
# Ensure that the label column is of integer type - if there is any NaN, it will be float
# Something has gone seriously wrong if the labels are not interpreted as integers
if not np.issubdtype(pp['Label'].dtype, np.integer):
raise ValueError('Label column not integer type, label values may be invalid')
return pp
26 changes: 22 additions & 4 deletions tests/test_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,27 @@ def tearDown(self):
pass

def test_example(self):
fi = ants.image_read( ants.get_ants_data('r16') )
fi = ants.resample_image(ants.image_read( ants.get_ants_data('r16') ), (140,140),1,0)
seg = ants.kmeans_segmentation( fi, 3 )['segmentation']
geom = ants.label_geometry_measures(seg,fi)
expected_cols = ['Label', 'VolumeInVoxels', 'VolumeInMillimeters',
'SurfaceAreaInMillimetersSquared', 'Eccentricity', 'Elongation',
'Roundness', 'Flatness', 'Centroid_x', 'Centroid_y', 'AxesLength_x',
'AxesLength_y', 'BoundingBoxLower_x', 'BoundingBoxLower_y',
'BoundingBoxUpper_x', 'BoundingBoxUpper_y', 'MeanIntensity',
'SigmaIntensity', 'MinIntensity', 'MaxIntensity',
'IntegratedIntensity']
for col in expected_cols:
self.assertTrue(col in geom.columns)
# Label column should have int type
self.assertTrue(np.issubdtype(geom['Label'].dtype, np.integer))
# So should VolumeInVoxels
self.assertTrue(np.issubdtype(geom['VolumeInVoxels'].dtype, np.integer))
# Check math of VolumeInMillimeters
self.assertTrue(np.allclose(geom['VolumeInMillimeters'], geom['VolumeInVoxels'] * np.prod(fi.spacing), atol=1e-6))
# should be three rows
self.assertEqual(geom.shape[0], 3)



class TestModule_prior_based_segmentation(unittest.TestCase):
Expand All @@ -198,7 +216,7 @@ def test_example(self):


class TestModule_random(unittest.TestCase):

def setUp(self):
pass
def tearDown(self):
Expand All @@ -208,15 +226,15 @@ def test_fuzzy_cmeans(self):
image = ants.image_read(ants.get_ants_data('r16'))
mask = ants.get_mask(image)
fuzzy = ants.fuzzy_spatial_cmeans_segmentation(image, mask, number_of_clusters=3)

def test_functional_lung(self):
image = ants.image_read(ants.get_data("mni")).resample_image((4,4,4))
mask = image.get_mask()
seg = ants.functional_lung_segmentation(image, mask, verbose=True,
number_of_iterations=1,
number_of_clusters=2,
number_of_atropos_iterations=1)

def test_anti_alias(self):
img = ants.image_read(ants.get_data('r16'))
mask = ants.get_mask(img)
Expand Down
Loading