Skip to content
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

Numpy2 fixes #959

Merged
merged 5 commits into from
Jul 11, 2024
Merged

Numpy2 fixes #959

merged 5 commits into from
Jul 11, 2024

Conversation

newville
Copy link
Member

Description

This fixes a few problems with NumPy 2.

It fixes #958 by explicitly removing a few odd non-serializable functions if using NumPy2 and a version of asteval that still includes those. That is, this is really a problem with asteval exporting numpy functions that it shouldn't (and are deprecated and not really needed anyway). But, until that is fixed in asteval (in progress), and that version of asteval is required, it is probably safest to look for and remove those offending functions,

This adds packaging as a dependency in order to compare versions.

This also updates the "latest" variations of the tests to use "numpy>=2.0"

This does not alter the test labeled "development version", but that might be slightly dated.

Type of Changes
  • Bug fix
  • New feature
  • Refactoring / maintenance
  • Documentation / examples
Tested on

Python: 3.12.4 | packaged by conda-forge | (main, Jun 17 2024, 10:11:10) [Clang 16.0.6 ]

lmfit: 1.3.1.post1+geee021f6.d20240629, scipy: 1.14.0, numpy: 2.0.0, asteval: 0.9.33, uncertainties: 3.2.1

Verification

Have you

  • included docstrings that follow PEP 257?
  • referenced existing Issue and/or provided relevant link to mailing list?
  • verified that existing tests pass locally?
  • verified that the documentation builds locally?
  • squashed/minimized your commits and written descriptive commit messages?
  • added or updated existing tests to cover the changes?
  • updated the documentation and/or added an entry to the release notes (doc/whatsnew.rst)?
  • added an example?

Copy link

codecov bot commented Jun 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.20%. Comparing base (7710da6) to head (0f23b10).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #959      +/-   ##
==========================================
- Coverage   93.20%   93.20%   -0.01%     
==========================================
  Files          10       10              
  Lines        3767     3765       -2     
==========================================
- Hits         3511     3509       -2     
  Misses        256      256              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@reneeotten reneeotten left a comment

Choose a reason for hiding this comment

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

@newville the proposed changes look fine but it seems to me there is some redundant stuff going on here that I would rather not add if not absolutely required. I am happy to clean up the commits and changes a bit if you agree.

@@ -5,6 +5,8 @@

from asteval import Interpreter, get_ast_names, valid_symbol_name
from numpy import arcsin, array, cos, inf, isclose, sin, sqrt
from numpy import version as npvers
from packaging.version import Version
Copy link
Contributor

@reneeotten reneeotten Jun 30, 2024

Choose a reason for hiding this comment

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

I would rather not add a dependency for such a simple version check. In a few places we just compare the version for SciPy just using: int(scipy_version.split('.')[1]) >= 11: any reason we can't do that here, especially since it's a short-term need anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

@reneeotten Yes, I came to the same conclusion, and was just making a similar change too (npversion.version.startswith('2'))

@@ -95,7 +109,8 @@ def update(self, other):
if not isinstance(other, Parameters):
raise ValueError(f"'{other}' is not a Parameters object")
self.add_many(*other.values())
for sym in other._asteval.user_defined_symbols():
usersyms = clean_np2_symbols(other._asteval.user_defined_symbols())
for sym in usersyms:
Copy link
Contributor

Choose a reason for hiding this comment

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

This is fine, but I would prefer to leave the original in commented form, with a FIXME note to revert to it as soon as asteval is updated. Unless you will remember which changes to undo as I might not without looking back to the commit history ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

will do.

@@ -183,7 +183,7 @@ stages:
versionSpec: '$(python.version)'
displayName: 'Use Python $(python.version)'
- script: |
python -m pip install --upgrade build pip setuptools wheel
python -m pip install --upgrade build pip setuptools wheel "numpy>=2.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the need for the specification of numpy here, doing an installation with pip will always install the latest version so specifying just numpy should suffice for every use-case (i.e., for Python 3.8 the latest pre-2.0 version and for all others the latest available 2.0.x version

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, we can revert, I just wanted to be specific. I don't think we need to worry about testing with both numpy 1.something and 2.something. I was burned recently by my own misconfiguration of testing, and wanted "numpy 2, dammit" just to make sure it would pass... but we can revert this file.

@reneeotten
Copy link
Contributor

so with the new release of asteval, we can just drop almost all of this and require version 1.0.0 instead?

@newville
Copy link
Member Author

newville commented Jul 5, 2024

@reneeotten We could do that. I had been thinking that we should wait for a version or so before requiring "most recent asteval", just in case some packaging systems are not as aggressive at updating packages. But I could be persuaded.

We could merge this, release a Numpy2-compatible version, and then immediately raise an Issue to clean this up ;)

I'll do whichever of those you prefer.
a) pin to asteval >= 1.0 and remove most of the changes here
b) keep asteval >= 0.9.28, merge this, and give 6 to 9 months before requiring asteval > 1.

FWIW, I decided to tag "asteval 1.0" after years of being stable and then adding in more flexibility for namespaces a year ago. That release "worked" in the sense that within days I was contacted by a group at one of those well-known tech companies that had found some security flaws. I hope to clear those up and push 'asteval 1.0.1' in a week or two.

That's mostly unrelated, but maybe an additional reason to keep lmfit tied to "very recent asteval".

@reneeotten
Copy link
Contributor

In my opinion we should just require the latest "asteval" and remove all the changes here that are then bot required anymore. Downstream packagers can easily update asteval to the latest version, it has no dependencies and build very quickly.

@newville
Copy link
Member Author

newville commented Jul 6, 2024

@reneeotten OK, requiring asteval>=1.0 does make this all a lot simpler. I would suggest Squash-and-Merge with a modified message, or I could close this and open a new PR with a single commit.

@reneeotten
Copy link
Contributor

okay, let me take a look later today and I'll squash-merge or do some git voodoo to clean it up.

pyproject.toml Outdated Show resolved Hide resolved
gentoo-bot pushed a commit to gentoo/gentoo that referenced this pull request Jul 8, 2024
Pull-Request: lmfit/lmfit-py#959
Signed-off-by: Michał Górny <mgorny@gentoo.org>
@reneeotten
Copy link
Contributor

@newville I clean-up the commits a bit, added the dependency version changes to the documentation and CI, and folded in two more small changes for the latest SciPy/NumPy versions and pre-commit hooks.

@newville
Copy link
Member Author

@reneeotten Thanks for cleaning that up and for the maintenance fixes too. I'll merge (not squash) this.
I think this warrants releasing version 1.3.2. I'll try to get to that later today or within a few days.

@newville newville merged commit b72cfb2 into master Jul 11, 2024
14 checks passed
@newville
Copy link
Member Author

@reneeotten ... except that I am also going to try to fix #960 before 1.3.2 -- but that should be too much of a delay.

@newville newville deleted the numpy2_fixes branch July 13, 2024 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Numpy 2 support
3 participants