comparison env/lib/python3.9/site-packages/networkx/utils/tests/test_misc.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import pytest
2 import networkx as nx
3 import random
4 from networkx.utils import (
5 create_py_random_state,
6 create_random_state,
7 discrete_sequence,
8 dict_to_numpy_array,
9 dict_to_numpy_array1,
10 dict_to_numpy_array2,
11 is_string_like,
12 iterable,
13 groups,
14 make_list_of_ints,
15 make_str,
16 pairwise,
17 powerlaw_sequence,
18 PythonRandomInterface,
19 to_tuple,
20 )
21
22
23 def test_is_string_like():
24 assert is_string_like("aaaa")
25 assert not is_string_like(None)
26 assert not is_string_like(123)
27
28
29 def test_iterable():
30 assert not iterable(None)
31 assert not iterable(10)
32 assert iterable([1, 2, 3])
33 assert iterable((1, 2, 3))
34 assert iterable({1: "A", 2: "X"})
35 assert iterable("ABC")
36
37
38 def test_graph_iterable():
39 K = nx.complete_graph(10)
40 assert iterable(K)
41 assert iterable(K.nodes())
42 assert iterable(K.edges())
43
44
45 def test_make_list_of_ints():
46 mylist = [1, 2, 3.0, 42, -2]
47 assert make_list_of_ints(mylist) is mylist
48 assert make_list_of_ints(mylist) == mylist
49 assert type(make_list_of_ints(mylist)[2]) is int
50 pytest.raises(nx.NetworkXError, make_list_of_ints, [1, 2, 3, "kermit"])
51 pytest.raises(nx.NetworkXError, make_list_of_ints, [1, 2, 3.1])
52
53
54 def test_random_number_distribution():
55 # smoke test only
56 z = powerlaw_sequence(20, exponent=2.5)
57 z = discrete_sequence(20, distribution=[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3])
58
59
60 def test_make_str_with_bytes():
61 x = "qualité"
62 y = make_str(x)
63 assert isinstance(y, str)
64 assert len(y) == 7
65
66
67 def test_make_str_with_unicode():
68 x = "qualité"
69 y = make_str(x)
70 assert isinstance(y, str)
71 assert len(y) == 7
72
73
74 class TestNumpyArray:
75 @classmethod
76 def setup_class(cls):
77 global numpy
78 global assert_allclose
79 numpy = pytest.importorskip("numpy")
80 assert_allclose = numpy.testing.assert_allclose
81
82 def test_numpy_to_list_of_ints(self):
83 a = numpy.array([1, 2, 3], dtype=numpy.int64)
84 b = numpy.array([1.0, 2, 3])
85 c = numpy.array([1.1, 2, 3])
86 assert type(make_list_of_ints(a)) == list
87 assert make_list_of_ints(b) == list(b)
88 B = make_list_of_ints(b)
89 assert type(B[0]) == int
90 pytest.raises(nx.NetworkXError, make_list_of_ints, c)
91
92 def test_dict_to_numpy_array1(self):
93 d = {"a": 1, "b": 2}
94 a = dict_to_numpy_array1(d, mapping={"a": 0, "b": 1})
95 assert_allclose(a, numpy.array([1, 2]))
96 a = dict_to_numpy_array1(d, mapping={"b": 0, "a": 1})
97 assert_allclose(a, numpy.array([2, 1]))
98
99 a = dict_to_numpy_array1(d)
100 assert_allclose(a.sum(), 3)
101
102 def test_dict_to_numpy_array2(self):
103 d = {"a": {"a": 1, "b": 2}, "b": {"a": 10, "b": 20}}
104
105 mapping = {"a": 1, "b": 0}
106 a = dict_to_numpy_array2(d, mapping=mapping)
107 assert_allclose(a, numpy.array([[20, 10], [2, 1]]))
108
109 a = dict_to_numpy_array2(d)
110 assert_allclose(a.sum(), 33)
111
112 def test_dict_to_numpy_array_a(self):
113 d = {"a": {"a": 1, "b": 2}, "b": {"a": 10, "b": 20}}
114
115 mapping = {"a": 0, "b": 1}
116 a = dict_to_numpy_array(d, mapping=mapping)
117 assert_allclose(a, numpy.array([[1, 2], [10, 20]]))
118
119 mapping = {"a": 1, "b": 0}
120 a = dict_to_numpy_array(d, mapping=mapping)
121 assert_allclose(a, numpy.array([[20, 10], [2, 1]]))
122
123 a = dict_to_numpy_array2(d)
124 assert_allclose(a.sum(), 33)
125
126 def test_dict_to_numpy_array_b(self):
127 d = {"a": 1, "b": 2}
128
129 mapping = {"a": 0, "b": 1}
130 a = dict_to_numpy_array(d, mapping=mapping)
131 assert_allclose(a, numpy.array([1, 2]))
132
133 a = dict_to_numpy_array1(d)
134 assert_allclose(a.sum(), 3)
135
136
137 def test_pairwise():
138 nodes = range(4)
139 node_pairs = [(0, 1), (1, 2), (2, 3)]
140 node_pairs_cycle = node_pairs + [(3, 0)]
141 assert list(pairwise(nodes)) == node_pairs
142 assert list(pairwise(iter(nodes))) == node_pairs
143 assert list(pairwise(nodes, cyclic=True)) == node_pairs_cycle
144 empty_iter = iter(())
145 assert list(pairwise(empty_iter)) == []
146 empty_iter = iter(())
147 assert list(pairwise(empty_iter, cyclic=True)) == []
148
149
150 def test_groups():
151 many_to_one = dict(zip("abcde", [0, 0, 1, 1, 2]))
152 actual = groups(many_to_one)
153 expected = {0: {"a", "b"}, 1: {"c", "d"}, 2: {"e"}}
154 assert actual == expected
155 assert {} == groups({})
156
157
158 def test_to_tuple():
159 a_list = [1, 2, [1, 3]]
160 actual = to_tuple(a_list)
161 expected = (1, 2, (1, 3))
162 assert actual == expected
163
164 a_tuple = (1, 2)
165 actual = to_tuple(a_tuple)
166 expected = a_tuple
167 assert actual == expected
168
169 a_mix = (1, 2, [1, 3])
170 actual = to_tuple(a_mix)
171 expected = (1, 2, (1, 3))
172 assert actual == expected
173
174
175 def test_create_random_state():
176 np = pytest.importorskip("numpy")
177 rs = np.random.RandomState
178
179 assert isinstance(create_random_state(1), rs)
180 assert isinstance(create_random_state(None), rs)
181 assert isinstance(create_random_state(np.random), rs)
182 assert isinstance(create_random_state(rs(1)), rs)
183 pytest.raises(ValueError, create_random_state, "a")
184
185 assert np.all(rs(1).rand(10) == create_random_state(1).rand(10))
186
187
188 def test_create_py_random_state():
189 pyrs = random.Random
190
191 assert isinstance(create_py_random_state(1), pyrs)
192 assert isinstance(create_py_random_state(None), pyrs)
193 assert isinstance(create_py_random_state(pyrs(1)), pyrs)
194 pytest.raises(ValueError, create_py_random_state, "a")
195
196 np = pytest.importorskip("numpy")
197
198 rs = np.random.RandomState
199 nprs = PythonRandomInterface
200 assert isinstance(create_py_random_state(np.random), nprs)
201 assert isinstance(create_py_random_state(rs(1)), nprs)
202 # test default rng input
203 assert isinstance(PythonRandomInterface(), nprs)
204
205
206 def test_PythonRandomInterface():
207 np = pytest.importorskip("numpy")
208 rs = np.random.RandomState
209 rng = PythonRandomInterface(rs(42))
210 rs42 = rs(42)
211
212 # make sure these functions are same as expected outcome
213 assert rng.randrange(3, 5) == rs42.randint(3, 5)
214 assert np.all(rng.choice([1, 2, 3]) == rs42.choice([1, 2, 3]))
215 assert rng.gauss(0, 1) == rs42.normal(0, 1)
216 assert rng.expovariate(1.5) == rs42.exponential(1 / 1.5)
217 assert np.all(rng.shuffle([1, 2, 3]) == rs42.shuffle([1, 2, 3]))
218 assert np.all(
219 rng.sample([1, 2, 3], 2) == rs42.choice([1, 2, 3], (2,), replace=False)
220 )
221 assert rng.randint(3, 5) == rs42.randint(3, 6)
222 assert rng.random() == rs42.random_sample()