mixle.data.schema module

Schema and logical types – the bridge between external data and the Python types encoders expect.

A deliberately small, closed logical type system (not an open type algebra). Each FieldType knows its canonical NumPy dtype and how to coerce a raw value into the Python object the existing DataSequenceEncoder already consumes (a label for Categorical, an np.ndarray for Vector, …). A Schema is an ordered tuple of named Field s; it can be derived from a model (formalizing the fields/sources duck-probe in the DataFrame adapter) and used to conform raw records (coerce + validate) before encoding – the thing connectors silently get wrong today.

class FieldType[source]

Bases: object

Base logical type: a canonical NumPy dtype plus a coercion to the encoder-ready Python value.

numpy_dtype

alias of float64

coerce(value)[source]
Parameters:

value (Any)

Return type:

Any

class Real[source]

Bases: FieldType

A real-valued scalar.

numpy_dtype

alias of float64

coerce(value)[source]
Parameters:

value (Any)

Return type:

float

class Count[source]

Bases: FieldType

A non-negative integer count.

numpy_dtype

alias of int64

coerce(value)[source]
Parameters:

value (Any)

Return type:

int

class Boolean[source]

Bases: FieldType

A boolean flag.

numpy_dtype

alias of bool

coerce(value)[source]
Parameters:

value (Any)

Return type:

bool

class Text[source]

Bases: FieldType

A free-text string.

numpy_dtype

alias of object_

coerce(value)[source]
Parameters:

value (Any)

Return type:

str

class Categorical(categories=None, numpy_dtype=<class 'numpy.object_'>)[source]

Bases: FieldType

A categorical label, optionally over a fixed set of categories.

Parameters:
  • categories (tuple[Any, ...] | None)

  • numpy_dtype (Any)

categories: tuple[Any, ...] | None = None
numpy_dtype

alias of object_

coerce(value)[source]
Parameters:

value (Any)

Return type:

Any

class Vector(dim=None, numpy_dtype=<class 'numpy.float64'>)[source]

Bases: FieldType

A fixed- or free-length real vector.

Parameters:
  • dim (int | None)

  • numpy_dtype (Any)

dim: int | None = None
numpy_dtype

alias of float64

coerce(value)[source]
Parameters:

value (Any)

Return type:

ndarray

class Timestamp[source]

Bases: FieldType

A point in time (datetime / numpy datetime64 / ISO string / POSIX seconds).

numpy_dtype: Any = dtype('<M8[ns]')
coerce(value)[source]
Parameters:

value (Any)

Return type:

Any

class Optional(inner=<factory>)[source]

Bases: FieldType

A value that may be missing (None passes through; otherwise the inner type coerces).

Parameters:

inner (FieldType)

inner: FieldType
property numpy_dtype: Any

Double-precision floating-point number type, compatible with Python float and C double.

Character code:

'd'

Canonical name:

numpy.double

Alias on this platform (Linux x86_64):

numpy.float64: 64-bit precision floating-point number type: sign bit, 11 bits exponent, 52 bits mantissa.

coerce(value)[source]
Parameters:

value (Any)

Return type:

Any

class Nested(schema, numpy_dtype=<class 'numpy.object_'>)[source]

Bases: FieldType

A sub-record with its own Schema.

Parameters:
  • schema (Schema)

  • numpy_dtype (Any)

schema: Schema
numpy_dtype

alias of object_

coerce(value)[source]
Parameters:

value (Any)

Return type:

Any

class Field(name, type)[source]

Bases: object

A named, typed column.

Parameters:
  • name (str)

  • type (FieldType)

name: str
type: FieldType
class Schema(fields)[source]

Bases: object

An ordered set of typed fields.

Parameters:

fields (tuple[Field, ...])

fields: tuple[Field, ...]
property names: tuple[str, ...]
conform_record(record)[source]

Coerce one record (a scalar for a 1-field schema, else a tuple/dict) to the schema’s types.

Parameters:

record (Any)

Return type:

Any

conform(records)[source]

Coerce every record in records to this schema (raising clear errors on mismatch).

Parameters:

records (Any)

Return type:

list[Any]

static for_model(model)[source]

Best-effort schema a model expects, from its fields/sources + child distributions.

Formalizes the duck-probe in the DataFrame adapter: a record/composite model exposes fields and sources plus child distributions whose support fixes each field’s logical type; a bare leaf yields a single field typed by its support (discrete -> Count, continuous -> Real, …).

Parameters:

model (Any)

Return type:

Schema