Skip to content

pixano.data.fields

Fields(field_dict)

Bases: BaseModel

Dataset PyArrow fields as string dictionary

Attributes:

Name Type Description
field_dict dict[str, str]

PyArrow fields as string dictionary

Parameters:

Name Type Description Default
field_dict dict[str, str]

PyArrow fields as string dictionary

required
Source code in pixano/data/fields.py
def __init__(self, field_dict: dict[str, str]) -> None:
    """Create Fields from string dictionary

    Args:
        field_dict (dict[str, str]): PyArrow fields as string dictionary
    """

    # Define public attributes through Pydantic BaseModel
    super().__init__(field_dict=field_dict)

to_schema()

Convert Fields string dictionary to PyArrow schema

Returns:

Type Description
schema

Fields as PyArrow schema

Source code in pixano/data/fields.py
def to_schema(self) -> pa.schema:
    """Convert Fields string dictionary to PyArrow schema

    Returns:
        pa.schema: Fields as PyArrow schema
    """
    fields = []
    for field_name, field_type in self.field_dict.items():
        # Convert the field type to PyArrow type
        field = pa.field(field_name, field_to_pyarrow(field_type), nullable=True)
        fields.append(field)
    return pa.schema(fields)

field_to_pyarrow(field)

Return PyArrpw type from string field

Parameters:

Name Type Description Default
field str

String field

required

Returns:

Type Description
DataType

PyArrow type

Source code in pixano/data/fields.py
def field_to_pyarrow(field: str) -> pa.DataType:
    """Return PyArrpw type from string field

    Args:
        field (str): String field

    Returns:
        pa.DataType: PyArrow type
    """

    pyarrow_dict = {
        "int": pa.int64(),
        "float": pa.float32(),
        "bool": pa.bool_(),
        "str": pa.string(),
        "bytes": pa.binary(),
        "np.ndarray": pa.list_(pa.float32()),
        "image": ImageType,
        "depthimage": DepthImageType,
        "camera": CameraType,
        "compressedrle": CompressedRLEType,
        "pose": PoseType,
        "bbox": BBoxType,
        "gtinfo": GtInfoType,
    }

    if isinstance(field, str):
        if field.startswith("[") and field.endswith("]"):
            return pa.list_(
                pyarrow_dict[field.removeprefix("[").removesuffix("]").lower()]
            )
        if field.startswith("vector(") and field.endswith(")"):
            size_str = field.removeprefix("vector(").removesuffix(")")
            if size_str.isnumeric():
                return pa.list_(pa.float32(), list_size=int(size_str))
        return pyarrow_dict[field.lower()]
    return None

field_to_python(field)

Return Python type from string field

Parameters:

Name Type Description Default
field str

String field

required

Returns:

Type Description
type

Python type

Source code in pixano/data/fields.py
def field_to_python(field: str) -> type:
    """Return Python type from string field

    Args:
        field (str): String field

    Returns:
        type: Python type
    """

    python_dict = {
        "int": int,
        "float": float,
        "bool": bool,
        "str": str,
        "bytes": bytes,
        "np.ndarray": np.ndarray,
        "image": Image,
        "depthimage": DepthImage,
        "camera": Camera,
        "compressedrle": CompressedRLE,
        "pose": Pose,
        "bbox": BBox,
        "gtinfo": GtInfo,
    }

    if isinstance(field, str):
        if field.startswith("[") and field.endswith("]"):
            return list
        if field.startswith("vector(") and field.endswith(")"):
            return np.ndarray
        return python_dict[field.lower()]
    return None