Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Fix assay-aware VPCs; retain existing public model releases
Browse files
bundle-manifest.json
CHANGED
|
@@ -2,16 +2,16 @@
|
|
| 2 |
"format": "pff-space-bundle-v1",
|
| 3 |
"sources": {
|
| 4 |
"pff_pk": {
|
| 5 |
-
"commit": "
|
| 6 |
-
"workingTreeStatus": "M
|
| 7 |
},
|
| 8 |
"synthetic_priors": {
|
| 9 |
-
"commit": "
|
| 10 |
-
"workingTreeStatus": "M
|
| 11 |
},
|
| 12 |
"dashboard": {
|
| 13 |
-
"commit": "
|
| 14 |
-
"workingTreeStatus": "
|
| 15 |
}
|
| 16 |
},
|
| 17 |
"sha256": {
|
|
@@ -123,8 +123,9 @@
|
|
| 123 |
"requirements.txt": "f23098eb41492bf9109d60530c12808a073e7bdabe4a2592d818f83ea3660cac",
|
| 124 |
"services/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 125 |
"services/inference/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 126 |
-
"services/inference/
|
| 127 |
-
"services/inference/
|
|
|
|
| 128 |
"synthetic_priors/README.md": "adfbfe94c3d304314698425cff866965809293c3815156732cdb0fe5721b6122",
|
| 129 |
"synthetic_priors/__init__.py": "f79c24950c0a1b219e871c364f4a2eac498de124758aa4ed66136b3b99394293",
|
| 130 |
"synthetic_priors/_compat.py": "038a751f16f806dcc224c648c20eee2591326668d6fb6eda447464db23ed1ede",
|
|
|
|
| 2 |
"format": "pff-space-bundle-v1",
|
| 3 |
"sources": {
|
| 4 |
"pff_pk": {
|
| 5 |
+
"commit": "ca75fa155d79105820e810262f966efc41ab7b9f",
|
| 6 |
+
"workingTreeStatus": "M pff_pk/__pycache__/__init__.cpython-311.pyc\n M pff_pk/__pycache__/_compat.cpython-311.pyc\n M pff_pk/configuration/__pycache__/__init__.cpython-311.pyc\n M pff_pk/configuration/__pycache__/normalization.cpython-311.pyc\n M pff_pk/configuration/__pycache__/schema.cpython-311.pyc\n M pff_pk/data/__pycache__/__init__.cpython-311.pyc\n M pff_pk/data/__pycache__/config.cpython-311.pyc\n M pff_pk/data/__pycache__/studies.cpython-311.pyc\n M pff_pk/inference/__pycache__/__init__.cpython-311.pyc\n M pff_pk/inference/__pycache__/censoring.cpython-311.pyc\n M pff_pk/inference/__pycache__/empirical.cpython-311.pyc\n M pff_pk/inference/__pycache__/model.cpython-311.pyc\n M pff_pk/metrics/__pycache__/__init__.cpython-311.pyc\n M pff_pk/metrics/__pycache__/dashboard_vpc.cpython-311.pyc\n M pff_pk/metrics/__pycache__/mesh_vpc.cpython-311.pyc\n M pff_pk/metrics/__pycache__/pharmpy_vpc.cpython-311.pyc\n M pff_pk/models/__pycache__/__init__.cpython-311.pyc\n M pff_pk/models/__pycache__/config.cpython-311.pyc\n M pff_pk/models/__pycache__/contracts.cpython-311.pyc\n M pff_pk/models/__pycache__/embeddings.cpython-311.pyc\n M pff_pk/models/__pycache__/flow.cpython-311.pyc\n M pff_pk/models/__pycache__/sources.cpython-311.pyc\n M pff_pk/models/architectures/__pycache__/__init__.cpython-311.pyc\n M pff_pk/models/architectures/__pycache__/blocks.cpython-311.pyc\n M pff_pk/models/architectures/__pycache__/operator_attn.cpython-311.pyc\n M pff_pk/models/architectures/__pycache__/vector_fields_pk.cpython-311.pyc\n M pff_pk/numerics/__pycache__/__init__.cpython-311.pyc\n M pff_pk/numerics/__pycache__/conventions.cpython-311.pyc\n M pff_pk/numerics/__pycache__/measure.cpython-311.pyc\n M pff_pk/observations/__pycache__/__init__.cpython-311.pyc\n M pff_pk/observations/__pycache__/acquisition.cpython-311.pyc\n M pff_pk/tasks/__pycache__/__init__.cpython-311.pyc\n M pff_pk/tasks/__pycache__/batch.cpython-311.pyc\n M pff_pk/tasks/__pycache__/batching.cpython-311.pyc\n M pff_pk/tasks/__pycache__/config.cpython-311.pyc\n M pff_pk/tasks/__pycache__/covariates.cpython-311.pyc\n M pff_pk/tasks/__pycache__/generation.cpython-311.pyc\n M pff_pk/training/__pycache__/__init__.cpython-311.pyc\n M pff_pk/training/__pycache__/config.cpython-311.pyc\n M pff_pk/training/__pycache__/module.cpython-311.pyc\n M services/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/__pycache__/_compat.cpython-311.pyc\n M synthetic_priors/corpus/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/corpus/__pycache__/identity.cpython-311.pyc\n M synthetic_priors/pipeline/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/pipeline/__pycache__/profiles.cpython-311.pyc\n M synthetic_priors/pipeline/__pycache__/study.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/cohort.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/config.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/graph.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/kinetics.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/physiology.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/profiles.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/v1.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/contracts.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/replay.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/study_writer.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/system_reader.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/arms.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/fluxes.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/integrators.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/meshes.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/pd.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/protocols.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/time_gauge.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/types.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/v1.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/views.cpython-311.pyc\n?? services/inference/__pycache__/"
|
| 7 |
},
|
| 8 |
"synthetic_priors": {
|
| 9 |
+
"commit": "ca75fa155d79105820e810262f966efc41ab7b9f",
|
| 10 |
+
"workingTreeStatus": "M pff_pk/__pycache__/__init__.cpython-311.pyc\n M pff_pk/__pycache__/_compat.cpython-311.pyc\n M pff_pk/configuration/__pycache__/__init__.cpython-311.pyc\n M pff_pk/configuration/__pycache__/normalization.cpython-311.pyc\n M pff_pk/configuration/__pycache__/schema.cpython-311.pyc\n M pff_pk/data/__pycache__/__init__.cpython-311.pyc\n M pff_pk/data/__pycache__/config.cpython-311.pyc\n M pff_pk/data/__pycache__/studies.cpython-311.pyc\n M pff_pk/inference/__pycache__/__init__.cpython-311.pyc\n M pff_pk/inference/__pycache__/censoring.cpython-311.pyc\n M pff_pk/inference/__pycache__/empirical.cpython-311.pyc\n M pff_pk/inference/__pycache__/model.cpython-311.pyc\n M pff_pk/metrics/__pycache__/__init__.cpython-311.pyc\n M pff_pk/metrics/__pycache__/dashboard_vpc.cpython-311.pyc\n M pff_pk/metrics/__pycache__/mesh_vpc.cpython-311.pyc\n M pff_pk/metrics/__pycache__/pharmpy_vpc.cpython-311.pyc\n M pff_pk/models/__pycache__/__init__.cpython-311.pyc\n M pff_pk/models/__pycache__/config.cpython-311.pyc\n M pff_pk/models/__pycache__/contracts.cpython-311.pyc\n M pff_pk/models/__pycache__/embeddings.cpython-311.pyc\n M pff_pk/models/__pycache__/flow.cpython-311.pyc\n M pff_pk/models/__pycache__/sources.cpython-311.pyc\n M pff_pk/models/architectures/__pycache__/__init__.cpython-311.pyc\n M pff_pk/models/architectures/__pycache__/blocks.cpython-311.pyc\n M pff_pk/models/architectures/__pycache__/operator_attn.cpython-311.pyc\n M pff_pk/models/architectures/__pycache__/vector_fields_pk.cpython-311.pyc\n M pff_pk/numerics/__pycache__/__init__.cpython-311.pyc\n M pff_pk/numerics/__pycache__/conventions.cpython-311.pyc\n M pff_pk/numerics/__pycache__/measure.cpython-311.pyc\n M pff_pk/observations/__pycache__/__init__.cpython-311.pyc\n M pff_pk/observations/__pycache__/acquisition.cpython-311.pyc\n M pff_pk/tasks/__pycache__/__init__.cpython-311.pyc\n M pff_pk/tasks/__pycache__/batch.cpython-311.pyc\n M pff_pk/tasks/__pycache__/batching.cpython-311.pyc\n M pff_pk/tasks/__pycache__/config.cpython-311.pyc\n M pff_pk/tasks/__pycache__/covariates.cpython-311.pyc\n M pff_pk/tasks/__pycache__/generation.cpython-311.pyc\n M pff_pk/training/__pycache__/__init__.cpython-311.pyc\n M pff_pk/training/__pycache__/config.cpython-311.pyc\n M pff_pk/training/__pycache__/module.cpython-311.pyc\n M services/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/__pycache__/_compat.cpython-311.pyc\n M synthetic_priors/corpus/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/corpus/__pycache__/identity.cpython-311.pyc\n M synthetic_priors/pipeline/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/pipeline/__pycache__/profiles.cpython-311.pyc\n M synthetic_priors/pipeline/__pycache__/study.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/cohort.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/config.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/graph.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/kinetics.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/physiology.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/profiles.cpython-311.pyc\n M synthetic_priors/priors/__pycache__/v1.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/contracts.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/replay.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/study_writer.cpython-311.pyc\n M synthetic_priors/schemas/__pycache__/system_reader.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/__init__.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/arms.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/fluxes.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/integrators.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/meshes.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/pd.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/protocols.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/time_gauge.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/types.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/v1.cpython-311.pyc\n M synthetic_priors/simulation/__pycache__/views.cpython-311.pyc\n?? services/inference/__pycache__/"
|
| 11 |
},
|
| 12 |
"dashboard": {
|
| 13 |
+
"commit": "4cb9134fed0947dd103d7e96f26b255fe3e093a1",
|
| 14 |
+
"workingTreeStatus": ""
|
| 15 |
}
|
| 16 |
},
|
| 17 |
"sha256": {
|
|
|
|
| 123 |
"requirements.txt": "f23098eb41492bf9109d60530c12808a073e7bdabe4a2592d818f83ea3660cac",
|
| 124 |
"services/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 125 |
"services/inference/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 126 |
+
"services/inference/censored_vpc.py": "08666fbc394dd86a5469837b9565ece09a3fbf6149d9a49f3f90f72aef15b246",
|
| 127 |
+
"services/inference/pff_service.py": "51f6216cb9845d63da5253b402dd198ba8e2a838b4fe8a1c31cdbeb4007a0c56",
|
| 128 |
+
"services/inference/synthetic_service.py": "4db0c00cd164b5db2a2c81b4505d3d69dda640ef02d1ad9146039665b1defb5b",
|
| 129 |
"synthetic_priors/README.md": "adfbfe94c3d304314698425cff866965809293c3815156732cdb0fe5721b6122",
|
| 130 |
"synthetic_priors/__init__.py": "f79c24950c0a1b219e871c364f4a2eac498de124758aa4ed66136b3b99394293",
|
| 131 |
"synthetic_priors/_compat.py": "038a751f16f806dcc224c648c20eee2591326668d6fb6eda447464db23ed1ede",
|
services/inference/censored_vpc.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Assay-aware dashboard VPC adapter; Pharmpy remains the binning authority.
|
| 2 |
+
|
| 3 |
+
Censored records keep their ranks. Unresolved records are interval-valued,
|
| 4 |
+
not silently classified as BLQ. Simulated curves use the same assay and design.
|
| 5 |
+
"""
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
from pharmpy.modeling import bin_observations
|
| 10 |
+
from pff_pk.metrics import dashboard_vpc as base
|
| 11 |
+
from pff_pk.metrics.mesh_vpc import rank_quantile
|
| 12 |
+
from pff_pk.metrics.pharmpy_vpc import _pharmpy_model, _pharmpy_statistics
|
| 13 |
+
|
| 14 |
+
CENSORING_VERSION = "assay-aware-v1"
|
| 15 |
+
DASHBOARD_VPC_VERSION = base.DASHBOARD_VPC_VERSION + "+" + CENSORING_VERSION
|
| 16 |
+
PROBABILITIES = {"q05": .05, "q50": .5, "q95": .95}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _limit(study):
|
| 20 |
+
value = (study or {}).get("assay", {}).get("lloq")
|
| 21 |
+
if value is None:
|
| 22 |
+
return None
|
| 23 |
+
if isinstance(value, bool) or not np.isfinite(float(value)) or float(value) <= 0:
|
| 24 |
+
raise ValueError("assay LLOQ must be positive and finite")
|
| 25 |
+
return float(value)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _records(cohort, study, lloq):
|
| 29 |
+
raw = {str(s["id"]): s for s in study["subjects"]}
|
| 30 |
+
rows = []
|
| 31 |
+
for person, (identifier, curve) in enumerate(cohort["subjects"].items()):
|
| 32 |
+
subject = raw[identifier]
|
| 33 |
+
flags = subject.get("cens")
|
| 34 |
+
if flags is not None and len(flags) != len(subject["points"]):
|
| 35 |
+
raise ValueError("censoring flags must align with observations")
|
| 36 |
+
by_time = {}
|
| 37 |
+
for i, (time, value) in enumerate(subject["points"]):
|
| 38 |
+
if flags is not None:
|
| 39 |
+
flag = flags[i]
|
| 40 |
+
if flag not in (0, 1, None):
|
| 41 |
+
raise ValueError("censoring flags must be 0, 1, or null")
|
| 42 |
+
else:
|
| 43 |
+
flag = 0 if value > lloq else None
|
| 44 |
+
# A below-assay number without a confirmed BLQ flag is unresolved.
|
| 45 |
+
if value < lloq and flag == 0:
|
| 46 |
+
flag = None
|
| 47 |
+
by_time[float(time)] = (float(value), flag)
|
| 48 |
+
for time, _ in curve:
|
| 49 |
+
value, flag = by_time[time]
|
| 50 |
+
rows.append((person, time, value, flag))
|
| 51 |
+
return rows
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _observed_summary(rows, lloq):
|
| 55 |
+
lower = np.array([value if flag == 0 else 0. for _, _, value, flag in rows])
|
| 56 |
+
upper = np.array([value if flag == 0 else lloq if flag == 1 else max(lloq, value)
|
| 57 |
+
for _, _, value, flag in rows])
|
| 58 |
+
quantiles = {}
|
| 59 |
+
for key, probability in PROBABILITIES.items():
|
| 60 |
+
lo, hi = float(rank_quantile(lower, probability)), float(rank_quantile(upper, probability))
|
| 61 |
+
quantiles[key] = hi if lo == hi and hi >= lloq else None
|
| 62 |
+
n = len(rows)
|
| 63 |
+
censored = sum(flag == 1 for _, _, _, flag in rows)
|
| 64 |
+
unresolved = sum(flag is None for _, _, _, flag in rows)
|
| 65 |
+
return quantiles, {"lower": censored / n, "upper": (censored + unresolved) / n,
|
| 66 |
+
"nCensored": censored, "nUnresolved": unresolved}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _apply(summary, cohort, study, pool=None, query_time=None, *, replicates=200, seed=43):
|
| 70 |
+
lloq = _limit(study)
|
| 71 |
+
if lloq is None:
|
| 72 |
+
return summary
|
| 73 |
+
rows = _records(cohort, study, lloq)
|
| 74 |
+
if summary.get("timeBinning") == "query_mesh":
|
| 75 |
+
row_times = np.array([r[1] for r in rows])
|
| 76 |
+
tolerance = max(cohort["horizon"] * 2e-6, 1e-9)
|
| 77 |
+
groups = [np.flatnonzero(np.abs(row_times - p["time"]) <= tolerance) for p in summary["points"]]
|
| 78 |
+
else:
|
| 79 |
+
# Use Pharmpy's actual assignments, including its edge/tie convention.
|
| 80 |
+
labels, _ = bin_observations(_pharmpy_model(base._observed(cohort)),
|
| 81 |
+
"equal_number", summary["effectiveBins"])
|
| 82 |
+
groups = [np.flatnonzero(labels.to_numpy() == i) for i in range(summary["effectiveBins"])]
|
| 83 |
+
values = None
|
| 84 |
+
if pool is not None:
|
| 85 |
+
pool, times = np.asarray(pool), np.asarray(query_time)
|
| 86 |
+
indices = np.abs(times[:, None] - np.array([r[1] for r in rows])[None, :]).argmin(axis=0)
|
| 87 |
+
selected = np.random.default_rng(seed).integers(len(pool), size=(replicates, len(cohort["subjects"])))
|
| 88 |
+
values = pool[selected[:, [r[0] for r in rows]], indices[None, :]]
|
| 89 |
+
for point, group in zip(summary["points"], groups, strict=True):
|
| 90 |
+
if not len(group) or len(group) != point["nObservations"]:
|
| 91 |
+
raise ValueError("censoring records do not match VPC bin membership")
|
| 92 |
+
point["observed"], observed_blq = _observed_summary([rows[i] for i in group], lloq)
|
| 93 |
+
point["blq"] = {"observed": observed_blq}
|
| 94 |
+
if values is not None:
|
| 95 |
+
simulated = values[:, group]
|
| 96 |
+
fraction = (simulated < lloq).mean(axis=1)
|
| 97 |
+
point["blq"]["simulated"] = {
|
| 98 |
+
"center": float(fraction.mean()), "lower": float(rank_quantile(fraction, .05)),
|
| 99 |
+
"upper": float(rank_quantile(fraction, .95)),
|
| 100 |
+
}
|
| 101 |
+
# Keep every replicate, including those whose percentile is BLQ.
|
| 102 |
+
# A CI crossing the LLOQ is visibly clipped, never re-estimated from
|
| 103 |
+
# only the quantifiable replicates.
|
| 104 |
+
for key, probability in PROBABILITIES.items():
|
| 105 |
+
qs = rank_quantile(simulated, probability, axis=1)
|
| 106 |
+
lo, hi = float(rank_quantile(qs, .05)), float(rank_quantile(qs, .95))
|
| 107 |
+
center = float(rank_quantile(simulated.reshape(-1), probability))
|
| 108 |
+
point["simulated"][key] = {
|
| 109 |
+
"center": center if center >= lloq else None,
|
| 110 |
+
"lower": max(lo, lloq) if hi >= lloq else None,
|
| 111 |
+
"upper": hi if hi >= lloq else None,
|
| 112 |
+
"lowerCensored": lo < lloq,
|
| 113 |
+
}
|
| 114 |
+
summary["censoring"] = {"methodVersion": CENSORING_VERSION, "lloq": lloq}
|
| 115 |
+
return summary
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def dashboard_vpc_summary(pool, query_time, cohort, *, study=None, replicates=200, seed=43, num_bins=None):
|
| 119 |
+
summary = base.dashboard_vpc_summary(pool, query_time, cohort, replicates=replicates, seed=seed, num_bins=num_bins)
|
| 120 |
+
return _apply(summary, cohort, study, pool, query_time, replicates=replicates, seed=seed)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def observed_vpc_summary(cohort, *, study=None, num_bins=None):
|
| 124 |
+
if _limit(study) is None:
|
| 125 |
+
return base.observed_vpc_summary(cohort, num_bins=num_bins)
|
| 126 |
+
frame = base._observed(cohort)
|
| 127 |
+
stats, bins = _pharmpy_statistics(frame, base._simulations(np.tile(frame.DV.to_numpy(), (2, 1))),
|
| 128 |
+
requested_bins=base._bins(frame, num_bins))
|
| 129 |
+
summary = _apply({"effectiveBins": bins, "points": base._points(stats)}, cohort, study)
|
| 130 |
+
return {"methodVersion": base.BINNED_VPC_VERSION, "effectiveBins": bins,
|
| 131 |
+
"censoring": summary["censoring"],
|
| 132 |
+
"points": [{"time": p["time"], "n": p["nObservations"], **p["observed"], "blq": p["blq"]}
|
| 133 |
+
for p in summary["points"]]}
|
services/inference/pff_service.py
CHANGED
|
@@ -39,7 +39,7 @@ from pff_pk.inference.empirical import ( # noqa: E402
|
|
| 39 |
)
|
| 40 |
from pff_pk.inference.model import load_inference_model # noqa: E402
|
| 41 |
|
| 42 |
-
from
|
| 43 |
|
| 44 |
DEFAULT_CONFIG = PFF_ROOT / "configs" / "amarel_v6_protocol_counterfactual_phase2_sparse.yaml"
|
| 45 |
DEFAULT_CHECKPOINT = (
|
|
@@ -160,6 +160,12 @@ def build_cohort(study: dict[str, Any]) -> dict[str, Any]:
|
|
| 160 |
raise ValueError(f"invalid point for subject {identifier!r}")
|
| 161 |
observation_time = finite(point[0], f"subject time {index}")
|
| 162 |
concentration = finite(point[1], f"subject concentration {index}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
if observation_time >= 0 and concentration > 0:
|
| 164 |
points.append((observation_time, concentration))
|
| 165 |
if identifier and len(points) >= 2:
|
|
@@ -403,6 +409,7 @@ class ModelRuntime:
|
|
| 403 |
samples,
|
| 404 |
query_time,
|
| 405 |
cohort,
|
|
|
|
| 406 |
replicates=VPC_REPLICATES,
|
| 407 |
seed=seed + VPC_SEED_OFFSET,
|
| 408 |
)
|
|
@@ -496,7 +503,7 @@ def cached_inference(request: dict[str, Any]) -> dict[str, Any]:
|
|
| 496 |
runtime = runtime_for_request(request)
|
| 497 |
runtime.load()
|
| 498 |
cache_key = {
|
| 499 |
-
"schemaVersion":
|
| 500 |
"vpcVersion": DASHBOARD_VPC_VERSION,
|
| 501 |
"request": request,
|
| 502 |
"checkpointSha256": runtime.checkpoint_sha256,
|
|
|
|
| 39 |
)
|
| 40 |
from pff_pk.inference.model import load_inference_model # noqa: E402
|
| 41 |
|
| 42 |
+
from services.inference.censored_vpc import DASHBOARD_VPC_VERSION, dashboard_vpc_summary # noqa: E402
|
| 43 |
|
| 44 |
DEFAULT_CONFIG = PFF_ROOT / "configs" / "amarel_v6_protocol_counterfactual_phase2_sparse.yaml"
|
| 45 |
DEFAULT_CHECKPOINT = (
|
|
|
|
| 160 |
raise ValueError(f"invalid point for subject {identifier!r}")
|
| 161 |
observation_time = finite(point[0], f"subject time {index}")
|
| 162 |
concentration = finite(point[1], f"subject concentration {index}")
|
| 163 |
+
assay = study.get("assay") or {}
|
| 164 |
+
if concentration == 0 and assay.get("lloq") is not None:
|
| 165 |
+
limit = finite(assay["lloq"], "assay LLOQ")
|
| 166 |
+
if limit <= 0:
|
| 167 |
+
raise ValueError("assay LLOQ must be positive")
|
| 168 |
+
concentration = limit
|
| 169 |
if observation_time >= 0 and concentration > 0:
|
| 170 |
points.append((observation_time, concentration))
|
| 171 |
if identifier and len(points) >= 2:
|
|
|
|
| 409 |
samples,
|
| 410 |
query_time,
|
| 411 |
cohort,
|
| 412 |
+
study=request.get("study"),
|
| 413 |
replicates=VPC_REPLICATES,
|
| 414 |
seed=seed + VPC_SEED_OFFSET,
|
| 415 |
)
|
|
|
|
| 503 |
runtime = runtime_for_request(request)
|
| 504 |
runtime.load()
|
| 505 |
cache_key = {
|
| 506 |
+
"schemaVersion": 5,
|
| 507 |
"vpcVersion": DASHBOARD_VPC_VERSION,
|
| 508 |
"request": request,
|
| 509 |
"checkpointSha256": runtime.checkpoint_sha256,
|
services/inference/synthetic_service.py
CHANGED
|
@@ -402,7 +402,7 @@ def synthetic_request(payload):
|
|
| 402 |
"""Bound expensive/unlucky numerical draws; never hang the public service."""
|
| 403 |
if isinstance(payload, dict) and payload.get("action") == "vpc":
|
| 404 |
from services.inference.pff_service import build_cohort
|
| 405 |
-
from
|
| 406 |
cohort = build_cohort(payload.get("study") or {})
|
| 407 |
bins = payload.get("numBins")
|
| 408 |
if bins is not None:
|
|
@@ -410,9 +410,9 @@ def synthetic_request(payload):
|
|
| 410 |
if "generatedConcentration" in payload:
|
| 411 |
return dashboard_vpc_summary(
|
| 412 |
payload["generatedConcentration"], payload.get("queryTime"), cohort,
|
| 413 |
-
num_bins=bins,
|
| 414 |
)
|
| 415 |
-
return observed_vpc_summary(cohort, num_bins=bins)
|
| 416 |
if isinstance(payload, dict) and payload.get("action") == "describe":
|
| 417 |
return generate(payload)
|
| 418 |
result = subprocess.run(
|
|
|
|
| 402 |
"""Bound expensive/unlucky numerical draws; never hang the public service."""
|
| 403 |
if isinstance(payload, dict) and payload.get("action") == "vpc":
|
| 404 |
from services.inference.pff_service import build_cohort
|
| 405 |
+
from services.inference.censored_vpc import dashboard_vpc_summary, observed_vpc_summary
|
| 406 |
cohort = build_cohort(payload.get("study") or {})
|
| 407 |
bins = payload.get("numBins")
|
| 408 |
if bins is not None:
|
|
|
|
| 410 |
if "generatedConcentration" in payload:
|
| 411 |
return dashboard_vpc_summary(
|
| 412 |
payload["generatedConcentration"], payload.get("queryTime"), cohort,
|
| 413 |
+
study=payload.get("study"), num_bins=bins,
|
| 414 |
)
|
| 415 |
+
return observed_vpc_summary(cohort, study=payload.get("study"), num_bins=bins)
|
| 416 |
if isinstance(payload, dict) and payload.get("action") == "describe":
|
| 417 |
return generate(payload)
|
| 418 |
result = subprocess.run(
|