Skip to content

Commit

Permalink
Faster TPI computation (#5)
Browse files Browse the repository at this point in the history
* use dask map_overlap to spread computation over multiple threads

* add dask dependency
  • Loading branch information
frazane authored Dec 15, 2022
1 parent 5231be7 commit be14a56
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
requirements = [
"netcdf4",
"numba",
"dask",
"numpy",
"pandas",
"scipy",
Expand Down
11 changes: 7 additions & 4 deletions topo_descriptors/topo.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
from numba.np.ufunc import parallel

import numpy as np
import numpy.ma as ma
import xarray as xr
import dask.array as da
from numba import njit, prange
from scipy import ndimage, signal
from multiprocessing import Pool, Value, cpu_count
Expand Down Expand Up @@ -100,9 +100,12 @@ def tpi(dem, size, sigma=None):
if sigma:
dem = ndimage.gaussian_filter(dem, sigma)

conv = signal.convolve(
dem, kernel, mode="same"
) # ndimage.convolve(dem, kernel, mode='reflect')
conv_fn = lambda a: signal.convolve(a, kernel, mode="same")

if isinstance(dem.data, da.Array):
conv = da.map_overlap(conv_fn, dem.data, depth=size * 2, boundary="none")
elif isinstance(dem.data, np.ndarray):
conv = conv_fn(dem.values)
return dem - conv / np.sum(kernel)


Expand Down

0 comments on commit be14a56

Please sign in to comment.