Skip to content

pn2v_algorithm_config

PN2V Algorithm configuration.

PN2VAlgorithm #

Bases: UNetBasedAlgorithm

PN2V Algorithm configuration.

Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
class PN2VAlgorithm(UNetBasedAlgorithm):
    """PN2V Algorithm configuration."""

    model_config = ConfigDict(validate_assignment=True)

    algorithm: Literal["pn2v"] = "pn2v"
    """PN2V Algorithm name."""

    loss: Literal["pn2v"] = "pn2v"
    """PN2V loss function (uses N2V loss with noise model)."""

    n2v_config: N2VManipulateConfig = N2VManipulateConfig()

    noise_model: GaussianMixtureNMConfig
    """Noise model configuration for probabilistic denoising."""

    model: Annotated[
        UNetConfig,
        # AfterValidator(model_matching_in_out_channels),
        # TODO for pn2v channel handling needs to be changed
        AfterValidator(model_without_final_activation),
    ]

    @model_validator(mode="after")
    def validate_n2v2(self) -> Self:
        """Validate that the N2V2 strategy and models are set correctly.

        Returns
        -------
        Self
            The validated configuration.

        Raises
        ------
        ValueError
            If N2V2 is used with the wrong pixel manipulation strategy.
        """
        if self.model.n2v2:
            if self.n2v_config.strategy != SupportedPixelManipulation.MEDIAN.value:
                raise ValueError(
                    f"PN2V2 can only be used with the "
                    f"{SupportedPixelManipulation.MEDIAN} pixel manipulation strategy. "
                    f"Change the `strategy` parameters in `n2v_config` to "
                    f"{SupportedPixelManipulation.MEDIAN}."
                )
        else:
            if self.n2v_config.strategy != SupportedPixelManipulation.UNIFORM.value:
                raise ValueError(
                    f"PN2V can only be used with the "
                    f"{SupportedPixelManipulation.UNIFORM} pixel manipulation strategy."
                    f" Change the `strategy` parameters in `n2v_config` to "
                    f"{SupportedPixelManipulation.UNIFORM}."
                )
        return self

    def set_n2v2(self, use_n2v2: bool) -> None:
        """
        Set the configuration to use PN2V2 or the vanilla Probabilistic Noise2Void.

        This method ensures that PN2V2 is set correctly and remain coherent, as opposed
        to setting the different parameters individually.

        Parameters
        ----------
        use_n2v2 : bool
            Whether to use PN2V2.
        """
        if use_n2v2:
            self.n2v_config.strategy = SupportedPixelManipulation.MEDIAN.value
            self.model.n2v2 = True
        else:
            self.n2v_config.strategy = SupportedPixelManipulation.UNIFORM.value
            self.model.n2v2 = False

    def is_struct_n2v(self) -> bool:
        """Check if the configuration is using structPN2V.

        Returns
        -------
        bool
            Whether the configuration is using structPN2V.
        """
        return self.n2v_config.struct_mask_axis != SupportedStructAxis.NONE.value

    def get_algorithm_friendly_name(self) -> str:
        """
        Get the friendly name of the algorithm.

        Returns
        -------
        str
            Friendly name.
        """
        use_n2v2 = self.model.n2v2
        use_structN2V = self.is_struct_n2v()

        if use_n2v2 and use_structN2V:
            return STRUCT_PN2V2
        elif use_n2v2:
            return PN2V2
        elif use_structN2V:
            return STRUCT_PN2V
        else:
            return PN2V

    def get_algorithm_keywords(self) -> list[str]:
        """
        Get algorithm keywords.

        Returns
        -------
        list[str]
            List of keywords.
        """
        use_n2v2 = self.model.n2v2
        use_structN2V = self.is_struct_n2v()

        keywords = [
            "denoising",
            "restoration",
            "UNet",
            "3D" if self.model.is_3D() else "2D",
            "CAREamics",
            "pytorch",
            PN2V,
            "probabilistic",
            "noise model",
        ]

        if use_n2v2:
            keywords.append(PN2V2)
        if use_structN2V:
            keywords.append(STRUCT_PN2V)

        return keywords

    def get_algorithm_references(self) -> str:
        """
        Get the algorithm references.

        This is used to generate the README of the BioImage Model Zoo export.

        Returns
        -------
        str
            Algorithm references.
        """
        use_n2v2 = self.model.n2v2
        use_structN2V = self.is_struct_n2v()

        references = [
            PN2V_REF.text + " doi: " + PN2V_REF.doi,
            N2V2_REF.text + " doi: " + N2V2_REF.doi,
            STRUCTN2V_REF.text + " doi: " + STRUCTN2V_REF.doi,
        ]

        # return the (struct)PN2V(2) references
        if use_n2v2 and use_structN2V:
            return "\n".join(references)
        elif use_n2v2:
            references.pop(-1)
            return "\n".join(references)
        elif use_structN2V:
            references.pop(-2)
            return "\n".join(references)
        else:
            return references[0]

    def get_algorithm_citations(self) -> list[CiteEntry]:
        """
        Return a list of citation entries of the current algorithm.

        This is used to generate the model description for the BioImage Model Zoo.

        Returns
        -------
        List[CiteEntry]
            List of citation entries.
        """
        use_n2v2 = self.model.n2v2
        use_structN2V = self.is_struct_n2v()

        references = [PN2V_REF]

        if use_n2v2:
            references.append(N2V2_REF)

        if use_structN2V:
            references.append(STRUCTN2V_REF)

        return references

    def get_algorithm_description(self) -> str:
        """
        Return a description of the algorithm.

        This method is used to generate the README of the BioImage Model Zoo export.

        Returns
        -------
        str
            Description of the algorithm.
        """
        use_n2v2 = self.model.n2v2
        use_structN2V = self.is_struct_n2v()

        if use_n2v2 and use_structN2V:
            return STRUCT_PN2V2_DESCRIPTION
        elif use_n2v2:
            return PN2V2_DESCRIPTION
        elif use_structN2V:
            return STRUCT_PN2V_DESCRIPTION
        else:
            return PN2V_DESCRIPTION

algorithm = 'pn2v' class-attribute instance-attribute #

PN2V Algorithm name.

loss = 'pn2v' class-attribute instance-attribute #

PN2V loss function (uses N2V loss with noise model).

noise_model instance-attribute #

Noise model configuration for probabilistic denoising.

get_algorithm_citations() #

Return a list of citation entries of the current algorithm.

This is used to generate the model description for the BioImage Model Zoo.

Returns:

Type Description
List[CiteEntry]

List of citation entries.

Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
def get_algorithm_citations(self) -> list[CiteEntry]:
    """
    Return a list of citation entries of the current algorithm.

    This is used to generate the model description for the BioImage Model Zoo.

    Returns
    -------
    List[CiteEntry]
        List of citation entries.
    """
    use_n2v2 = self.model.n2v2
    use_structN2V = self.is_struct_n2v()

    references = [PN2V_REF]

    if use_n2v2:
        references.append(N2V2_REF)

    if use_structN2V:
        references.append(STRUCTN2V_REF)

    return references

get_algorithm_description() #

Return a description of the algorithm.

This method is used to generate the README of the BioImage Model Zoo export.

Returns:

Type Description
str

Description of the algorithm.

Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
def get_algorithm_description(self) -> str:
    """
    Return a description of the algorithm.

    This method is used to generate the README of the BioImage Model Zoo export.

    Returns
    -------
    str
        Description of the algorithm.
    """
    use_n2v2 = self.model.n2v2
    use_structN2V = self.is_struct_n2v()

    if use_n2v2 and use_structN2V:
        return STRUCT_PN2V2_DESCRIPTION
    elif use_n2v2:
        return PN2V2_DESCRIPTION
    elif use_structN2V:
        return STRUCT_PN2V_DESCRIPTION
    else:
        return PN2V_DESCRIPTION

get_algorithm_friendly_name() #

Get the friendly name of the algorithm.

Returns:

Type Description
str

Friendly name.

Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
def get_algorithm_friendly_name(self) -> str:
    """
    Get the friendly name of the algorithm.

    Returns
    -------
    str
        Friendly name.
    """
    use_n2v2 = self.model.n2v2
    use_structN2V = self.is_struct_n2v()

    if use_n2v2 and use_structN2V:
        return STRUCT_PN2V2
    elif use_n2v2:
        return PN2V2
    elif use_structN2V:
        return STRUCT_PN2V
    else:
        return PN2V

get_algorithm_keywords() #

Get algorithm keywords.

Returns:

Type Description
list[str]

List of keywords.

Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
def get_algorithm_keywords(self) -> list[str]:
    """
    Get algorithm keywords.

    Returns
    -------
    list[str]
        List of keywords.
    """
    use_n2v2 = self.model.n2v2
    use_structN2V = self.is_struct_n2v()

    keywords = [
        "denoising",
        "restoration",
        "UNet",
        "3D" if self.model.is_3D() else "2D",
        "CAREamics",
        "pytorch",
        PN2V,
        "probabilistic",
        "noise model",
    ]

    if use_n2v2:
        keywords.append(PN2V2)
    if use_structN2V:
        keywords.append(STRUCT_PN2V)

    return keywords

get_algorithm_references() #

Get the algorithm references.

This is used to generate the README of the BioImage Model Zoo export.

Returns:

Type Description
str

Algorithm references.

Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
def get_algorithm_references(self) -> str:
    """
    Get the algorithm references.

    This is used to generate the README of the BioImage Model Zoo export.

    Returns
    -------
    str
        Algorithm references.
    """
    use_n2v2 = self.model.n2v2
    use_structN2V = self.is_struct_n2v()

    references = [
        PN2V_REF.text + " doi: " + PN2V_REF.doi,
        N2V2_REF.text + " doi: " + N2V2_REF.doi,
        STRUCTN2V_REF.text + " doi: " + STRUCTN2V_REF.doi,
    ]

    # return the (struct)PN2V(2) references
    if use_n2v2 and use_structN2V:
        return "\n".join(references)
    elif use_n2v2:
        references.pop(-1)
        return "\n".join(references)
    elif use_structN2V:
        references.pop(-2)
        return "\n".join(references)
    else:
        return references[0]

is_struct_n2v() #

Check if the configuration is using structPN2V.

Returns:

Type Description
bool

Whether the configuration is using structPN2V.

Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
def is_struct_n2v(self) -> bool:
    """Check if the configuration is using structPN2V.

    Returns
    -------
    bool
        Whether the configuration is using structPN2V.
    """
    return self.n2v_config.struct_mask_axis != SupportedStructAxis.NONE.value

set_n2v2(use_n2v2) #

Set the configuration to use PN2V2 or the vanilla Probabilistic Noise2Void.

This method ensures that PN2V2 is set correctly and remain coherent, as opposed to setting the different parameters individually.

Parameters:

Name Type Description Default
use_n2v2 bool

Whether to use PN2V2.

required
Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
def set_n2v2(self, use_n2v2: bool) -> None:
    """
    Set the configuration to use PN2V2 or the vanilla Probabilistic Noise2Void.

    This method ensures that PN2V2 is set correctly and remain coherent, as opposed
    to setting the different parameters individually.

    Parameters
    ----------
    use_n2v2 : bool
        Whether to use PN2V2.
    """
    if use_n2v2:
        self.n2v_config.strategy = SupportedPixelManipulation.MEDIAN.value
        self.model.n2v2 = True
    else:
        self.n2v_config.strategy = SupportedPixelManipulation.UNIFORM.value
        self.model.n2v2 = False

validate_n2v2() #

Validate that the N2V2 strategy and models are set correctly.

Returns:

Type Description
Self

The validated configuration.

Raises:

Type Description
ValueError

If N2V2 is used with the wrong pixel manipulation strategy.

Source code in src/careamics/config/algorithms/pn2v_algorithm_config.py
@model_validator(mode="after")
def validate_n2v2(self) -> Self:
    """Validate that the N2V2 strategy and models are set correctly.

    Returns
    -------
    Self
        The validated configuration.

    Raises
    ------
    ValueError
        If N2V2 is used with the wrong pixel manipulation strategy.
    """
    if self.model.n2v2:
        if self.n2v_config.strategy != SupportedPixelManipulation.MEDIAN.value:
            raise ValueError(
                f"PN2V2 can only be used with the "
                f"{SupportedPixelManipulation.MEDIAN} pixel manipulation strategy. "
                f"Change the `strategy` parameters in `n2v_config` to "
                f"{SupportedPixelManipulation.MEDIAN}."
            )
    else:
        if self.n2v_config.strategy != SupportedPixelManipulation.UNIFORM.value:
            raise ValueError(
                f"PN2V can only be used with the "
                f"{SupportedPixelManipulation.UNIFORM} pixel manipulation strategy."
                f" Change the `strategy` parameters in `n2v_config` to "
                f"{SupportedPixelManipulation.UNIFORM}."
            )
    return self