Structure#

class superglm.Structure(
features: dict[str, FeatureStructure],
superglm_version: str = '',
)#

Bases: object

The structural decisions of a model’s features, without coefficients.

Build one from a fitted model with from_model(), write it with to_json(), read it back with read_structure(), and build its decisions into a model with apply().

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 X or a declared levels= 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 0 as "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 path too 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, use read_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 model with 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 a ps or ns spline rebuild it as a bs spline 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 those model was declared with, so a selection_penalty="auto", REML-estimated smoothing or NegativeBinomial(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 unseen group, or each into a level of its own when that is "error" or "base", with one warning per feature; a model that declares levels= refuses them. Without X a grouped term covers only the structure’s levels. X also places spline ranges: a spline whose boundary the model leaves to the data is fitted out to hold a range reaching past X, with one warning, and a range the spline refuses on X is refused here.

Returns:
SuperGLM

An unfitted model, ready to fit.

Raises:
StructureError

If model is 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.