@@ -1185,7 +1185,16 @@ def np_calc_boundary_mask(
11851185 infect_list = []
11861186
11871187 def infect (x , y , z ):
1188- if any ([x < 0 , y < 0 , z < 0 , x == boundary .shape [0 ], y == boundary .shape [1 ], z == boundary .shape [2 ]]):
1188+ if any (
1189+ [
1190+ x < 0 ,
1191+ y < 0 ,
1192+ z < 0 ,
1193+ x == boundary .shape [0 ],
1194+ y == boundary .shape [1 ],
1195+ z == boundary .shape [2 ],
1196+ ]
1197+ ):
11891198 return
11901199 if boundary [x , y , z ] == 0 :
11911200 boundary [x , y , z ] = 1
@@ -1328,7 +1337,6 @@ def np_fill_holes_global_with_majority_voting(arr: UINTARRAY, connectivity: int
13281337 cc_msk ,
13291338 label_mask = arr_c ,
13301339 dilate_pixel = 1 ,
1331- label_ref = 1 ,
13321340 inplace = False ,
13331341 )
13341342 arr_c [seg_nii_new != 0 ] = seg_nii_new [seg_nii_new != 0 ]
@@ -1355,26 +1363,29 @@ def np_map_labels_based_on_majority_label_mask_overlap(
13551363 Returns:
13561364 arr: input array with all labels in labels relabeled
13571365 """
1358- arr_c = arr if inplace else arr .copy ()
1366+ arr_cc = arr if inplace else arr .copy ()
13591367
13601368 labels = _to_labels (arr , label_ref )
13611369
1362- label_list : list [int ] = [l for l in np_unique (arr ) if l in labels ]
1363- for l in label_list :
1364- arr_l = np_extract_label (arr , l , inplace = False )
1370+ label_list : list [int ] = [label for label in np_unique (arr ) if label in labels ]
1371+
1372+ for label in label_list :
1373+ arr_l = np_extract_label (arr , label , inplace = False )
13651374 arr_ld = np_dilate_msk (arr_l .copy (), n_pixel = dilate_pixel , label_ref = 1 , connectivity = 3 ) if dilate_pixel > 0 else arr_l
1375+ # crop speed up by factor 6
1376+ crop = np_bbox_binary (arr_ld , px_dist = 0 , raise_error = False )
13661377
1367- mult = label_mask * arr_ld
1378+ mult = label_mask [ crop ] * arr_ld [ crop ]
13681379 label_ref , count = np .unique (mult , return_counts = True )
13691380 if 0 in label_ref :
13701381 label_ref = label_ref [1 :]
13711382 count = count [1 :]
13721383 try :
1373- newlabel = label_ref [np .argmax (count )]
1374- except ValueError :
1375- newlabel = no_match_label
1376- arr_c [arr_l != 0 ] = newlabel
1377- return arr_c
1384+ new_label = label_ref [np .argmax (count )]
1385+ except ValueError : # should never happen if called from np_fill_holes_global_with_majority_voting
1386+ new_label = no_match_label
1387+ arr_cc [arr_l != 0 ] = new_label
1388+ return arr_cc
13781389
13791390
13801391def _pad_to_parameters (
0 commit comments