scikit-hep / vector

Vector classes and utilities
https://vector.readthedocs.io
BSD 3-Clause "New" or "Revised" License
77 stars 24 forks source link

Inconsistent behavior for item assignment to vector arrays #457

Open simonthor opened 4 months ago

simonthor commented 4 months ago

Vector Version

1.3.1

Python Version

3.11.4

OS / Environment

Kubuntu Linux 22.04. vector is installed using pip in a conda environment.

Describe the bug

When trying to assign certain items in a vector numpy array, the behavior is different depending on how it is done.

> v = vector.array({"E": np.arange(4), "px": np.zeros(4), "py": np.zeros(4), "pz": np.zeros(4)})
> print(v)
[(0., 0., 0., 0) (0., 0., 0., 1) (0., 0., 0., 2) (0., 0., 0., 3)]

Assignment with a slice:

> v[1:2] = vector.array({"E": [8], "px": [0], "py": [0], "pz": [0]})
> print(v) # Changes the item correctly
[(0., 0., 0., 0) (0., 0., 0., 8) (0., 0., 0., 2) (0., 0., 0., 3)]

Assignment with a boolean array:

> v[v.E > 2] = vector.array({"E": np.ones(2), "px": np.ones(2), "py": np.ones(2), "pz": np.ones(2)})
> print(v) # Does nothing and fails silently
[(0., 0., 0., 0) (0., 0., 0., 8) (0., 0., 0., 2) (0., 0., 0., 3)]

Assignment with a single index:

> v[1] = vector.array(dict(px=[0.0], py=[0.0], pz=[0.0], E=[3])) # Crashes
TypeError: 'MomentumObject4D' object does not support item assignment

Any additional but relevant log output

No response

Saransh-cpp commented 4 months ago

I was able to reproduce this locally. Thanks for the report! I'll look into this.

Saransh-cpp commented 3 months ago

Related to #158 - see https://github.com/scikit-hep/vector/issues/158#issuecomment-989901951 for a detailed explanation (working on this at the moment).