495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657 | def audit_model_readiness_strict(
data: pd.DataFrame,
contract: ModelContract,
condition_warning_fraction: float = 0.10,
condition_failure_fraction: float = 0.02,
identifier_unique_fraction: float = 0.90,
duration_allowed_range: Sequence[float] | None = None,
censor_col: str | None = None,
run_separation: bool = True,
) -> StrictReadinessAudit:
contract = _contract(contract)
base = audit_model_readiness(data, contract)
balance = summarise_condition_balance(
data, contract, condition_warning_fraction, condition_failure_fraction
)
identifier = identify_identifier_like_predictors(data, contract, identifier_unique_fraction)
rows = [
_check_row(
"overall_condition_balance",
"design",
"warn" if balance.status == "review" else balance.status,
balance.interpretation
if balance.status == "not_applicable"
else f"Minimum observed condition fraction = {balance.minimum_fraction:.4g}.",
),
_check_row(
"identifier_like_predictors",
"predictors",
"warn" if identifier.status == "review" else identifier.status,
"Identifier-like predictors require review: " + ", ".join(identifier.flagged)
if identifier.flagged
else "No declared predictor met the identifier-like heuristic.",
len(identifier.flagged),
),
]
# Reuse the design-support matrix builder for exact coding parity.
try:
matrix, _ = _closure_fixed_model_matrix(data, contract)
rank = int(np.linalg.matrix_rank(matrix))
columns = int(matrix.shape[1])
rank_info = {"rank": rank, "columns": columns, "error": None}
rows.append(
_check_row(
"fixed_effect_rank",
"design",
"fail" if rank < columns else "pass",
f"Fixed-effects matrix rank {rank} of {columns}.",
)
)
except Exception as exc:
rank_info = {"rank": None, "columns": None, "error": str(exc)} # type: ignore[dict-item]
rows.append(
_check_row(
"fixed_effect_rank",
"design",
"fail",
f"The fixed-effects matrix could not be constructed: {exc}",
)
)
variation = None
separation = None
extremes = None
boundaries = None
if contract.family == "binary":
variation = summarise_binary_group_variation(data, contract, "participant")
rows.append(
_check_row(
"participant_binary_outcome_variation",
"outcome",
"warn" if variation.status == "review" else variation.status,
f"{variation.n_no_variation} participant groups have no observed binary outcome variation.",
variation.n_no_variation,
)
)
if run_separation:
try:
from types import SimpleNamespace
from .advanced_optional_workflows import detect_binary_separation
separation = detect_binary_separation(
SimpleNamespace(contract=contract, prepared=SimpleNamespace(data=data))
)
detected = bool(
getattr(separation, "separation_detected", False)
if not isinstance(separation, Mapping)
else separation.get("separation_detected", False)
)
rows.append(
_check_row(
"fixed_effect_separation",
"design",
"warn" if detected else "pass",
"The fixed-effects logistic screen detected separation."
if detected
else "The fixed-effects logistic separation screen did not detect separation.",
)
)
except Exception as exc:
rows.append(
_check_row(
"fixed_effect_separation",
"design",
"warn",
f"Separation screening could not be completed: {exc}",
)
)
else:
extremes = review_duration_extremes(data, contract)
rows.append(
_check_row(
"duration_extreme_review",
"outcome",
"warn" if extremes.status == "review" else extremes.status,
f"{extremes.n_flagged} duration observations were flagged for extreme-value review.",
extremes.n_flagged,
)
)
boundaries = audit_duration_boundaries(data, contract, duration_allowed_range, censor_col)
rows.extend(boundaries.checks.to_dict("records")) # type: ignore[arg-type]
base_checks = getattr(base, "checks", pd.DataFrame())
if not isinstance(base_checks, pd.DataFrame):
base_checks = pd.DataFrame()
extras = pd.DataFrame(rows)
# Keep the common readiness columns if available, otherwise retain closure rows.
if not base_checks.empty:
common = [c for c in base_checks.columns if c in extras.columns]
combined = (
pd.concat([base_checks[common], extras[common]], ignore_index=True)
if common
else extras
)
else:
combined = extras
statuses = combined["status"].astype(str)
counts = {
name: int((statuses == name).sum()) for name in ("pass", "warn", "fail", "not_applicable")
}
ready = counts["fail"] == 0
status = "not_ready" if not ready else ("ready_with_warnings" if counts["warn"] else "ready")
return StrictReadinessAudit(
"0.2",
contract.family,
ready,
status,
counts,
combined,
base,
balance,
variation,
identifier,
rank_info,
separation,
extremes,
boundaries,
contract,
{
"condition_warning_fraction": condition_warning_fraction,
"condition_failure_fraction": condition_failure_fraction,
"identifier_unique_fraction": identifier_unique_fraction,
"duration_allowed_range": duration_allowed_range,
},
)
|