mdbartos / pysheds

:earth_americas: Simple and fast watershed delineation in python.
GNU General Public License v3.0
702 stars 188 forks source link

Correctly initialize output and handle nodata cases. #236

Closed groutr closed 8 months ago

groutr commented 8 months ago

A continuation of #235 that extends the same fix to the other routines of _sgrid.py.

mdbartos commented 8 months ago

Great, let me know when this is ready to review and pull in.

groutr commented 8 months ago

After looking at the code some more, I realized for some functions, it would be easy to extend the computation to the borders with the appropriate bounds checking.

An additional minor improvement in this PR is to call the scalar versions of sqrt, abs, atan2, and hypot. Using the scalar functions on scalar inputs is much faster than the equivalent numpy versions. For example:

In [7]: %timeit np.abs(-5.32)
678 ns ± 6.14 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [8]: %timeit abs(-5.32)
26.3 ns ± 0.383 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
groutr commented 8 months ago

I believe this PR is ready for review.

mdbartos commented 8 months ago

LGTM