Structure#
- class superglm.Structure(
- features: dict[str, FeatureStructure],
- superglm_version: str = '',
Bases:
objectThe structural decisions of a model’s features, without coefficients.
Build one from a fitted model with
from_model(), write it withto_json(), read it back withread_structure(), and build its decisions into a model withapply().- Parameters:
- featuresdict[str, FeatureStructure]
The decisions for each feature, by the text of its column label.
- superglm_versionstr
The superglm version that exported the structure.
- Raises:
- StructureError
If an entry is not self-consistent: a group member outside the levels, a reference that is not a level or group, an unseen group that does not exist, or a malformed field.
- classmethod from_model(model, X=None) Structure#
The structure of
model’s categorical, ordered and spline features.- Parameters:
- modelSuperGLM
A fitted model; the structure is the one in force, with each reference as the fit resolved it.
- XDataFrame, optional
The data the model was fit on. It is read only to give a grouped term’s levels their native types: a grouping matches levels as text, so without
Xor a declaredlevels=the fitted model knows them only as text.
- Returns:
- Structure
One entry per categorical, ordered and spline feature, keyed by its column label’s text (a column labelled
0as"0"). Other features carry no structural decisions and are left out.
- Raises:
- StructureError
If the model is not a fitted SuperGLM, or a level cannot be written as JSON.
- to_json(path=None) str#
The structure as JSON text, written to
pathtoo when one is given.Keys are sorted, the indent is two spaces and the text ends in one newline, so two exports of one structure are byte-identical. The file is written as UTF-8 bytes, the same on every platform.
- Parameters:
- pathstr or path-like, optional
Where to write the file.
- Returns:
- str
The JSON text.
- classmethod from_json(source) Structure#
Read a structure from JSON text, or from the mapping it parses to.
- Parameters:
- sourcestr, bytes or Mapping
The text
to_json()returns, or its parsed form. To read a file, useread_structure().
- Returns:
- Structure
- Raises:
- StructureError
If the text is not JSON, the format is not
"superglm.structure.v1", or an entry is malformed or not self-consistent.
- apply(model, X=None)#
An unfitted copy of
modelwith these decisions built into its features.Nothing is fitted. Each feature the structure names is rebuilt from the model’s declared spec with the structure’s grouping, reference, unseen policy and polynomial ranges, by the builders the editor uses; every other feature is copied as it is. Groupings are built from the structure’s level universe, as
levels=declares one, so a group or reference whose levels have no rows in the fit is pinned to the base with a warning rather than refused. Ranges on apsornsspline rebuild it as absspline with the same knots, degree and penalty order; a recorded basis rebuilds the declared spline as that kind before its knots and ranges. The copy’s penalties and family are thosemodelwas declared with, so aselection_penalty="auto", REML-estimated smoothing orNegativeBinomial(theta="auto")is estimated again at the next fit.- Parameters:
- modelSuperGLM
The model to build into, fitted or not. It is left unchanged.
- XDataFrame, optional
The data the model will be fit on, read for two things. Levels of a grouped term that the structure does not list go to its
unseengroup, or each into a level of its own when that is"error"or"base", with one warning per feature; a model that declareslevels=refuses them. WithoutXa grouped term covers only the structure’s levels.Xalso places spline ranges: a spline whose boundary the model leaves to the data is fitted out to hold a range reaching pastX, with one warning, and a range the spline refuses onXis refused here.
- Returns:
- SuperGLM
An unfitted model, ready to
fit.
- Raises:
- StructureError
If
modelis not a SuperGLM, a feature is not in the model or is another kind of term, its levels are not those the model declares (an ordered term’s bands in declared order), or its spline refuses a range. Any other error while a feature is rebuilt is reported as that feature’s refusal, with the error as its cause.