File size: 2,044 Bytes
00cf4bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Chemistry feature blocks for the tree-head models.

The block is ``[RDKit descriptors (217), cCPP + geometry (16), formulation
ratios (5), hierarchical fragment physics (6 x 16), helper-lipid physics
(3 x 16), whole-lipid physics (16)]``.  It deliberately contains **no
fingerprints and no experimental context**, because both act as study-identity
shortcuts on this benchmark (see ``results/REPORT.md`` section 4).
"""

from __future__ import annotations

import numpy as np

from . import data

FRAG_SLOTS = list(data.FRAGMENT_SLOTS)
HELPER_SLOTS = ["HL", "CHL", "PEG"]


def chemistry_block(store, idx: np.ndarray) -> np.ndarray:
    """Return the (len(idx), D) chemistry matrix for the given sample rows."""
    idx = np.asarray(idx)
    n = len(idx)
    il = store.il_id[idx]

    dim = store.frag_phys[FRAG_SLOTS[0]].shape[1]
    frag = np.zeros((n, len(FRAG_SLOTS) * dim), dtype=np.float32)
    for k, slot in enumerate(FRAG_SLOTS):
        tab = store.frag_phys[slot]
        if len(tab) == 0:
            continue
        ids = store.frag_ids[idx, k]
        ok = ids >= 0
        frag[ok, k * dim:(k + 1) * dim] = tab[np.clip(ids[ok], 0, len(tab) - 1)]

    helper = np.zeros((n, len(HELPER_SLOTS) * dim), dtype=np.float32)
    for k, slot in enumerate(HELPER_SLOTS):
        tab = store.frag_phys[slot]
        if len(tab) == 0:
            continue
        ids = store.helper_ids[idx, k]
        ok = ids >= 0
        helper[ok, k * dim:(k + 1) * dim] = tab[np.clip(ids[ok], 0, len(tab) - 1)]

    X = np.concatenate([
        store.il_desc[il],
        store.il_cpp[il],
        store.ratios[idx],
        frag,
        helper,
        store.il_phys[il],
    ], axis=1)
    X = np.nan_to_num(X, nan=0.0, posinf=0.0, neginf=0.0)
    return np.clip(X, -1e6, 1e6).astype(np.float64)


def chemistry_block_with_ctx(store, idx: np.ndarray) -> np.ndarray:
    """Same block plus the 109-d experimental context (the identity-shortcut arm)."""
    return np.concatenate([chemistry_block(store, idx), store.ctx[np.asarray(idx)]], axis=1)