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

[JOSS Review] Improve Python tests and unpin Numpy version #26

Merged
merged 2 commits into from
Mar 13, 2024

Conversation

santisoler
Copy link
Contributor

@santisoler santisoler commented Mar 8, 2024

Improve Python tests to work with latest Numpy version. Modify how the potential and acceleration are retrieved from the solution returned by evaluate() and GravityEvaluable in test functions so they can run with the latest Numpy versions. The previous tests were trying to generate an array from ragged tuples, which is no longer supported by Numpy. Also, modify the assert np.testing.assert_almost_equal() is None lines since the Numpy testing functions always return None. Unpin Numpy version in environment.yml file.


@schuhmaj I took some liberties here and made a few changes that I think worth taking a look at. I noticed that the Python tests were only running using the pinned version of Numpy you have in the environment.yml file, but not with the latest ones. The reason of this was the lines that try to convert the solutions (a list of tuples) into an array:

sol = np.array([np.array(x) for x in sol])

Each element in sol is a tuple with the three fields, and each one of them has different number of elements:

  • the potential is just a float,
  • the acceleration has three floats,
  • the tensor has six floats.

Numpy v1.23.4 is raising a deprecation warning when trying to convert this ragged lists to an array:

test/python/test_polyhedral_gravity.py::test_polyhedral_gravity[with_file_input]                        
  /home/santi/git/polyhedral-gravity-model/test/python/test_polyhedral_gravity.py:68: VisibleDeprecation
Warning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-o
r ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'd
type=object' when creating the ndarray.                                                                 
    sol = np.array([np.array(x) for x in sol]) 

This means that this line of code won't work with newer Numpy versions (like the latest one, v1.26.4).

The solution I suggest in this PR is to grab the results for the potential and the acceleration for each computation point directly from the sol variable (see lines changed in this PR).

I also unpinned Numpy in the environment.yml file since tests should run using the latest Numpy version.

Moreover, I modified the assert lines. The numpy.testing.assert...() functions always return None, so asserting if their outputs are None is trivial. Instead it's better to just leave these function calls as they are (see the changed lines in this PR).

Let me know what do you think about this, and feel free to ask any question, and suggest or push changes if you desire to.


This PR is part of the JOSS review: openjournals/joss-reviews#6384

Modify how the potential and acceleration are retrieved from the
solution returned by `evaluate()` and `GravityEvaluable` in tests so
the code can run with latest Numpy versions. The previous tests were
trying to generate an array from ragged tuples, which is no longer
supported by Numpy. Also, modify the `assert
np.testing.assert_almost_equal() is None` lines since the Numpy testing
functions always return `None`.
@mikegrudic
Copy link

Yeah this was the first issue I ran into in my testing - definitely a nice fix.

@schuhmaj schuhmaj self-requested a review March 13, 2024 12:17
Copy link
Collaborator

@schuhmaj schuhmaj left a comment

Choose a reason for hiding this comment

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

All good.
And, thanks for resolving and tracking down the numpy issue here, where I previously applied the dirty fix of pinning the version number.

@schuhmaj schuhmaj merged commit fa2dba3 into esa:main Mar 13, 2024
3 checks passed
@santisoler santisoler deleted the fix-python-tests branch March 13, 2024 18:44
@santisoler
Copy link
Contributor Author

No worries, my pleasure! Thanks for accepting my suggestion.

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.

3 participants