quivers.data.schema¶
DatasetSchema and the compose helper.
schema
¶
Dataframe-to-QVR schema bridge.
DatasetSchema is the single point that turns "I have a
dataframe" into "I have the object cardinalities, the observations
dict, and the plate-index tensors a QVR program needs." It accepts
pandas, polars, or any other
Narwhals-compatible
dataframe and emits the two artefacts inference consumes:
-
A
.qvrdeclaration prelude with oneobject X : Nline per declared object axis.Nis derived fromdf[col].n_unique(); the canonical ordering of categories is cached so plate indices are reproducible across reruns. -
An
observationsdict mapping observe-site / plate-index names totorch.Tensorvalues, ready to hand intoquivers.inference.MCMC.runorquivers.inference.SVI.step.
The companion compose wraps quivers.dsl.loads so a
user can write a .qvr body without spelling out
object Verb : 40 when 40 came from a dataframe column anyway.
DatasetSchema
didactic-model
¶
DatasetSchema(*, df: IntoDataFrame = field(opaque=True), objects: Mapping[str, str] = field(default_factory=dict, opaque=True), observations: Mapping[str, str] = field(default_factory=dict, opaque=True), plate_indices: Mapping[str, str] = field(default_factory=dict, opaque=True), covariates: Mapping[str, str] = field(default_factory=dict, opaque=True), missing_policy: MissingPolicy = RAISE)
Bases: Model
Mapping from dataframe columns to QVR program artefacts.
| ATTRIBUTE | DESCRIPTION |
|---|---|
df |
Source dataframe; pandas, polars, modin, dask, pyarrow, or
anything else Narwhals'
TYPE:
|
objects |
Map from column name to the QVR object name. The object's cardinality is inferred from the column's number of unique values; the canonical ordering is the sorted set of unique values, so plate indices are deterministic across reruns.
TYPE:
|
observations |
Map from column name to the QVR observe-site name. Categorical
columns are encoded to
TYPE:
|
plate_indices |
Map from column name (which must also appear under
TYPE:
|
covariates |
Map from numeric column name to the QVR variable name to
bind the column's values to (as a
TYPE:
|
missing_policy |
Policy applied to every column with nulls. Default
TYPE:
|
cardinalities
¶
Inferred object cardinalities, keyed by QVR object name.
Source code in src/quivers/data/schema.py
categories
¶
Canonical ordering of values for an object-column.
Codes are assigned as categories.index(value); the
ordering is the column's sorted unique non-null values, so
the same dataframe always produces the same indices.
Source code in src/quivers/data/schema.py
declarations
¶
Emit a .qvr declaration prelude.
Lines are object <Name> : FinSet <cardinality>, sorted
by name for reproducibility. Suitable for prepending to a
user's .qvr source via compose.
Source code in src/quivers/data/schema.py
observations_dict
¶
observations_dict() -> dict[str, Tensor]
Build the observations dict for inference.
Contains entries for every observation, plate-index, and
covariate column. Categorical observations and plate
indices use the canonical ordering returned by
categories; numeric observations and covariates
become FloatTensor.
Source code in src/quivers/data/schema.py
compose
¶
compose(qvr_body: str, schema: DatasetSchema, **kwargs)
Compile a .qvr body against a dataset schema.
Prepends the schema's object declarations to qvr_body,
then calls quivers.dsl.loads. The user writes only the
program body (latents, kernels, observations, return); object
cardinalities inferred from the dataframe are slotted in
automatically. If the body re-declares an object that appears
in the schema, the body's declaration wins.
| PARAMETER | DESCRIPTION |
|---|---|
qvr_body
|
QVR source without the
TYPE:
|
schema
|
Dataframe schema providing cardinalities.
TYPE:
|
**kwargs
|
Forwarded to
DEFAULT:
|