Skip to content

n2v_algorithm_model

"N2V Algorithm configuration.

N2VAlgorithm #

Bases: UNetBasedAlgorithm

N2V Algorithm configuration.

Source code in src/careamics/config/algorithms/n2v_algorithm_model.py
class N2VAlgorithm(UNetBasedAlgorithm):
    """N2V Algorithm configuration."""

    model_config = ConfigDict(validate_assignment=True)

    algorithm: Literal["n2v"] = "n2v"
    """N2V Algorithm name."""

    loss: Literal["n2v"] = "n2v"
    """N2V loss function."""

    n2v_config: N2VManipulateModel = N2VManipulateModel()

    model: Annotated[
        UNetModel,
        AfterValidator(model_matching_in_out_channels),
        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 validateed 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"N2V2 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"N2V 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 N2V2 or the vanilla Noise2Void.

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

        Parameters
        ----------
        use_n2v2 : bool
            Whether to use N2V2.
        """
        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 structN2V.

        Returns
        -------
        bool
            Whether the configuration is using structN2V.
        """
        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_N2V2
        elif use_n2v2:
            return N2V2
        elif use_structN2V:
            return STRUCT_N2V
        else:
            return N2V

    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",
            N2V,
        ]

        if use_n2v2:
            keywords.append(N2V2)
        if use_structN2V:
            keywords.append(STRUCT_N2V)

        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 = [
            N2V_REF.text + " doi: " + N2V_REF.doi,
            N2V2_REF.text + " doi: " + N2V2_REF.doi,
            STRUCTN2V_REF.text + " doi: " + STRUCTN2V_REF.doi,
        ]

        # return the (struct)N2V(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 = [N2V_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 STR_N2V2_DESCRIPTION
        elif use_n2v2:
            return N2V2_DESCRIPTION
        elif use_structN2V:
            return STR_N2V_DESCRIPTION
        else:
            return N2V_DESCRIPTION

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

N2V Algorithm name.

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

N2V loss function.

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/n2v_algorithm_model.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 = [N2V_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/n2v_algorithm_model.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 STR_N2V2_DESCRIPTION
    elif use_n2v2:
        return N2V2_DESCRIPTION
    elif use_structN2V:
        return STR_N2V_DESCRIPTION
    else:
        return N2V_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/n2v_algorithm_model.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_N2V2
    elif use_n2v2:
        return N2V2
    elif use_structN2V:
        return STRUCT_N2V
    else:
        return N2V

get_algorithm_keywords() #

Get algorithm keywords.

Returns:

Type Description
list[str]

List of keywords.

Source code in src/careamics/config/algorithms/n2v_algorithm_model.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",
        N2V,
    ]

    if use_n2v2:
        keywords.append(N2V2)
    if use_structN2V:
        keywords.append(STRUCT_N2V)

    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/n2v_algorithm_model.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 = [
        N2V_REF.text + " doi: " + N2V_REF.doi,
        N2V2_REF.text + " doi: " + N2V2_REF.doi,
        STRUCTN2V_REF.text + " doi: " + STRUCTN2V_REF.doi,
    ]

    # return the (struct)N2V(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 structN2V.

Returns:

Type Description
bool

Whether the configuration is using structN2V.

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

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

set_n2v2(use_n2v2) #

Set the configuration to use N2V2 or the vanilla Noise2Void.

This method ensures that N2V2 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 N2V2.

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

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

    Parameters
    ----------
    use_n2v2 : bool
        Whether to use N2V2.
    """
    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 validateed configuration.

Raises:

Type Description
ValueError

If N2V2 is used with the wrong pixel manipulation strategy.

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

    Returns
    -------
    Self
        The validateed 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"N2V2 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"N2V 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