Source code for cell_curator.models

"""Typed public models shared by the annotation engine and artifact readers."""

from __future__ import annotations

from datetime import UTC, datetime
from enum import StrEnum
from pathlib import Path
from typing import Any, Literal

from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator

SCHEMA_VERSION: Literal[3] = 3

Agreement = Literal["strict", "descendant", "relaxed"]


class StrictModel(BaseModel):
    """Base model that rejects misspelled or undeclared fields."""

    model_config = ConfigDict(extra="forbid", validate_assignment=True)


[docs] class CapabilityLevel(StrEnum): ANNOTATION = "annotation-capable" CORROBORATION = "corroboration-only" QC = "qc-only" UNAVAILABLE = "unavailable"
class AssayKind(StrEnum): RNA = "rna" PROTEIN = "protein" ATAC = "atac" GENE_ACTIVITY = "gene_activity" SPATIAL = "spatial" OTHER = "other" class RunMode(StrEnum): ANNOTATION = "annotation" EVIDENCE_ONLY = "evidence-only" class ParentOutcome(StrEnum): ACCEPT_SPLIT = "ACCEPT SPLIT" RETAIN_PARENT = "RETAIN PARENT" DOUBLET_MIXED = "DOUBLET/MIXED" TECHNICAL_UNRESOLVED = "TECHNICAL/UNRESOLVED"
[docs] class MarkerProgram(StrictModel): schema_version: Literal[3] = SCHEMA_VERSION label: str axis: Literal["cell_type", "cell_state"] = "cell_type" positive: tuple[str, ...] = () negative: tuple[str, ...] = () parent: str | None = None parent_cl_id: str | None = None cl_id: str | None = None feature_space: str = "gene" source: str = "local" source_version: str = "unversioned"
[docs] @field_validator("label") @classmethod def nonblank_label(cls, value: str) -> str: if not value.strip(): raise ValueError("marker program label must not be blank") return value.strip()
[docs] @model_validator(mode="after") def disjoint_signs(self) -> MarkerProgram: # A gene that is both expected and forbidden makes the program penalise # its own evidence; which sign was meant is the curator's call. overlap = sorted(set(self.positive) & set(self.negative)) if overlap: raise ValueError( f"marker program {self.label!r} lists {', '.join(overlap)} as both " "positive and negative" ) return self
[docs] class Capability(StrictModel): schema_version: Literal[3] = SCHEMA_VERSION assay: str representation: str assay_kind: AssayKind feature_space: str level: CapabilityLevel claims: tuple[str, ...] reasons: tuple[str, ...] = () backend: str requires_gpu: bool = False
[docs] class EvidenceRecord(StrictModel): schema_version: Literal[3] = SCHEMA_VERSION run_id: str scope: str cluster_id: str label: str axis: Literal["cell_type", "cell_state"] = "cell_type" assay: str representation: str capability_level: CapabilityLevel score: float positive_coverage: float negative_violation: float program_coverage: float = Field(ge=0.0, le=1.0) measured_positive: int measured_negative: int missing_positive: tuple[str, ...] = () missing_negative: tuple[str, ...] = () detection_valid: bool source: str
[docs] class AnnotationDecision(StrictModel): schema_version: Literal[3] = SCHEMA_VERSION run_id: str scope: str cluster_id: str parent_label: str | None = None cell_type: str cell_state: str = "normal" cl_id: str | None = None confidence: float = Field(ge=0.0, le=1.0) margin: float normalized_entropy: float = Field(ge=0.0, le=1.0) abstained: bool rationale: str rejected_alternatives: tuple[str, ...] = () required_capabilities_complete: bool approval_status: Literal["PENDING", "APPROVED", "REJECTED"] = "PENDING"
[docs] class MappingRecord(StrictModel): schema_version: Literal[3] = SCHEMA_VERSION run_id: str barcode: str scope: str canonical_parent: str leaf_id: str cell_type: str cell_state: str membership_role: Literal["retained_parent", "accepted_child", "parent_remainder"]
[docs] class BackendReceipt(StrictModel): schema_version: Literal[3] = SCHEMA_VERSION artifact_kind: str = "cell_curator_backend_receipt" run_id: str lane_key: str backend: str capability_level: CapabilityLevel status: Literal["complete", "unavailable", "failed"] input_sha256: str config_sha256: str output_sha256: str | None = None artifact_sha256: dict[str, str] = Field(default_factory=dict) feature_order_sha256: str | None = None group_membership_sha256: str | None = None fallback_used: bool = False marker_methods: tuple[Literal["wilcoxon", "logreg"], ...] = () cpu_fallback_available: bool = False cpu_fallback_used: bool = False convergence_validated: bool | None = None runtime: dict[str, Any] = Field(default_factory=dict) limitations: tuple[str, ...] = () created_utc: str = Field(default_factory=lambda: datetime.now(UTC).isoformat())
[docs] class RunState(StrictModel): schema_version: Literal[3] = SCHEMA_VERSION run_id: str mode: RunMode phase: str input_path: str input_sha256: str config_sha256: str canonical_membership_sha256: str blockers: tuple[str, ...] = () completed_phases: tuple[str, ...] = () assumptions_approved: bool = False critics_reconciled: bool = False human_approval: bool = False labels_permitted: bool = False writeback_permitted: bool = False stage: Literal["scene", "plan", "execute", "review", "writeback", "complete"] = "scene" current_level: str = "" current_parent: str = "" next_action: str = "complete scene setting" compute_profile: str = "" compute: dict[str, Any] = Field(default_factory=dict) context_complete: bool = False plan_complete: bool = False interactive: bool = True preauthorized: bool = False assumptions_sha256: str = "" approved_assumptions_sha256: str = "" signed_off_scopes: tuple[str, ...] = () decisions: tuple[dict[str, Any], ...] = () progress: dict[str, Any] = Field(default_factory=dict) updated_utc: str = Field(default_factory=lambda: datetime.now(UTC).isoformat())
[docs] class BenchmarkResult(StrictModel): schema_version: Literal[3] = SCHEMA_VERSION system: str dataset: str n: int accuracy: float macro_f1: float micro_f1: float coverage: float selective_accuracy: float coverage_risk: float hierarchical_accuracy: float | None = None descendant_accuracy: float | None = None relaxed_accuracy: float | None = None unknown_precision: float | None = None unknown_recall: float | None = None unknown_f1: float | None = None brier_score: float | None = None expected_calibration_error: float | None = None hierarchy_error: float | None = None ontology_distance_error: float | None = None mixed_detection_f1: float | None = None split_decision_accuracy: float | None = None repeat_reproducibility: float | None = None input_invariance: float | None = None unsplit_parent_invariance: float | None = None runtime_seconds: float | None = None peak_memory_mb: float | None = None artifact_completeness: float | None = None artifact_path: Path | None = None # label -> precision, recall, f1, and support (true cells). Read F1 with its support. per_type: dict[str, dict[str, float | int]] | None = None
[docs] class RiskCoveragePoint(StrictModel): """One operating point on the risk-coverage curve of a prediction table. A curve is the only form in which two systems can be compared at matched coverage, so each point is self-describing: it carries the counts it was computed from so a reader never has to trust a ratio whose denominator the artifact does not show, and it names the ``agreement`` it counted, because a curve scored on exact label equality and a curve scored on Cell Ontology compatibility are different claims about the same table and cannot be laid side by side as if they were the same measurement. Accuracy and risk are both optional and both absent together, because a threshold that calls nothing has no measured risk and reporting 0.0 or 1.0 there would invent the best and worst possible system at the same point. This is a returned record rather than a published artifact, so it carries no schema version of its own. """ threshold: float = Field(ge=0.0, le=1.0) n_total: int = Field(ge=0) n_called: int = Field(ge=0) n_correct: int = Field(ge=0) coverage: float = Field(ge=0.0, le=1.0) selective_accuracy: float | None = Field(default=None, ge=0.0, le=1.0) risk: float | None = Field(default=None, ge=0.0, le=1.0) agreement: Agreement = "strict"
[docs] @model_validator(mode="after") def consistent_counts(self) -> RiskCoveragePoint: if self.n_called > self.n_total: raise ValueError("risk-coverage n_called must not exceed n_total") if self.n_correct > self.n_called: raise ValueError("risk-coverage n_correct must not exceed n_called") if (self.selective_accuracy is None) != (self.risk is None): raise ValueError("risk-coverage accuracy and risk must both be absent or present") return self
[docs] class BenchmarkArtifact(StrictModel): """One checksum-pinned acquisition artifact in a benchmark recipe.""" name: str url: str sha256: str
[docs] @model_validator(mode="after") def validate_artifact(self) -> BenchmarkArtifact: if not self.name.strip(): raise ValueError("benchmark artifact name must not be blank") if not self.url.startswith("https://"): raise ValueError("benchmark artifact URLs must use HTTPS") if self.sha256 != "pending-after-download" and not _is_sha256(self.sha256): raise ValueError( "benchmark artifact checksum must be SHA-256 or explicitly pending" ) return self
[docs] class BenchmarkDatasetManifest(StrictModel): """Versioned, neutral public or local benchmark acquisition contract.""" schema_version: Literal[3] = SCHEMA_VERSION dataset: str modality: str source_url: str artifacts: tuple[BenchmarkArtifact, ...] = () sha256: str license: str status: Literal["available", "pending-external-resource"] blocker: str | None = None reproduce: tuple[str, ...] = () local_path: str | None = None scenarios: tuple[str, ...] = ()
[docs] @model_validator(mode="after") def validate_status_contract(self) -> BenchmarkDatasetManifest: for field_name in ("dataset", "modality", "source_url", "license"): if not str(getattr(self, field_name)).strip(): raise ValueError(f"benchmark {field_name} must not be blank") if not ( self.source_url.startswith("https://") or self.source_url.startswith("local://") ): raise ValueError("benchmark source_url must use HTTPS or local://") if self.status == "available": if not self.local_path: raise ValueError("available benchmarks require local_path") if not _is_sha256(self.sha256): raise ValueError("available benchmarks require an exact SHA-256 checksum") else: if not self.blocker or not self.blocker.strip(): raise ValueError("pending benchmarks require an explicit blocker") if not self.reproduce: raise ValueError("pending benchmarks require reproduction commands") if self.sha256 != "pending-after-artifact-selection": raise ValueError("pending benchmark checksum must be explicitly pending") if any(not command.strip() for command in self.reproduce): raise ValueError("benchmark reproduction commands must not be blank") return self
def _is_sha256(value: str) -> bool: return len(value) == 64 and all( character in "0123456789abcdef" for character in value.lower() )