[5d12a0]: / ants / label / label_stats.py

Download this file

44 lines (34 with data), 1.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
__all__ = ["label_stats"]
import pandas as pd
from ants.internal import get_lib_fn
from ants.decorators import image_method
@image_method
def label_stats(image, label_image):
"""
Get label statistics from image
ANTsR function: `labelStats`
Arguments
---------
image : ANTsImage
Image from which statistics will be calculated
label_image : ANTsImage
Label image
Returns
-------
ndarray ?
Example
-------
>>> import ants
>>> image = ants.image_read( ants.get_ants_data('r16') , 2 )
>>> image = ants.resample_image( image, (64,64), 1, 0 )
>>> mask = ants.get_mask(image)
>>> segs1 = ants.kmeans_segmentation( image, 3 )
>>> stats = ants.label_stats(image, segs1['segmentation'])
"""
image_float = image.clone("float")
label_image_int = label_image.clone("unsigned int")
libfn = get_lib_fn("labelStats%iD" % image.dimension)
df = libfn(image_float.pointer, label_image_int.pointer)
df = pd.DataFrame(df)
df.sort_values(by=["LabelValue"], inplace=True)
return df