1
|
|
|
""" |
2
|
|
|
Calculations of concordance between annotations. |
3
|
|
|
""" |
4
|
|
|
import abc |
5
|
|
|
import enum |
6
|
|
|
import math |
7
|
|
|
from collections import defaultdict |
8
|
|
|
from typing import Collection, Sequence, Type, Union |
9
|
|
|
|
10
|
|
|
import numpy as np |
|
|
|
|
11
|
|
|
import pandas as pd |
|
|
|
|
12
|
|
|
|
13
|
|
|
from mandos.analysis import AnalysisUtils as Au |
14
|
|
|
from mandos.analysis import SimilarityDfLongForm, SimilarityDfShortForm |
15
|
|
|
from mandos.model import CleverEnum |
16
|
|
|
from mandos.model.hits import AbstractHit |
17
|
|
|
|
18
|
|
|
# note that most of these math functions are much faster than their numpy counterparts |
19
|
|
|
# if we're not broadcasting, it's almost always better to use them |
20
|
|
|
# some are more accurate, too |
21
|
|
|
# e.g. we're using fsum rather than sum |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class MatrixCalculator(metaclass=abc.ABCMeta): |
|
|
|
|
25
|
|
|
def calc_all(self, hits: Sequence[AbstractHit]) -> SimilarityDfLongForm: |
|
|
|
|
26
|
|
|
raise NotImplemented() |
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class JPrimeMatrixCalculator(MatrixCalculator): |
|
|
|
|
30
|
|
|
def calc_all(self, hits: Sequence[AbstractHit]) -> SimilarityDfLongForm: |
31
|
|
|
key_to_hit = Au.hit_multidict(hits, "search_key") |
32
|
|
|
dfs = [] |
33
|
|
|
for key, key_hits in key_to_hit.items(): |
34
|
|
|
df = self.calc_one(key_hits) |
|
|
|
|
35
|
|
|
df = df.to_long_form(psi=key) |
|
|
|
|
36
|
|
|
dfs += [df] |
37
|
|
|
return SimilarityDfLongForm(pd.concat(dfs)) |
38
|
|
|
|
39
|
|
|
def calc_one(self, hits: Sequence[AbstractHit]) -> SimilarityDfShortForm: |
|
|
|
|
40
|
|
|
inchikey_to_hits = Au.hit_multidict(hits, "origin_inchikey") |
41
|
|
|
data = defaultdict(dict) |
42
|
|
|
for (c1, hits1), (c2, hits2) in zip(inchikey_to_hits.items(), inchikey_to_hits.items()): |
|
|
|
|
43
|
|
|
data[c1][c2] = self._j_prime(hits1, hits2) |
44
|
|
|
return SimilarityDfShortForm.from_dict(data) |
45
|
|
|
|
46
|
|
|
def _j_prime(self, hits1: Collection[AbstractHit], hits2: Collection[AbstractHit]) -> float: |
47
|
|
|
sources = {h.data_source for h in hits1}.intersection({h.data_source for h in hits2}) |
48
|
|
|
if len(sources) == 0: |
49
|
|
|
return np.nan |
50
|
|
|
values = [ |
51
|
|
|
self._jx( |
52
|
|
|
[h for h in hits1 if h.data_source == source], |
53
|
|
|
[h for h in hits1 if h.data_source == source], |
54
|
|
|
) |
55
|
|
|
for source in sources |
56
|
|
|
] |
57
|
|
|
return float(math.fsum(values) / len(values)) |
58
|
|
|
|
59
|
|
|
def _jx(self, hits1: Collection[AbstractHit], hits2: Collection[AbstractHit]) -> float: |
60
|
|
|
pair_to_weights = Au.weights_of_pairs(hits1, hits2) |
61
|
|
|
values = [self._wedge(ca, cb) / self._vee(ca, cb) for ca, cb in pair_to_weights.values()] |
62
|
|
|
return float(math.fsum(values) / len(values)) |
63
|
|
|
|
64
|
|
|
def _wedge(self, ca: float, cb: float) -> float: |
|
|
|
|
65
|
|
|
return math.sqrt(Au.elle(ca) * Au.elle(cb)) |
66
|
|
|
|
67
|
|
|
def _vee(self, ca: float, cb: float) -> float: |
|
|
|
|
68
|
|
|
return Au.elle(ca) + Au.elle(cb) - math.sqrt(Au.elle(ca) * Au.elle(cb)) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
class MatrixAlg(CleverEnum): |
|
|
|
|
72
|
|
|
j = enum.auto() |
73
|
|
|
|
74
|
|
|
@property |
75
|
|
|
def clazz(self) -> Type[MatrixCalculator]: |
|
|
|
|
76
|
|
|
return {MatrixAlg.j: JPrimeMatrixCalculator}[self] |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
class MatrixCalculation: |
|
|
|
|
80
|
|
|
@classmethod |
81
|
|
|
def create(cls, algorithm: Union[str, MatrixAlg]) -> MatrixCalculator: |
|
|
|
|
82
|
|
|
alg_name = algorithm if isinstance(algorithm, str) else algorithm.name |
83
|
|
|
alg = MatrixAlg.of(algorithm) |
84
|
|
|
return alg.clazz(alg_name) |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
__all__ = ["MatrixCalculator", "JPrimeMatrixCalculator"] |
88
|
|
|
|