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

Change to nbhood plugin to satisfy Numpy warning and future Numpy error. #1992

Merged
merged 2 commits into from
May 9, 2024

Conversation

Kat-90
Copy link
Contributor

@Kat-90 Kat-90 commented May 1, 2024

Description:

During the investigation work into IMPROVER wind gusts I noticed an error in our nbhood plugin when using a more up to date environment than we use for production:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

It tripped me up a bit trying to work out what the problem was.
This error is a VisibleDeprecationWarning in our current environment which explains things better:

/improver/nbhood/nbhood.py:102: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  open_grid = np.array(np.ogrid[[slice(-x, x + 1) for x in (ranges, ranges)]])

To avoid tripping up on this again in the future this PR suggests we implement the change it requires now.

Testing:

  • Ran tests and they passed OK

@Kat-90 Kat-90 self-assigned this May 1, 2024
@Kat-90 Kat-90 removed their assignment May 1, 2024
@bayliffe bayliffe self-assigned this May 9, 2024
open_grid = np.array(np.ogrid[[slice(-x, x + 1) for x in (ranges, ranges)]])
open_grid = np.array(
np.ogrid[[slice(-x, x + 1) for x in (ranges, ranges)]], dtype=object
)
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks fine, thanks for the change. I find the original syntax hard to understand. It is equivalent to:

np.array(np.ogrid[-ranges:ranges+1, -ranges:ranges+1], dtype=object)

which might have been nicer, but we don't need to change it here.

@bayliffe bayliffe merged commit c949419 into metoppv:master May 9, 2024
8 checks passed
@bayliffe bayliffe assigned Kat-90 and unassigned bayliffe May 9, 2024
MoseleyS pushed a commit to MoseleyS/improver that referenced this pull request Aug 22, 2024
…or. (metoppv#1992)

* Change to nbhood plugin to satisfy Numpy warning and future Numpy error.

* Ran style checks.

---------

Co-authored-by: Katharine Hurst <katharine.hurst@metoffice.gov.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants