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

perf(reader): replace the scipy.spatial.cKDTree with pygeos.STRtree to find the nearest node and element #894

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies:
- coloredlogs
- cmocean
- pip
- pygeos
- pip:
- motuclient
- roaring-landmask
15 changes: 13 additions & 2 deletions opendrift/readers/basereader/unstructured.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,24 @@ def _build_ckdtree_(self, x, y):
P = np.vstack((x, y)).T
return cKDTree(P)

def _build_strtree_(self, x, y):
from pygeos import STRtree, points
return STRtree(points(x, y))

def __nearest_ckdtree__(self, idx, x, y):
"""
Return index of nearest point in cKDTree
"""
q = np.vstack((x, y)).T
return idx.query(q, k = 1, workers = self.PARALLEL_WORKERS)[1]

def __nearest_strtree__(self, idx, x, y):
"""
Return index of nearest point in STRtree
"""
from pygeos import points
return idx.nearest(points(x, y))[1]

@staticmethod
def __nearest_rtree__(idx, x, y):
"""
Expand All @@ -177,10 +188,10 @@ def _nearest_node_(self, x, y):
"""
Return nearest node (id) for x and y
"""
return self.__nearest_ckdtree__(self.nodes_idx, x, y)
return self.__nearest_strtree__(self.nodes_idx, x, y)

def _nearest_face_(self, xc, yc):
"""
Return nearest element or face (id) for xc and yc
"""
return self.__nearest_ckdtree__(self.faces_idx, xc, yc)
return self.__nearest_strtree__(self.faces_idx, xc, yc)
4 changes: 2 additions & 2 deletions opendrift/readers/reader_netCDF_CF_unstructured.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ def __init__(self, filename=None, name=None, proj4=None):

self.timer_start("build index")
logger.debug("building index of nodes..")
self.nodes_idx = self._build_ckdtree_(self.x, self.y)
self.nodes_idx = self._build_strtree_(self.x, self.y)

logger.debug("building index of faces..")
self.faces_idx = self._build_ckdtree_(self.xc, self.yc)
self.faces_idx = self._build_strtree_(self.xc, self.yc)

self.timer_end("build index")

Expand Down
2 changes: 1 addition & 1 deletion opendrift/readers/unstructured/shyfem.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(self, filename=None, name=None):

self.timer_start("build index")
logger.debug("building index of nodes..")
self.nodes_idx = self._build_ckdtree_(self.x, self.y)
self.nodes_idx = self._build_strtree_(self.x, self.y)
self.timer_end("build index")

self.timer_end("open dataset")
Expand Down