@@ -30,10 +30,14 @@ def __init__(self):
3030 # 2. Selector for a distance layer.
3131 self .dist_selector_name1 = "Distances to Structure"
3232 self .dist_selector_widget1 = self ._create_layer_selector (self .dist_selector_name1 , layer_type = "Shapes" )
33+ # 3. Selector for a second distance layer (optional).
34+ self .dist_selector_name2 = "Distances to Structure 2"
35+ self .dist_selector_widget2 = self ._create_layer_selector (self .dist_selector_name2 , layer_type = "Shapes" )
3336
3437 # Add the selector widgets to the layout.
3538 layout .addWidget (self .vesicle_selector_widget )
3639 layout .addWidget (self .dist_selector_widget1 )
40+ layout .addWidget (self .dist_selector_widget2 )
3741
3842 # Create the UI elements for defining the vesicle pools:
3943 # The name of the output name, the name of the vesicle pool, and the criterion for the pool.
@@ -68,6 +72,11 @@ def on_pool_vesicles(self):
6872
6973 distance_layer = self ._get_layer_selector_layer (self .dist_selector_name1 )
7074 distances = None if distance_layer is None else distance_layer .properties
75+ distance_layer2 = self ._get_layer_selector_layer (self .dist_selector_name2 )
76+ # Check if the second distance is the same as the first.
77+ if distance_layer2 .name == distance_layer .name :
78+ distance_layer2 = None
79+ distances2 = None if distance_layer2 is None else distance_layer2 .properties
7180
7281 if segmentation is None :
7382 show_info ("INFO: Please choose a segmentation." )
@@ -87,7 +96,9 @@ def on_pool_vesicles(self):
8796 pool_name = self .pool_name_param .text ()
8897
8998 pool_color = self .pool_color_param .text ()
90- self ._compute_vesicle_pool (segmentation , distances , morphology , pool_layer_name , pool_name , query , pool_color )
99+ self ._compute_vesicle_pool (
100+ segmentation , distances , morphology , pool_layer_name , pool_name , query , pool_color , distances2
101+ )
91102
92103 def _update_pool_colors (self , pool_name , pool_color ):
93104 if pool_color == "" :
@@ -107,6 +118,7 @@ def _compute_vesicle_pool(
107118 pool_name : str ,
108119 query : str ,
109120 pool_color : str ,
121+ distances2 : Dict = None
110122 ):
111123 """Compute a vesicle pool based on the provided query parameters.
112124
@@ -118,6 +130,7 @@ def _compute_vesicle_pool(
118130 pool_name: Name for the pooled group to be assigned.
119131 query: Query parameters.
120132 pool_color: Optional color for the vesicle pool.
133+ distances2: Properties from the second distances layer (optional).
121134 """
122135 # Check which of the properties are present and construct the combined properties based on this.
123136 if distances is None and morphology is None : # No properties were given -> we can't do anything.
@@ -140,7 +153,14 @@ def _compute_vesicle_pool(
140153 distances = pd .DataFrame (distances ).drop (columns = ["index" ])
141154 morphology = pd .DataFrame (morphology ).drop (columns = ["index" ])
142155 merged_df = morphology .merge (distances , left_on = "label" , right_on = "label" , suffixes = ("_morph" , "_dist" ))
143-
156+ # Add distances2 if present.
157+ if distances2 is not None :
158+ distance_ids = distances2 .get ("label" , [])
159+ if set (distance_ids ) != set (merged_df .label ):
160+ show_info ("ERROR: The IDs in distances2 and morphology are not identical." )
161+ return
162+ distances2 = pd .DataFrame (distances2 ).drop (columns = ["index" ])
163+ merged_df = merged_df .merge (distances2 , left_on = "label" , right_on = "label" , suffixes = ("" , "2" ))
144164 # Assign the vesicles to the current pool by filtering the mergeddataframe based on the query.
145165 filtered_df = self ._parse_query (query , merged_df )
146166 if len (filtered_df ) == 0 :
@@ -167,6 +187,12 @@ def _compute_vesicle_pool(
167187 # Combine the vesicle ids corresponding to the previous assignment with the
168188 # assignment for the new / current pool.
169189 old_pool_ids = pool_properties .label .values .tolist ()
190+
191+ # Overwrite the intersection of the two pool assignments with the new pool.
192+ pool_intersections = np .intersect1d (pool_vesicle_ids , old_pool_ids )
193+ old_pool_ids = [item for item in old_pool_ids if item not in pool_intersections ]
194+ pool_properties = pool_properties [~ pool_properties ['label' ].isin (pool_intersections )]
195+
170196 pool_assignments = sorted (pool_vesicle_ids + old_pool_ids )
171197
172198 # Get a map for each vesicle id to its pool.
0 commit comments