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

Update InVEST to build against numpy 2 #1642

Merged
merged 33 commits into from
Oct 16, 2024

Conversation

phargogh
Copy link
Member

@phargogh phargogh commented Oct 8, 2024

This PR updates InVEST to build against numpy 2.

Fixes #1641

Checklist

  • Updated HISTORY.rst and link to any relevant issue (if these changes are user-facing)
    - [ ] Updated the user's guide (if needed)
    - [ ] Tested the Workbench UI (if relevant)

@phargogh phargogh self-assigned this Oct 8, 2024
@phargogh
Copy link
Member Author

phargogh commented Oct 8, 2024

There appears to be some overlap here with #1631 ... guess I need to be more careful with my branches!

An RST error in a footnote in Wave Energy was breaking the binary builds
in the main InVEST repository.  This is now addressed. RE:natcap#1641
The numpy2 update was producing slightly different numerical outputs,
but also we weren't asserting that they were numerically close, just
that they had the same number of digits after the decimal ... which
wasn't working well because most of these values were large.

RE:natcap#1641
This is part of the numpy2 migration. RE:natcap#1641
@phargogh phargogh marked this pull request as ready for review October 16, 2024 01:42
@dcdenu4 dcdenu4 self-requested a review October 16, 2024 12:26
Copy link
Member

@dcdenu4 dcdenu4 left a comment

Choose a reason for hiding this comment

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

Thanks @phargogh ! I had one small question around sample / test data in the Makefile.

Makefile Outdated

$(GIT_TEST_DATA_REPO_PATH): | $(DATA_DIR)
-git clone $(GIT_TEST_DATA_REPO) $(GIT_TEST_DATA_REPO_PATH)
git -C $(GIT_TEST_DATA_REPO_PATH) fetch
git -C $(GIT_TEST_DATA_REPO_PATH) lfs install
git -C $(GIT_TEST_DATA_REPO_PATH) checkout $(GIT_TEST_DATA_REPO_REV)
git -C $(GIT_TEST_DATA_REPO_PATH) lfs fetch
git -C $(GIT_TEST_DATA_REPO_PATH) lfs checkout
git -C $(GIT_TEST_DATA_REPO_PATH) lfs checkout $(GIT_TEST_DATA_REPO_REV)
Copy link
Member

Choose a reason for hiding this comment

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

Is that last checkout step needed? We've done the lfs fetch and should already be in $(GIT_TEST_DATA_REPO_REV) right?

Copy link
Member Author

@phargogh phargogh Oct 16, 2024

Choose a reason for hiding this comment

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

Yep, it should be fine without this change! Removed in 89d236b

pyproject.toml Outdated
@@ -46,7 +46,8 @@ invest = "natcap.invest.cli:main"
# available at runtime.
requires = [
'setuptools>=61', 'wheel', 'setuptools_scm>=8.0', 'cython>=3.0.0', 'babel',
'oldest-supported-numpy'
'oldest-supported-numpy; python_version<="3.8"',
'numpy>=2; python_version>"3.8"', # numpy 2 only available for 3.9+
Copy link
Member

Choose a reason for hiding this comment

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

Awesome, and >3.8 wouldn't grab 3.8.6 or something I'm assuming.

Copy link
Member Author

@phargogh phargogh Oct 16, 2024

Choose a reason for hiding this comment

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

It shouldn't (at least as far as I know), but I've clarified this anyways in 75107e7

@@ -1238,7 +1238,7 @@ def cn_op(lulc_array, soil_group_array):
# if lulc_array value not in lulc_to_soil[soil_group_id]['lulc_values']
# then numpy.digitize will not bin properly and cause an IndexError
# during the reshaping call
lulc_unique = set(numpy.unique(lulc_array))
lulc_unique = set(i.item() for i in numpy.unique(lulc_array))
Copy link
Member

Choose a reason for hiding this comment

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

I tried to look this up quickly, but I'm not sure I follow calling item() here...

Copy link
Member

Choose a reason for hiding this comment

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

Ah, unless we're concerned with datatypes, I bet that's it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that's it exactly! i would normally be a native numpy object (e.g. numpy.float32), so calling .item() gets the python representation of the object.

@@ -936,7 +936,8 @@ def validate(args, limit_to=None):
nan_mask = cn_df.isna()
if nan_mask.any(axis=None):
nan_lucodes = nan_mask[nan_mask.any(axis=1)].index
lucode_list = list(nan_lucodes.values)
# Convert numpy dtype values to native python types
Copy link
Member

Choose a reason for hiding this comment

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

Yep!

@phargogh phargogh requested a review from dcdenu4 October 16, 2024 16:14
Copy link
Member

@dcdenu4 dcdenu4 left a comment

Choose a reason for hiding this comment

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

This looks good! Thanks!

@dcdenu4 dcdenu4 merged commit 62c808b into natcap:main Oct 16, 2024
29 checks passed
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.

Update InVEST for numpy2
2 participants