22
33import imageio .v3 as imageio
44import napari
5+ import numpy as np
56import pandas as pd
67
8+ from elf .parallel import isin
79from ..file_utils import read_mrc
810
911
10- def _create_pools (vesicles , table ):
12+ def _create_pools (vesicles , table , split_pools ):
1113 label_ids , pool_colors = table .label .values , table .color .values
1214
1315 pools = vesicles
1416 colormap = {label_id : color for label_id , color in zip (label_ids , pool_colors )}
1517 colormap [None ] = [0 , 0 , 0 , 0 ]
1618
17- return pools , colormap
19+ if split_pools :
20+ unique_colors = np .unique (pool_colors )
21+ pool_ret = {}
22+ for this_color in unique_colors :
23+ this_pool = pools .copy ()
24+ this_ids = [label_id for label_id , color in colormap .items () if color == this_color ]
25+ pool_mask = np .zeros (this_pool .shape , dtype = "bool" )
26+ pool_mask = isin (this_pool , this_ids , out = pool_mask , block_shape = (32 , 128 , 128 ))
27+ this_pool [~ pool_mask ] = 0
28+ pool_ret [this_color ] = this_pool
29+ else :
30+ pool_ret = {"pools" : pools }
31+
32+ return pool_ret , colormap
1833
1934
2035def _parse_tables (table_paths ):
@@ -39,25 +54,36 @@ def load_table(path):
3954 return table
4055
4156
42- def _visualize_vesicle_pools (input_path , vesicle_path , table_paths , segmentation_paths ):
57+ def _visualize_vesicle_pools (input_path , vesicle_paths , table_paths , segmentation_paths , split_pools ):
4358 # Load the tomogram data, including scale information.
4459 data , voxel_size = read_mrc (input_path )
4560 axes = "zyx" if data .ndim == 3 else "yx"
4661 scale = tuple (float (voxel_size [ax ]) for ax in axes )
4762 print ("Loading data with scale" , scale , "nanometer" )
4863
49- # Load the vesicle layer.
50- vesicles = imageio .imread (vesicle_path )
64+ # Load the vesicle layer, either from a single file with
65+ if len (vesicle_paths ) == 1 :
66+ vesicles = imageio .imread (vesicle_paths )
67+ else :
68+ vesicles = None
69+ for path in vesicle_paths :
70+ this_vesicles = imageio .imread (path )
71+ if vesicles is None :
72+ vesicles = this_vesicles .copy ()
73+ else :
74+ ves_mask = this_vesicles != 0
75+ vesicles [ves_mask ] = this_vesicles [ves_mask ]
5176
5277 # Load the tables with the pool assignments.
5378 # Create and add the pool layer.
5479 table = _parse_tables (table_paths )
55- pools , colormap = _create_pools (vesicles , table )
80+ pools , colormap = _create_pools (vesicles , table , split_pools )
5681
5782 viewer = napari .Viewer ()
5883 viewer .add_image (data , scale = scale )
5984 viewer .add_labels (vesicles , scale = scale )
60- viewer .add_labels (pools , scale = scale , name = "pools" , colormap = colormap )
85+ for pool_name , pool in pools .items ():
86+ viewer .add_labels (pool , scale = scale , name = pool_name , colormap = colormap )
6187
6288 # Add the additional segmentations.
6389 if segmentation_paths is not None :
0 commit comments