Skip to content

Commit

Permalink
Merge branch 'main' into first-release
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinpape committed Dec 10, 2024
2 parents c0afedb + 458ec90 commit 9ceb256
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
13 changes: 2 additions & 11 deletions synapse_net/tools/base_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,8 @@ def _add_properties_and_table(self, layer, table_data, save_path=""):
layer.properties = table_data

if add_table is not None:
table = get_table(layer, self.viewer)
if table is None:
with _SilencePrint():
add_table(layer, self.viewer)
else:
# FIXME updating the table does not yet work
with _SilencePrint():
table.update_content()
# table_dict = table_data.to_dict()
# table_dict["index"] = table_dict["label"]
# table.set_content(table_dict)
with _SilencePrint():
add_table(layer, self.viewer)

# Save table to file if save path is provided.
if save_path != "":
Expand Down
3 changes: 2 additions & 1 deletion synapse_net/tools/distance_measure_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def on_measure_seg_to_object(self):
seg_ids=seg_ids,
)
table_data = self._to_table_data(distances, seg_ids, endpoints1, endpoints2)
self._add_lines_and_table(lines, table_data, name="distances")
structure_layer_name = self._get_layer_selector_layer(self.image_selector_name2).name
self._add_lines_and_table(lines, table_data, name="distances-to-" + structure_layer_name)

def on_measure_pairwise(self):
segmentation = self._get_layer_selector_data(self.image_selector_name1)
Expand Down
2 changes: 1 addition & 1 deletion synapse_net/tools/segmentation_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def on_predict(self):
for name, seg in segmentation.items():
self.viewer.add_labels(seg, name=name, metadata=metadata)
else:
self.viewer.add_labels(segmentation, name=f"{model_type}-segmentation", metadata=metadata)
self.viewer.add_labels(segmentation, name=f"{model_type}", metadata=metadata)
show_info(f"INFO: Segmentation of {model_type} added to layers.")

def _create_settings_widget(self):
Expand Down
30 changes: 28 additions & 2 deletions synapse_net/tools/vesicle_pool_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ def __init__(self):
# 2. Selector for a distance layer.
self.dist_selector_name1 = "Distances to Structure"
self.dist_selector_widget1 = self._create_layer_selector(self.dist_selector_name1, layer_type="Shapes")
# 3. Selector for a second distance layer (optional).
self.dist_selector_name2 = "Distances to Structure 2"
self.dist_selector_widget2 = self._create_layer_selector(self.dist_selector_name2, layer_type="Shapes")

# Add the selector widgets to the layout.
layout.addWidget(self.vesicle_selector_widget)
layout.addWidget(self.dist_selector_widget1)
layout.addWidget(self.dist_selector_widget2)

# Create the UI elements for defining the vesicle pools:
# The name of the output name, the name of the vesicle pool, and the criterion for the pool.
Expand Down Expand Up @@ -68,6 +72,11 @@ def on_pool_vesicles(self):

distance_layer = self._get_layer_selector_layer(self.dist_selector_name1)
distances = None if distance_layer is None else distance_layer.properties
distance_layer2 = self._get_layer_selector_layer(self.dist_selector_name2)
# Check if the second distance is the same as the first.
if distance_layer2.name == distance_layer.name:
distance_layer2 = None
distances2 = None if distance_layer2 is None else distance_layer2.properties

if segmentation is None:
show_info("INFO: Please choose a segmentation.")
Expand All @@ -87,7 +96,9 @@ def on_pool_vesicles(self):
pool_name = self.pool_name_param.text()

pool_color = self.pool_color_param.text()
self._compute_vesicle_pool(segmentation, distances, morphology, pool_layer_name, pool_name, query, pool_color)
self._compute_vesicle_pool(
segmentation, distances, morphology, pool_layer_name, pool_name, query, pool_color, distances2
)

def _update_pool_colors(self, pool_name, pool_color):
if pool_color == "":
Expand All @@ -107,6 +118,7 @@ def _compute_vesicle_pool(
pool_name: str,
query: str,
pool_color: str,
distances2: Dict = None
):
"""Compute a vesicle pool based on the provided query parameters.
Expand All @@ -118,6 +130,7 @@ def _compute_vesicle_pool(
pool_name: Name for the pooled group to be assigned.
query: Query parameters.
pool_color: Optional color for the vesicle pool.
distances2: Properties from the second distances layer (optional).
"""
# Check which of the properties are present and construct the combined properties based on this.
if distances is None and morphology is None: # No properties were given -> we can't do anything.
Expand All @@ -140,7 +153,14 @@ def _compute_vesicle_pool(
distances = pd.DataFrame(distances).drop(columns=["index"])
morphology = pd.DataFrame(morphology).drop(columns=["index"])
merged_df = morphology.merge(distances, left_on="label", right_on="label", suffixes=("_morph", "_dist"))

# Add distances2 if present.
if distances2 is not None:
distance_ids = distances2.get("label", [])
if set(distance_ids) != set(merged_df.label):
show_info("ERROR: The IDs in distances2 and morphology are not identical.")
return
distances2 = pd.DataFrame(distances2).drop(columns=["index"])
merged_df = merged_df.merge(distances2, left_on="label", right_on="label", suffixes=("", "2"))
# Assign the vesicles to the current pool by filtering the mergeddataframe based on the query.
filtered_df = self._parse_query(query, merged_df)
if len(filtered_df) == 0:
Expand All @@ -167,6 +187,12 @@ def _compute_vesicle_pool(
# Combine the vesicle ids corresponding to the previous assignment with the
# assignment for the new / current pool.
old_pool_ids = pool_properties.label.values.tolist()

# Overwrite the intersection of the two pool assignments with the new pool.
pool_intersections = np.intersect1d(pool_vesicle_ids, old_pool_ids)
old_pool_ids = [item for item in old_pool_ids if item not in pool_intersections]
pool_properties = pool_properties[~pool_properties['label'].isin(pool_intersections)]

pool_assignments = sorted(pool_vesicle_ids + old_pool_ids)

# Get a map for each vesicle id to its pool.
Expand Down

0 comments on commit 9ceb256

Please sign in to comment.