scripts/build_context_evidence_graph.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/build_context_evidence_graph.py · SHA-256: 2758ba22e378ffed…
"M0060:L4",
"M0079:L2",
"M0097:L2",
"M0105:L3",
"M0155:L3",
"M0178:L1",
"M0178:L2",
"M0382:L1",
"M0528:L1",
"M0545:L1",
"M1430:L3",
"M1439:L1",
"M1566:L3",
"M1867:L1",
"M2895:L1",
"M3020:L1",
"PMID:10531307:L1",
"PMID:16177096:L1",
"PMID:24222351:L1",
"PMID:25491100:L1",
"PMID:38844131:L1",
"R1_0134:L1",
"R1_0134:L2",
"R1_0137:L1",
"R1_0137:L2",
"R1_0189:L1",
"R1_0224:L4",
"R1_0238:L3",
"R1_0317:L4",
"R1_0378:L3",
"R1_0390:L1",
"R1_0393:L3",
"M1421:L1",
"M1422:L1",
"M1422:L2",
"M1453:L1",
"M2036:L1",
"M2449:L1",
"PMID:19555051:L1",
"M1522:L4",
"PMID:17375075:L1",
"PMID:42020736:L1",
"PMID:42056515:L1",
"PMID:42749803:L1",
"M0189:L1",
"M1407:L1",
"PMID:36944335:L1",
"PMID:39753706:L1",
"PMID:41436438:L1",
"PMID:41999746:L1",
"M0051:L4",
"M0154:L3",
"M0159:L3",
"M0206:L3",
"M0241:L3",
# Legacy edges downgraded under IIP-0035 after all cited outcomes reconciled to qualitative_no_pvalue (plus M0051:L3 & M0154:L2 unvalidated_transfer and M0206:L2 untested screening-policy novel_upgrade_hypothesis)
"M0051:L1",
"M0051:L2",
"M0051:L3",
"M0154:L1",
"M0154:L2",
"M0159:L1",
"M0159:L2",
"M0206:L2",
"M0241:L1",
"M0241:L2",
# Wave-5 (IIP-0041 / IIP-0042 / IIP-0043) 45 scoping/non-certifying edges (primary_certification_eligible=False)
# Includes M0300:L2 (Vendrame 2010 unvalidated_transfer / novel_upgrade_hypothesis on unedited islets; M0300:L1 stays eligible for direct empirical SPK recurrence)
"M0066:L3", "M0085:L4", "M0184:L4", "M0300:L2", "M0300:L3", "M0458:L3",
"M0612:L1", "M0630:L4", "M0941:L1", "M1054:L1", "M2123:L1", "M3217:L1",
"PMID:11024144:L1", "PMID:17494992:L1", "PMID:18023480:L1", "PMID:19264985:L1",
"PMID:2139669:L1", "PMID:26061776:L1", "PMID:28092375:L1", "PMID:28494868:L1",
"PMID:28553952:L1", "PMID:29514070:L1", "PMID:30012675:L1", "PMID:30074059:L1",
"PMID:30569273:L1", "PMID:30967424:L1", "PMID:31180194:L1", "PMID:33632742:L1",
"PMID:33658358:L1", "PMID:34130115:L1", "PMID:35883656:L1", "PMID:37531253:L1",
"PMID:37643018:L1", "PMID:37861217:L1", "PMID:39137044:L1", "PMID:39715591:L1",
"PMID:39915484:L1", "PMID:40598585:L1", "PMID:40976248:L2", "PMID:41026533:L1",
"PMID:41196293:L1", "PMID:42875686:L1", "R1_0095:L4", "R1_0099:L3", "R1_0287:L4",
# CEG v1 quality sweep & cutover (49 Part-B/M0452 downgrades + 23 net-new non-certifying edges incl. PMID:38879678:L2/L3)
"M0452:L1", "M0452:L2", "PMID:10429675:L2", "PMID:10850706:L1", "PMID:10850706:L2", "PMID:11242052:L1", "PMID:11242052:L2", "PMID:11323700:L1", "PMID:11323700:L2", "PMID:11907065:L1", "PMID:11907065:L2", "PMID:12411439:L1", "PMID:12411439:L2", "PMID:14566337:L1", "PMID:1512546:L1", "PMID:16034111:L1", "PMID:17475856:L1", "PMID:17475856:L2", "PMID:17982459:L1", "PMID:17982459:L2", "PMID:18339401:L1", "PMID:18448674:L1", "PMID:18448674:L2", "PMID:18585785:L1", "PMID:18657508:L1", "PMID:19262615:L1", "PMID:21743387:L1", "PMID:21743387:L2", "PMID:23539542:L2", "PMID:27872147:L1", "PMID:27872147:L2", "PMID:29302013:L2", "PMID:29345283:L1", "PMID:29345283:L2", "PMID:29632329:L1", "PMID:29632329:L2", "PMID:30833751:L1", "PMID:30833751:L3", "PMID:32134744:L1", "PMID:32134744:L2", "PMID:33599743:L1", "PMID:33846289:L2", "PMID:34380648:L1", "PMID:37127662:L1", "PMID:37127662:L2", "PMID:38246194:L1", "PMID:38401844:L2", "PMID:38401844:L3", "PMID:38402281:L1", "PMID:38402281:L2", "PMID:38879678:L2", "PMID:38879678:L3", "PMID:38926615:L1", "PMID:38926615:L2", "PMID:39004142:L1", "PMID:39004142:L2", "PMID:39382088:L1", "PMID:40605797:L1", "PMID:40605797:L2", "PMID:40769155:L1", "PMID:41642947:L1", "PMID:41840058:L1", "PMID:42425151:L1", "PMID:42660457:L1", "PMID:42660457:L2", "PMID:42660457:L3", "PMID:42926365:L1", "PMID:8120026:L1", "PMID:8910377:L1", "PMID:8910377:L2", "PMID:8910377:L3", "PMID:9660937:L1",
})
CANONICAL_8_FIDELITY_TIERS = frozenset({
"T1_Human_Clinical",
"T2_NHP",
"T3_Humanized_Mouse",
"T4_Human_Islet_or_SC_Islet_InVitro",
"T4B_Human_CellLine_InVitro",
"T5_Rodent_or_Xenogeneic_Only",
"T6_Biophysical_Model_or_Regulatory",
"T5U_Unresolved_Species_or_Model",
})
CURATED_NON_EMPIRICAL_OUTCOME_IDS = frozenset({
"M0049:O1", "M0049:O2", "M0049:O3", "M0049:O4", "M0049:O5",
"M0049:O6", "M0049:O7", "M0049:O8", "M0049:O9", "M0049:O10", "M0049:O15",
"R1_0142:O10",
"M0174:O3", "M0174:O7", "M0174:O8",
"M0930:O7", "M0930:O12",
"M0065:O13",
"M1511:O3",
"M1964:O1",
"PMID:41840058:O5",
"PMID:39004142:O6",
})
def classify_ceg_study_record(study: dict[str, Any]) -> str:
"""Deterministically classify a CEG study record into a typed record class."""
id_type = str(study.get("id_type") or "").strip().lower()
id_val = str(study.get("id_value") or "").strip().lower()
pmid = str(study.get("pmid") or (study.get("id_value") if id_type == "pmid" else "") or "").strip()
bio_model = str(study.get("biological_model") or "").strip().lower()
imm_ctx = str(study.get("immune_context") or "").strip().lower()
follow_up = str(study.get("follow_up") or "").strip().lower()
ft = str(study.get("fidelity_tier") or "").strip()
explicit_rc = str(study.get("record_class") or "").strip()
if id_type == "nct" or id_val.startswith("nct"):
return "trial_registry_protocol"
if (
id_type in ("url", "spl set id")
or any(dom in id_val for dom in ("fda.gov", "federalregister.gov", "ema.europa.eu"))
or bio_model.startswith(("regulatory guidance", "regulatory label", "regulatory text"))
or explicit_rc == "regulatory_or_guidance_document"
):
return "regulatory_or_guidance_document"
if (
study.get("superseded_by_queue_id")
or study.get("superseded_by_pmid")
or pmid in SUPERSEDED_PREPRINT_PMIDS_IN_CEG
):
return "superseded_preprint_duplicate"
if (
study.get("publication_type") == "correspondence_or_secondary_summary"
or pmid in CORRESPONDENCE_SECONDARY_PMIDS_IN_CEG
):
return "correspondence_or_secondary_summary"
if ft == "T6_Biophysical_Model_or_Regulatory" and (
explicit_rc == "computational_model_or_simulation"
or any(tok in imm_ctx for tok in ("computational model", "none modeled", "not applicable (computational"))
or any(tok in follow_up for tok in ("not applicable (computational model)", "pseudo-steady-state model"))
or (
bio_model.startswith((
"mathematical ",
"two-dimensional axisymmetric finite-element",
"three-dimensional finite-element",
))
and "validation" not in bio_model
and "fibroblast" not in bio_model
)
):
return "computational_model_or_simulation"
if explicit_rc in VALID_CEG_RECORD_CLASSES and explicit_rc != "empirical_primary_study":
return explicit_rc
return "empirical_primary_study"
def build_layer1_atlas_from_ceg(ceg: dict[str, Any]) -> dict[str, Any]:
"""Deterministically derive the compact Layer-1 CEG atlas from `context_evidence_graph_v1.json`."""
studies = ceg["studies"]
arms = ceg["arms"]
outcomes = ceg["outcomes"]
edges = ceg["edges"]
effectors = ceg["effectors"]
graveyard = ceg["graveyard_index"]
tier_studies = Counter(s.get("fidelity_tier") or "unspecified" for s in studies.values())
tier_outcomes = Counter(o.get("manuscript_tier") or "unspecified" for o in outcomes.values())
id_types = Counter(s.get("id_type") or "unspecified" for s in studies.values())
record_class_counts = Counter(classify_ceg_study_record(s) for s in studies.values())
r1_total_count = sum(1 for q in studies if q.startswith("R1_"))
r1_empirical_count = sum(
1 for q, s in studies.items() if q.startswith("R1_") and classify_ceg_study_record(s) == "empirical_primary_study"
)
non_r1_empirical_count = sum(
1 for q, s in studies.items() if not q.startswith("R1_") and classify_ceg_study_record(s) == "empirical_primary_study"
)
non_r1_total_count = sum(1 for q in studies if not q.startswith("R1_"))
direction_counts = Counter(o.get("direction") or "unspecified" for o in outcomes.values())
stat_counts = Counter(o.get("stat_type") or "unspecified" for o in outcomes.values())
cited_outcomes = sum(1 for o in outcomes.values() if o.get("cited_by_edge_ids"))
relation_counts = Counter(e.get("relation") or "unspecified" for e in edges)
incorporation_counts = Counter(e.get("incorporation") or "unspecified" for e in edges)
applicability_counts = Counter(e.get("applicability") or "unspecified" for e in edges)
by_gate: dict[str, dict[str, int]] = {}
by_design: dict[str, dict[str, int]] = {}
study_relations: dict[str, Counter[str]] = {q: Counter() for q in studies}
study_gates: dict[str, set[str]] = {q: set() for q in studies}
for e in edges:
rel = str(e.get("relation") or "unspecified")
qid = e["queue_id"]
study_relations[qid][rel] += 1
gates, designs = _classify_gates_and_designs(str(e.get("target_gate_or_module") or ""))
for g in gates:
study_gates[qid].add(g)
bucket = by_gate.setdefault(g, {"total_links": 0})
bucket["total_links"] += 1
bucket[rel] = bucket.get(rel, 0) + 1
for d_id in designs:
db = by_design.setdefault(d_id, {"total_links": 0})
db["total_links"] += 1
db[rel] = db.get(rel, 0) + 1
effector_summaries = []
for eff_id in sorted(effectors):
eff = effectors[eff_id]
effector_summaries.append({
"effector_id": eff["effector_id"],
"topic_id": eff["topic_id"],
"label": eff["label"],
"category": eff["category"],
"causal_isolation": eff["causal_isolation"],
"mediation_status": eff["mediation_status"],
"linked_studies_count": len(eff.get("linked_study_ids") or []),
"linked_arms_count": len(eff.get("linked_variable_ids") or []),
"linked_outcomes_count": len(eff.get("linked_outcome_tuples") or []),
})
graveyard_summaries = [
{
"graveyard_id": g["graveyard_id"],
"category": g.get("category"),
}
for g in graveyard
]
study_index = []
for qid in sorted(studies):
s = studies[qid]
rec: dict[str, Any] = {
"queue_id": qid,
"record_class": classify_ceg_study_record(s),
"id_type": s["id_type"],
"id_value": s["id_value"],
"fidelity_tier": s.get("fidelity_tier"),
"arms_count": len(s.get("variable_arm_ids") or []),
"outcomes_count": s.get("outcomes_count", 0),
"links_count": s.get("links_count", 0),
"gates_touched": sorted(study_gates.get(qid) or []),
"link_relations": dict(sorted(study_relations[qid].items())),
}
if s.get("pub_year") is not None:
rec["pub_year"] = s["pub_year"]
for opt in ("pmid", "pmcid", "doi"):
if s.get(opt):
rec[opt] = s[opt]
if s.get("candidate_effector_topics"):
rec["effector_topics"] = s["candidate_effector_topics"]