51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 | def validate_gp3bayes_object(
x: Any, recursive: bool = True, strict: bool = False
) -> ObjectValidation:
if not isinstance(recursive, bool) or not isinstance(strict, bool):
raise GP3BayesError("`recursive` and `strict` must be TRUE or FALSE.")
rows: list[dict[str, str]] = []
def check(name: str, ok: bool, detail: str, review: bool = False) -> None:
status = "pass" if ok else ("review" if review else "fail")
rows.append({"check": name, "status": status, "detail": detail})
check("gp3bayes_class", _recognized(x), type(x).__name__)
family = _family(x)
if family is not None:
check("approved_family", family in {"binary", "duration", "pupil"}, family)
if hasattr(x, "contract_version"):
required: tuple[str, ...] = (
"family",
"model_family",
"mappings",
"predictors",
"likelihood",
"link",
)
missing = [name for name in required if not hasattr(x, name)]
check("contract_fields", not missing, "complete" if not missing else ", ".join(missing))
if hasattr(x, "data") and hasattr(x, "contract") and hasattr(x, "transformations"):
data = x.data
check("prepared_fields", True, "complete")
check(
"prepared_data",
isinstance(data, pd.DataFrame) and len(data) > 0,
f"{len(data)} rows" if isinstance(data, pd.DataFrame) else "not a data frame",
)
if hasattr(x, "priors") and hasattr(x, "formula"):
required = ("family", "contract", "formula", "priors")
missing = [name for name in required if not hasattr(x, name)]
check(
"specification_fields", not missing, "complete" if not missing else ", ".join(missing)
)
if hasattr(x, "backend_fit") and hasattr(x, "sampling_backend"):
required = (
"family",
"specification",
"backend_fit",
"sampling_backend",
"algorithm",
"sampling",
"fit_performed",
)
missing = [name for name in required if not hasattr(x, name)]
check("fit_fields", not missing, "complete" if not missing else ", ".join(missing))
performed = getattr(x, "fit_performed", False) is True
check("fit_performed", performed, str(performed), review=not performed)
if hasattr(x, "status") and "diagnostic" in type(x).__name__.lower():
check(
"diagnostic_status",
getattr(x, "status", None) is not None,
str(getattr(x, "status", "missing")),
)
table = getattr(x, "table", None)
if "summary" in type(x).__name__.lower() and table is not None:
check(
"posterior_summary_table",
isinstance(table, pd.DataFrame) and len(table) > 0,
f"{len(table)} rows" if isinstance(table, pd.DataFrame) else "missing",
)
if recursive:
for name in ("contract", "prepared", "specification"):
child = getattr(x, name, None)
if child is None or child is x or not _recognized(child):
continue
child_result = validate_gp3bayes_object(child, recursive=False, strict=False)
check(f"nested_{name}", child_result.status != "fail", child_result.status)
frame = pd.DataFrame(rows)
status = (
"fail"
if (frame["status"] == "fail").any()
else ("review" if (frame["status"] == "review").any() else "pass")
)
result = ObjectValidation("0.2", status, type(x).__name__, family, frame)
if strict and status == "fail":
failed = ", ".join(frame.loc[frame["status"] == "fail", "check"])
raise GP3BayesError(f"gp3bayes object validation failed: {failed}.")
return result
|