Repository navigation
FIX: Raise for single row/column grids with nearest shading #32405
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6535,6 +6535,15 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs): | |
| ' X (%d) and/or Y (%d); see help(%s)' % ( | ||
| C.shape, Nx, Ny, funcname)) | ||
| if shading == 'nearest': | ||
| if Nx == 1 or Ny == 1: | ||
| # If a coordinate array has a single cell center, | ||
| # its corresponding cell size cannot be determined | ||
| # from neighboring centers. | ||
| raise TypeError(f"C has shape ({nrows}, {ncols}) but " | ||
| "shading='nearest' requires at least two rows " | ||
| "and two columns. Please supply explicit cell " | ||
| "edges for X and Y and use shading='flat' " | ||
| f"with {funcname}.") | ||
| # grid is specified at the center, so define corners | ||
| # at the midpoints between the grid centers and then use the | ||
| # flat algorithm. | ||
|
|
@@ -6543,26 +6552,21 @@ def _interp_grid(X, require_monotonicity=False): | |
| # correctly, when expanding columns, the monotonicity of | ||
| # X coords needs to be checked. When expanding rows, the | ||
| # monotonicity of Y coords needs to be checked. | ||
| if np.shape(X)[1] > 1: | ||
| dX = np.diff(X, axis=1) * 0.5 | ||
| if (require_monotonicity and | ||
| not (np.all(dX >= 0) or np.all(dX <= 0))): | ||
| _api.warn_external( | ||
| f"The input coordinates to {funcname} are " | ||
| "interpreted as cell centers, but are not " | ||
| "monotonically increasing or decreasing. " | ||
| "This may lead to incorrectly calculated cell " | ||
| "edges, in which case, please supply " | ||
| f"explicit cell edges to {funcname}.") | ||
|
|
||
| hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack | ||
| X = hstack((X[:, [0]] - dX[:, [0]], | ||
| X[:, :-1] + dX, | ||
| X[:, [-1]] + dX[:, [-1]])) | ||
| else: | ||
| # This is just degenerate, but we can't reliably guess | ||
| # a dX if there is just one value. | ||
| X = np.hstack((X, X)) | ||
| dX = np.diff(X, axis=1) * 0.5 | ||
| if (require_monotonicity and | ||
| not (np.all(dX >= 0) or np.all(dX <= 0))): | ||
| _api.warn_external( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not following why this had to change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the new check for single row or single column grids with nearest shading, interp_grid() is guaranteed to handle coordinate arrays with at least two columns (therefore np.shape(X)[1] is always greater than one). So it made sense to me to remove the else branch entirely (which handles arrays with one column) and remove the first conditional which will always be true. |
||
| f"The input coordinates to {funcname} are " | ||
| "interpreted as cell centers, but are not " | ||
| "monotonically increasing or decreasing. " | ||
| "This may lead to incorrectly calculated cell " | ||
| "edges, in which case, please supply " | ||
| f"explicit cell edges to {funcname}.") | ||
|
|
||
| hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack | ||
| X = hstack((X[:, [0]] - dX[:, [0]], | ||
| X[:, :-1] + dX, | ||
| X[:, [-1]] + dX[:, [-1]])) | ||
| return X | ||
|
|
||
| if ncols == Nx: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the error message could be confusing between number of dimensions and the length of the appropriate dimension. I think you need to refer to the size of the data array not X and Y.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that it may be confusing. I pushed a new commit to make the message clearer and refer to the data array.