1313# from skimage.segmentation import relabel_sequential
1414
1515
16- def filter_component (fs , segmentation , cochlea , seg_name , components ):
16+ def filter_component (fs , segmentation , cochlea , seg_name , components , min_size ):
1717 # First, we download the MoBIE table for this segmentation.
1818 internal_path = os .path .join (BUCKET_NAME , cochlea , "tables" , seg_name , "default.tsv" )
1919 with fs .open (internal_path , "r" ) as f :
2020 table = pd .read_csv (f , sep = "\t " )
2121
2222 # Then we get the ids for the components and us them to filter the segmentation.
2323 component_mask = np .isin (table .component_labels .values , components )
24+ if min_size is not None :
25+ component_mask = np .logical_and (component_mask , table .n_pixels > min_size )
26+
2427 keep_label_ids = table .label_id .values [component_mask ].astype ("int64" )
2528 if max (keep_label_ids ) > np .iinfo ("uint16" ).max :
2629 warnings .warn (f"Label ID exceeds maximum of data type 'uint16': { np .iinfo ('uint16' ).max } ." )
@@ -155,9 +158,13 @@ def export_lower_resolution(args):
155158 with zarr .open (s3_store , mode = "r" ) as f :
156159 data = f [input_key ][:].astype ("float32" )
157160 print ("Data shape" , data .shape )
161+
158162 if args .filter_by_components is not None :
159163 print (f"Filtering channel { channel } by components { args .filter_by_components } ." )
160- data = filter_component (fs , data , args .cochlea , channel , args .filter_by_components )
164+ data = filter_component (
165+ fs , data , args .cochlea , channel , args .filter_by_components , min_size = args .min_size
166+ )
167+
161168 if args .filter_cochlea_channels is not None :
162169 us_factor = ds_factor // (2 ** scale )
163170 upscaled_filter = upscale_volume (data , filter_volume , upscale_factor = us_factor )
@@ -183,6 +190,7 @@ def main():
183190 parser .add_argument ("--binarize" , action = "store_true" )
184191 parser .add_argument ("--filter_cochlea_channels" , nargs = "+" , type = str , default = None )
185192 parser .add_argument ("--filter_dilation_iterations" , type = int , default = 8 )
193+ parser .add_argument ("--min_size" , default = None , type = int )
186194 args = parser .parse_args ()
187195
188196 export_lower_resolution (args )
0 commit comments