-
Notifications
You must be signed in to change notification settings - Fork 0
Add blueprint release summary #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from collections import Counter | ||
| from datetime import datetime, timezone | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from casecrawler.models.blueprint import ReleaseReadinessTier | ||
| from casecrawler.storage.dataset_store import DatasetStore | ||
|
|
||
|
|
||
| def build_blueprint_release_summary( | ||
| store: DatasetStore, | ||
| dataset_id: str, | ||
| *, | ||
| generated_at: str | None = None, | ||
| ) -> dict[str, Any]: | ||
| blueprints = store.list_blueprints(dataset_id=dataset_id, limit=None) | ||
| records = store.list_records(dataset_id=dataset_id, limit=None) | ||
| judge_reports = store.list_judge_reports(dataset_id=dataset_id, limit=None) | ||
| attempts = store.list_generation_attempts(dataset_id=dataset_id, limit=None) | ||
|
|
||
| validation_reports = { | ||
| blueprint.blueprint_id: store.get_blueprint_validation_report( | ||
| blueprint.blueprint_id | ||
| ) | ||
| for blueprint in blueprints | ||
| } | ||
| materialized_blueprint_ids = sorted( | ||
| { | ||
| str(record.metadata["blueprint_id"]) | ||
| for record in records | ||
| if record.metadata.get("blueprint_id") | ||
| } | ||
| ) | ||
| ready_blueprint_ids = sorted( | ||
| blueprint_id | ||
| for blueprint_id, report in validation_reports.items() | ||
| if report is not None and report.research_release_ready | ||
| ) | ||
| missing_materialized = sorted( | ||
| set(ready_blueprint_ids) - set(materialized_blueprint_ids) | ||
| ) | ||
|
|
||
| return { | ||
| "dataset_id": dataset_id, | ||
| "generated_at": generated_at or datetime.now(timezone.utc).isoformat(), | ||
| "blueprint_count": len(blueprints), | ||
| "validation_report_count": sum( | ||
| 1 for report in validation_reports.values() if report is not None | ||
| ), | ||
| "research_release_ready_count": len(ready_blueprint_ids), | ||
| "materialized_record_count": sum( | ||
| 1 for record in records if record.metadata.get("blueprint_id") | ||
| ), | ||
| "materialized_blueprint_ids": materialized_blueprint_ids, | ||
| "missing_materialized_blueprint_ids": missing_materialized, | ||
| "judge_report_count": len(judge_reports), | ||
| "passing_judge_report_count": sum(1 for report in judge_reports if report.passed), | ||
| "attempt_counts": dict(Counter(attempt.status.value for attempt in attempts)), | ||
| "role_attempt_counts": dict(Counter(attempt.role.value for attempt in attempts)), | ||
| "tier_counts": _tier_counts(validation_reports), | ||
| "organ_system_counts": dict( | ||
| Counter(blueprint.organ_system for blueprint in blueprints) | ||
| ), | ||
| "setting_counts": dict(Counter(blueprint.setting for blueprint in blueprints)), | ||
| "non_ready_blueprints": _non_ready_blueprints(validation_reports), | ||
| } | ||
|
|
||
|
|
||
| def write_blueprint_release_summary( | ||
| store: DatasetStore, | ||
| dataset_id: str, | ||
| output: str | Path, | ||
| *, | ||
| generated_at: str | None = None, | ||
| ) -> dict[str, Any]: | ||
| summary = build_blueprint_release_summary( | ||
| store, | ||
| dataset_id, | ||
| generated_at=generated_at, | ||
| ) | ||
| output_path = Path(output) | ||
| output_path.parent.mkdir(parents=True, exist_ok=True) | ||
| output_path.write_text(json.dumps(summary, indent=2, sort_keys=True) + "\n") | ||
| return summary | ||
|
|
||
|
|
||
| def _tier_counts(validation_reports: dict) -> dict[str, int]: | ||
| counts = Counter() | ||
| counts["missing"] = 0 | ||
| for report in validation_reports.values(): | ||
| if report is None: | ||
| counts["missing"] += 1 | ||
| else: | ||
| counts[report.tier.value] += 1 | ||
| for tier in ReleaseReadinessTier: | ||
| counts.setdefault(tier.value, 0) | ||
| return dict(counts) | ||
|
|
||
|
|
||
| def _non_ready_blueprints(validation_reports: dict) -> list[dict[str, Any]]: | ||
| non_ready = [] | ||
| for blueprint_id, report in sorted(validation_reports.items()): | ||
| if report is not None and report.research_release_ready: | ||
| continue | ||
| if report is None: | ||
| non_ready.append( | ||
| { | ||
| "blueprint_id": blueprint_id, | ||
| "tier": "missing", | ||
| "issues": [ | ||
| { | ||
| "field": "blueprint_validation_report", | ||
| "message": "No validation report is persisted.", | ||
| } | ||
| ], | ||
| } | ||
| ) | ||
| continue | ||
| non_ready.append( | ||
| { | ||
| "blueprint_id": blueprint_id, | ||
| "tier": report.tier.value, | ||
| "issues": report.issues, | ||
| "schema_valid": report.schema_valid, | ||
| "clinically_plausible": report.clinically_plausible, | ||
| "grounded": report.grounded, | ||
| "judge_validated": report.judge_validated, | ||
| } | ||
| ) | ||
| return non_ready | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import json | ||
|
|
||
| from casecrawler.export.blueprint_release import ( | ||
| build_blueprint_release_summary, | ||
| write_blueprint_release_summary, | ||
| ) | ||
| from casecrawler.generation.blueprint_materializer import BlueprintMaterializer | ||
| from casecrawler.models.blueprint import ( | ||
| BlueprintEvidence, | ||
| BlueprintValidationReport, | ||
| ClinicalBlueprint, | ||
| GenerationAttempt, | ||
| GenerationAttemptStatus, | ||
| GenerationRole, | ||
| JudgeReport, | ||
| ReleaseReadinessTier, | ||
| ) | ||
| from casecrawler.storage.dataset_store import DatasetStore | ||
|
|
||
|
|
||
| def _blueprint(blueprint_id: str, *, organ_system: str = "cardiovascular"): | ||
| return ClinicalBlueprint( | ||
| blueprint_id=blueprint_id, | ||
| dataset_id="ds-1", | ||
| cohort_plan_id="plan-1", | ||
| archetype_name="anticoagulation decision", | ||
| organ_system=organ_system, | ||
| setting="outpatient", | ||
| patient={"age": 72, "sex": "female"}, | ||
| chief_concern="Atrial fibrillation anticoagulation follow-up.", | ||
| diagnoses=[ | ||
| { | ||
| "name": "atrial fibrillation", | ||
| "supporting_findings": ["ECG confirms AF"], | ||
| } | ||
| ], | ||
| evidence=BlueprintEvidence( | ||
| supported_claims=["AF anticoagulation requires renal-dose review."], | ||
| citations=[{"source": "dailymed", "claim": "renal-dose review"}], | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def _judge_report(blueprint_id: str, *, passed: bool = True) -> JudgeReport: | ||
| return JudgeReport( | ||
| report_id=f"judge-{blueprint_id}", | ||
| dataset_id="ds-1", | ||
| artifact_id=blueprint_id, | ||
| role=GenerationRole.JUDGE, | ||
| score=0.93 if passed else 0.41, | ||
| passed=passed, | ||
| rubric="blueprint_plausibility", | ||
| ) | ||
|
|
||
|
|
||
| def _validation( | ||
| blueprint_id: str, | ||
| *, | ||
| tier: ReleaseReadinessTier, | ||
| judge_report: JudgeReport | None = None, | ||
| ) -> BlueprintValidationReport: | ||
| return BlueprintValidationReport( | ||
| blueprint_id=blueprint_id, | ||
| tier=tier, | ||
| schema_valid=True, | ||
| clinically_plausible=tier | ||
| in { | ||
| ReleaseReadinessTier.CLINICALLY_PLAUSIBLE, | ||
| ReleaseReadinessTier.JUDGE_VALIDATED, | ||
| ReleaseReadinessTier.RESEARCH_RELEASE_READY, | ||
| }, | ||
| grounded=tier == ReleaseReadinessTier.RESEARCH_RELEASE_READY, | ||
| judge_validated=tier | ||
| in { | ||
| ReleaseReadinessTier.JUDGE_VALIDATED, | ||
| ReleaseReadinessTier.RESEARCH_RELEASE_READY, | ||
| }, | ||
| judge_reports=[judge_report] if judge_report is not None else [], | ||
| issues=[] if tier == ReleaseReadinessTier.RESEARCH_RELEASE_READY else [ | ||
| {"field": "evidence.citations", "message": "missing citation"} | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| def test_build_blueprint_release_summary_aggregates_readiness_and_materialization( | ||
| tmp_path, | ||
| ): | ||
| store = DatasetStore(db_path=str(tmp_path / "datasets.db")) | ||
| ready_blueprint = _blueprint("bp-ready") | ||
| draft_blueprint = _blueprint("bp-draft", organ_system="endocrine") | ||
| ready_judge = _judge_report("bp-ready") | ||
| draft_judge = _judge_report("bp-draft", passed=False) | ||
| store.save_blueprint(ready_blueprint) | ||
| store.save_blueprint(draft_blueprint) | ||
| store.save_judge_report(ready_judge) | ||
| store.save_judge_report(draft_judge) | ||
| store.save_blueprint_validation_report( | ||
| _validation( | ||
| "bp-ready", | ||
| tier=ReleaseReadinessTier.RESEARCH_RELEASE_READY, | ||
| judge_report=ready_judge, | ||
| ) | ||
| ) | ||
| store.save_blueprint_validation_report( | ||
| _validation( | ||
| "bp-draft", | ||
| tier=ReleaseReadinessTier.SCHEMA_VALID, | ||
| judge_report=draft_judge, | ||
| ) | ||
| ) | ||
| store.save_generation_attempt( | ||
| GenerationAttempt( | ||
| attempt_id="attempt-1", | ||
| dataset_id="ds-1", | ||
| role=GenerationRole.REPAIR, | ||
| status=GenerationAttemptStatus.REPAIR_REQUESTED, | ||
| provider="openai", | ||
| model="repair-model", | ||
| artifact_id="bp-draft", | ||
| ) | ||
| ) | ||
| BlueprintMaterializer(created_at="2026-05-06T10:00:00").materialize( | ||
| ready_blueprint, | ||
| validation_report=store.get_blueprint_validation_report("bp-ready"), | ||
| store=store, | ||
| require_release_ready=True, | ||
| ) | ||
|
|
||
| summary = build_blueprint_release_summary(store, "ds-1") | ||
|
|
||
| assert summary["dataset_id"] == "ds-1" | ||
| assert summary["blueprint_count"] == 2 | ||
| assert summary["validation_report_count"] == 2 | ||
| assert summary["research_release_ready_count"] == 1 | ||
| assert summary["materialized_record_count"] == 1 | ||
| assert summary["materialized_blueprint_ids"] == ["bp-ready"] | ||
| assert summary["missing_materialized_blueprint_ids"] == [] | ||
| assert summary["judge_report_count"] == 2 | ||
| assert summary["passing_judge_report_count"] == 1 | ||
| assert summary["attempt_counts"]["repair_requested"] == 1 | ||
| assert summary["tier_counts"]["research_release_ready"] == 1 | ||
| assert summary["tier_counts"]["schema_valid"] == 1 | ||
| assert summary["tier_counts"]["missing"] == 0 | ||
| assert summary["organ_system_counts"] == { | ||
| "cardiovascular": 1, | ||
| "endocrine": 1, | ||
| } | ||
| assert summary["non_ready_blueprints"][0]["blueprint_id"] == "bp-draft" | ||
|
|
||
|
|
||
| def test_write_blueprint_release_summary_writes_json(tmp_path): | ||
| store = DatasetStore(db_path=str(tmp_path / "datasets.db")) | ||
| store.save_blueprint(_blueprint("bp-1")) | ||
| output = tmp_path / "summary.json" | ||
|
|
||
| summary = write_blueprint_release_summary(store, "ds-1", output) | ||
|
|
||
| assert json.loads(output.read_text()) == summary | ||
| assert summary["blueprint_count"] == 1 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.