scikit-hep / scikit-hep/vector

Inconsistent behavior for item assignment to vector arrays

Open
#457 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug duplicate feature
Dominant language
Python
Stars
99
Forks
42
Avg merge
1d 13h
Merged PRs (30d)
6

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported vector.array assignments with slices, boolean arrays, and a single index in Python 3.11. Compare the three indexing behaviors and add regression coverage for the expected consistent assignment behavior; done means the boolean assignment no longer fails silently and single-index assignment has defined behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.