github-actions[bot]
Automated Deploy: a19ede424a80b7b8d7f6978585141d1bb45de69c
326d3cc
Raw History Blame Contribute Delete
3 kB
from .contracts import Finding
from pathlib import Path
import yaml
def evaluate_rules(metrics,events,context,trials,path):
catalogue=yaml.safe_load(Path(path).read_text());findings=[]
current={m.metric_id:m for m in metrics}
for rule in catalogue.get("rules",[]):
required=("id","version","required_metrics","required_events","geometry_sources","validation_scopes",
"maximum_sensitivity","minimum_trials","context","source","evidence_strength","interpretation","exercise","objective")
if any(name not in rule for name in required):raise ValueError("Incomplete coaching rule contract")
source=rule["source"]
if not source.get("url") or source.get("supports_intervention") is not True or not source.get("context"):continue
if any(context.get(k)!=v for k,v in rule["context"].items()):continue
if any(not any(e.type==name and e.state=="confirmed" for e in events) for name in rule["required_events"]):continue
selected=[current.get(name) for name in rule["required_metrics"]]
if any(m is None or m.value is None or m.suitability!="supported" for m in selected):continue
if any(m.geometry_source not in rule["geometry_sources"] or m.validation_status not in rule["validation_scopes"] for m in selected):continue
if rule["maximum_sensitivity"] is not None and any(m.sensitivity is None or m.sensitivity["maximum_value_change"]>rule["maximum_sensitivity"] for m in selected):continue
comparable=[trial for trial in trials if all(any(m["metric_id"]==name and m["value"] is not None for m in trial["metrics"]) for name in rule["required_metrics"])]
if len(comparable)+1<rule["minimum_trials"]:continue
exercise={**rule["exercise"],"source":source,"objective":rule["objective"],
"status":"educational_research_hypothesis","expected_benefit":"not_established","comparable_trial_count":len(comparable)+1}
findings.append(Finding("rule_"+rule["id"],rule["required_metrics"],rule["id"],rule["version"],rule["interpretation"],
sorted({i for m in selected for i in m.evidence_frames}),"not_reviewed",rule["evidence_strength"],exercise))
return findings
def assess(metrics, context, events=None, trials=None, rules_path=None, mode="descriptive_only"):
findings = []
for m in metrics:
if m.value is None or m.suitability != "supported":
continue
findings.append(Finding("observation_"+m.metric_id, [m.metric_id], "describe_available_measurement", "1.0",
f"{m.metric_id.replace('_', ' ')}: {m.value:.2f} {m.unit}. "
"This describes this recording; no persistent fault or benefit is established.",
m.evidence_frames))
if mode=="evidence_linked_experimental" and rules_path:
findings.extend(evaluate_rules(metrics,events or [],context,trials or [],rules_path))
return findings