framework-py 0.1.0.dev24__py3-none-any.whl → 0.1.0.dev25__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- framework/_artifacts.py +453 -6
- {framework_py-0.1.0.dev24.dist-info → framework_py-0.1.0.dev25.dist-info}/METADATA +1 -1
- {framework_py-0.1.0.dev24.dist-info → framework_py-0.1.0.dev25.dist-info}/RECORD +5 -5
- {framework_py-0.1.0.dev24.dist-info → framework_py-0.1.0.dev25.dist-info}/WHEEL +0 -0
- {framework_py-0.1.0.dev24.dist-info → framework_py-0.1.0.dev25.dist-info}/licenses/LICENSE +0 -0
framework/_artifacts.py
CHANGED
|
@@ -9,8 +9,12 @@ from them is published.
|
|
|
9
9
|
|
|
10
10
|
from __future__ import annotations
|
|
11
11
|
|
|
12
|
+
import ast
|
|
13
|
+
import contextlib
|
|
14
|
+
import itertools
|
|
12
15
|
import json
|
|
13
16
|
import math
|
|
17
|
+
import re
|
|
14
18
|
from collections.abc import Callable, Iterable, Sequence
|
|
15
19
|
from datetime import date, datetime
|
|
16
20
|
from pathlib import Path
|
|
@@ -658,9 +662,426 @@ def _statsmodels_document(result: Any) -> dict[str, Any]:
|
|
|
658
662
|
formula = getattr(model, "formula", None)
|
|
659
663
|
if isinstance(formula, str):
|
|
660
664
|
document["formula"] = formula
|
|
665
|
+
# Variables are optional; a design this code can't read leaves them out.
|
|
666
|
+
with contextlib.suppress(Exception):
|
|
667
|
+
document |= _statsmodels_variables(result, tables[0])
|
|
661
668
|
return document
|
|
662
669
|
|
|
663
670
|
|
|
671
|
+
# --------------------------------------------------------------------------- model variables
|
|
672
|
+
|
|
673
|
+
VARIABLE_LIMIT = 100
|
|
674
|
+
LEVEL_LIMIT = 50
|
|
675
|
+
TERM_LIMIT = 500
|
|
676
|
+
PART_LIMIT = 10
|
|
677
|
+
_LABEL_ATTRS = ("column_labels", "variable_labels", "labels")
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
class _Part:
|
|
681
|
+
"""One factor's contribution to a design column: its name piece, variable, and level."""
|
|
682
|
+
|
|
683
|
+
def __init__(
|
|
684
|
+
self, name: str, variable: str | None, level: str | None = None, **coding: Any
|
|
685
|
+
) -> None:
|
|
686
|
+
self.name, self.variable, self.level = name, variable, level
|
|
687
|
+
# A categorical part is mappable only when its column is one level's indicator.
|
|
688
|
+
self.mappable = variable is not None and coding.get("indicator", True)
|
|
689
|
+
self.categories: list[str] | None = coding.get("categories")
|
|
690
|
+
self.reference: str | None = coding.get("reference")
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
class _Design:
|
|
694
|
+
"""What a model's design says about its variables and coefficient terms."""
|
|
695
|
+
|
|
696
|
+
def __init__(self, columns: Sequence[str]) -> None:
|
|
697
|
+
self.columns = set(columns)
|
|
698
|
+
self.predictors: list[str] = []
|
|
699
|
+
self.terms: dict[str, list[dict[str, str]]] = {}
|
|
700
|
+
self.categories: dict[str, list[list[str]]] = {}
|
|
701
|
+
self.references: dict[str, set[str | None]] = {}
|
|
702
|
+
self.numeric: set[str] = set()
|
|
703
|
+
|
|
704
|
+
def predictor(self, expression: str) -> str | None:
|
|
705
|
+
"""Note the variables an expression uses; return the one it transforms, if one."""
|
|
706
|
+
used = _referenced(expression, self.columns)
|
|
707
|
+
for name in sorted(used):
|
|
708
|
+
if name not in self.predictors:
|
|
709
|
+
self.predictors.append(name)
|
|
710
|
+
return next(iter(used)) if len(used) == 1 else None
|
|
711
|
+
|
|
712
|
+
def match(self, names: Sequence[str], factors: Sequence[Sequence[_Part]]) -> None:
|
|
713
|
+
"""Map each design column to the parts whose names join to its name."""
|
|
714
|
+
candidates: dict[str, tuple[_Part, ...]] = {}
|
|
715
|
+
for combination in itertools.product(*factors):
|
|
716
|
+
candidates.setdefault(":".join(part.name for part in combination), combination)
|
|
717
|
+
for name in names:
|
|
718
|
+
parts = candidates.get(name)
|
|
719
|
+
if parts is None or not all(part.mappable for part in parts):
|
|
720
|
+
continue
|
|
721
|
+
self.add(name, parts)
|
|
722
|
+
|
|
723
|
+
def factor(self, variable: str | None, categories: list[str] | None = None) -> None:
|
|
724
|
+
"""Note how the model codes a variable: numeric, or categorical with these levels."""
|
|
725
|
+
if variable is None:
|
|
726
|
+
return
|
|
727
|
+
if categories is None:
|
|
728
|
+
self.numeric.add(variable)
|
|
729
|
+
else:
|
|
730
|
+
self.categories.setdefault(variable, []).append(categories)
|
|
731
|
+
|
|
732
|
+
def add(self, name: str, parts: Sequence[_Part]) -> None:
|
|
733
|
+
self.terms[name] = [
|
|
734
|
+
{"variable": str(p.variable)} | ({"level": p.level} if p.level is not None else {})
|
|
735
|
+
for p in parts
|
|
736
|
+
]
|
|
737
|
+
for part in parts:
|
|
738
|
+
if part.categories is not None:
|
|
739
|
+
self.references.setdefault(str(part.variable), set()).add(part.reference)
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
def _referenced(expression: str, columns: set[str]) -> set[str]:
|
|
743
|
+
"""Return the data columns a formula factor reads, or an empty set when unsure."""
|
|
744
|
+
if expression in columns:
|
|
745
|
+
return {expression}
|
|
746
|
+
used = {name for name in re.findall(r"`([^`]+)`", expression)}
|
|
747
|
+
code = re.sub(r"`[^`]+`", "_fw_quoted_", expression)
|
|
748
|
+
try:
|
|
749
|
+
tree = ast.parse(code, mode="eval")
|
|
750
|
+
except SyntaxError:
|
|
751
|
+
return set()
|
|
752
|
+
skip = set()
|
|
753
|
+
for node in ast.walk(tree):
|
|
754
|
+
if isinstance(node, ast.Call):
|
|
755
|
+
skip.add(id(node.func))
|
|
756
|
+
quoted = node.args[0] if len(node.args) == 1 else None
|
|
757
|
+
if (
|
|
758
|
+
isinstance(node.func, ast.Name)
|
|
759
|
+
and node.func.id == "Q"
|
|
760
|
+
and isinstance(quoted, ast.Constant)
|
|
761
|
+
and isinstance(quoted.value, str)
|
|
762
|
+
):
|
|
763
|
+
used.add(quoted.value)
|
|
764
|
+
elif isinstance(node, ast.Attribute):
|
|
765
|
+
skip.add(id(node.value))
|
|
766
|
+
used |= {
|
|
767
|
+
node.id
|
|
768
|
+
for node in ast.walk(tree)
|
|
769
|
+
if isinstance(node, ast.Name) and id(node) not in skip and node.id in columns
|
|
770
|
+
}
|
|
771
|
+
return used if used <= columns else set()
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
def _indicator(column: Any, categories: Sequence[str]) -> str | None:
|
|
775
|
+
"""Return the level a contrast column indicates, or None for other codings."""
|
|
776
|
+
values = np.asarray(column, dtype="float64")
|
|
777
|
+
ones = np.flatnonzero(values == 1)
|
|
778
|
+
if len(ones) == 1 and np.all((values == 0) | (values == 1)):
|
|
779
|
+
return categories[int(ones[0])]
|
|
780
|
+
return None
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
def _coding(matrix: Any, categories: Sequence[str]) -> tuple[list[str | None], str | None]:
|
|
784
|
+
"""Return each contrast column's level and the coding's reference level, if any."""
|
|
785
|
+
matrix = np.asarray(matrix, dtype="float64")
|
|
786
|
+
levels = [_indicator(matrix[:, j], categories) for j in range(matrix.shape[1])]
|
|
787
|
+
reference = None
|
|
788
|
+
zero = [i for i in range(matrix.shape[0]) if not np.any(matrix[i])]
|
|
789
|
+
if all(level is not None for level in levels) and len(zero) == 1:
|
|
790
|
+
reference = categories[zero[0]]
|
|
791
|
+
return levels, reference
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
def _patsy_design(info: Any, design: _Design) -> None:
|
|
795
|
+
for term, subterms in info.term_codings.items():
|
|
796
|
+
if not term.factors:
|
|
797
|
+
continue
|
|
798
|
+
names = info.column_names[info.term_slices[term]]
|
|
799
|
+
variables = {factor: design.predictor(factor.name()) for factor in term.factors}
|
|
800
|
+
for subterm in subterms:
|
|
801
|
+
factors: list[list[_Part]] = []
|
|
802
|
+
for factor in subterm.factors:
|
|
803
|
+
factor_info = info.factor_infos[factor]
|
|
804
|
+
variable = variables[factor]
|
|
805
|
+
if factor_info.type == "categorical":
|
|
806
|
+
contrast = subterm.contrast_matrices[factor]
|
|
807
|
+
categories = [str(c) for c in factor_info.categories]
|
|
808
|
+
levels, reference = _coding(contrast.matrix, categories)
|
|
809
|
+
design.factor(variable, categories)
|
|
810
|
+
factors.append(
|
|
811
|
+
[
|
|
812
|
+
_Part(
|
|
813
|
+
factor.name() + suffix,
|
|
814
|
+
variable,
|
|
815
|
+
level,
|
|
816
|
+
indicator=level is not None,
|
|
817
|
+
categories=categories,
|
|
818
|
+
reference=reference,
|
|
819
|
+
)
|
|
820
|
+
for suffix, level in zip(contrast.column_suffixes, levels, strict=True)
|
|
821
|
+
]
|
|
822
|
+
)
|
|
823
|
+
else:
|
|
824
|
+
width = factor_info.num_columns
|
|
825
|
+
design.factor(variable)
|
|
826
|
+
factors.append(
|
|
827
|
+
[
|
|
828
|
+
_Part(
|
|
829
|
+
factor.name() if width == 1 else f"{factor.name()}[{i}]", variable
|
|
830
|
+
)
|
|
831
|
+
for i in range(width)
|
|
832
|
+
]
|
|
833
|
+
)
|
|
834
|
+
design.match(names, factors)
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
def _formulaic_design(spec: Any, design: _Design) -> None:
|
|
838
|
+
for term in spec.terms:
|
|
839
|
+
names = [spec.column_names[i] for i in spec.term_indices[term]]
|
|
840
|
+
factors: list[list[_Part]] = []
|
|
841
|
+
single_numeric: str | None = None
|
|
842
|
+
for factor in term.factors:
|
|
843
|
+
expression = str(factor.expr)
|
|
844
|
+
state = spec.encoder_state.get(expression)
|
|
845
|
+
kind = getattr(state[0], "value", None) if state else None
|
|
846
|
+
if kind not in ("categorical", "numerical"):
|
|
847
|
+
factors = []
|
|
848
|
+
break
|
|
849
|
+
variable = design.predictor(expression)
|
|
850
|
+
if kind == "numerical":
|
|
851
|
+
design.factor(variable)
|
|
852
|
+
factors.append([_Part(expression, variable)])
|
|
853
|
+
single_numeric = variable if len(term.factors) == 1 else None
|
|
854
|
+
continue
|
|
855
|
+
contrasts = state[1].get("contrasts")
|
|
856
|
+
categories = [str(c) for c in contrasts.levels]
|
|
857
|
+
design.factor(variable, categories)
|
|
858
|
+
parts = []
|
|
859
|
+
for reduced in (True, False):
|
|
860
|
+
coding = contrasts.contrasts.get_coding_matrix(
|
|
861
|
+
contrasts.levels, reduced_rank=reduced
|
|
862
|
+
)
|
|
863
|
+
template = contrasts.contrasts.get_factor_format(
|
|
864
|
+
contrasts.levels, reduced_rank=reduced
|
|
865
|
+
)
|
|
866
|
+
levels, reference = _coding(coding.to_numpy(), categories)
|
|
867
|
+
parts += [
|
|
868
|
+
_Part(
|
|
869
|
+
template.format(name=expression, field=field),
|
|
870
|
+
variable,
|
|
871
|
+
level,
|
|
872
|
+
indicator=level is not None,
|
|
873
|
+
categories=categories,
|
|
874
|
+
reference=reference,
|
|
875
|
+
)
|
|
876
|
+
for field, level in zip(coding.columns, levels, strict=True)
|
|
877
|
+
]
|
|
878
|
+
factors.append(parts)
|
|
879
|
+
if not factors:
|
|
880
|
+
continue
|
|
881
|
+
design.match(names, factors)
|
|
882
|
+
if single_numeric is not None:
|
|
883
|
+
# Multi-column transformations such as poly(x, 2) name columns their own way.
|
|
884
|
+
for name in names:
|
|
885
|
+
if name not in design.terms:
|
|
886
|
+
design.add(name, [_Part(name, single_numeric)])
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
def _position(name: str, formula: str) -> int:
|
|
890
|
+
right = formula.split("~", 1)[-1]
|
|
891
|
+
match = re.search(rf"(?<![\w.`]){re.escape(name)}(?![\w`])", right)
|
|
892
|
+
return match.start() if match else len(right)
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
def _labels(frame: Any) -> dict[str, str]:
|
|
896
|
+
attrs = getattr(frame, "attrs", None)
|
|
897
|
+
if not isinstance(attrs, dict):
|
|
898
|
+
return {}
|
|
899
|
+
for key in _LABEL_ATTRS:
|
|
900
|
+
labels = attrs.get(key)
|
|
901
|
+
if isinstance(labels, dict):
|
|
902
|
+
return {
|
|
903
|
+
str(name): label.strip()
|
|
904
|
+
for name, label in labels.items()
|
|
905
|
+
if isinstance(label, str) and label.strip()
|
|
906
|
+
}
|
|
907
|
+
return {}
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
def _variable(
|
|
911
|
+
name: str,
|
|
912
|
+
series: pd.Series | None,
|
|
913
|
+
role: str,
|
|
914
|
+
design: _Design | None = None,
|
|
915
|
+
label: str | None = None,
|
|
916
|
+
) -> dict[str, Any]:
|
|
917
|
+
categories = design.categories.get(name) if design else None
|
|
918
|
+
categorical = bool(categories) and name not in (design.numeric if design else set())
|
|
919
|
+
kind = _column_type(series) if series is not None else "number"
|
|
920
|
+
entry: dict[str, Any] = {"name": name, "role": role}
|
|
921
|
+
if categorical and categories:
|
|
922
|
+
entry["type"] = "boolean" if kind == "boolean" else "categorical"
|
|
923
|
+
if all(levels == categories[0] for levels in categories):
|
|
924
|
+
entry["levels"] = categories[0][:LEVEL_LIMIT]
|
|
925
|
+
references = design.references.get(name, set()) if design else set()
|
|
926
|
+
if len(references) == 1 and None not in references:
|
|
927
|
+
entry["reference"] = next(iter(references))
|
|
928
|
+
else:
|
|
929
|
+
entry["type"] = kind
|
|
930
|
+
if label:
|
|
931
|
+
entry["label"] = label
|
|
932
|
+
if series is not None:
|
|
933
|
+
summary = _summary(series, "string" if entry["type"] == "categorical" else kind)
|
|
934
|
+
summary.pop("histogram", None)
|
|
935
|
+
entry["summary"] = summary
|
|
936
|
+
return entry
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
def _used_rows(model: Any, frame: Any) -> pd.DataFrame | None:
|
|
940
|
+
"""Return the rows of a formula model's data that the fit used, or None if unsure."""
|
|
941
|
+
if not isinstance(frame, pd.DataFrame):
|
|
942
|
+
return None
|
|
943
|
+
count = len(np.asarray(model.endog))
|
|
944
|
+
labels = getattr(model.data, "row_labels", None)
|
|
945
|
+
if (
|
|
946
|
+
labels is not None
|
|
947
|
+
and len(labels) == count
|
|
948
|
+
and frame.index.is_unique
|
|
949
|
+
and pd.Index(labels).isin(frame.index).all()
|
|
950
|
+
):
|
|
951
|
+
used: pd.DataFrame = frame.loc[labels]
|
|
952
|
+
return used
|
|
953
|
+
return frame if len(frame) == count else None
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
def _group_values(model: Any, count: int) -> pd.Series | None:
|
|
957
|
+
"""Rebuild a mixed model's grouping column from its per-group row positions."""
|
|
958
|
+
rows = getattr(model, "row_indices", None)
|
|
959
|
+
if not isinstance(rows, dict):
|
|
960
|
+
return None
|
|
961
|
+
values = np.empty(count, dtype=object)
|
|
962
|
+
for group, positions in rows.items():
|
|
963
|
+
values[np.asarray(positions, dtype=int)] = group
|
|
964
|
+
series = pd.Series(values)
|
|
965
|
+
return series.infer_objects() if not series.isna().any() else None
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
def _group_name(model: Any) -> str | None:
|
|
969
|
+
names = list(getattr(model.data, "exog_re_names", None) or [])
|
|
970
|
+
exog_re = np.asarray(getattr(model, "exog_re", np.empty((0, 0))))
|
|
971
|
+
intercept = exog_re.ndim == 2 and exog_re.size > 0 and bool(np.all(exog_re[:, 0] == 1))
|
|
972
|
+
if intercept and names and names[0] != "Group":
|
|
973
|
+
return str(names[0])
|
|
974
|
+
return None
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
def _statsmodels_variables(result: Any, coefficients: dict[str, Any]) -> dict[str, Any]:
|
|
978
|
+
"""Return ``variables`` and ``terms`` for a statsmodels result, or {} when unknown."""
|
|
979
|
+
model = result.model
|
|
980
|
+
data = model.data
|
|
981
|
+
spec = getattr(data, "model_spec", None)
|
|
982
|
+
variables: list[dict[str, Any]] = []
|
|
983
|
+
if spec is not None:
|
|
984
|
+
frame = _used_rows(model, getattr(data, "frame", None))
|
|
985
|
+
if frame is None:
|
|
986
|
+
return {}
|
|
987
|
+
columns = [str(c) for c in frame.columns]
|
|
988
|
+
if isinstance(frame.columns, pd.MultiIndex) or len(set(columns)) != len(columns):
|
|
989
|
+
return {}
|
|
990
|
+
frame = frame.set_axis(columns, axis=1)
|
|
991
|
+
design = _Design(columns)
|
|
992
|
+
if _has_class(spec, "patsy", "DesignInfo"):
|
|
993
|
+
_patsy_design(spec, design)
|
|
994
|
+
elif _has_class(spec, "formulaic", "ModelSpec"):
|
|
995
|
+
_formulaic_design(spec, design)
|
|
996
|
+
else:
|
|
997
|
+
return {}
|
|
998
|
+
labels = _labels(frame)
|
|
999
|
+
formula = getattr(model, "formula", None)
|
|
1000
|
+
response = None
|
|
1001
|
+
if isinstance(formula, str) and "~" in formula:
|
|
1002
|
+
used = _referenced(formula.split("~", 1)[0].strip(), design.columns)
|
|
1003
|
+
response = next(iter(used)) if len(used) == 1 else None
|
|
1004
|
+
if response is not None:
|
|
1005
|
+
variables.append(
|
|
1006
|
+
_variable(response, frame[response], "response", label=labels.get(response))
|
|
1007
|
+
)
|
|
1008
|
+
predictors = design.predictors
|
|
1009
|
+
if isinstance(formula, str):
|
|
1010
|
+
# Patsy and formulaic sort terms; list predictors as the formula names them.
|
|
1011
|
+
predictors = sorted(predictors, key=lambda name: _position(name, formula))
|
|
1012
|
+
variables += [
|
|
1013
|
+
_variable(name, frame[name], "predictor", design, labels.get(name))
|
|
1014
|
+
for name in predictors
|
|
1015
|
+
if name != response
|
|
1016
|
+
]
|
|
1017
|
+
mapping = design.terms
|
|
1018
|
+
if type(model).__name__ == "MixedLM":
|
|
1019
|
+
group = _group_name(model)
|
|
1020
|
+
values = _group_values(model, len(frame)) if group else None
|
|
1021
|
+
if group and values is not None and group not in {v["name"] for v in variables}:
|
|
1022
|
+
variables.append(_variable(group, values, "group", label=labels.get(group)))
|
|
1023
|
+
else:
|
|
1024
|
+
exog = getattr(data, "orig_exog", None)
|
|
1025
|
+
endog = getattr(data, "orig_endog", None)
|
|
1026
|
+
count = len(np.asarray(model.endog))
|
|
1027
|
+
if not isinstance(exog, pd.DataFrame) or len(exog) != count:
|
|
1028
|
+
return {}
|
|
1029
|
+
const = getattr(data, "const_idx", None)
|
|
1030
|
+
names = [str(c) for i, c in enumerate(exog.columns) if i != const]
|
|
1031
|
+
if len(set(names)) != len(names):
|
|
1032
|
+
return {}
|
|
1033
|
+
labels = _labels(exog)
|
|
1034
|
+
if isinstance(endog, pd.Series) and endog.name is not None and len(endog) == count:
|
|
1035
|
+
variables.append(
|
|
1036
|
+
_variable(str(endog.name), endog, "response", label=labels.get(str(endog.name)))
|
|
1037
|
+
)
|
|
1038
|
+
variables += [
|
|
1039
|
+
_variable(str(c), exog.iloc[:, i], "predictor", label=labels.get(str(c)))
|
|
1040
|
+
for i, c in enumerate(exog.columns)
|
|
1041
|
+
if i != const
|
|
1042
|
+
]
|
|
1043
|
+
mapping = {name: [{"variable": name}] for name in names}
|
|
1044
|
+
two_way = getattr(result.params, "ndim", 1) == 2
|
|
1045
|
+
return _variables_fields(variables, mapping, coefficients, two_way)
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
def _variables_fields(
|
|
1049
|
+
variables: list[dict[str, Any]],
|
|
1050
|
+
mapping: dict[str, list[dict[str, str]]],
|
|
1051
|
+
coefficients: dict[str, Any],
|
|
1052
|
+
prefixed: bool = False,
|
|
1053
|
+
) -> dict[str, Any]:
|
|
1054
|
+
"""Cap the variables, then map each coefficient term cell to its parts."""
|
|
1055
|
+
seen: set[str] = set()
|
|
1056
|
+
kept = []
|
|
1057
|
+
for variable in variables:
|
|
1058
|
+
if variable["name"] not in seen:
|
|
1059
|
+
seen.add(variable["name"])
|
|
1060
|
+
kept.append(variable)
|
|
1061
|
+
kept = kept[:VARIABLE_LIMIT]
|
|
1062
|
+
names = {v["name"] for v in kept}
|
|
1063
|
+
terms = []
|
|
1064
|
+
for row in coefficients["rows"]:
|
|
1065
|
+
cell = row[0]
|
|
1066
|
+
parts = mapping.get(cell) if isinstance(cell, str) else None
|
|
1067
|
+
if parts is None and prefixed and isinstance(cell, str) and ": " in cell:
|
|
1068
|
+
# Multi-equation tables label each term "<equation>: <term>".
|
|
1069
|
+
parts = next((p for name, p in mapping.items() if cell.endswith(f": {name}")), None)
|
|
1070
|
+
if not parts or len(parts) > PART_LIMIT:
|
|
1071
|
+
continue
|
|
1072
|
+
if any(part["variable"] not in names for part in parts):
|
|
1073
|
+
continue
|
|
1074
|
+
terms.append({"term": cell, "parts": parts})
|
|
1075
|
+
if len(terms) == TERM_LIMIT:
|
|
1076
|
+
break
|
|
1077
|
+
fields: dict[str, Any] = {}
|
|
1078
|
+
if kept:
|
|
1079
|
+
fields["variables"] = kept
|
|
1080
|
+
if terms:
|
|
1081
|
+
fields["terms"] = terms
|
|
1082
|
+
return fields
|
|
1083
|
+
|
|
1084
|
+
|
|
664
1085
|
# --------------------------------------------------------------------------- machine learning
|
|
665
1086
|
|
|
666
1087
|
|
|
@@ -738,6 +1159,17 @@ def _sklearn_document(model: Any) -> dict[str, Any]:
|
|
|
738
1159
|
exponentiate=None if binary else False,
|
|
739
1160
|
)
|
|
740
1161
|
frame = pd.DataFrame({"term": terms, "estimate": estimates})
|
|
1162
|
+
coefficients = table(frame, "Coefficients", role="coefficients")
|
|
1163
|
+
variables: dict[str, Any] = {}
|
|
1164
|
+
if getattr(model, "feature_names_in_", None) is not None and features == [
|
|
1165
|
+
str(n) for n in model.feature_names_in_
|
|
1166
|
+
]:
|
|
1167
|
+
# Linear models fit on a data frame: one numeric input column per coefficient.
|
|
1168
|
+
variables = _variables_fields(
|
|
1169
|
+
[{"name": f, "role": "predictor", "type": "number"} for f in features],
|
|
1170
|
+
{f"{label}{f}": [{"variable": f}] for label in labels for f in features},
|
|
1171
|
+
coefficients,
|
|
1172
|
+
)
|
|
741
1173
|
metrics = _metrics(
|
|
742
1174
|
[
|
|
743
1175
|
*_params(model, ("alpha", "l1_ratio", "C", "penalty", "fit_intercept")),
|
|
@@ -745,11 +1177,15 @@ def _sklearn_document(model: Any) -> dict[str, Any]:
|
|
|
745
1177
|
("n_classes", len(classes) if classes else None),
|
|
746
1178
|
]
|
|
747
1179
|
)
|
|
748
|
-
return
|
|
749
|
-
"model"
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
1180
|
+
return (
|
|
1181
|
+
_document(model, "model")
|
|
1182
|
+
| {
|
|
1183
|
+
"model": description,
|
|
1184
|
+
"tables": [coefficients],
|
|
1185
|
+
"metrics": metrics,
|
|
1186
|
+
}
|
|
1187
|
+
| variables
|
|
1188
|
+
)
|
|
753
1189
|
if hasattr(model, "feature_importances_"):
|
|
754
1190
|
importance = np.asarray(model.feature_importances_, dtype="float64")
|
|
755
1191
|
document = _importance_document(
|
|
@@ -841,7 +1277,18 @@ def structured(source: Any) -> dict[str, Any] | None:
|
|
|
841
1277
|
if document is None:
|
|
842
1278
|
return None
|
|
843
1279
|
# Readable key order: what it is, then the description, then tables and metrics.
|
|
844
|
-
order = (
|
|
1280
|
+
order = (
|
|
1281
|
+
"schema",
|
|
1282
|
+
"kind",
|
|
1283
|
+
"source",
|
|
1284
|
+
"formula",
|
|
1285
|
+
"model",
|
|
1286
|
+
"test",
|
|
1287
|
+
"tables",
|
|
1288
|
+
"metrics",
|
|
1289
|
+
"variables",
|
|
1290
|
+
"terms",
|
|
1291
|
+
)
|
|
845
1292
|
return {key: document[key] for key in order if key in document} | document
|
|
846
1293
|
|
|
847
1294
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: framework-py
|
|
3
|
-
Version: 0.1.0.
|
|
3
|
+
Version: 0.1.0.dev25
|
|
4
4
|
Summary: Structured data science projects: a data catalog, integrity tracking, caching, outputs, and AI guardrails
|
|
5
5
|
Project-URL: Homepage, https://framework.pub
|
|
6
6
|
Project-URL: Source, https://github.com/frameworktools/framework-py
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
framework/__init__.py,sha256=wu0kbAo9WBq74juy_W5v55CuB2KsabuiS21hZC5iAJU,6700
|
|
2
2
|
framework/__main__.py,sha256=S0ChmRQnNa9W5_MPXhHnH61Kjr_pNthkeS41eJU1w0U,9093
|
|
3
3
|
framework/_agents_md.py,sha256=PSWPJiCUTRirk81sz-Y33qHnqY16HOhHY8pH77ct1ik,11857
|
|
4
|
-
framework/_artifacts.py,sha256=
|
|
4
|
+
framework/_artifacts.py,sha256=rJVMifCuZ0nnaEFoD8TCMFaca-tga20kngsAtXDyXLI,54226
|
|
5
5
|
framework/_backups.py,sha256=ra1DhGcIbIXB0zqDFVFJ3yPEjmXpYKFX-fjV01b9Rtg,1958
|
|
6
6
|
framework/_csv.py,sha256=oAaLUChyUsHJsh0YuUDt0viC8SAImMgUseNKGk7kLXo,4855
|
|
7
7
|
framework/_defaults_cache.py,sha256=pyqQ7rMcKwo_fTSAvDDNyQXIAjiJZfWccnkRPw340WQ,1711
|
|
@@ -139,7 +139,7 @@ framework/templates/stubs/notebook-default.qmd,sha256=8Js1S2BcHYeQWmJS-kt31gBvIj
|
|
|
139
139
|
framework/templates/stubs/notebook-minimal.qmd,sha256=2U2k8judffhrh_6m6r6OAbQBCYEtViAjeKRBqBYCJTM,178
|
|
140
140
|
framework/templates/stubs/notebook-revealjs.qmd,sha256=_ztT_gHoudpB5DIjlbvtB5zQ8163DeV8xbWcT-HgJrI,409
|
|
141
141
|
framework/templates/stubs/script-default.py,sha256=vbWqgW0_rHot17mQC3tYb92g_9L-cbTOeL3y99BRrSk,216
|
|
142
|
-
framework_py-0.1.0.
|
|
143
|
-
framework_py-0.1.0.
|
|
144
|
-
framework_py-0.1.0.
|
|
145
|
-
framework_py-0.1.0.
|
|
142
|
+
framework_py-0.1.0.dev25.dist-info/METADATA,sha256=n_1EYAEJZWoCVgexl1ZvSNxxLWCyp7M8DXgDM-rLD78,4682
|
|
143
|
+
framework_py-0.1.0.dev25.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
144
|
+
framework_py-0.1.0.dev25.dist-info/licenses/LICENSE,sha256=giOOgSwALkZimC7uhLZQK8Untez641bLW1-2-Cjvmq0,1072
|
|
145
|
+
framework_py-0.1.0.dev25.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|