Python icon indicating copy to clipboard operation
Python copied to clipboard

Handling scalars in tests with NEP 51 numpy >=2

Open quant12345 opened this issue 1 year ago • 2 comments

Contributes to #11320

There are many test failures where numbers do not match when printed in the new numpy >= 2 (the number is printed with the type). This is about printing scalars in numpy >= 2, here is an explanation in numpy itself. I see three options to fix this:

  1. Either convert numbers, arrays to Python instead of numpy.
  2. Fix the tests themselves: 12.0 by replacing with np.float64(12.0)
  3. You can put np.set_printoptions(legacy="1.25") .

I chose the first option. Where I was not sure what the types would be, I used item. (will return an error if there is more than one element) I tried to make changes to the tests themselves only as a last resort.

Example: pytest electronics/circular_convolution.py

FAILURES:

Expected:
    [10, 10, 6, 14]
Got:
    [np.int64(10), np.int64(10), np.int64(6), np.int64(14)]

All files passed tests locally in numpy 2.1.0 and numpy 1.26.4. Each file was checked separately. Except for the file linear_algebra/src/power_iteration.py in numpy 1.26.4 there is a different number on the fourteenth digit after the period, which I think is not significant.

Update

the build fails on numpy 2.0.0 and 2.1.0: Getting requirements to build wheel did not run successfully.

  • [ ] Add an algorithm?
  • [ ] Fix a bug or typo in an existing algorithm?
  • [x] Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • [ ] Documentation change?

Checklist:

  • [x] I have read CONTRIBUTING.md.
  • [x] This pull request is all my own work -- I have not plagiarized.
  • [x] I know that pull requests will not be merged if they fail the automated tests.
  • [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • [ ] All new Python files are placed inside an existing directory.
  • [ ] All filenames are in all lowercase characters with no spaces or dashes.
  • [ ] All functions and variable names follow Python naming conventions.
  • [ ] All function parameters and return values are annotated with Python type hints.
  • [ ] All functions have doctests that pass the automated testing.
  • [ ] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • [ ] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

quant12345 avatar Aug 19 '24 16:08 quant12345

The tests don't pass, I'll try with the original requirements.txt. I tried to return numpy-1.26.4 to requirements.txt, it works.

quant12345 avatar Aug 19 '24 17:08 quant12345

@imSanko the reason is that in numpy >=2 numbers are printed with a type, which is why tests fail. I converted the numbers and arrays to Python instead of numpy so the tests would pass. Also in tests I saw Python lists, but inside there were numpy numbers. In this case I did this: np.array(arr).tolist(), that is, I converted the array to numpy, and then to a Python list, so as not to use list comprehension to convert each value. I updated the PR description. But build still fails. There is a tangled mess with downgrading versions.

quant12345 avatar Aug 20 '24 07:08 quant12345