@@ -116,6 +116,37 @@ def image_uncertainty(img_list=[], img_squared_list=[], N=0, sigma_flag=False, t
116116 img_uncertainty .CopyInformation (img_sum )
117117 return img_uncertainty
118118
119+ def image_uncertainty_per_region (img_list = [], img_squared_list = [], img_label = None , N = 0 , sigma_flag = False , threshold = 0 ):
120+ check_N (N )
121+
122+ # Check label image
123+ if img_label is None :
124+ image_uncertainty (img_list , img_squared_list , N , sigma_flag , threshold )
125+ return
126+
127+ # Get the sums
128+ img_sum = gt .image_sum (img_list )
129+ img_sq_sum = gt .image_sum (img_squared_list )
130+
131+ # View as np
132+ np_sum = itk .array_view_from_image (img_sum )
133+ np_sq_sum = itk .array_view_from_image (img_sq_sum )
134+ np_label = itk .array_from_image (img_label ).astype (int )
135+
136+ # Group values by label
137+ max_label = np .max (np_label )
138+ np_sum_by_label = np .bincount (np_label .ravel (), weights = np_sum .ravel (), minlength = max_label + 1 )
139+ np_sq_sum_by_label = np .bincount (np_label .ravel (), weights = np_sq_sum .ravel (), minlength = max_label + 1 )
140+
141+ # Compute relative uncertainty [Chetty 2006]
142+ t = np .max (np_sum_by_label )* threshold
143+ uncertainty = relative_uncertainty (np_sum_by_label , np_sq_sum_by_label , N , t )
144+
145+ # create a text file to save uncertainty by region
146+ with open ('uncertainty_by_region.txt' , 'w' ) as f :
147+ for i , value in enumerate (uncertainty ):
148+ f .write (f"{ i } \t { value } \n " )
149+ return uncertainty
119150
120151def image_uncertainty_by_slice (img_list = [], img_squared_list = [], N = 0 , sigma_flag = False , threshold = 0 ):
121152 check_N (N )
@@ -213,7 +244,7 @@ def test_image_uncertainty(self):
213244 bytesNew = fnew .read ()
214245 new_hash = hashlib .sha256 (bytesNew ).hexdigest ()
215246 self .assertTrue ("0e1f8e0f0d7d7d3921c726dc33409345e6d9b8bfcc53b797d67f8b48997fa1a5" == new_hash )
216- print (tmpdirpath )
247+ shutil . rmtree (tmpdirpath )
217248 def test_image_uncertainty_by_slice (self ):
218249 x = np .arange (0 , 1 , 0.01 )
219250 y = np .arange (0 , 1 , 0.01 )
@@ -270,3 +301,20 @@ def test_image_uncertainty_Poisson_by_slice(self):
270301 new_hash = hashlib .sha256 (bytesNew ).hexdigest ()
271302 self .assertTrue ("cb58fb2f5490546bb83b9e0e51ce1d87b13eab2f0f4ebddc4e9c767c8b98e57b" == new_hash )
272303 shutil .rmtree (tmpdirpath )
304+ def test_image_uncertainty_per_region (self ):
305+ x = np .arange (0 , 1 , 0.01 )
306+ y = np .arange (0 , 1 , 0.01 )
307+ z = np .arange (0 , 1 , 0.01 )
308+ xx , yy , zz = np .meshgrid (x , y , z )
309+ npImage = 10 * xx + 4.5
310+ npsImage = 10 * xx ** 2 + 9 * xx + 2.85
311+ pattern = np .arange (9 )
312+ pattern_array = np .tile (pattern , npImage .size // pattern .size + 1 )[:npImage .size ]
313+ pattern_array = pattern_array .reshape (npImage .shape )
314+ image = itk .image_from_array (np .float64 (npImage ))
315+ images = [image ]
316+ simage = itk .image_from_array (np .float64 (npsImage ))
317+ simages = [simage ]
318+ limage = itk .image_from_array (np .float64 (pattern_array ))
319+ uncertainty = image_uncertainty_per_region (images , simages , limage , N = 1000000000000 )
320+ self .assertTrue (np .allclose (uncertainty , np .array ([0.00103301 , 0.00103302 , 0.00103302 , 0.00103302 , 0.00103302 , 0.00103301 , 0.00103301 , 0.00103301 , 0.00103301 ])))
0 commit comments