Skip to content

pixano.data.item.item_feature

FeatureValues(**data)

Bases: BaseModel

Feature available values, as a restricted list or open list

Attributes:

Name Type Description
restricted bool

restricted list or open list

values list[str | int]

list of available values

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,
        )

FeaturesValues(**data)

Bases: BaseModel

Features availables values

Attributes:

Name Type Description
main dict[str, FeatureValues]

Scene features available values ("main" table)

objects dict[str, FeatureValues]

Objects features available values

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,
        )

ItemFeature(**data)

Bases: BaseModel

Feature

Attributes:

Name Type Description
name str

Feature name

dtype str

Feature type

value str | int | float | bool

Feature value

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(table, schema) staticmethod

Create dictionary of ItemFeature from PyArrow Table

Parameters:

Name Type Description Default
table Table

PyArrow table

required
schema schema

PyArrow schema

required

Returns:

Type Description
dict[str, ItemFeature]

Dictionary of ItemFeature

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

    Args:
        table (pa.Table): PyArrow table
        schema (pa.schema): PyArrow schema

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

    item = table.to_pylist()[0]
    features = {}
    ignored_fields = [
        "id",
        "item_id",
        "view_id",
        "source_id",
        "split",
        "review_state",
    ]

    # Iterate on fields
    for field in schema:
        if field.name not in ignored_fields:
            # Integer fields
            if is_integer(field.type):
                features[field.name] = ItemFeature(
                    name=field.name,
                    dtype="int",
                    value=item[field.name],
                )

            # Float fields
            if is_float(field.type):
                # Parse float value from string
                # (Float conversions from PyArrow to Python can currently add a lot of random decimal places)
                value_as_string: str = table[field.name].to_string()
                value_as_string = (
                    value_as_string.replace("[", "").replace("]", "").strip()
                )
                try:
                    features[field.name] = ItemFeature(
                        name=field.name,
                        dtype="float",
                        value=float(value_as_string),
                    )
                except ValueError:
                    features[field.name] = ItemFeature(
                        name=field.name,
                        dtype="float",
                        value=float(item[field.name]),
                    )

            # String fields
            elif is_string(field.type):
                features[field.name] = ItemFeature(
                    name=field.name,
                    dtype="str",
                    value=str(item[field.name]),
                )

            # Boolean fields
            elif is_boolean(field.type):
                features[field.name] = ItemFeature(
                    name=field.name,
                    dtype="bool",
                    value=bool(item[field.name]),
                )

    # Additional distance field in case of semantic search
    for field_name in item.keys():
        if field_name == "distance":
            features["search distance"] = ItemFeature(
                name="search distance",
                dtype="float",
                value=round(item[field_name], 2),
            )

    return features