EditorSession#
- class superglm.editor.EditorSession(
- model,
- terms: dict[str, EditableTerm],
- *,
- n_points: int = 200,
- centering: str = 'native',
- reference_model=None,
- evaluation_data: dict[str, Any] | None = None,
- cv_report: Any = None,
- cv: CrossValidationResult | None = None,
- cv_data: EvaluationDataset | None = None,
- with_se: bool = True,
Bases:
objectStateful editor for fitted 1D main effects.
The session never mutates the source model. Edits are kept in memory until
to_model()is called.- classmethod from_model(
- model,
- terms: list[str] | tuple[str, ...] | None = None,
- *,
- n_points: int = 200,
- centering: str = 'native',
- with_se: bool = True,
- train_data=None,
- validation_data=None,
- test_data=None,
- cv_report: Any = None,
- cv: CrossValidationResult | None = None,
- cv_data=None,
- amount_unit: str | None = None,
Build an editor session from fitted 1D main-effect inference.
cvis asuperglm.cross_validate()result for the Cross-validation tab, andcv_datathe(X, y[, sample_weight[, offset]])rows its folds index. Withoutcv_datathe train data is used when its row count matches the folds.cv_reportis the older free-form report the Validation tab shows.A
SuperLSSmodel needstrain_data, with its offsets a dict keyed by parameter name. Its terms are keyed"<parameter>:<term>"; a bare column name opens that column’s term in every parameter.amount_unitnames the currency or unit of the response.
- property terms: dict[str, EditableTerm]#
The editable terms, as of the session’s last action (
FitFollow).
- property edit_epoch: int#
Monotonic invalidation token for the current edited-model materialization.
- select_indices(
- term: str,
- indices,
Select explicit point or level indices.
- select_x( ) EditorSession#
Select grid points whose x values fall inside [start, stop].
- select_levels( ) EditorSession#
Select categorical or ordered-categorical levels by label.
- clear_selection(term: str) EditorSession#
Clear the active selection for a term.
- reset(term: str) EditorSession#
Reset selected values to the original fit, or the whole term if nothing is selected.
- additive_step(term: str) float#
What Increase adds to
term’s effect on a curve that shows the effect itself.5% of the weighted mean of
|eta|, the linear predictortermadds to, over the fit’s training rows, so on an identity link it moves an average row by about what x1.05 moves it on a log link. Where that mean is 0, or the fit kept no rows, 5% of the range of the term’s fitted curve, and 0.05 when that is 0 too. The mean is read once per fit (_fit_identity).
- shift(term: str, delta: float) EditorSession#
Add a link-scale delta to the selected values.
- set_value( ) EditorSession#
Set selected values to one link-scale value.
- set_values(
- term: str,
- indices,
- values,
Set explicit values on the internal link/log-effect scale.
- weighted_average(term: str) EditorSession#
Flatten the selected region to its weighted mean.
- linear_interpolate( ) EditorSession#
Move selected values toward a line between selected endpoints.
- level_left(term: str) EditorSession#
Set selected values to the left-most selected value.
- level_right(term: str) EditorSession#
Set selected values to the right-most selected value.
- snap_highest(term: str) EditorSession#
Set selected values to the highest selected value.
- snap_lowest(term: str) EditorSession#
Set selected values to the lowest selected value.
- reorder_levels( ) EditorSession#
Move selected categorical levels in display order.
- reset_level_order(term: str) EditorSession#
Reset a categorical term’s display order to the fitted model order.
- set_unseen( ) EditorSession#
Choose where
term’s levels unseen at fit go when the model predicts.policyis"error"(refuse them),"base"(rate them at the reference) or the label of one of the term’s groups (give them that group’s effect). Only a plain categorical has the choice. Nothing is refit: the in-force model becomes a copy with the new policy, and the choice is one entry that Undo takes back. The body lives insuperglm.editor.unseen.
- isotonic( ) EditorSession#
Make selected values monotone by holding the highest value so far, or the lowest.
Each contiguous run holds its running extreme until the curve passes it (
running_extreme_values), not tied to its unselected neighbours, so the edit can step at a selection edge. A categorical term also clamps to the level before the run. If no points are selected, the operation applies to the whole term.
- smooth( ) EditorSession#
Apply light local smoothing to selected values.
Selected contiguous runs are anchored to adjacent unselected neighbors when available. This avoids visible jumps at edit boundaries. If no points are selected, the operation applies to the whole term.
- ordered_spline(
- term: str,
The fitted spline of an ordered term on its level axis.
Noneunless the term is an ordered categorical with a spline basis; a fixed sentence when its handles are off.
- ordered_spline_coefficients(
- term: str,
- geometry: OrderedSplineGeometry,
The spline coefficients behind an ordered term’s current level effects.
Starts from the coefficients the latest handle move on the term wrote, else the fit’s, and keeps them wherever the edits since allow (
least_change_coefficients). Undo and Redo move the history, so the handles follow them.
- move_control_point( ) EditorSession#
Move one spline control handle vertically and refit the displayed curve.
- undo(term: str | None = None) EditorSession#
Undo the latest action, in time: an edit, a waiting change or an applied step.
Undoing a waiting change puts back the term’s previous draft; the model and the curves are unchanged, so the model revision stays. Undoing an applied step puts back the whole editor state from before it, edits and waiting changes included, without refitting.
termlimits the undo to that term’s latest edit since the last structural step.
- redo(term: str | None = None) EditorSession#
Redo the latest undone action, in the order Undo took them back.
- undo_target() EditRecord | PendingStep | StructuralStep | None#
What a plain
undo()takes next: the latest edit or waiting change, else a step.
- redo_target() EditRecord | PendingStep | StructuralStep | None#
What a plain
redo()puts back next, in the reverse of the order Undo took.
- draft_spec(term: str)#
term’s spec as waiting changes leave it.See
superglm.editor.staging.draft_spec.
- stage_structural(
- operation: str,
- term: str,
- params: dict[str, Any],
- *,
- keep_reference: bool = True,
- X=None,
- sample_weight=None,
Stage one structural change to wait for a Refit.
See
superglm.editor.staging.stage_structural.
- refit_pending( ) StructuralStep#
Apply every waiting change in one fit, as one step.
See
superglm.editor.staging.refit_pending.
- timeline_items() tuple[list[tuple[Any, str]], list[tuple[Any, str]]]#
Every action, split at the current position.
See
superglm.editor.staging.timeline_items.
- set_step_note(step_id: str, note: str | None) None#
Write
noteon the timeline entrystep_id; an empty note removes it.
- editor_history_records() list[dict[str, Any]]#
The timeline up to now as an exported model’s
_editor_history.
- to_model(*, X=None, y=None, sample_weight=None, offset=None)#
Return an edited copy of the source model.
If explicit evaluation data is supplied, scalar fit statistics on the copy are refreshed against that data. Otherwise the fitted model’s retained fit data is used when available.
- capture_materialization_request(
- *,
- dataset=None,
- include_unchanged: bool = False,
Capture plot-scale state and immutable evaluation references.
- materialization_request_is_current(
- request: EditMaterializationRequest,
Return whether a captured model snapshot still identifies this session state.
- cached_materialized_model( )#
Return the cached model only for matching live session identity.
- publish_materialized_model(
- request: EditMaterializationRequest,
- model,
Publish a private model only if its entire snapshot is still current.
- export_structure(path: str | Path | None = None) str#
The in-force model’s structural decisions as JSON, written to
pathtoo when given.The groupings, references, polynomial ranges and where new levels go, per feature, as
superglm.read_structure()reads them; no coefficients and no hand edits. Changes waiting for a Refit are not included: the file is the last Refit.
- edited_offset( ) NDArray#
Return the link-scale offset implied by edited term curves.
This is the additive offset to pass to
fit/fit_remlwhen the edited terms should be treated as fixed rating factors. Iftermsis omitted, all changed terms in the session are used.
- edited_offset_factor( ) NDArray#
Return the multiplicative factor represented by
edited_offset.
- refit_with_edited_offset(
- terms: str | list[str] | tuple[str, ...] | None = None,
- *,
- X=None,
- y=None,
- sample_weight=None,
- offset=None,
- method: str = 'auto',
- lambda1=Ellipsis,
- lambda2=Ellipsis,
- **fit_kwargs: Any,
Refit a copy with edited terms fixed as an offset.
The returned model excludes the offset terms from its feature set and fits the remaining terms conditional on the edited offset. Inference on the returned model is therefore for the refitted remaining terms, not for the manually edited offset curves.
- reprofile_distribution(
- parameter: str,
- *,
- X=None,
- y=None,
- sample_weight=None,
- offset=None,
- fit_mode: str = 'inherit',
- progress_callback=None,
- **profile_kwargs: Any,
Explicitly re-estimate a distribution parameter for the in-force model.
- refit_with_collapsed_levels( )#
Collapse selected categorical levels and refit a full model copy.
keep_referenceholds the in-force reference level, or the group that takes it in;Falselets the declared base policy choose again.refit_kwargsareX,y,sample_weight,offset,method,lambda1,lambda2and fit keywords.Deprecated: use
replace_with_collapsed_levels(), which records the change as one step.
- replace_with_collapsed_levels( )#
Collapse the selected levels and refit at once, as one structural step.
This is
stage_structural()followed by the refit of every waiting change; Undo takes the whole call back.refit_kwargsareX,y,sample_weight,offset,method,lambda1,lambda2and fit keywords.
- refit_with_ungrouped_levels( )#
Remove selected levels from collapsed groups and refit a model copy.
keep_referenceholds the in-force reference; a reference group that loses members follows the members that stay.Deprecated: use
replace_with_ungrouped_levels(), which records the change as one step.
- replace_with_ungrouped_levels( )#
Ungroup the selected levels and refit at once, as one structural step.
An ungroup that reproduces the fit from before the latest step puts that fit back instead of refitting, as
staging.stage_and_refitstates.
- replace_with_reference_level( )#
Pin
levelasterm’s reference and refit at once, as one structural step.
- replace_with_special_levels( )#
Take
levelsoffterm’s curve (or,special=False, put them back) and refit.One structural step, which one Undo takes back.
levelsare display labels of an ordered term; a level taken off is fitted with a free estimate of its own, asspecials=declares.
- replace_with_knots( )#
Change
term’s knots and refit at once, as one structural step.paramsis{"count", "strategy"}with an optional"alpha",{"positions": [...]}in chart coordinates, or{"reset": True}for the opened model’s knots (superglm.editor.knots).
- replace_with_basis( )#
Change
term’s basis and refit at once, as one structural step.paramsis{"kind": "ps" | "bs" | "cr" | "ns"}or{"select": True | False}, the double penalty that lets the fit shrink the term towards a straight line and to nothing (superglm.editor.basis).
- replace_with_shaped_range( )#
Pin
termto adegreepolynomial on[lo, hi]and refit at once.loandhiare values on a numeric term (snapped outward to three significant figures of the fitted span) and band labels on an ordered one.joinis"tangent"or"kink"(Corner).
- revert_to_reference_model()#
Put the opened model back in force, in its own level order, as one structural step.
- replace_in_force_model( ) EditorSession#
Replace the editable in-force model while retaining the original reference model.
Display level reorders carry over unless
level_ordersreplaces them. Edits and waiting changes do not carry over. A SuperLSS is wrapped in the view of the model in force, over the same rows.
- classmethod load( ) EditorSession#
Load an edit artifact against a fitted model.
The artifact holds curve edits only. When the session it came from had structural steps or New levels choices in force, one warning names them: they were not restored. Those a revert to the original model undid are not named: loaded against that model, nothing is missing.
- widget(mode: str | None = None)#
Return the editor app for this session, to display in a notebook.
mode="server"serves the app from a local server, which the browser must be able to reach, as it can when it runs on the kernel’s machine.mode="notebook"runs it inside the notebook cell over widget messages, for hosted notebooks such as Databricks, and needspip install 'superglm[notebook]'.Nonepicks notebook on Databricks and server elsewhere; on Databricks without anywidget it keeps server and warns with the install line.