papertrail/backend/engines/pykeen/tests/test_checkpoints.py
caelum0x 174222bcaa feat: Builder-Track named-user tools, bio + evidence-intelligence platform, Apache-2.0
Builds PaperTrail from the base skeleton into a full evidence-verification platform
for the "Built with Claude: Life Sciences" hackathon (Builder Track).

New named-user features (Build Beyond the Bench):
- Lab Notebook Companion — rough bench notes -> structured, searchable, grounded
  experiment records (lib/labNotebook, /console/lab-notebook, migration 0059)
- Clinical Trial Matcher — de-identified patient notes -> ranked eligible
  ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning
  (lib/trialMatcher, /console/trial-matcher, migrations 0060)

Platform:
- Deterministic verification moat (biostats recompute, exact-span grounding)
- 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar,
  PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier)
- Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise
  governance (RBAC, /api/v1 gateway, metering, audit chain, retention)
- 17 OSS projects assimilated into native TypeScript on our stack (backend/engines
  retained + credited in NOTICE)
- Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site
- Next.js 16 upgrade, benchmark harness (honest smoke-test caveat)

Licensing: Apache-2.0 (LICENSE + NOTICE).
2026-07-10 16:23:13 +03:00

116 lines
3.6 KiB
Python

"""Tests for checkpointing."""
from collections.abc import Iterator, MutableMapping
from typing import Any, ClassVar
import torch
import unittest_templates
from pykeen.checkpoints import keeper, schedule
from pykeen.checkpoints.utils import MetricSelection
from pykeen.trackers.base import PythonResultTracker
from tests.cases import CheckpointKeeperTests, CheckpointScheduleTests
class CheckpointScheduleMetaTestCase(unittest_templates.MetaTestCase[schedule.CheckpointSchedule]):
"""Meta test case for checkpoint schedules."""
base_cls: ClassVar = schedule.CheckpointSchedule
base_test: ClassVar = CheckpointScheduleTests
class EveryCheckpointScheduleTests(CheckpointScheduleTests):
"""Test for every."""
cls = schedule.EveryCheckpointSchedule
class ExplicitCheckpointScheduleTests(CheckpointScheduleTests):
"""Test for explicit."""
cls = schedule.ExplicitCheckpointSchedule
kwargs = {"steps": (4, 6)}
class BestCheckpointScheduleTests(CheckpointScheduleTests):
"""Test for best."""
cls = schedule.BestCheckpointSchedule
kwargs = {"metric_selection": MetricSelection(metric="loss", prefix="validation")}
def _pre_instantiation_hook(self, kwargs: MutableMapping[str, Any]) -> MutableMapping[str, Any]:
kwargs = super()._pre_instantiation_hook(kwargs)
kwargs["result_tracker"] = self.result_tracker = PythonResultTracker()
return kwargs
def iter_steps(self) -> Iterator[int]:
for step in super().iter_steps():
loss = torch.rand(1, generator=self.generator)
self.result_tracker.log_metrics(metrics={"loss": loss}, step=step, prefix="validation")
yield step
class UnionCheckpointScheduleTests(CheckpointScheduleTests):
"""Test for union."""
cls = schedule.UnionCheckpointSchedule
kwargs = {"bases": ["every", "explicit"], "bases_kwargs": [None, {"steps": (3,)}]}
class CheckpointKeeperMetaTestCase(unittest_templates.MetaTestCase[keeper.CheckpointKeeper]):
"""Meta test case for checkpoint keepers."""
base_cls: ClassVar = keeper.CheckpointKeeper
base_test: ClassVar = CheckpointKeeperTests
class ExplicitCheckpointKeeperTests(CheckpointKeeperTests):
"""Tests for explicit."""
cls = keeper.ExplicitCheckpointKeeper
kwargs = {"keep": (3, 6)}
class LastCheckpointKeeperTests(CheckpointKeeperTests):
"""Tests for last."""
cls = keeper.LastCheckpointKeeper
class BestCheckpointKeeperTests(CheckpointKeeperTests):
"""Tests for best."""
cls = keeper.BestCheckpointKeeper
kwargs = {"metric_selection": MetricSelection(metric="loss", prefix="validation")}
def _pre_instantiation_hook(self, kwargs: MutableMapping[str, Any]) -> MutableMapping[str, Any]:
kwargs = super()._pre_instantiation_hook(kwargs)
kwargs["result_tracker"] = self.result_tracker = PythonResultTracker()
return kwargs
def iter_steps(self) -> Iterator[list[int]]:
for steps in super().iter_steps():
losses = torch.rand(len(steps), generator=self.generator)
for step, loss in zip(steps, losses.tolist(), strict=False):
self.result_tracker.log_metrics(metrics={"loss": loss}, step=step, prefix="validation")
yield steps
class ModuloCheckpointKeeperTests(CheckpointKeeperTests):
"""Tests for modulo."""
cls = keeper.ModuloCheckpointKeeper
class UnionCheckpointKeeperTests(CheckpointKeeperTests):
"""Tests for union."""
cls = keeper.UnionCheckpointKeeper
kwargs = {
"bases": ["last", "explicit"],
"bases_kwargs": [
{"keep": 1},
{"keep": (3, 7)},
],
}