Skip to content

vector.array method has unexpected behavior with lists of lists vs. tuples #341

@denehoffman

Description

@denehoffman

Vector Version

Vector 1.0.0

Python Version

Python 3.10.5

OS / Environment

I'm operating on MacOS 13.1, but I can confirm this happens on CentOS7 so it's probably platform independent

Describe the bug

In 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 output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug (unverified)The problem described would be a bug, but needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions