Skip to content

n2n_config_window

N2NConfigurationWindow #

Bases: AdvancedConfigurationWindow

A dialog widget for modifying N2N advanced settings.

Source code in src/careamics_napari/widgets/n2n_config_window.py
class N2NConfigurationWindow(AdvancedConfigurationWindow):
    """A dialog widget for modifying N2N advanced settings."""

    def __init__(
        self,
        parent: QWidget | None,
        careamics_config: BaseConfig,
        algorithm_config: N2NAdvancedConfig,
    ) -> None:
        """Initialize the window.

        Parameters
        ----------
        parent : QWidget | None
            Parent widget.
        careamics_config : BaseConfig
            Careamics configuration object.
        algorithm_config : N2NAdvancedConfig
            N2N advanced configuration object.
        """
        super().__init__(parent, careamics_config, algorithm_config)

        self.advanced_configuration = algorithm_config

        self.add_algorithm_specific_tab()

        self.bind_properties()

    def add_algorithm_specific_tab(self) -> None:
        """Add algorithm specific advanced settings tab."""
        # tab widget
        tab_widget = QWidget()

        # number of input channels
        _n_channels = self.advanced_configuration.n_channels_in or 1
        self.num_channels_in_spin = create_int_spinbox(1, 10, _n_channels, 1)
        self.num_channels_in_spin.setEnabled(
            "C" in self.configuration.data_config.axes  # type: ignore
        )
        self.num_channels_in_spin.setToolTip(
            "Number of input channels of the input image (C must be in axes)."
        )

        # number of output channels
        _n_channels = self.advanced_configuration.n_channels_out or 1
        self.num_channels_out_spin = create_int_spinbox(1, 10, _n_channels, 1)
        # self.num_channels_out_spin.setEnabled(
        #     "C" in self.configuration.data_config.axes  # type: ignore
        # )
        self.num_channels_out_spin.setToolTip("Number of output channels.")

        # layout
        layout = QVBoxLayout()
        form = QFormLayout()
        form.setFormAlignment(Qt.AlignLeft | Qt.AlignTop)  # type: ignore
        form.setFieldGrowthPolicy(
            QFormLayout.AllNonFixedFieldsGrow  # type: ignore
        )
        form.addRow("Number of Input Channels:", self.num_channels_in_spin)
        form.addRow("Number of Output Channels:", self.num_channels_out_spin)
        layout.addLayout(form)

        tab_widget.setLayout(layout)
        self.tabs.addTab(tab_widget, "N2N")

    def save(self) -> None:
        """Save the current state of the UI into configurations."""
        super().update_config()
        self.update_config()
        self.close()

    def bind_properties(self) -> None:
        """Create and bind the properties to the UI elements."""
        # bind the properties from the base class first
        super().bind_properties()
        # type(self) returns the class of the instance, so we are adding
        # properties to the class itself, not the instance.
        type(self).in_channels = bind(self.num_channels_in_spin, "value")
        type(self).out_channels = bind(self.num_channels_out_spin, "value")

    def update_config(self) -> None:
        """Update the configuration object from UI elements."""
        self.advanced_configuration.n_channels_in = self.in_channels
        self.advanced_configuration.n_channels_out = self.out_channels

        if isinstance(self.configuration.algorithm_config, CAREAlgorithm):
            self.configuration.algorithm_config.model.in_channels = self.in_channels
            self.configuration.algorithm_config.model.num_classes = self.out_channels

__init__(parent, careamics_config, algorithm_config) #

Initialize the window.

Parameters:

Name Type Description Default
parent QWidget | None

Parent widget.

required
careamics_config BaseConfig

Careamics configuration object.

required
algorithm_config N2NAdvancedConfig

N2N advanced configuration object.

required
Source code in src/careamics_napari/widgets/n2n_config_window.py
def __init__(
    self,
    parent: QWidget | None,
    careamics_config: BaseConfig,
    algorithm_config: N2NAdvancedConfig,
) -> None:
    """Initialize the window.

    Parameters
    ----------
    parent : QWidget | None
        Parent widget.
    careamics_config : BaseConfig
        Careamics configuration object.
    algorithm_config : N2NAdvancedConfig
        N2N advanced configuration object.
    """
    super().__init__(parent, careamics_config, algorithm_config)

    self.advanced_configuration = algorithm_config

    self.add_algorithm_specific_tab()

    self.bind_properties()

add_algorithm_specific_tab() #

Add algorithm specific advanced settings tab.

Source code in src/careamics_napari/widgets/n2n_config_window.py
def add_algorithm_specific_tab(self) -> None:
    """Add algorithm specific advanced settings tab."""
    # tab widget
    tab_widget = QWidget()

    # number of input channels
    _n_channels = self.advanced_configuration.n_channels_in or 1
    self.num_channels_in_spin = create_int_spinbox(1, 10, _n_channels, 1)
    self.num_channels_in_spin.setEnabled(
        "C" in self.configuration.data_config.axes  # type: ignore
    )
    self.num_channels_in_spin.setToolTip(
        "Number of input channels of the input image (C must be in axes)."
    )

    # number of output channels
    _n_channels = self.advanced_configuration.n_channels_out or 1
    self.num_channels_out_spin = create_int_spinbox(1, 10, _n_channels, 1)
    # self.num_channels_out_spin.setEnabled(
    #     "C" in self.configuration.data_config.axes  # type: ignore
    # )
    self.num_channels_out_spin.setToolTip("Number of output channels.")

    # layout
    layout = QVBoxLayout()
    form = QFormLayout()
    form.setFormAlignment(Qt.AlignLeft | Qt.AlignTop)  # type: ignore
    form.setFieldGrowthPolicy(
        QFormLayout.AllNonFixedFieldsGrow  # type: ignore
    )
    form.addRow("Number of Input Channels:", self.num_channels_in_spin)
    form.addRow("Number of Output Channels:", self.num_channels_out_spin)
    layout.addLayout(form)

    tab_widget.setLayout(layout)
    self.tabs.addTab(tab_widget, "N2N")

bind_properties() #

Create and bind the properties to the UI elements.

Source code in src/careamics_napari/widgets/n2n_config_window.py
def bind_properties(self) -> None:
    """Create and bind the properties to the UI elements."""
    # bind the properties from the base class first
    super().bind_properties()
    # type(self) returns the class of the instance, so we are adding
    # properties to the class itself, not the instance.
    type(self).in_channels = bind(self.num_channels_in_spin, "value")
    type(self).out_channels = bind(self.num_channels_out_spin, "value")

save() #

Save the current state of the UI into configurations.

Source code in src/careamics_napari/widgets/n2n_config_window.py
def save(self) -> None:
    """Save the current state of the UI into configurations."""
    super().update_config()
    self.update_config()
    self.close()

update_config() #

Update the configuration object from UI elements.

Source code in src/careamics_napari/widgets/n2n_config_window.py
def update_config(self) -> None:
    """Update the configuration object from UI elements."""
    self.advanced_configuration.n_channels_in = self.in_channels
    self.advanced_configuration.n_channels_out = self.out_channels

    if isinstance(self.configuration.algorithm_config, CAREAlgorithm):
        self.configuration.algorithm_config.model.in_channels = self.in_channels
        self.configuration.algorithm_config.model.num_classes = self.out_channels