papertrail/backend/engines/pykeen/tests/test_uncertainty.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

90 lines
2.7 KiB
Python

"""Tests for uncertainty workflows."""
from collections.abc import Callable
import pytest
import torch
from pykeen.models import ERMLPE, TransE
from pykeen.models.uncertainty import (
MissingDropoutError,
UncertainPrediction,
predict_h_uncertain,
predict_hrt_uncertain,
predict_r_uncertain,
predict_t_uncertain,
)
from tests import cases
class UncertaintyFailureTest(cases.PredictBaseTestCase):
"""Test for when uncertainty can't be assessed with MC method."""
model_cls = TransE
model_kwargs = {}
def test_missing_dropout(self):
"""Test that a value error is run if the model has no dropout."""
with pytest.raises(MissingDropoutError):
predict_hrt_uncertain(model=self.model, hrt_batch=self.batch)
class UncertaintyPredictionTestCase(cases.PredictBaseTestCase):
"""Tests for uncertainty prediction."""
model_cls = ERMLPE # this model does indeed have dropouts!
model_kwargs = {
"embedding_dim": 2,
"hidden_dim": 3,
}
num_samples: int = 3
def _test_predict_uncertain(
self,
method: Callable[..., tuple[torch.FloatTensor, torch.FloatTensor]],
expected_shape: tuple[int, ...],
**kwargs,
):
"""General testing of uncertainty prediction."""
result = method(
model=self.model,
num_samples=self.num_samples,
**kwargs,
)
assert isinstance(result, UncertainPrediction)
assert (result.uncertainty >= 0).all()
assert result.score.shape == expected_shape
assert result.uncertainty.shape == expected_shape
def test_predict_hrt_uncertain(self):
"""Test predict_hrt_uncertain."""
self._test_predict_uncertain(
method=predict_hrt_uncertain,
expected_shape=(self.batch_size, 1),
hrt_batch=self.batch,
)
def test_predict_h_uncertain(self):
"""Test predict_h_uncertain."""
self._test_predict_uncertain(
method=predict_h_uncertain,
expected_shape=(self.batch_size, self.factory.num_entities),
rt_batch=self.batch[:, 1:],
)
def test_predict_r_uncertain(self):
"""Test predict_r_uncertain."""
self._test_predict_uncertain(
method=predict_r_uncertain,
expected_shape=(self.batch_size, self.factory.num_relations),
ht_batch=self.batch[:, [0, 2]],
)
def test_predict_t_uncertain(self):
"""Test predict_t_uncertain."""
self._test_predict_uncertain(
method=predict_t_uncertain,
expected_shape=(self.batch_size, self.factory.num_entities),
hr_batch=self.batch[:, :2],
)