1414
1515import nibabel as nb
1616import numpy as np
17- from scipy .ndimage .morphology import binary_erosion
18- from scipy .spatial .distance import cdist , euclidean , dice , jaccard
19- from scipy .ndimage .measurements import center_of_mass , label
2017
2118from .. import config , logging
2219
@@ -74,6 +71,7 @@ class Distance(BaseInterface):
7471 _hist_filename = "hist.pdf"
7572
7673 def _find_border (self , data ):
74+ from scipy .ndimage .morphology import binary_erosion
7775 eroded = binary_erosion (data )
7876 border = np .logical_and (data , np .logical_not (eroded ))
7977 return border
@@ -87,6 +85,7 @@ def _get_coordinates(self, data, affine):
8785 return coordinates [:3 , :]
8886
8987 def _eucl_min (self , nii1 , nii2 ):
88+ from scipy .spatial .distance import cdist , euclidean
9089 origdata1 = nii1 .get_data ().astype (np .bool )
9190 border1 = self ._find_border (origdata1 )
9291
@@ -105,6 +104,8 @@ def _eucl_min(self, nii1, nii2):
105104 set1_coordinates .T [point1 , :], set2_coordinates .T [point2 , :])
106105
107106 def _eucl_cog (self , nii1 , nii2 ):
107+ from scipy .spatial .distance import cdist
108+ from scipy .ndimage .measurements import center_of_mass , label
108109 origdata1 = np .logical_and (nii1 .get_data () != 0 ,
109110 np .logical_not (np .isnan (nii1 .get_data ())))
110111 cog_t = np .array (center_of_mass (origdata1 .copy ())).reshape (- 1 , 1 )
@@ -128,6 +129,7 @@ def _eucl_cog(self, nii1, nii2):
128129 return np .mean (dist_matrix )
129130
130131 def _eucl_mean (self , nii1 , nii2 , weighted = False ):
132+ from scipy .spatial .distance import cdist
131133 origdata1 = nii1 .get_data ().astype (np .bool )
132134 border1 = self ._find_border (origdata1 )
133135
@@ -154,6 +156,7 @@ def _eucl_mean(self, nii1, nii2, weighted=False):
154156 return np .mean (min_dist_matrix )
155157
156158 def _eucl_max (self , nii1 , nii2 ):
159+ from scipy .spatial .distance import cdist
157160 origdata1 = nii1 .get_data ()
158161 origdata1 = np .logical_not (
159162 np .logical_or (origdata1 == 0 , np .isnan (origdata1 )))
@@ -287,6 +290,7 @@ class Overlap(BaseInterface):
287290 output_spec = OverlapOutputSpec
288291
289292 def _bool_vec_dissimilarity (self , booldata1 , booldata2 , method ):
293+ from scipy .spatial .distance import dice , jaccard
290294 methods = {'dice' : dice , 'jaccard' : jaccard }
291295 if not (np .any (booldata1 ) or np .any (booldata2 )):
292296 return 0
0 commit comments