-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathtest_dictionary_helpers.py
98 lines (85 loc) · 2.56 KB
/
test_dictionary_helpers.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import numpy as np
from fancyimpute.dictionary_helpers import (
dense_matrix_from_pair_dictionary,
dense_matrix_from_nested_dictionary,
reverse_lookup_from_nested_dict,
transpose_nested_dictionary,
)
from nose.tools import eq_
def test_dense_matrix_from_nested_dictionary():
d = {
"a": {"b": 10},
"b": {"c": 20}
}
X, rows, columns = dense_matrix_from_nested_dictionary(d)
eq_(rows, ["a", "b"])
eq_(columns, ["b", "c"])
eq_(X[0, 0], 10)
assert np.isnan(X[0, 1])
assert np.isnan(X[1, 0])
eq_(X[1, 1], 20)
def test_dense_matrix_from_nested_dictionary_square():
d = {
"a": {"b": 10},
"b": {"c": 20}
}
X, rows, columns = dense_matrix_from_nested_dictionary(d, square_result=True)
eq_(rows, ["a", "b", "c"])
eq_(columns, ["a", "b", "c"])
assert np.isnan(X[0, 0])
eq_(X[0, 1], 10)
assert np.isnan(X[0, 2])
assert np.isnan(X[1, 0])
assert np.isnan(X[1, 1])
eq_(X[1, 2], 20)
assert np.isnan(X[2, 0])
assert np.isnan(X[2, 1])
assert np.isnan(X[2, 2])
def test_dense_matrix_from_pair_dictionary():
d = {
("a", "b"): 10,
("b", "c"): 20
}
X, rows, columns = dense_matrix_from_pair_dictionary(d)
eq_(rows, ["a", "b"])
eq_(columns, ["b", "c"])
eq_(X[0, 0], 10)
assert np.isnan(X[0, 1])
assert np.isnan(X[1, 0])
eq_(X[1, 1], 20)
def test_dense_matrix_from_pair_dictionary_square():
d = {
("a", "b"): 10,
("b", "c"): 20
}
X, rows, columns = dense_matrix_from_pair_dictionary(d, square_result=True)
eq_(rows, ["a", "b", "c"])
eq_(columns, ["a", "b", "c"])
assert np.isnan(X[0, 0])
eq_(X[0, 1], 10)
assert np.isnan(X[0, 2])
assert np.isnan(X[1, 0])
assert np.isnan(X[1, 1])
eq_(X[1, 2], 20)
assert np.isnan(X[2, 0])
assert np.isnan(X[2, 1])
assert np.isnan(X[2, 2])
def test_reverse_lookup_from_nested_dict():
d = {
"a": {"b": 10, "c": 20},
"b": {"c": 5},
"z": {"c": 100}
}
reverse_dict = reverse_lookup_from_nested_dict(d)
len(reverse_dict.keys()) == 2
assert "c" in reverse_dict
eq_(set(reverse_dict["c"]), {("a", 20), ("b", 5), ("z", 100)})
assert "b" in reverse_dict
eq_(reverse_dict["b"], [("a", 10)])
def test_transpose_nested_dictionary():
d = {"a": {"b": 20, "c": 50}, "c": {"q": 500}}
transposed = transpose_nested_dictionary(d)
eq_(set(transposed.keys()), {"b", "c", "q"})
eq_(transposed["q"], {"c": 500})
eq_(transposed["c"], {"a": 50})
eq_(transposed["b"], {"a": 20})