Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/pgraph/PGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4518,7 +4518,8 @@ def __init__(self, coord: ArrayLike | None = None, name: str | None = None):
# invalidate; writing the attributes directly avoids the cost of
# ``__setattr__`` on each of them.
self.__dict__.update(
coord=None if coord is None else np.r_[coord],
# np.array is a tenth of the cost of np.r_ for the same result here
coord=None if coord is None else np.array(coord, ndmin=1),
_name=name,
_label=None,
_connectivitychange=True,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_vertex_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ def test_the_coordinate_is_a_float_array_copy(self):
self.assertEqual(v.coord[0], 1.0) # not the caller's array
self.assertIsInstance(v.coord, np.ndarray)

def test_the_coordinate_is_what_np_r_made(self):
# the coordinate used to be made with np.r_[coord]; for everything a
# coordinate can be it is the same array, in shape, type and values
for coord in (
[1, 2],
[1.5, 2],
(1, 2, 3),
5,
2.5,
np.array([1.0, 2.0]),
np.array([1, 2], dtype=np.int32),
range(3),
[[1, 2], [3, 4]],
):
with self.subTest(coord=repr(coord)):
v = Vertex(coord=coord)
expected = np.r_[coord]
self.assertEqual(v.coord.shape, expected.shape)
self.assertEqual(v.coord.dtype, expected.dtype)
np.testing.assert_array_equal(v.coord, expected)

def test_a_scalar_is_a_vector_of_one(self):
self.assertEqual(Vertex(coord=7).coord.shape, (1,))

def test_an_array_given_is_copied(self):
c = np.array([1, 2])
v = Vertex(coord=c)
c[0] = 99
self.assertEqual(v.coord[0], 1)
self.assertFalse(np.shares_memory(v.coord, c))

def test_the_lists_are_not_shared_between_vertices(self):
a, b = Vertex(), Vertex()
self.assertIsNot(a._edgelist, b._edgelist)
Expand Down
Loading