Download code/src/lnpbench/context.py from eduzrh/LNPBench: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/src/lnpbench/context.py
- Command line
-
hf download hf://datasets/eduzrh/LNPBench/code/src/lnpbench/context.py
-
curl -L -o context.py https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/src/lnpbench/context.py
2.78 kB
| """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])) |