synapse_net.tools.postprocessing_widget

 1import napari
 2
 3import numpy as np
 4
 5from napari.utils.notifications import show_info
 6from qtpy.QtWidgets import QVBoxLayout, QPushButton
 7from skimage.measure import regionprops
 8from skimage.segmentation import find_boundaries
 9
10from .base_widget import BaseWidget
11
12
13class PostprocessingWidget(BaseWidget):
14    def __init__(self):
15        super().__init__()
16
17        self.viewer = napari.current_viewer()
18        layout = QVBoxLayout()
19
20        # Create the dropdown to select the segmentation to post-process.
21        self.segmentation_selector_name = "Segmentation"
22        self.segmentation_selector_widget = self._create_layer_selector(
23            self.segmentation_selector_name, layer_type="Labels"
24        )
25        layout.addWidget(self.segmentation_selector_widget)
26
27        # Create dropdown to select the mask for filtering / intersection.
28        self.mask_selector_name = "Mask"
29        self.mask_selector_widget = self._create_layer_selector(self.mask_selector_name, layer_type="Labels")
30        layout.addWidget(self.mask_selector_widget)
31
32        # Create input for label id in the mask.
33        self.mask_id_param, _ = self._add_int_param(
34            "mask_id", 0, min_val=0, max_val=1000, layout=layout, title="Mask ID"
35        )
36
37        # Create text field to choose the name of the output layer.
38        self.output_layer_param, _ = self._add_string_param("output_layer", "", title="Output Layer", layout=layout)
39
40        # First postprocessing option: Filter with mask.
41        self.button1 = QPushButton("Filter")
42        self.button1.clicked.connect(self.on_filter)
43        layout.addWidget(self.button1)
44
45        # Second postprocessing option: intersect with boundary of the mask.
46        self.button2 = QPushButton("Intersect with Boundary")
47        self.button2.clicked.connect(self.on_intersect_boundary)
48        layout.addWidget(self.button2)
49
50        # Third postprocessing option: intersect with the mask.
51        self.button3 = QPushButton("Intersect")
52        self.button3.clicked.connect(self.on_intersect)
53        layout.addWidget(self.button3)
54
55        # Add the widgets to the layout.
56        self.setLayout(layout)
57
58    def _write_pp(self, segmentation):
59        layer_name = self.output_layer_param.text()
60        self.add_or_update_labels(layer_name, segmentation)
61
62    def _conditions_met(self):
63        if self.output_layer_param.text() == "":
64            show_info("Please choose an output layer.")
65            return False
66        return True
67
68    def _get_segmentation_and_mask(self):
69        segmentation = self._get_layer_selector_data(self.segmentation_selector_name).copy()
70        mask = self._get_layer_selector_data(self.mask_selector_name)
71        mask_id = self.mask_id_param.value()
72        if mask_id != 0:
73            mask = (mask == mask_id).astype(mask.dtype)
74        return segmentation, mask
75
76    def on_filter(self):
77        if not self._conditions_met():
78            return
79        segmentation, mask = self._get_segmentation_and_mask()
80        props = regionprops(segmentation, mask)
81        filter_ids = [prop.label for prop in props if prop.intensity_max == 0]
82        segmentation[np.isin(segmentation, filter_ids)] = 0
83        self._write_pp(segmentation)
84
85    def on_intersect_boundary(self):
86        if not self._conditions_met():
87            return
88        segmentation, mask = self._get_segmentation_and_mask()
89        boundary = find_boundaries(mask)
90        segmentation = np.logical_and(segmentation > 0, boundary).astype(segmentation.dtype)
91        self._write_pp(segmentation)
92
93    def on_intersect(self):
94        if not self._conditions_met():
95            return
96        segmentation, mask = self._get_segmentation_and_mask()
97        segmentation = np.logical_and(segmentation > 0, mask > 0).astype(segmentation.dtype)
98        self._write_pp(segmentation)
class PostprocessingWidget(synapse_net.tools.base_widget.BaseWidget):
14class PostprocessingWidget(BaseWidget):
15    def __init__(self):
16        super().__init__()
17
18        self.viewer = napari.current_viewer()
19        layout = QVBoxLayout()
20
21        # Create the dropdown to select the segmentation to post-process.
22        self.segmentation_selector_name = "Segmentation"
23        self.segmentation_selector_widget = self._create_layer_selector(
24            self.segmentation_selector_name, layer_type="Labels"
25        )
26        layout.addWidget(self.segmentation_selector_widget)
27
28        # Create dropdown to select the mask for filtering / intersection.
29        self.mask_selector_name = "Mask"
30        self.mask_selector_widget = self._create_layer_selector(self.mask_selector_name, layer_type="Labels")
31        layout.addWidget(self.mask_selector_widget)
32
33        # Create input for label id in the mask.
34        self.mask_id_param, _ = self._add_int_param(
35            "mask_id", 0, min_val=0, max_val=1000, layout=layout, title="Mask ID"
36        )
37
38        # Create text field to choose the name of the output layer.
39        self.output_layer_param, _ = self._add_string_param("output_layer", "", title="Output Layer", layout=layout)
40
41        # First postprocessing option: Filter with mask.
42        self.button1 = QPushButton("Filter")
43        self.button1.clicked.connect(self.on_filter)
44        layout.addWidget(self.button1)
45
46        # Second postprocessing option: intersect with boundary of the mask.
47        self.button2 = QPushButton("Intersect with Boundary")
48        self.button2.clicked.connect(self.on_intersect_boundary)
49        layout.addWidget(self.button2)
50
51        # Third postprocessing option: intersect with the mask.
52        self.button3 = QPushButton("Intersect")
53        self.button3.clicked.connect(self.on_intersect)
54        layout.addWidget(self.button3)
55
56        # Add the widgets to the layout.
57        self.setLayout(layout)
58
59    def _write_pp(self, segmentation):
60        layer_name = self.output_layer_param.text()
61        self.add_or_update_labels(layer_name, segmentation)
62
63    def _conditions_met(self):
64        if self.output_layer_param.text() == "":
65            show_info("Please choose an output layer.")
66            return False
67        return True
68
69    def _get_segmentation_and_mask(self):
70        segmentation = self._get_layer_selector_data(self.segmentation_selector_name).copy()
71        mask = self._get_layer_selector_data(self.mask_selector_name)
72        mask_id = self.mask_id_param.value()
73        if mask_id != 0:
74            mask = (mask == mask_id).astype(mask.dtype)
75        return segmentation, mask
76
77    def on_filter(self):
78        if not self._conditions_met():
79            return
80        segmentation, mask = self._get_segmentation_and_mask()
81        props = regionprops(segmentation, mask)
82        filter_ids = [prop.label for prop in props if prop.intensity_max == 0]
83        segmentation[np.isin(segmentation, filter_ids)] = 0
84        self._write_pp(segmentation)
85
86    def on_intersect_boundary(self):
87        if not self._conditions_met():
88            return
89        segmentation, mask = self._get_segmentation_and_mask()
90        boundary = find_boundaries(mask)
91        segmentation = np.logical_and(segmentation > 0, boundary).astype(segmentation.dtype)
92        self._write_pp(segmentation)
93
94    def on_intersect(self):
95        if not self._conditions_met():
96            return
97        segmentation, mask = self._get_segmentation_and_mask()
98        segmentation = np.logical_and(segmentation > 0, mask > 0).astype(segmentation.dtype)
99        self._write_pp(segmentation)

QWidget(parent: Optional[QWidget] = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags())

viewer
segmentation_selector_name
segmentation_selector_widget
mask_selector_name
mask_selector_widget
button1
button2
button3
def on_filter(self):
77    def on_filter(self):
78        if not self._conditions_met():
79            return
80        segmentation, mask = self._get_segmentation_and_mask()
81        props = regionprops(segmentation, mask)
82        filter_ids = [prop.label for prop in props if prop.intensity_max == 0]
83        segmentation[np.isin(segmentation, filter_ids)] = 0
84        self._write_pp(segmentation)
def on_intersect_boundary(self):
86    def on_intersect_boundary(self):
87        if not self._conditions_met():
88            return
89        segmentation, mask = self._get_segmentation_and_mask()
90        boundary = find_boundaries(mask)
91        segmentation = np.logical_and(segmentation > 0, boundary).astype(segmentation.dtype)
92        self._write_pp(segmentation)
def on_intersect(self):
94    def on_intersect(self):
95        if not self._conditions_met():
96            return
97        segmentation, mask = self._get_segmentation_and_mask()
98        segmentation = np.logical_and(segmentation > 0, mask > 0).astype(segmentation.dtype)
99        self._write_pp(segmentation)