Skip to content

gp3bayespy.unified_workflow_api

6 public functions in this module.

← API reference hub

Source code in src/gp3bayespy/unified_workflow_api.py
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
def check_model_ppc(
    fit: Any,
    draws: int = 500,
    seed: int = 1,
    pass_probability: float = 0.80,
    review_probability: float = 0.95,
):
    validate_gp3bayes_object(fit, strict=True)
    family = _family(fit)
    if family == "binary":
        from .binary import check_binary_posterior_predictive

        return check_binary_posterior_predictive(
            fit,
            draws=draws,
            seed=seed,
            pass_probability=pass_probability,
            review_probability=review_probability,
        )
    if family == "duration":
        from .duration import check_duration_posterior_predictive

        return check_duration_posterior_predictive(
            fit,
            draws=draws,
            seed=seed,
            pass_probability=pass_probability,
            review_probability=review_probability,
        )
    if family == "pupil":
        try:
            from .pupil import check_pupil_posterior_predictive
        except (ImportError, AttributeError) as exc:
            raise GP3BayesError(
                "Pupil posterior predictive checks are not available in this build."
            ) from exc
        return check_pupil_posterior_predictive(fit, ndraws=draws)
    raise GP3BayesError("Unsupported gp3bayes fit family.")
Source code in src/gp3bayespy/unified_workflow_api.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def diagnose_model_fit(
    fit: Any,
    rhat_pass: float = 1.01,
    rhat_fail: float = 1.05,
    ess_per_chain_pass: float = 100,
    ess_per_chain_fail: float = 50,
    maximum_treedepth_fraction: float = 0.01,
    ebfmi_pass: float = 0.3,
    ebfmi_fail: float = 0.2,
):
    validate_gp3bayes_object(fit, strict=True)
    family = _family(fit)
    common = dict(
        rhat_pass=rhat_pass,
        rhat_fail=rhat_fail,
        ess_per_chain_pass=ess_per_chain_pass,
        ess_per_chain_fail=ess_per_chain_fail,
        maximum_treedepth_fraction=maximum_treedepth_fraction,
        ebfmi_pass=ebfmi_pass,
        ebfmi_fail=ebfmi_fail,
    )
    if family == "binary":
        from .binary import diagnose_binary_fit

        return diagnose_binary_fit(fit, **common)
    if family == "duration":
        from .duration import diagnose_duration_fit

        return diagnose_duration_fit(fit, **common)
    if family == "pupil":
        try:
            from .pupil import diagnose_pupil_fit
        except (ImportError, AttributeError) as exc:
            raise GP3BayesError("Pupil diagnostics are not available in this build.") from exc
        return diagnose_pupil_fit(fit)
    raise GP3BayesError("Unsupported gp3bayes fit family.")
Source code in src/gp3bayespy/unified_workflow_api.py
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
def estimate_model_estimands(fit: Any, probability: float = 0.95):
    validate_gp3bayes_object(fit, strict=True)
    family = _family(fit)
    if family == "binary":
        try:
            from .specification_closure import estimate_standardized_probability_contrast
        except (ImportError, AttributeError) as exc:
            raise GP3BayesError(
                "Binary standardized estimands are not available in this build."
            ) from exc
        return estimate_standardized_probability_contrast(fit)
    if family == "duration":
        try:
            from .specification_closure import estimate_standardized_duration_estimands
        except (ImportError, AttributeError) as exc:
            raise GP3BayesError(
                "Duration standardized estimands are not available in this build."
            ) from exc
        return estimate_standardized_duration_estimands(fit)
    if family == "pupil":
        raise GP3BayesError(
            "The frozen unified estimand dispatcher supports binary and duration fits only. "
            "Use an explicit pupil estimand function for pupil fits."
        )
    raise GP3BayesError("Unsupported gp3bayes fit family.")
Source code in src/gp3bayespy/unified_workflow_api.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
def model_workflow_status(x: Any) -> pd.DataFrame:
    source_fit = x if getattr(x, "fit_performed", False) is True else getattr(x, "fit", None)
    specification = (
        x if hasattr(x, "priors") and hasattr(x, "formula") else getattr(x, "specification", None)
    )
    if specification is None and source_fit is not None:
        specification = getattr(source_fit, "specification", None)
    prepared = (
        x
        if hasattr(x, "data") and hasattr(x, "transformations")
        else getattr(specification, "prepared", None)
    )
    contract = (
        x
        if hasattr(x, "contract_version")
        else getattr(specification, "contract", None) or getattr(prepared, "contract", None)
    )
    components = getattr(x, "components", {}) or {}
    stages = pd.DataFrame(
        {
            "stage": [
                "contract",
                "prepared_data",
                "specification",
                "fit",
                "diagnostics",
                "posterior_summary",
                "ppc",
                "estimands",
                "sensitivity",
                "predictive_validation",
                "manifest",
            ],
            "completed": [
                contract is not None,
                prepared is not None,
                specification is not None,
                source_fit is not None or getattr(x, "fit_performed", False) is True,
                components.get("diagnostics") is not None if hasattr(components, "get") else False,
                components.get("posterior") is not None if hasattr(components, "get") else False,
                components.get("ppc") is not None if hasattr(components, "get") else False,
                components.get("estimands") is not None if hasattr(components, "get") else False,
                components.get("sensitivity") is not None if hasattr(components, "get") else False,
                (components.get("loo") is not None or components.get("kfold") is not None)
                if hasattr(components, "get")
                else False,
                components.get("manifest") is not None
                if hasattr(components, "get")
                else "manifest" in type(x).__name__.lower(),
            ],
        }
    )
    stages.attrs["structural_stage_map_only"] = True
    return stages
Source code in src/gp3bayespy/unified_workflow_api.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def summarise_model_posterior(
    fit: Any,
    probability: float = 0.95,
    variables: Any = None,
):
    validate_gp3bayes_object(fit, strict=True)
    family = _family(fit)
    if family == "binary":
        from .binary import summarise_binary_posterior

        return summarise_binary_posterior(fit, probability=probability, variables=variables)
    if family == "duration":
        from .duration import summarise_duration_posterior

        return summarise_duration_posterior(fit, probability=probability, variables=variables)
    if family == "pupil":
        try:
            from .pupil import summarise_pupil_posterior
        except (ImportError, AttributeError) as exc:
            raise GP3BayesError(
                "Pupil posterior summaries are not available in this build."
            ) from exc
        return summarise_pupil_posterior(fit, probability=probability)
    raise GP3BayesError("Unsupported gp3bayes fit family.")
Source code in src/gp3bayespy/unified_workflow_api.py
 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