@@ -249,7 +249,7 @@ def erode_subset(
249249 Returns:
250250 The dataframe containing elements left after the erosion.
251251 """
252- print ("initial length" , len (table ))
252+ print (f"Initial length: { len (table )} " )
253253 n_neighbors = 100
254254 for i in range (iterations ):
255255 table = table [table [keyword ] < threshold ]
@@ -406,10 +406,12 @@ def components_sgn(
406406 min_cells = 20000
407407 threshold = threshold_erode if threshold_erode is not None else 40
408408
409- print (f"Using threshold of { threshold } micrometer for eroding segmentation with keyword { keyword } ." )
410-
411- new_subset = erode_subset (table .copy (), iterations = iterations ,
412- threshold = threshold , min_cells = min_cells , keyword = keyword )
409+ if iterations != 0 :
410+ print (f"Using threshold of { threshold } micrometer for eroding segmentation with keyword { keyword } ." )
411+ new_subset = erode_subset (table .copy (), iterations = iterations ,
412+ threshold = threshold , min_cells = min_cells , keyword = keyword )
413+ else :
414+ new_subset = table .copy ()
413415
414416 # create graph from coordinates of eroded subset
415417 centroids_subset = list (zip (new_subset ["anchor_x" ], new_subset ["anchor_y" ], new_subset ["anchor_z" ]))
@@ -486,41 +488,10 @@ def label_components_sgn(
486488 table .sort_values ("label_id" )
487489
488490 component_labels = [0 for _ in range (len (table ))]
491+ table .loc [:, "component_labels" ] = component_labels
489492 # be aware of 'label_id' of dataframe starting at 1
490493 for lab , comp in enumerate (components ):
491- for comp_index in comp :
492- component_labels [comp_index - 1 ] = lab + 1
493-
494- return component_labels
495-
496-
497- def postprocess_sgn_seg (
498- table : pd .DataFrame ,
499- min_size : int = 1000 ,
500- threshold_erode : Optional [float ] = None ,
501- min_component_length : int = 50 ,
502- max_edge_distance : float = 30 ,
503- iterations_erode : Optional [int ] = None ,
504- ) -> pd .DataFrame :
505- """Postprocessing SGN segmentation of cochlea.
506-
507- Args:
508- table: Dataframe of segmentation table.
509- min_size: Minimal number of pixels for filtering small instances.
510- threshold_erode: Threshold of column value after erosion step with spatial statistics.
511- min_component_length: Minimal length for filtering out connected components.
512- max_edge_distance: Maximal distance in micrometer between points to create edges for connected components.
513- iterations_erode: Number of iterations for erosion, normally determined automatically.
514-
515- Returns:
516- Dataframe with component labels.
517- """
518-
519- comp_labels = label_components_sgn (table , min_size = min_size , threshold_erode = threshold_erode ,
520- min_component_length = min_component_length ,
521- max_edge_distance = max_edge_distance , iterations_erode = iterations_erode )
522-
523- table .loc [:, "component_labels" ] = comp_labels
494+ table .loc [table ["label_id" ].isin (comp ), "component_labels" ] = lab + 1
524495
525496 return table
526497
@@ -583,37 +554,10 @@ def label_components_ihc(
583554 length_components , components = zip (* sorted (zip (length_components , components ), reverse = True ))
584555
585556 component_labels = [0 for _ in range (len (table ))]
557+ table .loc [:, "component_labels" ] = component_labels
586558 # be aware of 'label_id' of dataframe starting at 1
587559 for lab , comp in enumerate (components ):
588- for comp_index in comp :
589- component_labels [comp_index - 1 ] = lab + 1
590-
591- return component_labels
592-
593-
594- def postprocess_ihc_seg (
595- table : pd .DataFrame ,
596- min_size : int = 1000 ,
597- min_component_length : int = 50 ,
598- max_edge_distance : float = 30 ,
599- ) -> pd .DataFrame :
600- """Postprocessing IHC segmentation of cochlea.
601-
602- Args:
603- table: Dataframe of segmentation table.
604- min_size: Minimal number of pixels for filtering small instances.
605- min_component_length: Minimal length for filtering out connected components.
606- max_edge_distance: Maximal distance in micrometer between points to create edges for connected components.
607-
608- Returns:
609- Dataframe with component labels.
610- """
611-
612- comp_labels = label_components_ihc (table , min_size = min_size ,
613- min_component_length = min_component_length ,
614- max_edge_distance = max_edge_distance )
615-
616- table .loc [:, "component_labels" ] = comp_labels
560+ table .loc [table ["label_id" ].isin (comp ), "component_labels" ] = lab + 1
617561
618562 return table
619563
0 commit comments