Self-contained interactive HTML rules explorer for fitted FlagGAM estimators.
This module is an ORIGINAL ADDITION (not part of Zhao & Welsch, arXiv:2605.31189):
the paper specifies no explorer/export API. export_rules_html renders a single
HTML document with inlined CSS and vanilla JS and NO external resources (no CDN,
fonts, or images), so the result is fully self-contained: it works offline and
can be embedded in an iframe (e.g. the docs site) with no network access.
export_rules_html
export_rules_html(estimator: Any, path: str | Path | None = None, title: str = 'FlagGAM rules explorer') -> str
Render a self-contained interactive HTML explorer of the fitted rules.
Requires an estimator fitted with representation='full' and the additive
head (binary classification or regression only; the compact head's
coefficients are per-class scores, not per-rule weights). Returns the
complete HTML document as a string; if path is given, also writes it
(UTF-8) and still returns the string.
Source code in src/flaggam/explorer.py
| def export_rules_html(
estimator: Any, path: "str | Path | None" = None, title: str = "FlagGAM rules explorer"
) -> str:
"""Render a self-contained interactive HTML explorer of the fitted rules.
Requires an estimator fitted with `representation='full'` and the additive
head (binary classification or regression only; the compact head's
coefficients are per-class scores, not per-rule weights). Returns the
complete HTML document as a string; if `path` is given, also writes it
(UTF-8) and still returns the string.
"""
check_is_fitted(estimator, "core_")
if getattr(estimator, "representation", "full") == "compact":
raise ValueError(
"export_rules_html requires representation='full'; under 'compact' the head "
"coefficients are per-class scores, not per-rule weights"
)
if hasattr(estimator, "classes_") and len(estimator.classes_) > 2:
raise ValueError("export_rules_html supports binary classification and regression only")
if not isinstance(estimator.head_, _ADDITIVE_HEADS):
raise ValueError("export_rules_html requires the additive head")
payload = _build_payload(estimator, title)
data = json.dumps(payload, allow_nan=False).replace("</", "<\\/")
doc = _TEMPLATE.replace("%%DATA%%", data).replace("%%TITLE%%", html.escape(title, quote=True))
if path is not None:
Path(path).write_text(doc, encoding="utf-8")
return doc
|