Skip to content

Handling scalars in tests with NEP 51 numpy >=2 #11521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed

Handling scalars in tests with NEP 51 numpy >=2 #11521

wants to merge 7 commits into from

Conversation

quant12345
Copy link
Contributor

@quant12345 quant12345 commented Aug 19, 2024

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?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • 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".

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Aug 19, 2024
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Aug 19, 2024
@quant12345
Copy link
Contributor Author

quant12345 commented Aug 19, 2024

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.

Copy link
Contributor

@imSanko imSanko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the Reason behind adding Float, int, item in every other code?

@quant12345
Copy link
Contributor Author

@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 quant12345 changed the title Handling scalars in tests with NEP 51 numpy Handling scalars in tests with NEP 51 numpy >=2 Aug 20, 2024
@quant12345 quant12345 closed this Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants