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:
objectBase logical type: a canonical NumPy dtype plus a coercion to the encoder-ready Python value.
- numpy_dtype
alias of
float64
- class Real[source]
Bases:
FieldTypeA real-valued scalar.
- numpy_dtype
alias of
float64
- class Integer[source]
Bases:
FieldTypeA signed integer logical type.
- numpy_dtype
alias of
int64
- class Count[source]
Bases:
FieldTypeA non-negative integer count logical type.
- numpy_dtype
alias of
int64
- coerce(value)[source]
Coerce
valueto a non-negative Pythonint, validating exactness first.Bare
int(value)used to silently truncate (1.9->1), silently acceptTrue/False(bool is anintsubclass in Python) as1/0, and silently accept negative values – turning a data problem into a wrong-but-plausible-looking observation instead of an error. A numeric string ("42") is still accepted, since that is common, legitimate connector input (CSV/SQL text columns); a bool, a non-integral value, a negative value, or a non-finite (NaN/Inf) value is rejected instead of silently coerced.
- class Categorical(categories=None, numpy_dtype=<class 'numpy.object_'>)[source]
Bases:
FieldTypeA categorical label, optionally over a fixed set of
categories.- numpy_dtype
alias of
object_
- class Vector(dim=None, numpy_dtype=<class 'numpy.float64'>)[source]
Bases:
FieldTypeA fixed- or free-length real vector.
- numpy_dtype
alias of
float64
- coerce(value)[source]
Coerce
valueto a one-dimensionalfloat64array and validatedimwhen set.A multi-dimensional input is flattened in C order ONLY when
dimis set and the element count matches it. Declaring the flat length is what makes the reshape explicit rather than silent: sources that emit blocks – PatchSampler hands back apatch_size-shaped patch – have a genuinely rectangular observation whose model-facing form is the flat vector, and with no field kind for N-D arrays the alternative was to refuse them outright. Without a declareddimthe shape is still an error, because then nothing states what length the caller expected and a silent flatten could hide a transposed or mis-sliced block.
- class Timestamp[source]
Bases:
FieldTypeA point in time (datetime / numpy datetime64 / ISO string / POSIX seconds).
- class Optional(inner=<factory>)[source]
Bases:
FieldTypeA value that may be missing (
Nonepasses through; otherwise the inner type coerces).- Parameters:
inner (FieldType)
- property numpy_dtype: Any
Return the NumPy dtype of the wrapped field type.
- class Nested(schema, numpy_dtype=<class 'numpy.object_'>)[source]
Bases:
FieldTypeA sub-record with its own
Schema.- Parameters:
schema (Schema)
numpy_dtype (Any)
- numpy_dtype
alias of
object_
- class Field(name, type)[source]
Bases:
objectA named, typed column.
- Parameters:
name (str)
type (FieldType)
- class Schema(fields, container_value=False)[source]
Bases:
objectAn ordered set of typed fields.
- conform_record(record)[source]
Coerce one record (a scalar for a 1-field schema, else a tuple/dict) to the schema’s types.
- conform(records)[source]
Coerce every record in
recordsto this schema (raising clear errors on mismatch).
- 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
fieldsandsourcesplus 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