1
|
|
|
import pytest |
|
|
|
|
2
|
|
|
import numpy as np |
|
|
|
|
3
|
|
|
|
4
|
|
|
from mandos.model.correlation_math import * |
|
|
|
|
5
|
|
|
|
6
|
|
|
from .. import get_test_resource |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class TestAffinityMatrix: |
|
|
|
|
10
|
|
|
def test_read(self): |
|
|
|
|
11
|
|
|
df = AffinityMatrix.read_csv(get_test_resource("affinity_mx.csv")) |
|
|
|
|
12
|
|
|
assert df.rows == ["cocaine", "gabapentin"] |
13
|
|
|
assert df.cols == ["cocaine", "gabapentin"] |
14
|
|
|
|
15
|
|
|
def test_jaccard(self): |
|
|
|
|
16
|
|
|
items = {"cocaine": {"a", "b", "c"}, "gabapentin": {"a", "b"}} |
17
|
|
|
df = AffinityMatrix.from_function(items, AffinityFunctions.jaccard()) |
|
|
|
|
18
|
|
|
assert df.rows == ["cocaine", "gabapentin"] |
19
|
|
|
assert df.cols == ["cocaine", "gabapentin"] |
20
|
|
|
assert df.values.tolist() == [[1, 2 / 3], [2 / 3, 1]] |
21
|
|
|
|
22
|
|
|
def test_minkowski_1_1(self): |
|
|
|
|
23
|
|
|
items = {"cocaine": [1, 2], "gabapentin": [1, 2]} |
24
|
|
|
df = AffinityMatrix.from_function(items, AffinityFunctions.negative_minkowski(1)) |
|
|
|
|
25
|
|
|
assert df.rows == ["cocaine", "gabapentin"] |
26
|
|
|
assert df.cols == ["cocaine", "gabapentin"] |
27
|
|
|
assert df.values.tolist() == [[0, 0], [0, 0]] |
28
|
|
|
|
29
|
|
|
def test_minkowski_1_2(self): |
|
|
|
|
30
|
|
|
items = {"cocaine": [1, 2], "gabapentin": [2, 1]} |
31
|
|
|
df = AffinityMatrix.from_function(items, AffinityFunctions.negative_minkowski(1)) |
|
|
|
|
32
|
|
|
assert df.rows == ["cocaine", "gabapentin"] |
33
|
|
|
assert df.cols == ["cocaine", "gabapentin"] |
34
|
|
|
assert df.values.tolist() == [[0, -2], [-2, 0]] |
35
|
|
|
|
36
|
|
|
def test_minkowski_2(self): |
|
|
|
|
37
|
|
|
items = {"cocaine": [1, 3], "gabapentin": [3, 1]} |
38
|
|
|
df = AffinityMatrix.from_function(items, AffinityFunctions.negative_minkowski(2)) |
|
|
|
|
39
|
|
|
assert df.rows == ["cocaine", "gabapentin"] |
40
|
|
|
assert df.cols == ["cocaine", "gabapentin"] |
41
|
|
|
assert df.values.tolist() == [[0, -np.sqrt(8)], [-np.sqrt(8), 0]] |
42
|
|
|
|
43
|
|
|
def test_minkowski_0(self): |
|
|
|
|
44
|
|
|
items = {"cocaine": [1, 2], "gabapentin": [2, 1]} |
45
|
|
|
df = AffinityMatrix.from_function(items, AffinityFunctions.negative_minkowski(0)) |
|
|
|
|
46
|
|
|
assert df.rows == ["cocaine", "gabapentin"] |
47
|
|
|
assert df.cols == ["cocaine", "gabapentin"] |
48
|
|
|
assert df.values.tolist() == [[0, -2], [-2, 0]] |
49
|
|
|
|
50
|
|
|
def test_minkowski_inf(self): |
|
|
|
|
51
|
|
|
items = {"cocaine": [1, 4], "gabapentin": [2, 1]} |
52
|
|
|
df = AffinityMatrix.from_function( |
|
|
|
|
53
|
|
|
items, AffinityFunctions.negative_minkowski(float("infinity")) |
54
|
|
|
) |
55
|
|
|
assert df.rows == ["cocaine", "gabapentin"] |
56
|
|
|
assert df.cols == ["cocaine", "gabapentin"] |
57
|
|
|
assert df.values.tolist() == [[0, -3], [-3, 0]] |
58
|
|
|
|
59
|
|
|
def test_pairs(self): |
|
|
|
|
60
|
|
|
df = AffinityMatrix.read_csv(get_test_resource("affinity_mx.csv")) |
|
|
|
|
61
|
|
|
dct = df.all_pairs() |
62
|
|
|
assert dct == { |
63
|
|
|
("cocaine", "cocaine"): 0.2, |
64
|
|
|
("gabapentin", "gabapentin"): 0.2, |
65
|
|
|
("cocaine", "gabapentin"): 0.1, |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
def test_non_self_pairs(self): |
|
|
|
|
69
|
|
|
df = AffinityMatrix.read_csv(get_test_resource("affinity_mx.csv")) |
|
|
|
|
70
|
|
|
dct = df.non_self_pairs() |
71
|
|
|
assert dct == {("cocaine", "gabapentin"): 0.1} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
if __name__ == "__main__": |
75
|
|
|
pytest.main() |
76
|
|
|
|