File size: 2,783 Bytes
4e4f237 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | """Coarse, cross-study experiment context.
The 109-d context vector is a one-hot encoding of fifteen categorical fields
(cargo, cell line, buffer, readout, helper-lipid names, ...) plus nine numeric
features. Most of those fields are study fingerprints: a buffer recipe or a
readout instrument identifies the source publication almost as well as the
lipid does (see the study-identifiability audit in the manuscript). A handful,
however, are shared across essentially every study and describe the *biology*
of the measurement rather than the laboratory that produced it:
* ``cargo_type`` mRNA / siRNA / pDNA / protein ...
* ``model_type`` in vitro / in vivo / ex vivo
* ``model_target`` cell line, organ or animal model at coarse granularity
* ``route`` intravenous / intramuscular / subcutaneous ...
* ``mixing`` pipetting / microfluidic / ...
* nine numeric features: molar fractions, IL/RNA ratio, dose and the three
bond-presence flags of the ionisable lipid (ester / carbonate / disulfide),
which describe the chemistry rather than the study.
``coarse_context`` returns that subset as a dense vector. It is *not* leak-free
in a formal sense -- any feature correlated with the study carries some
identity -- but every component is a variable an experimenter chooses before
seeing the results, and all of them are shared by at least two independent
studies, so a model cannot use them to recognise a held-out publication.
"""
from __future__ import annotations
import json
import numpy as np
# the categorical fields kept in the coarse context, in data.CATEGORICAL_COLS
# order (the one-hot blocks are concatenated in that order)
COARSE_CATEGORICAL = ("cargo_type", "model_type", "model_target", "route", "mixing")
COARSE_NUMERIC_NAMES = (
"w_il", "w_hl", "w_chl", "w_peg", "log_il_na_ratio",
"log_dose", "has_ester", "has_carbonate", "has_disulfide",
)
def ctx_block_layout(vocab: dict) -> tuple[list, int]:
"""(field, start, stop) for every one-hot block of the context vector."""
out, off = [], 0
for name, cats in vocab.items():
out.append((name, off, off + len(cats)))
off += len(cats)
return out, off
def coarse_mask(vocab: dict) -> np.ndarray:
"""Boolean mask over the 109-d context: True = keep."""
blocks, n_cat = ctx_block_layout(vocab)
mask = np.zeros(n_cat + 9, dtype=bool)
for name, a, b in blocks:
if name in COARSE_CATEGORICAL:
mask[a:b] = True
mask[n_cat:] = True
return mask
def coarse_context(ctx: np.ndarray, vocab: dict) -> np.ndarray:
return ctx[:, coarse_mask(vocab)].astype(np.float32)
def load_vocab(sample_features: str) -> dict:
z = np.load(sample_features, allow_pickle=True)
return json.loads(str(z["vocab_json"][0])) |