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

add missing minlength to np.bincount #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JonKing93
Copy link
Contributor

@JonKing93 JonKing93 commented Nov 15, 2024

Summary

This merge request fixes a missing minlength argument in two calls to np.bincount

Details

Currently, the distance_to_ridge method will sometimes fail unexpectedly with an IndexError. For example, here's an error trace I receive when attempting to run the method in northern Colorado:

  File "...\pfdf\watershed.py", line 318, in relief   
    relief = grid.distance_to_ridge(
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "...\pysheds\sgrid.py", line 1649, in distance_to_ridge
    rdist = self._d8_distance_to_ridge(fdir=fdir, weights=weights,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\pysheds\sgrid.py", line 1676, in _d8_distance_to_ridge
    startnodes = startnodes[(indegree == 0)]
                 ~~~~~~~~~~^^^^^^^^^^^^^^^^^
IndexError: boolean index did not match indexed array along axis 0; size of axis is 6205794 but size of corresponding boolean axis is 6205763

This issue occurs when there are NoData values along the bottom edge of the flow direction raster. Essentially, the maximum value in the endnodes array is less than the size of the fdir array, and so the subsequent call to np.bincount returns an index array with fewer elements than the startnodes array.

This pull request fixes the bug by adding the minlength option to np.bincount (as is done elsewhere in sgrid.py) to ensure that the index array always has the correct number of elements.

closes #255

@JonKing93
Copy link
Contributor Author

JonKing93 commented Nov 15, 2024

@mdbartos Note that the tests for Python 3.9 - 3.12 are failing due to the use of numpy 2+ (which changed the can_cast API), and not due to this commit. Please see fix #261 for a possible solution.

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.

Indexing Error in _d8_distance_from_ridge
1 participant