vector.array method has unexpected behavior with lists of lists vs. tuples #342
-
Vector VersionVector 1.0.0 Python VersionPython 3.10.5 OS / EnvironmentI'm operating on MacOS 13.1, but I can confirm this happens on CentOS7 so it's probably platform independent Describe the bugIn the tutorial, there is the following example: # NumPy-like arguments (literally passed through to NumPy)
vector.array(
[(1.1, 2.1), (1.2, 2.2), (1.3, 2.3), (1.4, 2.4), (1.5, 2.5)],
dtype=[("x", float), ("y", float)],
) This seems like it could be a convenient way to convert a numpy array to an array of vector objects. However, this function seems to only play nice if the subitems are tuples and the object is a list. For example, see the following code: >>> import numpy as np
>>> import vector
>>> v = np.array([[1.1, 2.1], [1.2, 2.2], [1.3, 2.3], [1.4, 2.4], [1.5, 2.5]])
>>> vector.array(v, dtype=[('x', float), ('y', float)])
VectorNumpy2D([[(1.1, 1.1), (2.1, 2.1)],
[(1.2, 1.2), (2.2, 2.2)],
[(1.3, 1.3), (2.3, 2.3)],
[(1.4, 1.4), (2.4, 2.4)],
[(1.5, 1.5), (2.5, 2.5)]], dtype=[('x', '<f8'), ('y', '<f8')])
>>> v = np.array([(1.1, 2.1), (1.2, 2.2), (1.3, 2.3), (1.4, 2.4), (1.5, 2.5)])
>>> vector.array(v, dtype=[('x', float), ('y', float)])
VectorNumpy2D([[(1.1, 1.1), (2.1, 2.1)],
[(1.2, 1.2), (2.2, 2.2)],
[(1.3, 1.3), (2.3, 2.3)],
[(1.4, 1.4), (2.4, 2.4)],
[(1.5, 1.5), (2.5, 2.5)]], dtype=[('x', '<f8'), ('y', '<f8')])
>>> vector.array(v.tolist(), dtype=[('x', float), ('y', float)])
VectorNumpy2D([[(1.1, 1.1), (2.1, 2.1)],
[(1.2, 1.2), (2.2, 2.2)],
[(1.3, 1.3), (2.3, 2.3)],
[(1.4, 1.4), (2.4, 2.4)],
[(1.5, 1.5), (2.5, 2.5)]], dtype=[('x', '<f8'), ('y', '<f8')]) In all of these cases, I expect the result from the tutorial example, and I think a reasonable user would also expect that. Maybe there's a preferred way of doing this, I could transpose the numpy array and call it like vector.array({"x": v.T[1], "y": v.T[2], "z": v.T[3], "t": v.T[0]}) but my point is that a user reading the docs (me) would try the first example, it wouldn't fail immediately, but it would give a very wrong result/shape. P.S. I wasn't sure if I should classify this as a bug, since I've strayed from the actual example a bit, so if this is just the way it is and there's not a better way to do this, go ahead and close the issue! Any additional but relevant log outputNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Thanks for pointing this out! I think this should be the intended behavior given that In [1]: v = np.array([[1.1, 2.1], [1.2, 2.2], [1.3, 2.3], [1.4, 2.4], [1.5, 2.5]
...: ], dtype=[('x', float), ('y', float)])
In [2]: v
Out[2]:
array([[(1.1, 1.1), (2.1, 2.1)],
[(1.2, 1.2), (2.2, 2.2)],
[(1.3, 1.3), (2.3, 2.3)],
[(1.4, 1.4), (2.4, 2.4)],
[(1.5, 1.5), (2.5, 2.5)]], dtype=[('x', '<f8'), ('y', '<f8')]) Moreover, vector/src/vector/backends/numpy.py Lines 940 to 965 in 44620fd |
Beta Was this translation helpful? Give feedback.
Thanks for pointing this out! I think this should be the intended behavior given that
numpy
works with structured arrays in a similar way -Moreover,
VectorNumpy
classes internally callnumpy.array
and pass aview
of it to the users; hence, the behavior stays intact -vector/src/vector/backends/numpy.py
Lines 940 to 965 in 44620fd