Fixed database typo and removed unnecessary class identifier.
This commit is contained in:
parent
00ad49a143
commit
45fb349a7d
5098 changed files with 952558 additions and 85 deletions
0
venv/Lib/site-packages/networkx/utils/tests/__init__.py
Normal file
0
venv/Lib/site-packages/networkx/utils/tests/__init__.py
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,18 @@
|
|||
import networkx as nx
|
||||
|
||||
|
||||
def test_reversed():
|
||||
G = nx.DiGraph()
|
||||
G.add_edge("A", "B")
|
||||
|
||||
# no exception
|
||||
with nx.utils.reversed(G):
|
||||
pass
|
||||
assert "B" in G["A"]
|
||||
|
||||
# exception
|
||||
try:
|
||||
with nx.utils.reversed(G):
|
||||
raise Exception
|
||||
except:
|
||||
assert "B" in G["A"]
|
300
venv/Lib/site-packages/networkx/utils/tests/test_decorators.py
Normal file
300
venv/Lib/site-packages/networkx/utils/tests/test_decorators.py
Normal file
|
@ -0,0 +1,300 @@
|
|||
import tempfile
|
||||
import os
|
||||
import pathlib
|
||||
import random
|
||||
|
||||
import pytest
|
||||
|
||||
import networkx as nx
|
||||
from networkx.utils.decorators import open_file, not_implemented_for
|
||||
from networkx.utils.decorators import (
|
||||
preserve_random_state,
|
||||
py_random_state,
|
||||
np_random_state,
|
||||
random_state,
|
||||
)
|
||||
from networkx.utils.misc import PythonRandomInterface
|
||||
|
||||
|
||||
def test_not_implemented_decorator():
|
||||
@not_implemented_for("directed")
|
||||
def test1(G):
|
||||
pass
|
||||
|
||||
test1(nx.Graph())
|
||||
|
||||
|
||||
def test_not_implemented_decorator_key():
|
||||
with pytest.raises(KeyError):
|
||||
|
||||
@not_implemented_for("foo")
|
||||
def test1(G):
|
||||
pass
|
||||
|
||||
test1(nx.Graph())
|
||||
|
||||
|
||||
def test_not_implemented_decorator_raise():
|
||||
with pytest.raises(nx.NetworkXNotImplemented):
|
||||
|
||||
@not_implemented_for("graph")
|
||||
def test1(G):
|
||||
pass
|
||||
|
||||
test1(nx.Graph())
|
||||
|
||||
|
||||
class TestOpenFileDecorator:
|
||||
def setup_method(self):
|
||||
self.text = ["Blah... ", "BLAH ", "BLAH!!!!"]
|
||||
self.fobj = tempfile.NamedTemporaryFile("wb+", delete=False)
|
||||
self.name = self.fobj.name
|
||||
|
||||
def teardown_method(self):
|
||||
self.fobj.close()
|
||||
os.unlink(self.name)
|
||||
|
||||
def write(self, path):
|
||||
for text in self.text:
|
||||
path.write(text.encode("ascii"))
|
||||
|
||||
@open_file(1, "r")
|
||||
def read(self, path):
|
||||
return path.readlines()[0]
|
||||
|
||||
@staticmethod
|
||||
@open_file(0, "wb")
|
||||
def writer_arg0(path):
|
||||
path.write(b"demo")
|
||||
|
||||
@open_file(1, "wb+")
|
||||
def writer_arg1(self, path):
|
||||
self.write(path)
|
||||
|
||||
@open_file(2, "wb")
|
||||
def writer_arg2default(self, x, path=None):
|
||||
if path is None:
|
||||
with tempfile.NamedTemporaryFile("wb+") as fh:
|
||||
self.write(fh)
|
||||
else:
|
||||
self.write(path)
|
||||
|
||||
@open_file(4, "wb")
|
||||
def writer_arg4default(self, x, y, other="hello", path=None, **kwargs):
|
||||
if path is None:
|
||||
with tempfile.NamedTemporaryFile("wb+") as fh:
|
||||
self.write(fh)
|
||||
else:
|
||||
self.write(path)
|
||||
|
||||
@open_file("path", "wb")
|
||||
def writer_kwarg(self, **kwargs):
|
||||
path = kwargs.get("path", None)
|
||||
if path is None:
|
||||
with tempfile.NamedTemporaryFile("wb+") as fh:
|
||||
self.write(fh)
|
||||
else:
|
||||
self.write(path)
|
||||
|
||||
def test_writer_arg0_str(self):
|
||||
self.writer_arg0(self.name)
|
||||
|
||||
def test_writer_arg0_fobj(self):
|
||||
self.writer_arg0(self.fobj)
|
||||
|
||||
def test_writer_arg0_pathlib(self):
|
||||
self.writer_arg0(pathlib.Path(self.name))
|
||||
|
||||
def test_writer_arg1_str(self):
|
||||
self.writer_arg1(self.name)
|
||||
assert self.read(self.name) == "".join(self.text)
|
||||
|
||||
def test_writer_arg1_fobj(self):
|
||||
self.writer_arg1(self.fobj)
|
||||
assert not self.fobj.closed
|
||||
self.fobj.close()
|
||||
assert self.read(self.name) == "".join(self.text)
|
||||
|
||||
def test_writer_arg2default_str(self):
|
||||
self.writer_arg2default(0, path=None)
|
||||
self.writer_arg2default(0, path=self.name)
|
||||
assert self.read(self.name) == "".join(self.text)
|
||||
|
||||
def test_writer_arg2default_fobj(self):
|
||||
self.writer_arg2default(0, path=self.fobj)
|
||||
assert not self.fobj.closed
|
||||
self.fobj.close()
|
||||
assert self.read(self.name) == "".join(self.text)
|
||||
|
||||
def test_writer_arg2default_fobj_path_none(self):
|
||||
self.writer_arg2default(0, path=None)
|
||||
|
||||
def test_writer_arg4default_fobj(self):
|
||||
self.writer_arg4default(0, 1, dog="dog", other="other")
|
||||
self.writer_arg4default(0, 1, dog="dog", other="other", path=self.name)
|
||||
assert self.read(self.name) == "".join(self.text)
|
||||
|
||||
def test_writer_kwarg_str(self):
|
||||
self.writer_kwarg(path=self.name)
|
||||
assert self.read(self.name) == "".join(self.text)
|
||||
|
||||
def test_writer_kwarg_fobj(self):
|
||||
self.writer_kwarg(path=self.fobj)
|
||||
self.fobj.close()
|
||||
assert self.read(self.name) == "".join(self.text)
|
||||
|
||||
def test_writer_kwarg_path_none(self):
|
||||
self.writer_kwarg(path=None)
|
||||
|
||||
|
||||
@preserve_random_state
|
||||
def test_preserve_random_state():
|
||||
try:
|
||||
import numpy.random
|
||||
|
||||
r = numpy.random.random()
|
||||
except ImportError:
|
||||
return
|
||||
assert abs(r - 0.61879477158568) < 1e-16
|
||||
|
||||
|
||||
class TestRandomState:
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
global np
|
||||
np = pytest.importorskip("numpy")
|
||||
|
||||
@random_state(1)
|
||||
def instantiate_random_state(self, random_state):
|
||||
assert isinstance(random_state, np.random.RandomState)
|
||||
return random_state.random_sample()
|
||||
|
||||
@np_random_state(1)
|
||||
def instantiate_np_random_state(self, random_state):
|
||||
assert isinstance(random_state, np.random.RandomState)
|
||||
return random_state.random_sample()
|
||||
|
||||
@py_random_state(1)
|
||||
def instantiate_py_random_state(self, random_state):
|
||||
assert isinstance(random_state, random.Random) or isinstance(
|
||||
random_state, PythonRandomInterface
|
||||
)
|
||||
return random_state.random()
|
||||
|
||||
def test_random_state_None(self):
|
||||
np.random.seed(42)
|
||||
rv = np.random.random_sample()
|
||||
np.random.seed(42)
|
||||
assert rv == self.instantiate_random_state(None)
|
||||
np.random.seed(42)
|
||||
assert rv == self.instantiate_np_random_state(None)
|
||||
|
||||
random.seed(42)
|
||||
rv = random.random()
|
||||
random.seed(42)
|
||||
assert rv == self.instantiate_py_random_state(None)
|
||||
|
||||
def test_random_state_np_random(self):
|
||||
np.random.seed(42)
|
||||
rv = np.random.random_sample()
|
||||
np.random.seed(42)
|
||||
assert rv == self.instantiate_random_state(np.random)
|
||||
np.random.seed(42)
|
||||
assert rv == self.instantiate_np_random_state(np.random)
|
||||
np.random.seed(42)
|
||||
assert rv == self.instantiate_py_random_state(np.random)
|
||||
|
||||
def test_random_state_int(self):
|
||||
np.random.seed(42)
|
||||
np_rv = np.random.random_sample()
|
||||
random.seed(42)
|
||||
py_rv = random.random()
|
||||
|
||||
np.random.seed(42)
|
||||
seed = 1
|
||||
rval = self.instantiate_random_state(seed)
|
||||
rval_expected = np.random.RandomState(seed).rand()
|
||||
assert rval, rval_expected
|
||||
|
||||
rval = self.instantiate_np_random_state(seed)
|
||||
rval_expected = np.random.RandomState(seed).rand()
|
||||
assert rval, rval_expected
|
||||
# test that global seed wasn't changed in function
|
||||
assert np_rv == np.random.random_sample()
|
||||
|
||||
random.seed(42)
|
||||
rval = self.instantiate_py_random_state(seed)
|
||||
rval_expected = random.Random(seed).random()
|
||||
assert rval, rval_expected
|
||||
# test that global seed wasn't changed in function
|
||||
assert py_rv == random.random()
|
||||
|
||||
def test_random_state_np_random_RandomState(self):
|
||||
np.random.seed(42)
|
||||
np_rv = np.random.random_sample()
|
||||
|
||||
np.random.seed(42)
|
||||
seed = 1
|
||||
rng = np.random.RandomState(seed)
|
||||
rval = self.instantiate_random_state(rng)
|
||||
rval_expected = np.random.RandomState(seed).rand()
|
||||
assert rval, rval_expected
|
||||
|
||||
rval = self.instantiate_np_random_state(seed)
|
||||
rval_expected = np.random.RandomState(seed).rand()
|
||||
assert rval, rval_expected
|
||||
|
||||
rval = self.instantiate_py_random_state(seed)
|
||||
rval_expected = np.random.RandomState(seed).rand()
|
||||
assert rval, rval_expected
|
||||
# test that global seed wasn't changed in function
|
||||
assert np_rv == np.random.random_sample()
|
||||
|
||||
def test_random_state_py_random(self):
|
||||
seed = 1
|
||||
rng = random.Random(seed)
|
||||
rv = self.instantiate_py_random_state(rng)
|
||||
assert rv, random.Random(seed).random()
|
||||
|
||||
pytest.raises(ValueError, self.instantiate_random_state, rng)
|
||||
pytest.raises(ValueError, self.instantiate_np_random_state, rng)
|
||||
|
||||
|
||||
def test_random_state_string_arg_index():
|
||||
with pytest.raises(nx.NetworkXError):
|
||||
|
||||
@random_state("a")
|
||||
def make_random_state(rs):
|
||||
pass
|
||||
|
||||
rstate = make_random_state(1)
|
||||
|
||||
|
||||
def test_py_random_state_string_arg_index():
|
||||
with pytest.raises(nx.NetworkXError):
|
||||
|
||||
@py_random_state("a")
|
||||
def make_random_state(rs):
|
||||
pass
|
||||
|
||||
rstate = make_random_state(1)
|
||||
|
||||
|
||||
def test_random_state_invalid_arg_index():
|
||||
with pytest.raises(nx.NetworkXError):
|
||||
|
||||
@random_state(2)
|
||||
def make_random_state(rs):
|
||||
pass
|
||||
|
||||
rstate = make_random_state(1)
|
||||
|
||||
|
||||
def test_py_random_state_invalid_arg_index():
|
||||
with pytest.raises(nx.NetworkXError):
|
||||
|
||||
@py_random_state(2)
|
||||
def make_random_state(rs):
|
||||
pass
|
||||
|
||||
rstate = make_random_state(1)
|
130
venv/Lib/site-packages/networkx/utils/tests/test_heaps.py
Normal file
130
venv/Lib/site-packages/networkx/utils/tests/test_heaps.py
Normal file
|
@ -0,0 +1,130 @@
|
|||
import pytest
|
||||
import networkx as nx
|
||||
from networkx.utils import BinaryHeap, PairingHeap
|
||||
|
||||
|
||||
class X:
|
||||
def __eq__(self, other):
|
||||
raise self is other
|
||||
|
||||
def __ne__(self, other):
|
||||
raise self is not other
|
||||
|
||||
def __lt__(self, other):
|
||||
raise TypeError("cannot compare")
|
||||
|
||||
def __le__(self, other):
|
||||
raise TypeError("cannot compare")
|
||||
|
||||
def __ge__(self, other):
|
||||
raise TypeError("cannot compare")
|
||||
|
||||
def __gt__(self, other):
|
||||
raise TypeError("cannot compare")
|
||||
|
||||
def __hash__(self):
|
||||
return hash(id(self))
|
||||
|
||||
|
||||
x = X()
|
||||
|
||||
|
||||
data = [ # min should not invent an element.
|
||||
("min", nx.NetworkXError),
|
||||
# Popping an empty heap should fail.
|
||||
("pop", nx.NetworkXError),
|
||||
# Getting nonexisting elements should return None.
|
||||
("get", 0, None),
|
||||
("get", x, None),
|
||||
("get", None, None),
|
||||
# Inserting a new key should succeed.
|
||||
("insert", x, 1, True),
|
||||
("get", x, 1),
|
||||
("min", (x, 1)),
|
||||
# min should not pop the top element.
|
||||
("min", (x, 1)),
|
||||
# Inserting a new key of different type should succeed.
|
||||
("insert", 1, -2.0, True),
|
||||
# int and float values should interop.
|
||||
("min", (1, -2.0)),
|
||||
# pop removes minimum-valued element.
|
||||
("insert", 3, -(10 ** 100), True),
|
||||
("insert", 4, 5, True),
|
||||
("pop", (3, -(10 ** 100))),
|
||||
("pop", (1, -2.0)),
|
||||
# Decrease-insert should succeed.
|
||||
("insert", 4, -50, True),
|
||||
("insert", 4, -60, False, True),
|
||||
# Decrease-insert should not create duplicate keys.
|
||||
("pop", (4, -60)),
|
||||
("pop", (x, 1)),
|
||||
# Popping all elements should empty the heap.
|
||||
("min", nx.NetworkXError),
|
||||
("pop", nx.NetworkXError),
|
||||
# Non-value-changing insert should fail.
|
||||
("insert", x, 0, True),
|
||||
("insert", x, 0, False, False),
|
||||
("min", (x, 0)),
|
||||
("insert", x, 0, True, False),
|
||||
("min", (x, 0)),
|
||||
# Failed insert should not create duplicate keys.
|
||||
("pop", (x, 0)),
|
||||
("pop", nx.NetworkXError),
|
||||
# Increase-insert should succeed when allowed.
|
||||
("insert", None, 0, True),
|
||||
("insert", 2, -1, True),
|
||||
("min", (2, -1)),
|
||||
("insert", 2, 1, True, False),
|
||||
("min", (None, 0)),
|
||||
# Increase-insert should fail when disallowed.
|
||||
("insert", None, 2, False, False),
|
||||
("min", (None, 0)),
|
||||
# Failed increase-insert should not create duplicate keys.
|
||||
("pop", (None, 0)),
|
||||
("pop", (2, 1)),
|
||||
("min", nx.NetworkXError),
|
||||
("pop", nx.NetworkXError),
|
||||
]
|
||||
|
||||
|
||||
def _test_heap_class(cls, *args, **kwargs):
|
||||
heap = cls(*args, **kwargs)
|
||||
# Basic behavioral test
|
||||
for op in data:
|
||||
if op[-1] is not nx.NetworkXError:
|
||||
assert op[-1] == getattr(heap, op[0])(*op[1:-1])
|
||||
else:
|
||||
pytest.raises(op[-1], getattr(heap, op[0]), *op[1:-1])
|
||||
# Coverage test.
|
||||
for i in range(99, -1, -1):
|
||||
assert heap.insert(i, i)
|
||||
for i in range(50):
|
||||
assert heap.pop() == (i, i)
|
||||
for i in range(100):
|
||||
assert heap.insert(i, i) == (i < 50)
|
||||
for i in range(100):
|
||||
assert not heap.insert(i, i + 1)
|
||||
for i in range(50):
|
||||
assert heap.pop() == (i, i)
|
||||
for i in range(100):
|
||||
assert heap.insert(i, i + 1) == (i < 50)
|
||||
for i in range(49):
|
||||
assert heap.pop() == (i, i + 1)
|
||||
assert sorted([heap.pop(), heap.pop()]) == [(49, 50), (50, 50)]
|
||||
for i in range(51, 100):
|
||||
assert not heap.insert(i, i + 1, True)
|
||||
for i in range(51, 70):
|
||||
assert heap.pop() == (i, i + 1)
|
||||
for i in range(100):
|
||||
assert heap.insert(i, i)
|
||||
for i in range(100):
|
||||
assert heap.pop() == (i, i)
|
||||
pytest.raises(nx.NetworkXError, heap.pop)
|
||||
|
||||
|
||||
def test_PairingHeap():
|
||||
_test_heap_class(PairingHeap)
|
||||
|
||||
|
||||
def test_BinaryHeap():
|
||||
_test_heap_class(BinaryHeap)
|
157
venv/Lib/site-packages/networkx/utils/tests/test_mapped_queue.py
Normal file
157
venv/Lib/site-packages/networkx/utils/tests/test_mapped_queue.py
Normal file
|
@ -0,0 +1,157 @@
|
|||
from networkx.utils.mapped_queue import MappedQueue
|
||||
|
||||
|
||||
class TestMappedQueue:
|
||||
def setup(self):
|
||||
pass
|
||||
|
||||
def _check_map(self, q):
|
||||
d = {elt: pos for pos, elt in enumerate(q.h)}
|
||||
assert d == q.d
|
||||
|
||||
def _make_mapped_queue(self, h):
|
||||
q = MappedQueue()
|
||||
q.h = h
|
||||
q.d = {elt: pos for pos, elt in enumerate(h)}
|
||||
return q
|
||||
|
||||
def test_heapify(self):
|
||||
h = [5, 4, 3, 2, 1, 0]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._heapify()
|
||||
self._check_map(q)
|
||||
|
||||
def test_init(self):
|
||||
h = [5, 4, 3, 2, 1, 0]
|
||||
q = MappedQueue(h)
|
||||
self._check_map(q)
|
||||
|
||||
def test_len(self):
|
||||
h = [5, 4, 3, 2, 1, 0]
|
||||
q = MappedQueue(h)
|
||||
self._check_map(q)
|
||||
assert len(q) == 6
|
||||
|
||||
def test_siftup_leaf(self):
|
||||
h = [2]
|
||||
h_sifted = [2]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._siftup(0)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_siftup_one_child(self):
|
||||
h = [2, 0]
|
||||
h_sifted = [0, 2]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._siftup(0)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_siftup_left_child(self):
|
||||
h = [2, 0, 1]
|
||||
h_sifted = [0, 2, 1]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._siftup(0)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_siftup_right_child(self):
|
||||
h = [2, 1, 0]
|
||||
h_sifted = [0, 1, 2]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._siftup(0)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_siftup_multiple(self):
|
||||
h = [0, 1, 2, 4, 3, 5, 6]
|
||||
h_sifted = [1, 3, 2, 4, 0, 5, 6]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._siftup(0)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_siftdown_leaf(self):
|
||||
h = [2]
|
||||
h_sifted = [2]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._siftdown(0)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_siftdown_single(self):
|
||||
h = [1, 0]
|
||||
h_sifted = [0, 1]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._siftdown(len(h) - 1)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_siftdown_multiple(self):
|
||||
h = [1, 2, 3, 4, 5, 6, 7, 0]
|
||||
h_sifted = [0, 1, 3, 2, 5, 6, 7, 4]
|
||||
q = self._make_mapped_queue(h)
|
||||
q._siftdown(len(h) - 1)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_push(self):
|
||||
to_push = [6, 1, 4, 3, 2, 5, 0]
|
||||
h_sifted = [0, 2, 1, 6, 3, 5, 4]
|
||||
q = MappedQueue()
|
||||
for elt in to_push:
|
||||
q.push(elt)
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
|
||||
def test_push_duplicate(self):
|
||||
to_push = [2, 1, 0]
|
||||
h_sifted = [0, 2, 1]
|
||||
q = MappedQueue()
|
||||
for elt in to_push:
|
||||
inserted = q.push(elt)
|
||||
assert inserted
|
||||
assert q.h == h_sifted
|
||||
self._check_map(q)
|
||||
inserted = q.push(1)
|
||||
assert not inserted
|
||||
|
||||
def test_pop(self):
|
||||
h = [3, 4, 6, 0, 1, 2, 5]
|
||||
h_sorted = sorted(h)
|
||||
q = self._make_mapped_queue(h)
|
||||
q._heapify()
|
||||
popped = []
|
||||
for elt in sorted(h):
|
||||
popped.append(q.pop())
|
||||
assert popped == h_sorted
|
||||
self._check_map(q)
|
||||
|
||||
def test_remove_leaf(self):
|
||||
h = [0, 2, 1, 6, 3, 5, 4]
|
||||
h_removed = [0, 2, 1, 6, 4, 5]
|
||||
q = self._make_mapped_queue(h)
|
||||
removed = q.remove(3)
|
||||
assert q.h == h_removed
|
||||
|
||||
def test_remove_root(self):
|
||||
h = [0, 2, 1, 6, 3, 5, 4]
|
||||
h_removed = [1, 2, 4, 6, 3, 5]
|
||||
q = self._make_mapped_queue(h)
|
||||
removed = q.remove(0)
|
||||
assert q.h == h_removed
|
||||
|
||||
def test_update_leaf(self):
|
||||
h = [0, 20, 10, 60, 30, 50, 40]
|
||||
h_updated = [0, 15, 10, 60, 20, 50, 40]
|
||||
q = self._make_mapped_queue(h)
|
||||
removed = q.update(30, 15)
|
||||
assert q.h == h_updated
|
||||
|
||||
def test_update_root(self):
|
||||
h = [0, 20, 10, 60, 30, 50, 40]
|
||||
h_updated = [10, 20, 35, 60, 30, 50, 40]
|
||||
q = self._make_mapped_queue(h)
|
||||
removed = q.update(0, 35)
|
||||
assert q.h == h_updated
|
222
venv/Lib/site-packages/networkx/utils/tests/test_misc.py
Normal file
222
venv/Lib/site-packages/networkx/utils/tests/test_misc.py
Normal file
|
@ -0,0 +1,222 @@
|
|||
import pytest
|
||||
import networkx as nx
|
||||
import random
|
||||
from networkx.utils import (
|
||||
create_py_random_state,
|
||||
create_random_state,
|
||||
discrete_sequence,
|
||||
dict_to_numpy_array,
|
||||
dict_to_numpy_array1,
|
||||
dict_to_numpy_array2,
|
||||
is_string_like,
|
||||
iterable,
|
||||
groups,
|
||||
make_list_of_ints,
|
||||
make_str,
|
||||
pairwise,
|
||||
powerlaw_sequence,
|
||||
PythonRandomInterface,
|
||||
to_tuple,
|
||||
)
|
||||
|
||||
|
||||
def test_is_string_like():
|
||||
assert is_string_like("aaaa")
|
||||
assert not is_string_like(None)
|
||||
assert not is_string_like(123)
|
||||
|
||||
|
||||
def test_iterable():
|
||||
assert not iterable(None)
|
||||
assert not iterable(10)
|
||||
assert iterable([1, 2, 3])
|
||||
assert iterable((1, 2, 3))
|
||||
assert iterable({1: "A", 2: "X"})
|
||||
assert iterable("ABC")
|
||||
|
||||
|
||||
def test_graph_iterable():
|
||||
K = nx.complete_graph(10)
|
||||
assert iterable(K)
|
||||
assert iterable(K.nodes())
|
||||
assert iterable(K.edges())
|
||||
|
||||
|
||||
def test_make_list_of_ints():
|
||||
mylist = [1, 2, 3.0, 42, -2]
|
||||
assert make_list_of_ints(mylist) is mylist
|
||||
assert make_list_of_ints(mylist) == mylist
|
||||
assert type(make_list_of_ints(mylist)[2]) is int
|
||||
pytest.raises(nx.NetworkXError, make_list_of_ints, [1, 2, 3, "kermit"])
|
||||
pytest.raises(nx.NetworkXError, make_list_of_ints, [1, 2, 3.1])
|
||||
|
||||
|
||||
def test_random_number_distribution():
|
||||
# smoke test only
|
||||
z = powerlaw_sequence(20, exponent=2.5)
|
||||
z = discrete_sequence(20, distribution=[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3])
|
||||
|
||||
|
||||
def test_make_str_with_bytes():
|
||||
x = "qualité"
|
||||
y = make_str(x)
|
||||
assert isinstance(y, str)
|
||||
assert len(y) == 7
|
||||
|
||||
|
||||
def test_make_str_with_unicode():
|
||||
x = "qualité"
|
||||
y = make_str(x)
|
||||
assert isinstance(y, str)
|
||||
assert len(y) == 7
|
||||
|
||||
|
||||
class TestNumpyArray:
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
global numpy
|
||||
global assert_allclose
|
||||
numpy = pytest.importorskip("numpy")
|
||||
assert_allclose = numpy.testing.assert_allclose
|
||||
|
||||
def test_numpy_to_list_of_ints(self):
|
||||
a = numpy.array([1, 2, 3], dtype=numpy.int64)
|
||||
b = numpy.array([1.0, 2, 3])
|
||||
c = numpy.array([1.1, 2, 3])
|
||||
assert type(make_list_of_ints(a)) == list
|
||||
assert make_list_of_ints(b) == list(b)
|
||||
B = make_list_of_ints(b)
|
||||
assert type(B[0]) == int
|
||||
pytest.raises(nx.NetworkXError, make_list_of_ints, c)
|
||||
|
||||
def test_dict_to_numpy_array1(self):
|
||||
d = {"a": 1, "b": 2}
|
||||
a = dict_to_numpy_array1(d, mapping={"a": 0, "b": 1})
|
||||
assert_allclose(a, numpy.array([1, 2]))
|
||||
a = dict_to_numpy_array1(d, mapping={"b": 0, "a": 1})
|
||||
assert_allclose(a, numpy.array([2, 1]))
|
||||
|
||||
a = dict_to_numpy_array1(d)
|
||||
assert_allclose(a.sum(), 3)
|
||||
|
||||
def test_dict_to_numpy_array2(self):
|
||||
d = {"a": {"a": 1, "b": 2}, "b": {"a": 10, "b": 20}}
|
||||
|
||||
mapping = {"a": 1, "b": 0}
|
||||
a = dict_to_numpy_array2(d, mapping=mapping)
|
||||
assert_allclose(a, numpy.array([[20, 10], [2, 1]]))
|
||||
|
||||
a = dict_to_numpy_array2(d)
|
||||
assert_allclose(a.sum(), 33)
|
||||
|
||||
def test_dict_to_numpy_array_a(self):
|
||||
d = {"a": {"a": 1, "b": 2}, "b": {"a": 10, "b": 20}}
|
||||
|
||||
mapping = {"a": 0, "b": 1}
|
||||
a = dict_to_numpy_array(d, mapping=mapping)
|
||||
assert_allclose(a, numpy.array([[1, 2], [10, 20]]))
|
||||
|
||||
mapping = {"a": 1, "b": 0}
|
||||
a = dict_to_numpy_array(d, mapping=mapping)
|
||||
assert_allclose(a, numpy.array([[20, 10], [2, 1]]))
|
||||
|
||||
a = dict_to_numpy_array2(d)
|
||||
assert_allclose(a.sum(), 33)
|
||||
|
||||
def test_dict_to_numpy_array_b(self):
|
||||
d = {"a": 1, "b": 2}
|
||||
|
||||
mapping = {"a": 0, "b": 1}
|
||||
a = dict_to_numpy_array(d, mapping=mapping)
|
||||
assert_allclose(a, numpy.array([1, 2]))
|
||||
|
||||
a = dict_to_numpy_array1(d)
|
||||
assert_allclose(a.sum(), 3)
|
||||
|
||||
|
||||
def test_pairwise():
|
||||
nodes = range(4)
|
||||
node_pairs = [(0, 1), (1, 2), (2, 3)]
|
||||
node_pairs_cycle = node_pairs + [(3, 0)]
|
||||
assert list(pairwise(nodes)) == node_pairs
|
||||
assert list(pairwise(iter(nodes))) == node_pairs
|
||||
assert list(pairwise(nodes, cyclic=True)) == node_pairs_cycle
|
||||
empty_iter = iter(())
|
||||
assert list(pairwise(empty_iter)) == []
|
||||
empty_iter = iter(())
|
||||
assert list(pairwise(empty_iter, cyclic=True)) == []
|
||||
|
||||
|
||||
def test_groups():
|
||||
many_to_one = dict(zip("abcde", [0, 0, 1, 1, 2]))
|
||||
actual = groups(many_to_one)
|
||||
expected = {0: {"a", "b"}, 1: {"c", "d"}, 2: {"e"}}
|
||||
assert actual == expected
|
||||
assert {} == groups({})
|
||||
|
||||
|
||||
def test_to_tuple():
|
||||
a_list = [1, 2, [1, 3]]
|
||||
actual = to_tuple(a_list)
|
||||
expected = (1, 2, (1, 3))
|
||||
assert actual == expected
|
||||
|
||||
a_tuple = (1, 2)
|
||||
actual = to_tuple(a_tuple)
|
||||
expected = a_tuple
|
||||
assert actual == expected
|
||||
|
||||
a_mix = (1, 2, [1, 3])
|
||||
actual = to_tuple(a_mix)
|
||||
expected = (1, 2, (1, 3))
|
||||
assert actual == expected
|
||||
|
||||
|
||||
def test_create_random_state():
|
||||
np = pytest.importorskip("numpy")
|
||||
rs = np.random.RandomState
|
||||
|
||||
assert isinstance(create_random_state(1), rs)
|
||||
assert isinstance(create_random_state(None), rs)
|
||||
assert isinstance(create_random_state(np.random), rs)
|
||||
assert isinstance(create_random_state(rs(1)), rs)
|
||||
pytest.raises(ValueError, create_random_state, "a")
|
||||
|
||||
assert np.all(rs(1).rand(10) == create_random_state(1).rand(10))
|
||||
|
||||
|
||||
def test_create_py_random_state():
|
||||
pyrs = random.Random
|
||||
|
||||
assert isinstance(create_py_random_state(1), pyrs)
|
||||
assert isinstance(create_py_random_state(None), pyrs)
|
||||
assert isinstance(create_py_random_state(pyrs(1)), pyrs)
|
||||
pytest.raises(ValueError, create_py_random_state, "a")
|
||||
|
||||
np = pytest.importorskip("numpy")
|
||||
|
||||
rs = np.random.RandomState
|
||||
nprs = PythonRandomInterface
|
||||
assert isinstance(create_py_random_state(np.random), nprs)
|
||||
assert isinstance(create_py_random_state(rs(1)), nprs)
|
||||
# test default rng input
|
||||
assert isinstance(PythonRandomInterface(), nprs)
|
||||
|
||||
|
||||
def test_PythonRandomInterface():
|
||||
np = pytest.importorskip("numpy")
|
||||
rs = np.random.RandomState
|
||||
rng = PythonRandomInterface(rs(42))
|
||||
rs42 = rs(42)
|
||||
|
||||
# make sure these functions are same as expected outcome
|
||||
assert rng.randrange(3, 5) == rs42.randint(3, 5)
|
||||
assert np.all(rng.choice([1, 2, 3]) == rs42.choice([1, 2, 3]))
|
||||
assert rng.gauss(0, 1) == rs42.normal(0, 1)
|
||||
assert rng.expovariate(1.5) == rs42.exponential(1 / 1.5)
|
||||
assert np.all(rng.shuffle([1, 2, 3]) == rs42.shuffle([1, 2, 3]))
|
||||
assert np.all(
|
||||
rng.sample([1, 2, 3], 2) == rs42.choice([1, 2, 3], (2,), replace=False)
|
||||
)
|
||||
assert rng.randint(3, 5) == rs42.randint(3, 6)
|
||||
assert rng.random() == rs42.random_sample()
|
|
@ -0,0 +1,37 @@
|
|||
import pytest
|
||||
from networkx.utils import (
|
||||
powerlaw_sequence,
|
||||
zipf_rv,
|
||||
random_weighted_sample,
|
||||
weighted_choice,
|
||||
)
|
||||
|
||||
|
||||
def test_degree_sequences():
|
||||
seq = powerlaw_sequence(10, seed=1)
|
||||
seq = powerlaw_sequence(10)
|
||||
assert len(seq) == 10
|
||||
|
||||
|
||||
def test_zipf_rv():
|
||||
r = zipf_rv(2.3, xmin=2, seed=1)
|
||||
r = zipf_rv(2.3, 2, 1)
|
||||
r = zipf_rv(2.3)
|
||||
assert type(r), int
|
||||
pytest.raises(ValueError, zipf_rv, 0.5)
|
||||
pytest.raises(ValueError, zipf_rv, 2, xmin=0)
|
||||
|
||||
|
||||
def test_random_weighted_sample():
|
||||
mapping = {"a": 10, "b": 20}
|
||||
s = random_weighted_sample(mapping, 2, seed=1)
|
||||
s = random_weighted_sample(mapping, 2)
|
||||
assert sorted(s) == sorted(mapping.keys())
|
||||
pytest.raises(ValueError, random_weighted_sample, mapping, 3)
|
||||
|
||||
|
||||
def test_random_weighted_choice():
|
||||
mapping = {"a": 10, "b": 0}
|
||||
c = weighted_choice(mapping, seed=1)
|
||||
c = weighted_choice(mapping)
|
||||
assert c == "a"
|
63
venv/Lib/site-packages/networkx/utils/tests/test_rcm.py
Normal file
63
venv/Lib/site-packages/networkx/utils/tests/test_rcm.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
from networkx.utils import reverse_cuthill_mckee_ordering
|
||||
import networkx as nx
|
||||
|
||||
|
||||
def test_reverse_cuthill_mckee():
|
||||
# example graph from
|
||||
# http://www.boost.org/doc/libs/1_37_0/libs/graph/example/cuthill_mckee_ordering.cpp
|
||||
G = nx.Graph(
|
||||
[
|
||||
(0, 3),
|
||||
(0, 5),
|
||||
(1, 2),
|
||||
(1, 4),
|
||||
(1, 6),
|
||||
(1, 9),
|
||||
(2, 3),
|
||||
(2, 4),
|
||||
(3, 5),
|
||||
(3, 8),
|
||||
(4, 6),
|
||||
(5, 6),
|
||||
(5, 7),
|
||||
(6, 7),
|
||||
]
|
||||
)
|
||||
rcm = list(reverse_cuthill_mckee_ordering(G))
|
||||
assert rcm in [[0, 8, 5, 7, 3, 6, 2, 4, 1, 9], [0, 8, 5, 7, 3, 6, 4, 2, 1, 9]]
|
||||
|
||||
|
||||
def test_rcm_alternate_heuristic():
|
||||
# example from
|
||||
G = nx.Graph(
|
||||
[
|
||||
(0, 0),
|
||||
(0, 4),
|
||||
(1, 1),
|
||||
(1, 2),
|
||||
(1, 5),
|
||||
(1, 7),
|
||||
(2, 2),
|
||||
(2, 4),
|
||||
(3, 3),
|
||||
(3, 6),
|
||||
(4, 4),
|
||||
(5, 5),
|
||||
(5, 7),
|
||||
(6, 6),
|
||||
(7, 7),
|
||||
]
|
||||
)
|
||||
|
||||
answers = [
|
||||
[6, 3, 5, 7, 1, 2, 4, 0],
|
||||
[6, 3, 7, 5, 1, 2, 4, 0],
|
||||
[7, 5, 1, 2, 4, 0, 6, 3],
|
||||
]
|
||||
|
||||
def smallest_degree(G):
|
||||
deg, node = min((d, n) for n, d in G.degree())
|
||||
return node
|
||||
|
||||
rcm = list(reverse_cuthill_mckee_ordering(G, heuristic=smallest_degree))
|
||||
assert rcm in answers
|
|
@ -0,0 +1,42 @@
|
|||
import networkx as nx
|
||||
|
||||
|
||||
def test_unionfind():
|
||||
# Fixed by: 2cddd5958689bdecdcd89b91ac9aaf6ce0e4f6b8
|
||||
# Previously (in 2.x), the UnionFind class could handle mixed types.
|
||||
# But in Python 3.x, this causes a TypeError such as:
|
||||
# TypeError: unorderable types: str() > int()
|
||||
#
|
||||
# Now we just make sure that no exception is raised.
|
||||
x = nx.utils.UnionFind()
|
||||
x.union(0, "a")
|
||||
|
||||
|
||||
def test_subtree_union():
|
||||
# See https://github.com/networkx/networkx/pull/3224
|
||||
# (35db1b551ee65780794a357794f521d8768d5049).
|
||||
# Test if subtree unions hare handled correctly by to_sets().
|
||||
uf = nx.utils.UnionFind()
|
||||
uf.union(1, 2)
|
||||
uf.union(3, 4)
|
||||
uf.union(4, 5)
|
||||
uf.union(1, 5)
|
||||
assert list(uf.to_sets()) == [{1, 2, 3, 4, 5}]
|
||||
|
||||
|
||||
def test_unionfind_weights():
|
||||
# Tests if weights are computed correctly with unions of many elements
|
||||
uf = nx.utils.UnionFind()
|
||||
uf.union(1, 4, 7)
|
||||
uf.union(2, 5, 8)
|
||||
uf.union(3, 6, 9)
|
||||
uf.union(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
assert uf.weights[uf[1]] == 9
|
||||
|
||||
|
||||
def test_empty_union():
|
||||
# Tests if a null-union does nothing.
|
||||
uf = nx.utils.UnionFind((0, 1))
|
||||
uf.union()
|
||||
assert uf[0] == 0
|
||||
assert uf[1] == 1
|
Loading…
Add table
Add a link
Reference in a new issue