Data Layer¶
mixle.data is the bridge between external data and the encoder contract
used by distributions. It is optional: plain Python lists still work. Use the
data layer when you need typed schemas, lazy sources, reproducible hashes,
structure-aware partitioning, or reusable encoded payloads.
Core Objects¶
DataSourceA lazy, typed, structured source of records. A source exposes
records()andencode(...)and carries optional schema and sample-structure metadata.SchemaAn ordered set of named fields with logical types such as
Real,Count,Categorical,Text,Vector,Optional, andNested.SampleStructureThe exchangeability assumption for a dataset:
IID,EXCHANGEABLE,SEQUENTIAL, or a partially exchangeable grouping.exchangeability_checkA permutation-based diagnostic for whether numeric row order carries trend or regime-shift information.
dataset_hashandmodel_hashStable identifiers used by provenance, registries, drift checks, and reproducibility workflows.
Schema Validation¶
Schemas coerce records into the Python values expected by encoders and report problems early.
from mixle.data import Field, Real, Schema, Text, check_dataset
schema = Schema(
(
Field("country", Text()),
Field("age", Real()),
Field("spend", Real()),
)
)
rows = [
{"country": "US", "age": "41", "spend": 12.5},
{"country": "CA", "age": 39, "spend": 8.0},
]
conformed = schema.conform(rows)
report = check_dataset(rows, schema=schema)
Use schema validation at the boundary of a pipeline. The fitted model can still receive ordinary Python records after they are conformed.
Sources¶
as_source wraps in-memory data. open_source constructs a lazy source for
supported external formats when the relevant optional extra is installed.
from mixle.data import EXCHANGEABLE, as_source
source = as_source(rows, structure=EXCHANGEABLE, schema=schema)
for record in source.records():
print(record)
The public source system includes adapters for pandas, Arrow, SQL, Mongo, Hadoop, Spark, text, and graph data. Heavy integrations are imported lazily so the base install stays small.
Sample Structure¶
Sample structure tells mixle how records may be partitioned or interpreted.
Structure |
Use when |
|---|---|
|
independent and identically distributed samples |
|
order does not matter, but exact iid assumptions are not asserted |
|
record order is meaningful, as in time series or event streams |
|
samples are exchangeable within groups, such as users or sessions |
Structure-aware partitioning matters for streaming, distributed fitting, and validation. It prevents sequence or group boundaries from being broken accidentally.
Exchangeability Diagnostics¶
Many high-level verbs assume rows can be pooled into one model or sampled as
“more rows like these.” exchangeability_check tests that assumption for
numeric scalar or tuple/list fields by looking for order trends and first-half
versus second-half shifts.
from mixle.data import exchangeability_check
report = exchangeability_check(values, alpha=0.01, seed=0)
print(report.label)
print(report.as_dict())
The report label is one of:
exchangeableNo order signal was found at the tested level.
trendValues co-move with row position; fit a temporal or sequential model instead of pooling silently.
shiftThe early and late halves differ in location; treat the rows as a regime change unless the split is intended.
mixle.inference.create and mixle.inference.synthesize run this check
when applicable and store the verdict in provenance. It is a warning signal,
not an automatic refusal, because some applications intentionally pool after
domain review.
Encoded Data¶
Most users do not need to call encoders directly. When repeated fits should reuse the same preprocessing boundary, save encoded data:
from mixle.data import load_encoded, save_encoded
encoder = model.dist_to_encoder()
encoded = encoder.seq_encode(rows)
save_encoded("encoded.mixle", encoded)
encoded_again = load_encoded("encoded.mixle")
The encoded payload is the same kind of data consumed by optimize internally.
Hashes and Provenance¶
dataset_hash and model_hash provide durable identifiers:
from mixle.data import dataset_hash, model_hash
data_id = dataset_hash(rows)
model_id = model_hash(model)
Production helpers use these values in model headers, registries, drift reports, and lineage checks.
DataFrame and Spark Helpers¶
Optional adapters keep tabular and distributed data close to the same record shape used by ordinary lists.
from mixle.data import dataframe_records
rows = list(dataframe_records(df, fields=["country", "age", "spend"]))
Spark helpers include sampling functions for RDD-backed workflows. Use them after the local model shape works on an in-memory sample.
Graph Data¶
Graph adapters expose graph observations to graph distributions without making
graphs part of the scalar record path. GraphDataEncoder and
GraphObservation are loaded lazily to avoid import cycles with graph
families.
Practical Workflow¶
Start with a plain list of representative records.
Fit the model locally and confirm the estimator shape.
Add a
Schemato make field coercion explicit.Wrap the data in a
DataSourcewhen you need lazy loading, external data, sample structure, or partitioning.Add hashes and encoded-data persistence once the workflow becomes repeatable or production-facing.
API Map¶
Import |
Purpose |
|---|---|
|
source abstraction and in-memory/lazy wrappers |
|
typed field schemas |
|
additional schema types and the base field-type protocol |
|
sample-structure declarations |
|
row-order diagnostics used by creation and synthesis provenance |
|
validation and diagnostics |
|
reproducibility identifiers |
|
persist encoded payloads |
|
external data source discovery and construction |
|
tabular and Spark/RDD sampling or encoding helpers |