Skip to content

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

Closed

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

Activity

  1. added
    bug (unverified)
    on May 2, 2023
  2. Saransh-cpp commented on May 2, 2023

    @Saransh-cpp
    Member

    Thanks for pointing this out! I think this should be the intended behavior given that numpy works with structured arrays in a similar way -

    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, VectorNumpy classes internally call numpy.array and pass a view of it to the users; hence, the behavior stays intact -

    class VectorNumpy2D(VectorNumpy, Planar, Vector2D, FloatArray): # type: ignore[misc]
    """
    Two dimensional vector class for the NumPy backend. This class can be directly
    used to construct two dimensional NumPy vectors. For two dimensional Momentum
    NumPy vectors see :class:`vector.backends.numpy.MomentumNumpy2D`.
    Examples:
    >>> import vector
    >>> vec = vector.VectorNumpy2D([(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)])
    >>> vec
    VectorNumpy2D([(1.1, 2.1), (1.2, 2.2), (1.3, 2.3), (1.4, 2.4), (1.5, 2.5)],
    dtype=[('x', '<f8'), ('y', '<f8')])
    """
    ObjectClass = vector.backends.object.VectorObject2D
    _IS_MOMENTUM = False
    _azimuthal_type: type[AzimuthalNumpyXY] | type[AzimuthalNumpyRhoPhi]
    def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> VectorNumpy2D:
    """Returns the object of ``VectorNumpy2D``. Behaves as ``__init__`` in this case."""
    if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], dict):
    array = _array_from_columns(args[0])
    else:
    array = numpy.array(*args, **kwargs)
    return array.view(cls)

  3. locked and limited conversation to collaborators on May 2, 2023
  4. converted this issue into a discussion #342 on May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug (unverified)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions