|
1
|
|
|
""" |
|
2
|
|
|
Calculations. |
|
3
|
|
|
""" |
|
4
|
|
|
import math |
|
5
|
|
|
from collections import defaultdict |
|
6
|
|
|
from typing import Collection, Dict, Sequence, Set, Tuple, Union, Optional |
|
7
|
|
|
|
|
8
|
|
|
from typeddfs import TypedDfs |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
from mandos.model.hits import AbstractHit, HitFrame, Pair |
|
11
|
|
|
|
|
12
|
|
|
SimilarityDfShortForm = TypedDfs.typed("SimilarityDfShortForm").symmetric().build() |
|
13
|
|
|
|
|
14
|
|
|
SimilarityDfLongForm = ( |
|
15
|
|
|
TypedDfs.typed("SimilarityDfLongForm") |
|
16
|
|
|
.require("i", "j", dtype=str) |
|
17
|
|
|
.require("value", dtype=float) |
|
18
|
|
|
.reserve("phi", "psi", dtype=str) |
|
19
|
|
|
).build() |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
SimilarityDf = Union[SimilarityDfLongForm, SimilarityDfShortForm] |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def _to_long_form( |
|
26
|
|
|
self: SimilarityDfShortForm, phi: Optional[str] = None, psi: Optional[str] = None |
|
|
|
|
|
|
27
|
|
|
) -> SimilarityDfLongForm: |
|
28
|
|
|
if (phi is None) == (psi is None): |
|
29
|
|
|
raise ValueError(f"Set either phi OR psi (phi={phi}, psi={psi}") |
|
30
|
|
|
stacked = self.stack(level=[self.column_names()]).reset_index() |
|
31
|
|
|
stacked.columns = ["i", "j", "value", *["phi" if phi else "psi"]] |
|
32
|
|
|
return SimilarityDfLongForm(stacked) |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
SimilarityDfShortForm.to_long_form = _to_long_form |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
class AnalysisUtils: |
|
|
|
|
|
|
39
|
|
|
@classmethod |
|
40
|
|
|
def elle(cls, x: float) -> float: |
|
|
|
|
|
|
41
|
|
|
return math.log10(1 + x) |
|
42
|
|
|
|
|
43
|
|
|
@classmethod |
|
44
|
|
|
def hit_multidict(cls, hits: Sequence[AbstractHit], key: str): |
|
|
|
|
|
|
45
|
|
|
x_to_hits = defaultdict(list) |
|
46
|
|
|
for hit in hits: |
|
47
|
|
|
x_to_hits[getattr(hit, key)].append(hit) |
|
48
|
|
|
return x_to_hits |
|
49
|
|
|
|
|
50
|
|
|
@classmethod |
|
51
|
|
|
def weights_of_pairs( |
|
52
|
|
|
cls, hits1: Collection[AbstractHit], hits2: Collection[AbstractHit] |
|
|
|
|
|
|
53
|
|
|
) -> Dict[Pair, Tuple[float, float]]: |
|
54
|
|
|
""" |
|
55
|
|
|
Calculates the sum of |
|
56
|
|
|
""" |
|
57
|
|
|
union = {h.to_pair for h in hits1}.union({h.to_pair for h in hits2}) |
|
58
|
|
|
return {p: (cls._score(hits1, p), cls._score(hits2, p)) for p in union} |
|
59
|
|
|
|
|
60
|
|
|
@classmethod |
|
61
|
|
|
def _score(cls, hits: Collection[AbstractHit], pair: Pair) -> int: |
|
62
|
|
|
return sum([h.weight for h in hits if h.to_pair == pair]) |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
__all__ = ["AnalysisUtils", "SimilarityDfShortForm", "SimilarityDfLongForm", "SimilarityDf"] |
|
66
|
|
|
|