Skip to content

pixano.app.models.dataset_items

DatasetItemModel(**data)

Bases: BaseModel

DatasetItem model.

It represents a dataset item with its associated entities, annotations and views.

The mappings consist of the table name as key and the corresponding model or list of models as value.

Attributes:

Name Type Description
id str

The dataset item id.

item ItemModel

The item model.

entities dict[str, list[EntityModel] | EntityModel | None]

The entities models mapping.

annotations dict[str, list[AnnotationModel] | AnnotationModel | None]

The annotations models mapping.

views dict[str, list[ViewModel] | ViewModel | None]

The views models mapping.

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.',
            stacklevel=2,
        )

from_dataset_item(dataset_item, dataset_schema) classmethod

Create a model from a DatasetItem.

Parameters:

Name Type Description Default
dataset_item DatasetItem

The dataset item to create the model from.

required
dataset_schema DatasetSchema

The schema of the dataset containing the dataset item.

required

Returns:

Type Description
Self

The created model.

Source code in pixano/app/models/dataset_items.py
@classmethod
def from_dataset_item(cls, dataset_item: DatasetItem, dataset_schema: DatasetSchema) -> Self:
    """Create a model from a [DatasetItem][pixano.datasets.DatasetItem].

    Args:
        dataset_item: The dataset item to create the model from.
        dataset_schema: The schema of the dataset containing the dataset item.

    Returns:
        The created model.
    """

    def _row_or_rows_to_model_or_models(
        row_or_rows: BaseSchema | list[BaseSchema], name: str, group: SchemaGroup, model: type[BaseSchemaModel]
    ) -> BaseSchemaModel | list[BaseSchemaModel]:
        base_schema = get_super_type_from_dict(
            type(row_or_rows[0]) if isinstance(row_or_rows, list) else type(row_or_rows), _PIXANO_SCHEMA_REGISTRY
        )
        if base_schema is None:
            raise ValueError(f"Unsupported schema type {type(row_or_rows)}")
        table_info = TableInfo(name=name, group=group.value, base_schema=base_schema.__name__)
        if isinstance(row_or_rows, list):
            return model.from_rows(row_or_rows, table_info=table_info)
        else:
            return model.from_row(row_or_rows, table_info=table_info)

    model_dict: dict[str, Any] = {
        "entities": {},
        "annotations": {},
        "views": {},
    }
    for key, value in dataset_item.to_schemas_data(dataset_schema).items():
        if value is None or value == []:
            if issubclass(dataset_schema.schemas[key], View):
                model_dict["views"][key] = None if value is None else []
            elif issubclass(dataset_schema.schemas[key], Entity):
                model_dict["entities"][key] = None if value is None else []
            elif issubclass(dataset_schema.schemas[key], Annotation):
                model_dict["annotations"][key] = None if value is None else []
            else:
                raise ValueError(f"Unsupported schema type {type(value)}")
        elif isinstance(value, Item) or isinstance(value, list) and isinstance(value[0], Item):
            model_dict[key] = _row_or_rows_to_model_or_models(value, key, SchemaGroup.ITEM, ItemModel)
        elif isinstance(value, Annotation) or isinstance(value, list) and isinstance(value[0], Annotation):
            model_dict["annotations"][key] = _row_or_rows_to_model_or_models(
                value, key, SchemaGroup.ANNOTATION, AnnotationModel
            )
        elif isinstance(value, Entity) or isinstance(value, list) and isinstance(value[0], Entity):
            model_dict["entities"][key] = _row_or_rows_to_model_or_models(
                value, key, SchemaGroup.ENTITY, EntityModel
            )
        elif isinstance(value, View) or isinstance(value, list) and isinstance(value[0], View):
            model_dict["views"][key] = _row_or_rows_to_model_or_models(value, key, SchemaGroup.VIEW, ViewModel)
        else:
            raise ValueError(f"Unsupported schema type {type(value)}")
    model_dict["id"] = dataset_item.id
    return cls.model_validate(model_dict)

from_dataset_items(dataset_items, dataset_schema) classmethod

Create a list of models from a list of DatasetItems.

Parameters:

Name Type Description Default
dataset_items list[DatasetItem]

The dataset items to create the models from.

required
dataset_schema DatasetSchema

The schema of the dataset containing the dataset item.

required

Returns:

Type Description
list[Self]

The list of created models.

Source code in pixano/app/models/dataset_items.py
@classmethod
def from_dataset_items(cls, dataset_items: list[DatasetItem], dataset_schema: DatasetSchema) -> list[Self]:
    """Create a list of models from a list of [DatasetItem][pixano.datasets.DatasetItem]s.

    Args:
        dataset_items: The dataset items to create the models from.
        dataset_schema: The schema of the dataset containing the dataset item.

    Returns:
        The list of created models.
    """
    return [cls.from_dataset_item(dataset_item, dataset_schema) for dataset_item in dataset_items]

model_dump(exclude_timestamps=False, **kwargs)

Dump the model to a dictionary.

Parameters:

Name Type Description Default
exclude_timestamps bool

Exclude timestamps "created_at" and "updated_at" from the model dump. Useful for comparing models without timestamps.

False
kwargs Any

Arguments for pydantic BaseModel.model_dump().

{}

Returns:

Type Description
dict[str, Any]

The model dump.

Source code in pixano/app/models/dataset_items.py
def model_dump(self, exclude_timestamps: bool = False, **kwargs: Any) -> dict[str, Any]:
    """Dump the model to a dictionary.

    Args:
        exclude_timestamps: Exclude timestamps "created_at" and "updated_at" from the model dump. Useful for
            comparing models without timestamps.
        kwargs: Arguments for pydantic `BaseModel.model_dump()`.

    Returns:
        The model dump.
    """
    model_dump = super().model_dump(**kwargs)
    if exclude_timestamps:
        model_dump["item"].pop("created_at", None)
        model_dump["item"].pop("updated_at", None)
        for k in ["entities", "annotations", "views"]:
            for model in model_dump[k].values():
                if model is None:
                    continue
                elif isinstance(model, list):  # Only one level deep.
                    for item in model:
                        item.pop("created_at", None)
                        item.pop("updated_at", None)
                else:
                    model.pop("created_at", None)
                    model.pop("updated_at", None)
    return model_dump

to_dataset_item(dataset_schema)

Create a DatasetItem from a model.

Parameters:

Name Type Description Default
dataset_schema DatasetSchema

The schema of the dataset containing the dataset item.

required

Returns:

Type Description
DatasetItem

The created dataset item.

Source code in pixano/app/models/dataset_items.py
def to_dataset_item(self, dataset_schema: DatasetSchema) -> DatasetItem:
    """Create a [DatasetItem][pixano.datasets.DatasetItem] from a model.

    Args:
        dataset_schema: The schema of the dataset containing the dataset item.

    Returns:
        The created dataset item.
    """
    schema_dict = {}

    item = self.item
    schema_dict.update(item.to_row(dataset_schema.schemas["item"]).model_dump())

    for group in [self.annotations, self.entities, self.views]:
        for key, value in group.items():
            schema = dataset_schema.schemas[key]
            if isinstance(value, list):
                schema_dict[key] = [v.to_row(schema) for v in value]
            elif value is None:
                schema_dict[key] = None
            else:
                schema_dict[key] = value.to_row(schema)

    return DatasetItem.from_dataset_schema(dataset_schema, exclude_embeddings=True).model_validate(schema_dict)

to_dataset_items(models, dataset_schema) staticmethod

Create a list of DatasetItems from a list of models.

Parameters:

Name Type Description Default
models list[DatasetItemModel]

The models to create the dataset items from.

required
dataset_schema DatasetSchema

The schema of the dataset containing the dataset items.

required

Returns:

Type Description
list[DatasetItem]

The list of created dataset items.

Source code in pixano/app/models/dataset_items.py
@staticmethod
def to_dataset_items(models: list["DatasetItemModel"], dataset_schema: DatasetSchema) -> list[DatasetItem]:
    """Create a list of [DatasetItem][pixano.datasets.DatasetItem]s from a list of models.

    Args:
        models: The models to create the dataset items from.
        dataset_schema: The schema of the dataset containing the dataset items.

    Returns:
        The list of created dataset items.
    """
    return [model.to_dataset_item(dataset_schema) for model in models]