Module datatap.droplet.segmentation

Expand source code
from __future__ import annotations

from typing import Optional

from typing_extensions import TypedDict

from ..geometry import Mask, MaskJson
from ..utils import basic_repr

class _SegmentationJsonOptional(TypedDict, total = False):
        confidence: float

class SegmentationJson(_SegmentationJsonOptional, TypedDict):
        """
        The serialized JSON representation of a segmentation.
        """
        mask: MaskJson

class Segmentation:
        """
        A `Segmentation` represents the area within an image taken up by a
        detection, specified as a `Mask`.
        """

        mask: Mask
        """
        The area within the image where the corresponding detection appears.
        """

        confidence: Optional[float]
        """
        The confidence associated with this segmentation.
        """

        @staticmethod
        def from_json(json: SegmentationJson) -> Segmentation:
                """
                Constructs a `Segmentation` from a `SegmentationJson`.
                """
                return Segmentation(
                        Mask.from_json(json["mask"]),
                        confidence = json.get("confidence")
                )

        def __init__(self, mask: Mask, *, confidence: Optional[float] = None):
                self.mask = mask
                self.confidence = confidence

                self.mask.assert_valid()

        def __repr__(self) -> str:
                return basic_repr("Segmentation", self.mask, confidence = self.confidence)

        def __eq__(self, other: Segmentation) -> bool:
                if not isinstance(other, Segmentation): # type: ignore - pyright complains about the isinstance check being redundant
                        return NotImplemented
                return self.mask == other.mask and self.confidence == other.confidence

        def to_json(self) -> SegmentationJson:
                """
                Serializes this `Segmentation` to a `SegmentationJson`.
                """
                json: SegmentationJson = {
                        "mask": self.mask.to_json()
                }

                if self.confidence is not None:
                        json["confidence"] = self.confidence

                return json

        def meets_confidence_threshold(self, threshold: float) -> bool:
                """
                Returns `True` if and only if the confidence of this segmentation is
                either unset or is at least the given `threshold`.
                """
                return self.confidence is None or self.confidence >= threshold

Classes

class Segmentation (mask: Mask, *, confidence: Optional[float] = None)

A Segmentation represents the area within an image taken up by a detection, specified as a Mask.

Expand source code
class Segmentation:
        """
        A `Segmentation` represents the area within an image taken up by a
        detection, specified as a `Mask`.
        """

        mask: Mask
        """
        The area within the image where the corresponding detection appears.
        """

        confidence: Optional[float]
        """
        The confidence associated with this segmentation.
        """

        @staticmethod
        def from_json(json: SegmentationJson) -> Segmentation:
                """
                Constructs a `Segmentation` from a `SegmentationJson`.
                """
                return Segmentation(
                        Mask.from_json(json["mask"]),
                        confidence = json.get("confidence")
                )

        def __init__(self, mask: Mask, *, confidence: Optional[float] = None):
                self.mask = mask
                self.confidence = confidence

                self.mask.assert_valid()

        def __repr__(self) -> str:
                return basic_repr("Segmentation", self.mask, confidence = self.confidence)

        def __eq__(self, other: Segmentation) -> bool:
                if not isinstance(other, Segmentation): # type: ignore - pyright complains about the isinstance check being redundant
                        return NotImplemented
                return self.mask == other.mask and self.confidence == other.confidence

        def to_json(self) -> SegmentationJson:
                """
                Serializes this `Segmentation` to a `SegmentationJson`.
                """
                json: SegmentationJson = {
                        "mask": self.mask.to_json()
                }

                if self.confidence is not None:
                        json["confidence"] = self.confidence

                return json

        def meets_confidence_threshold(self, threshold: float) -> bool:
                """
                Returns `True` if and only if the confidence of this segmentation is
                either unset or is at least the given `threshold`.
                """
                return self.confidence is None or self.confidence >= threshold

Class variables

var confidence : Optional[float]

The confidence associated with this segmentation.

var maskMask

The area within the image where the corresponding detection appears.

Static methods

def from_json(json: SegmentationJson) ‑> Segmentation

Constructs a Segmentation from a SegmentationJson.

Expand source code
@staticmethod
def from_json(json: SegmentationJson) -> Segmentation:
        """
        Constructs a `Segmentation` from a `SegmentationJson`.
        """
        return Segmentation(
                Mask.from_json(json["mask"]),
                confidence = json.get("confidence")
        )

Methods

def meets_confidence_threshold(self, threshold: float) ‑> bool

Returns True if and only if the confidence of this segmentation is either unset or is at least the given threshold.

Expand source code
def meets_confidence_threshold(self, threshold: float) -> bool:
        """
        Returns `True` if and only if the confidence of this segmentation is
        either unset or is at least the given `threshold`.
        """
        return self.confidence is None or self.confidence >= threshold
def to_json(self) ‑> SegmentationJson

Serializes this Segmentation to a SegmentationJson.

Expand source code
def to_json(self) -> SegmentationJson:
        """
        Serializes this `Segmentation` to a `SegmentationJson`.
        """
        json: SegmentationJson = {
                "mask": self.mask.to_json()
        }

        if self.confidence is not None:
                json["confidence"] = self.confidence

        return json
class SegmentationJson (*args, **kwargs)

The serialized JSON representation of a segmentation.

Expand source code
class SegmentationJson(_SegmentationJsonOptional, TypedDict):
        """
        The serialized JSON representation of a segmentation.
        """
        mask: MaskJson

Ancestors

  • builtins.dict

Class variables

var confidence : float
var mask : Sequence[Sequence[Tuple[float, float]]]