Clustering and diversity selection#
nvmolkit.clustering provides Butina and directed sphere exclusion (DISE) clustering, and
nvmolkit.pickers provides Leader and MaxMin selection.
Matrix and fused forms#
Each algorithm has two forms:
Algorithm |
Matrix form |
Fused form |
|---|---|---|
Butina |
||
Leader |
||
MaxMin |
||
DISE |
The matrix form takes a precomputed distance matrix, from any source. Its memory grows with the square of the number of items.
The fused form takes fingerprints or molecules and a similarity metric, and computes distances as it needs them, so its memory grows linearly. Use it when the distance matrix would not fit in GPU memory.
Both forms return the same result for the same distances. Cutoffs and
thresholds are distances, 1 - similarity: a similarity threshold of 0.7 is a
cutoff of 0.3.
Metrics#
Fused forms select a similarity metric with metric=, given as a name or as a
metric object:
Metric |
Name |
Input |
|---|---|---|
|
Packed |
|
|
Packed |
|
|
Sequence of RDKit molecules |
Every fused form accepts every metric, except that
fused_butina() does not yet support
AAPMetric. Metric objects carry parameters; for
example, AAPMetric(max_path_length=8).
from rdkit import Chem
from nvmolkit.clustering import OutputMode
from nvmolkit.fingerprints import MorganFingerprintGenerator
from nvmolkit.pickers import fused_leader, fused_maxmin
from nvmolkit.similarity import AAPMetric
smiles = ["CCO", "CCN", "CCCC", "c1ccccc1", "c1ccncc1", "CC(=O)O"]
molecules = [Chem.MolFromSmiles(value) for value in smiles]
fingerprints = MorganFingerprintGenerator(radius=2, fpSize=2048).GetFingerprints(molecules)
leaders = fused_leader(fingerprints, 0.6, metric="tanimoto", output=OutputMode.RDKIT)
picks, last_distance = fused_maxmin(fingerprints, 3, metric="tanimoto", seed=23, output=OutputMode.RDKIT)
aap_leaders = fused_leader(molecules, 0.8, metric=AAPMetric(max_path_length=8), output=OutputMode.RDKIT)
Output modes#
output=OutputMode.DEVICE (the default) returns GPU buffers: clustering
functions return a ClusterDeviceResult, and
pickers return an AsyncGpuResult of selected indices.
output=OutputMode.RDKIT returns host tuples of input indices in RDKit’s
format.
Differences from RDKit#
Leader and MaxMin follow RDKit’s LeaderPicker and MaxMinPicker, and a
given MaxMin seed selects the same first pick as RDKit. DISE uses the
leaders LeaderPicker selects as centroids and adds cluster assignment.
Leader, MaxMin, and DISE compare distances in float32, while RDKit uses double
precision, so a distance within float32 rounding of a cutoff or threshold can be
classified differently. For Tanimoto distances this happens only at values that
float32 cannot represent exactly, such as 0.3; values such as 0.25 and 0.5
give RDKit’s results. A distance matrix built from nvMolKit similarities can
differ from fused distances in the last float32 bit.