diff options
author | Casper van Elteren <caspervanelteren@gmail.com> | 2021-10-14 15:21:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-14 09:21:33 -0400 |
commit | ba48935f4c1bcec19f93090a81409fde823561e4 (patch) | |
tree | 5564d8505b5729b3917a57055d54d19911b26075 | |
parent | 92225435bbb1461391289bd5b168717e2318e8bb (diff) | |
download | networkx-ba48935f4c1bcec19f93090a81409fde823561e4.tar.gz |
Consistent return type in dictionary output of rescale_layout and rescale_layout_dict (#5091)
* changed tuple to np.array
* changed return to zip
* modified unittest to match rescale_layout_dict return type
* modified doctest rescale_layout_dict
* added import statement to doctest; test pass
* Add release note about change in return type.
Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
-rw-r--r-- | doc/release/release_dev.rst | 4 | ||||
-rw-r--r-- | networkx/drawing/layout.py | 11 | ||||
-rw-r--r-- | networkx/drawing/tests/test_layout.py | 18 |
3 files changed, 26 insertions, 7 deletions
diff --git a/doc/release/release_dev.rst b/doc/release/release_dev.rst index 0c4b8054..a779ab16 100644 --- a/doc/release/release_dev.rst +++ b/doc/release/release_dev.rst @@ -38,6 +38,10 @@ Improvements API Changes ----------- +- The values in the dictionary returned by + `~networkx.drawing.layout.rescale_layout_dict` are now `numpy.ndarray` objects + instead of tuples. This makes the return type of ``rescale_layout_dict`` + consistent with that of all of the other layout functions. Deprecations ------------ diff --git a/networkx/drawing/layout.py b/networkx/drawing/layout.py index 8a6e4ace..f0aa0b4b 100644 --- a/networkx/drawing/layout.py +++ b/networkx/drawing/layout.py @@ -1167,13 +1167,14 @@ def rescale_layout_dict(pos, scale=1): Examples -------- - >>> pos = {0: (0, 0), 1: (1, 1), 2: (0.5, 0.5)} + >>> import numpy as np + >>> pos = {0: np.array((0, 0)), 1: np.array((1, 1)), 2: np.array((0.5, 0.5))} >>> nx.rescale_layout_dict(pos) - {0: (-1.0, -1.0), 1: (1.0, 1.0), 2: (0.0, 0.0)} + {0: array([-1., -1.]), 1: array([1., 1.]), 2: array([0., 0.])} - >>> pos = {0: (0, 0), 1: (-1, 1), 2: (-0.5, 0.5)} + >>> pos = {0: np.array((0, 0)), 1: np.array((-1, 1)), 2: np.array((-0.5, 0.5))} >>> nx.rescale_layout_dict(pos, scale=2) - {0: (2.0, -2.0), 1: (-2.0, 2.0), 2: (0.0, 0.0)} + {0: array([ 2., -2.]), 1: array([-2., 2.]), 2: array([0., 0.])} See Also -------- @@ -1185,4 +1186,4 @@ def rescale_layout_dict(pos, scale=1): return {} pos_v = np.array(list(pos.values())) pos_v = rescale_layout(pos_v, scale=scale) - return {k: tuple(v) for k, v in zip(pos.keys(), pos_v)} + return dict(zip(pos, pos_v)) diff --git a/networkx/drawing/tests/test_layout.py b/networkx/drawing/tests/test_layout.py index e878f5e7..183c9fbc 100644 --- a/networkx/drawing/tests/test_layout.py +++ b/networkx/drawing/tests/test_layout.py @@ -395,6 +395,20 @@ class TestLayout: G = nx.empty_graph(3) vpos = {0: (0, 0), 1: (1, 1), 2: (0.5, 0.5)} s_vpos = nx.rescale_layout_dict(vpos) - assert s_vpos == {0: (-1, -1), 1: (1, 1), 2: (0, 0)} + + expectation = { + 0: np.array((-1, -1)), + 1: np.array((1, 1)), + 2: np.array((0, 0)), + } + for k, v in expectation.items(): + assert (s_vpos[k] == v).all() s_vpos = nx.rescale_layout_dict(vpos, scale=2) - assert s_vpos == {0: (-2, -2), 1: (2, 2), 2: (0, 0)} + + expectation = { + 0: np.array((-2, -2)), + 1: np.array((2, 2)), + 2: np.array((0, 0)), + } + for k, v in expectation.items(): + assert (s_vpos[k] == v).all() |