quivers.data¶
Dataframe-side surface: schema inference, observation packing, and
DSL composition helpers. Accepts pandas, polars, or any other
Narwhals-compatible
backend via the IntoDataFrame shim.
data
¶
Dataframe-side surface: schema inference, observation packing, and DSL composition helpers.
Bridges between user dataframes (pandas, polars, or any
Narwhals-compatible
backend) and the QVR DSL: derives object cardinalities from
df[col].n_unique(), builds the per-row plate-index tensors from
deterministic categorical orderings, and emits the object
declarations + observations dict consumed by inference.
The dataframe library is not a hard dependency. Users install pandas,
polars, or any other Narwhals-supported backend; DatasetSchema
accepts whichever they hand in.
ColumnRole
¶
Bases: str, Enum
How a dataframe column participates in a QVR program.
MissingPolicy
¶
Bases: str, Enum
How to handle NaN / null entries when encoding a column.
DatasetSchema
¶
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
¶
cardinalities() -> Mapping[str, int]
Inferred object cardinalities, keyed by QVR object name.
Source code in src/quivers/data/schema.py
123 124 125 126 127 128 129 130 131 132 | |
categories
¶
categories(column: str) -> tuple[str, ...]
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
134 135 136 137 138 139 140 141 142 143 144 145 146 | |
declarations
¶
declarations() -> str
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
148 149 150 151 152 153 154 155 156 157 158 159 160 | |
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
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
encode_column
¶
encode_column(df: DataFrame, column: str, *, role: ColumnRole, categories: tuple[str, ...] | None = None, missing_policy: MissingPolicy = RAISE) -> Tensor
Encode a single column into a torch.Tensor ready for
QVR inference.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Narwhals-wrapped dataframe.
TYPE:
|
column
|
Column to encode.
TYPE:
|
role
|
How the column participates in the program.
TYPE:
|
categories
|
Canonical ordering of categorical values; if provided, codes
are assigned by
TYPE:
|
missing_policy
|
Policy for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
|
Source code in src/quivers/data/encoding.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
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:
|
Source code in src/quivers/data/schema.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |