Skip to content

napari_cellseg3d.code_plugins.plugin_utilities

Central plugin for all utilities.

Classes:

Name Description
Utilities

Central plugin for all utilities.

Utilities

Bases: QWidget

Central plugin for all utilities.

Methods:

Name Description
__init__

Creates a widget with all utilities.

Source code in napari_cellseg3d/code_plugins/plugin_utilities.py
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class Utilities(QWidget, metaclass=ui.QWidgetSingleton):
    """Central plugin for all utilities."""

    def __init__(self, viewer: "napari.viewer.Viewer"):
        """Creates a widget with all utilities."""
        super().__init__()
        self._viewer = viewer
        self.current_widget = None

        attr_names = [
            "crop",
            "frag",
            "aniso",
            "small",
            "inst",
            "sem",
            "thresh",
            "crf",
            "stats",
            "artifacts",
            "find_thresh",
        ]
        self._create_utils_widgets(attr_names)
        self.utils_choice = ui.DropdownMenu(
            UTILITIES_WIDGETS.keys(), text_label="Utilities"
        )

        self._build()

        self.utils_choice.currentIndexChanged.connect(self._update_visibility)
        self.utils_choice.currentIndexChanged.connect(self._update_current_widget)
        # self._dock_util()
        self._update_visibility()
        qInstallMessageHandler(ui.handle_adjust_errors_wrapper(self))

    def _update_current_widget(self):
        self.current_widget = self.utils_widgets[self.utils_choice.currentIndex()]

    def _update_results_path(self, widget):
        self.results_filewidget.text_field.setText(str(widget.save_path))

    def _build(self):
        layout = QVBoxLayout()
        ui.add_widgets(layout, self.utils_widgets)
        ui.GroupedWidget.create_single_widget_group(
            "Utilities",
            widget=self.utils_choice,
            layout=layout,
            alignment=ui.BOTT_AL,
        )

        # layout.addWidget(self.utils_choice.label, alignment=ui.BOTT_AL)
        # layout.addWidget(self.utils_choice, alignment=ui.BOTT_AL)

        # layout.setSizeConstraint(QLayout.SetFixedSize)
        self.setLayout(layout)
        # self.setMinimumHeight(2000)
        self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
        self._update_visibility()

    def _create_utils_widgets(self, names):
        for key, name in zip(UTILITIES_WIDGETS, names, strict=False):
            logger.debug(f"Creating {name} widget")
            setattr(self, name, UTILITIES_WIDGETS[key](self._viewer))

        self.utils_widgets = []
        for n in names:
            wid = getattr(self, n)
            self.utils_widgets.append(wid)

        self.current_widget = self.utils_widgets[0]
        if len(self.utils_widgets) != len(UTILITIES_WIDGETS.keys()):
            raise RuntimeError("One or several utility widgets are missing/erroneous")

    def _update_layers(self, current_loader, new_loader):
        current_layer = current_loader.layer()
        if not isinstance(current_layer, new_loader.layer_type):
            return
        if (
            current_layer is not None
            and current_layer.name in new_loader.layer_list.get_items()
        ):
            index = new_loader.layer_list.get_items().index(current_layer.name)
            logger.debug(f"Index of layer {current_layer.name} in new loader : {index}")
            new_loader.layer_list.setCurrentIndex(index)

    def _update_fields(self, widget: BasePluginUtils):
        try:
            # checks all combinations to find if a layer could be recovered across widgets
            # correctness is ensured by the types of the layer loaders
            self._update_layers(
                self.current_widget.image_layer_loader,
                widget.image_layer_loader,
            )
            self._update_layers(
                self.current_widget.image_layer_loader,
                widget.label_layer_loader,
            )
            self._update_layers(
                self.current_widget.label_layer_loader,
                widget.image_layer_loader,
            )
            self._update_layers(
                self.current_widget.label_layer_loader,
                widget.label_layer_loader,
            )
        except KeyError:
            pass

        logger.debug(f"Current widget save path : {self.current_widget.save_path}")
        logger.debug(
            f"Current widget text field : {self.current_widget.results_filewidget.text_field.text()}"
        )
        logger.debug(
            f"Matching : {self.current_widget.results_filewidget.text_field.text() == self.current_widget.results_path}"
        )
        if len(self.current_widget.utils_default_paths) > 1:
            try:
                path = self.current_widget.utils_default_paths
                default = utils.parse_default_path(path)
                widget.results_filewidget.text_field.setText(default)
                widget.utils_default_paths.append(default)
            except AttributeError:
                pass

    def _update_visibility(self):
        widget_class = UTILITIES_WIDGETS[self.utils_choice.currentText()]
        # print("vis. updated")
        # print(self.utils_widgets)
        self._hide_all()
        widget = None
        for _i, w in enumerate(self.utils_widgets):
            if isinstance(w, widget_class):
                w.setVisible(True)
                w.adjustSize()
                widget = w
            # else:
            #     self.utils_widgets[i].setVisible(False)
        self._update_fields(widget)
        self.current_widget = widget

    def _hide_all(self):
        for w in self.utils_widgets:
            w.setVisible(False)

__init__

__init__(viewer: Viewer)

Creates a widget with all utilities.

Source code in napari_cellseg3d/code_plugins/plugin_utilities.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def __init__(self, viewer: "napari.viewer.Viewer"):
    """Creates a widget with all utilities."""
    super().__init__()
    self._viewer = viewer
    self.current_widget = None

    attr_names = [
        "crop",
        "frag",
        "aniso",
        "small",
        "inst",
        "sem",
        "thresh",
        "crf",
        "stats",
        "artifacts",
        "find_thresh",
    ]
    self._create_utils_widgets(attr_names)
    self.utils_choice = ui.DropdownMenu(
        UTILITIES_WIDGETS.keys(), text_label="Utilities"
    )

    self._build()

    self.utils_choice.currentIndexChanged.connect(self._update_visibility)
    self.utils_choice.currentIndexChanged.connect(self._update_current_widget)
    # self._dock_util()
    self._update_visibility()
    qInstallMessageHandler(ui.handle_adjust_errors_wrapper(self))