File size: 663 Bytes
c289d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from typing import Iterable

import numpy as np


def _tanimoto(fp_a: np.ndarray, fp_b: np.ndarray) -> float:
    inter = float(np.sum((fp_a > 0) & (fp_b > 0)))
    union = float(np.sum((fp_a > 0) | (fp_b > 0)))
    if union == 0:
        return 0.0
    return inter / union


def selection_diversity(fingerprints: Iterable[np.ndarray]) -> float:
    fps = list(fingerprints)
    if len(fps) < 2:
        return 0.0
    distances = []
    for i in range(len(fps)):
        for j in range(i + 1, len(fps)):
            distances.append(1.0 - _tanimoto(fps[i], fps[j]))
    return float(np.mean(distances)) if distances else 0.0