Skip to content

Commit

Permalink
Update vesicle pool widget (#77)
Browse files Browse the repository at this point in the history
* Update vesicle pool widget WIP

* Finish vesicle pool widget
  • Loading branch information
constantinpape authored Dec 7, 2024
1 parent 9b94807 commit 8a4d26d
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 104 deletions.
6 changes: 3 additions & 3 deletions synapse_net/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contributions:
# Commands for widgets.
- id: synapse_net.segment
python_name: synapse_net.tools.segmentation_widget:SegmentationWidget
title: Segment
title: Segmentation
- id: synapse_net.distance_measure
python_name: synapse_net.tools.distance_measure_widget:DistanceMeasureWidget
title: Distance Measurement
Expand All @@ -21,7 +21,7 @@ contributions:
title: Morphology Analysis
- id: synapse_net.vesicle_pooling
python_name: synapse_net.tools.vesicle_pool_widget:VesiclePoolWidget
title: Vesicle Pooling
title: Pool Assignment

# Commands for sample data.
- id: synapse_net.sample_data_tem_2d
Expand All @@ -47,7 +47,7 @@ contributions:
- command: synapse_net.morphology
display_name: Morphology Analysis
- command: synapse_net.vesicle_pooling
display_name: Vesicle Pooling
display_name: Pool Assignment

sample_data:
- command: synapse_net.sample_data_tem_2d
Expand Down
34 changes: 27 additions & 7 deletions synapse_net/tools/base_widget.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from pathlib import Path

import napari
Expand All @@ -11,9 +12,19 @@
from superqt import QCollapsible

try:
from napari_skimage_regionprops import add_table
from napari_skimage_regionprops import add_table, get_table
except ImportError:
add_table = None
add_table, get_table = None, None


class _SilencePrint:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, "w")

def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout


class BaseWidget(QWidget):
Expand Down Expand Up @@ -316,12 +327,21 @@ def _save_table(self, save_path, data):
return file_path

def _add_properties_and_table(self, layer, table_data, save_path=""):
if layer.properties:
layer.properties = layer.properties.update(table_data)
else:
layer.properties = table_data
layer.properties = table_data

if add_table is not None:
add_table(layer, self.viewer)
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)

# Save table to file if save path is provided.
if save_path != "":
file_path = self._save_table(self.save_path.text(), table_data)
Expand Down
4 changes: 2 additions & 2 deletions synapse_net/tools/distance_measure_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def __init__(self):
def _to_table_data(self, distances, seg_ids, endpoints1=None, endpoints2=None):
assert len(distances) == len(seg_ids), f"{distances.shape}, {seg_ids.shape}"
if seg_ids.ndim == 2:
table_data = {"label_id1": seg_ids[:, 0], "label_id2": seg_ids[:, 1], "distance": distances}
table_data = {"label1": seg_ids[:, 0], "label2": seg_ids[:, 1], "distance": distances}
else:
table_data = {"label_id": seg_ids, "distance": distances}
table_data = {"label": seg_ids, "distance": distances}
if endpoints1 is not None:
axis_names = "zyx" if endpoints1.shape[1] == 3 else "yx"
table_data.update({f"begin-{ax}": endpoints1[:, i] for i, ax in enumerate(axis_names)})
Expand Down
2 changes: 1 addition & 1 deletion synapse_net/tools/morphology_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _to_table_data(self, coords, radii, props):
# Define columns based on dimension (2D or 3D)
col_names = ["x", "y"] if coords.shape[1] == 2 else ["x", "y", "z"]
table_data = {
"label_id": [prop.label for prop in props],
"label": [prop.label for prop in props],
**{col: coords[:, i] for i, col in enumerate(col_names)},
"radius": radii,
"intensity_max": [prop.intensity_max for prop in props],
Expand Down
Loading

0 comments on commit 8a4d26d

Please sign in to comment.