Skip to content

pixano.data.item.item_object

ItemBBox(**data)

Bases: BaseModel

BBox type for DatasetItem

Type for BBox.to_dict()

Attributes:

Name Type Description
coords list[float]

List of coordinates in given format

format str

Coordinates format, 'xyxy' or 'xywh'

is_normalized bool

True if coordinates are normalized to image size

confidence float

Bounding box confidence if predicted

Raises ValidationError if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Source code in pydantic/main.py
def __init__(self, /, **data: Any) -> None:
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.
    """
    # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    __tracebackhide__ = True
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    if self is not validated_self:
        warnings.warn(
            'A custom validator is returning a value other than `self`.\n'
            "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
            'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
            category=None,
        )

from_pyarrow(bbox) staticmethod

Create ItemBBox from bounding box

Parameters:

Name Type Description Default
bbox BBox

Bounding box

required

Returns:

Type Description
ItemBBox

ItemBBox

Source code in pixano/data/item/item_object.py
@staticmethod
def from_pyarrow(
    bbox: BBox,
) -> "ItemBBox":
    """Create ItemBBox from bounding box

    Args:
        bbox (BBox): Bounding box

    Returns:
        ItemBBox: ItemBBox
    """

    return (
        ItemBBox.model_validate(bbox.to_xywh().to_dict())
        if bbox.coords != [0.0, 0.0, 0.0, 0.0]
        else None
    )

to_pyarrow()

Return ItemBbox as BBox

Returns:

Type Description
BBox

Bounding box

Source code in pixano/data/item/item_object.py
def to_pyarrow(self) -> BBox:
    """Return ItemBbox as BBox

    Returns:
        BBox: Bounding box
    """

    return (
        BBox.from_dict(self.model_dump())
        if self.coords != [0.0, 0.0, 0.0, 0.0]
        else None
    )

ItemObject(**data)

Bases: BaseModel

Object type for DatasetItem

Attributes:

Name Type Description
id str

Object ID

original_id str

Object original ID

item_id str

Object item ID

view_id str

Object view ID

source_id str

Object source ID

review_state str

Object review state ("accepted", "rejected", None)

bbox ItemBBox

Object bounding box

mask ItemURLE

Object mask

features dict[str, ItemFeature]

Object features

Raises ValidationError if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Source code in pydantic/main.py
def __init__(self, /, **data: Any) -> None:
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.
    """
    # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    __tracebackhide__ = True
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    if self is not validated_self:
        warnings.warn(
            'A custom validator is returning a value other than `self`.\n'
            "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
            'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
            category=None,
        )

add_or_update(ds_table)

Add or update item object

Parameters:

Name Type Description Default
ds_table LanceTable

Object table

required
Source code in pixano/data/item/item_object.py
def add_or_update(
    self,
    ds_table: lancedb.db.LanceTable,
):
    """Add or update item object

    Args:
        ds_table (lancedb.db.LanceTable): Object table
    """

    # Convert object to PyArrow
    pyarrow_obj = self.to_pyarrow()
    table_obj = pa.Table.from_pylist(
        [pyarrow_obj],
        schema=ds_table.schema,
    )

    # Delete object (if it exists)
    scanner = ds_table.to_lance().scanner(filter=f"id in ('{self.id}')")
    existing_obj = scanner.to_table()
    if existing_obj.num_rows > 0:
        ds_table.delete(f"id in ('{self.id}')")

    # Add object
    ds_table.add(table_obj, mode="append")

    # Clear change history to prevent dataset from becoming too large
    ds_table.to_lance().cleanup_old_versions()

from_pyarrow(table, schema, source_id) staticmethod

Create dictionary of ItemObject from PyArrow Table

Parameters:

Name Type Description Default
table dict[str, Any]

PyArrow table

required
schema schema

PyArrow schema

required
source_id str

Objects source ID

required

Returns:

Type Description
dict[str, ItemObject]

Dictionary of ItemObject

Source code in pixano/data/item/item_object.py
@staticmethod
def from_pyarrow(
    table: pa.Table,
    schema: pa.schema,
    source_id: str,
) -> dict[str, "ItemObject"]:
    """Create dictionary of ItemObject from PyArrow Table

    Args:
        table (dict[str, Any]): PyArrow table
        schema (pa.schema): PyArrow schema
        source_id (str): Objects source ID

    Returns:
        dict[str, ItemObject]: Dictionary of ItemObject
    """

    items = table.to_pylist()
    objects = {}

    # Iterate on objects
    for index, item in enumerate(items):
        # Create object
        obj = ItemObject(
            id=item["id"],
            item_id=item["item_id"],
            view_id=item["view_id"],
            source_id=source_id,
        )

        # Add other base fields (review state, bbox, mask)
        for field in schema:
            if field.name == "review_state" and item["review_state"]:
                obj.review_state = item["review_state"]
            if field.name == "bbox" and item["bbox"]:
                obj.bbox = ItemBBox.from_pyarrow(item["bbox"])
            elif field.name == "mask" and item["mask"]:
                obj.mask = ItemURLE.from_pyarrow(item["mask"])
        # Add features
        obj.features = ItemFeature.from_pyarrow(table.take([index]), schema)
        # Append object
        objects[item["id"]] = obj

    return objects

to_pyarrow()

Return ItemObject in PyArrow format

Returns:

Type Description
dict[str, Any]

Object in PyArrow format

Source code in pixano/data/item/item_object.py
def to_pyarrow(self) -> dict[str, Any]:
    """Return ItemObject in PyArrow format

    Returns:
        dict[str, Any]: Object in PyArrow format
    """

    pyarrow_object = {}

    # IDs
    pyarrow_object["id"] = self.id
    pyarrow_object["item_id"] = self.item_id
    pyarrow_object["view_id"] = self.view_id

    # Base fields (review state, bbox, mask)
    if self.review_state is not None:
        pyarrow_object["review_state"] = self.review_state

    pyarrow_mask = self.mask.to_pyarrow() if self.mask else None
    pyarrow_bbox = (
        self.bbox.to_pyarrow()
        if self.bbox
        else BBox.from_rle(pyarrow_mask) if pyarrow_mask else None
    )

    pyarrow_object["mask"] = pyarrow_mask.to_dict() if pyarrow_mask else None
    pyarrow_object["bbox"] = pyarrow_bbox.to_dict() if pyarrow_bbox else None

    # Features
    if self.features is not None:
        # Add features
        for feat in self.features.values():
            pyarrow_object[feat.name] = (
                field_to_python(feat.dtype)(feat.value)
                if feat.value is not None
                else None
            )

        # Check feature types
        for feat in self.features.values():
            if pyarrow_object[feat.name] is not None and not isinstance(
                pyarrow_object[feat.name], field_to_python(feat.dtype)
            ):
                raise ValueError(
                    f"Feature {feat.name} of object {self.id} is of type {type(self.features[feat.name].value)} instead of type {field_to_python(feat.dtype)}"
                )

    return pyarrow_object

ItemURLE(**data)

Bases: BaseModel

Uncompressed URLE mask type for DatasetItem

Type for CompressedRLE.to_urle()

Attributes:

Name Type Description
size list[int]

Mask size

counts list[int]

Mask URLE encoding

Raises ValidationError if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Source code in pydantic/main.py
def __init__(self, /, **data: Any) -> None:
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.
    """
    # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    __tracebackhide__ = True
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    if self is not validated_self:
        warnings.warn(
            'A custom validator is returning a value other than `self`.\n'
            "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
            'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
            category=None,
        )

from_pyarrow(rle) staticmethod

Create ItemURLE from compressed RLE

Parameters:

Name Type Description Default
rle CompressedRLE

Compressed RLE

required

Returns:

Type Description
ItemURLE

ItemURLE

Source code in pixano/data/item/item_object.py
@staticmethod
def from_pyarrow(
    rle: CompressedRLE,
) -> "ItemURLE":
    """Create ItemURLE from compressed RLE

    Args:
        rle (CompressedRLE): Compressed RLE

    Returns:
        ItemURLE: ItemURLE
    """

    return ItemURLE.model_validate(rle.to_urle()) if rle.counts else None

to_pyarrow()

Return ItemURLE as compressed RLE

Returns:

Type Description
CompressedRLE

Compressed RLE

Source code in pixano/data/item/item_object.py
def to_pyarrow(self) -> CompressedRLE:
    """Return ItemURLE as compressed RLE

    Returns:
        CompressedRLE: Compressed RLE
    """

    return CompressedRLE.from_urle(self.model_dump()) if self.counts else None