| 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 | |