Skip to content

The difference between scipy.spatial.cKDTree and pygeos.STRtree #889

@lyingTree

Description

@lyingTree

In the opendrift/readers/basereader/unstructured.py ,I found the method build_ckdtree and nearest_ckdtree take a long time when running the openoil, so I made the following changes in the method which find the nearest point:

# ----------------------------unstructured.py----------------------------
# def _build_ckdtree_(self, x, y):
#     from scipy.spatial import cKDTree
#     P = np.vstack((x, y)).T
#     return cKDTree(P)
# Replace _build_ckdtree_ with _build_strtree_
def _build_strtree_(self, x, y):
    from pygeos import STRtree, points
    return STRtree(points(x,y))

#def __nearest_ckdtree__(self, idx, x, y):
#    q = np.vstack((x, y)).T
#    return idx.query(q, k = 1, workers = self.PARALLEL_WORKERS)[1]
# Replace __nearest_ckdtree__ with __nearest_strtree__
def __nearest_strtree__(self, idx, x, y):
    from pygeos import points
    return idx.nearest(pygeos.points(x,y))[1]

# line 180
# return self.__nearest_ckdtree__(self.nodes_idx, x, y)
return self.__nearest_strtree__(self.nodes_idx, x, y)

# line 186
# return self.__nearest_ckdtree__(self.nodes_idx, x, y)
return self.__nearest_strtree__(self.nodes_idx, x, y)
# ----------------------------reader_netCDF_CF_unstructured.py----------------------------
# line 173
# self.nodes_idx = self._build_ckdtree_(self.x, self.y)
self.nodes_idx = self._build_strtree_(self.x, self.y)

# line 176
# self.nodes_idx = self._build_ckdtree_(self.x, self.y)
self.faces_idx = self._build_strtree_(self.x, self.y)

Here I use the current data from FVCOM and the constant wind to drive openoil, so I only modified the reader_netCDF_CF_unstructured.py and unstructured.py, but the graph of tracks is different from before the modification. I wonder if this modification is correct? If it's correct, STRtree may be a better choice than cKDTree, beacauce it can increase the operating speed of opendrift by more than 10%.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions