@@ -209,13 +209,13 @@ def test_crop_image_example(self):
209
209
210
210
# label image not float
211
211
cropped = ants .crop_image (fi , fi .clone ("unsigned int" ), 100 )
212
-
212
+
213
213
# channel image
214
214
fi = ants .image_read ( ants .get_ants_data ('r16' ) )
215
215
cropped = ants .crop_image (fi )
216
216
fi2 = ants .merge_channels ([fi ,fi ])
217
217
cropped2 = ants .crop_image (fi2 )
218
-
218
+
219
219
self .assertEqual (cropped .shape , cropped2 .shape )
220
220
221
221
def test_crop_indices_example (self ):
@@ -583,11 +583,27 @@ def setUp(self):
583
583
def tearDown (self ):
584
584
pass
585
585
586
- def test_label_clusters_example (self ):
586
+ def test_label_image_centroids (self ):
587
587
image = ants .from_numpy (
588
588
np .asarray ([[[0 , 2 ], [1 , 3 ]], [[4 , 6 ], [5 , 7 ]]]).astype ("float32" )
589
589
)
590
590
labels = ants .label_image_centroids (image )
591
+ self .assertEqual (len (labels ['labels' ]), 7 )
592
+
593
+ # Test non-sequential labels
594
+ image = ants .from_numpy (
595
+ np .asarray ([[[0 , 2 ], [2 , 2 ]], [[2 , 0 ], [5 , 0 ]]]).astype ("float32" )
596
+ )
597
+
598
+ labels = ants .label_image_centroids (image )
599
+ self .assertTrue (len (labels ['labels' ]) == 2 )
600
+ self .assertTrue (labels ['labels' ][1 ] == 5 )
601
+ self .assertTrue (np .allclose (labels ['vertices' ][0 ], [0.5 , 0.5 , 0.25 ], atol = 1e-5 ))
602
+ # With convex = False, the centroid position should change
603
+ labels = ants .label_image_centroids (image , convex = False )
604
+ self .assertTrue (np .allclose (labels ['vertices' ][0 ], [1.0 , 1.0 , 0.0 ], atol = 1e-5 ))
605
+ # single point unchanged
606
+ self .assertTrue (np .allclose (labels ['vertices' ][1 ], [0.0 , 1.0 , 1.0 ], atol = 1e-5 ))
591
607
592
608
593
609
class TestModule_label_overlap_measures (unittest .TestCase ):
@@ -633,6 +649,28 @@ def test_labels_to_matrix_example(self):
633
649
labmat = ants .labels_to_matrix (labs , mask )
634
650
635
651
652
+ class TestModule_make_points_image (unittest .TestCase ):
653
+ def setUp (self ):
654
+ pass
655
+
656
+ def tearDown (self ):
657
+ pass
658
+
659
+ def test_make_points_image_example (self ):
660
+ image = ants .image_read (ants .get_ants_data ("r16" ))
661
+ points = np .array ([[102 , 76 ],[134 , 129 ]])
662
+ points_image = ants .make_points_image (points , image , radius = 5 )
663
+ stats = ants .label_stats (image , points_image )
664
+ self .assertTrue (np .allclose (stats ['Volume' ].to_numpy ()[1 :3 ], 97.0 , atol = 1e-5 ))
665
+ self .assertTrue (np .allclose (stats ['x' ].to_numpy ()[1 :3 ], points [:,0 ], atol = 1e-5 ))
666
+ self .assertTrue (np .allclose (stats ['y' ].to_numpy ()[1 :3 ], points [:,1 ], atol = 1e-5 ))
667
+
668
+ points = np .array ([[102 , 76 , 50 ],[134 , 129 , 50 ]])
669
+ # Shouldn't allow 3D points on a 2D image
670
+ with self .assertRaises (Exception ):
671
+ points_image = ants .make_points_image (image , points , radius = 3 )
672
+
673
+
636
674
class TestModule_mask_image (unittest .TestCase ):
637
675
def setUp (self ):
638
676
pass
@@ -846,7 +884,7 @@ def setUp(self):
846
884
pass
847
885
def tearDown (self ):
848
886
pass
849
-
887
+
850
888
def test_bspline_field (self ):
851
889
points = np .array ([[- 50 , - 50 ]])
852
890
deltas = np .array ([[10 , 10 ]])
@@ -874,29 +912,29 @@ def test_ilr(self):
874
912
result = ants .ilr ( df , vlist , myform )
875
913
myform = " mat2 ~ covar + mat1 "
876
914
result = ants .ilr ( df , vlist , myform )
877
-
915
+
878
916
def test_quantile (self ):
879
917
img = ants .image_read (ants .get_data ('r16' ))
880
918
ants .quantile (img , 0.5 )
881
919
ants .quantile (img , (0.5 , 0.75 ))
882
-
920
+
883
921
def test_bandpass (self ):
884
922
brainSignal = np .random .randn ( 400 , 1000 )
885
923
tr = 1
886
924
filtered = ants .bandpass_filter_matrix ( brainSignal , tr = tr )
887
-
925
+
888
926
def test_compcorr (self ):
889
927
cc = ants .compcor ( ants .image_read (ants .get_ants_data ("ch2" )) )
890
928
891
929
def test_histogram_match (self ):
892
930
src_img = ants .image_read (ants .get_data ('r16' ))
893
931
ref_img = ants .image_read (ants .get_data ('r64' ))
894
932
src_ref = ants .histogram_match_image (src_img , ref_img )
895
-
933
+
896
934
src_img = ants .image_read (ants .get_data ('r16' ))
897
935
ref_img = ants .image_read (ants .get_data ('r64' ))
898
936
src_ref = ants .histogram_match_image2 (src_img , ref_img )
899
-
937
+
900
938
def test_averaging (self ):
901
939
x0 = [ ants .get_data ('r16' ), ants .get_data ('r27' ), ants .get_data ('r62' ), ants .get_data ('r64' ) ]
902
940
x1 = []
@@ -906,7 +944,7 @@ def test_averaging(self):
906
944
avg1 = ants .average_images (x1 )
907
945
avg2 = ants .average_images (x1 ,mask = 0 )
908
946
avg3 = ants .average_images (x1 ,mask = 1 ,normalize = True )
909
-
947
+
910
948
def test_n3_2 (self ):
911
949
image = ants .image_read ( ants .get_ants_data ('r16' ) )
912
950
image_n3 = ants .n3_bias_field_correction2 (image )
@@ -929,29 +967,29 @@ def test_thin_plate_spline(self):
929
967
displacement_origins = points , displacements = deltas ,
930
968
origin = [0.0 , 0.0 ], spacing = [1.0 , 1.0 ], size = [100 , 100 ],
931
969
direction = np .array ([[- 1 , 0 ], [0 , - 1 ]]))
932
-
970
+
933
971
def test_multi_label_morph (self ):
934
972
img = ants .image_read (ants .get_data ('r16' ))
935
973
labels = ants .get_mask (img ,1 ,150 ) + ants .get_mask (img ,151 ,225 ) * 2
936
974
labels_dilated = ants .multi_label_morphology (labels , 'MD' , 2 )
937
975
# should see original label regions preserved in dilated version
938
976
# label N should have mean N and 0 variance
939
977
print (ants .label_stats (labels_dilated , labels ))
940
-
978
+
941
979
def test_hausdorff_distance (self ):
942
980
r16 = ants .image_read ( ants .get_ants_data ('r16' ) )
943
981
r64 = ants .image_read ( ants .get_ants_data ('r64' ) )
944
982
s16 = ants .kmeans_segmentation ( r16 , 3 )['segmentation' ]
945
983
s64 = ants .kmeans_segmentation ( r64 , 3 )['segmentation' ]
946
984
stats = ants .hausdorff_distance (s16 , s64 )
947
-
985
+
948
986
def test_channels_first (self ):
949
987
import ants
950
988
image = ants .image_read (ants .get_ants_data ('r16' ))
951
989
image2 = ants .image_read (ants .get_ants_data ('r16' ))
952
990
img3 = ants .merge_channels ([image ,image2 ])
953
991
img4 = ants .merge_channels ([image ,image2 ], channels_first = True )
954
-
992
+
955
993
self .assertTrue (np .allclose (img3 .numpy ()[:,:,0 ], img4 .numpy ()[0 ,:,:]))
956
994
self .assertTrue (np .allclose (img3 .numpy ()[:,:,1 ], img4 .numpy ()[1 ,:,:]))
957
995
0 commit comments