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

COMPAT: ensure we pass numpy array to cKDtree and combat with sklearn 1.3.0 #107

Merged
merged 3 commits into from
Jul 2, 2023
Merged
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
2 changes: 1 addition & 1 deletion ci/envs/38-minimal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
- libpysal=4.5
- py-opencv=4.6
# tests
- scikit-learn
- scikit-learn==1.2
- shapely
- geopandas
- pytest
Expand Down
17 changes: 13 additions & 4 deletions pointpats/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,20 @@ def build_best_tree(coordinates, metric):
coordinates = numpy.asarray(coordinates)
tree = spatial.cKDTree
try:
import sklearn
from sklearn.neighbors import KDTree, BallTree
from packaging.version import Version

if metric in KDTree.valid_metrics:
if Version(sklearn.__version__) >= Version("1.3.0"):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be

if Version(sklearn.__version__) == Version("1.3.0"):

The change was reverted in upstream after my report.

kdtree_valid_metrics = KDTree.valid_metrics()
balltree_valid_metrics = BallTree.valid_metrics()
else:
kdtree_valid_metrics = KDTree.valid_metrics
balltree_valid_metrics = BallTree.valid_metrics

if metric in kdtree_valid_metrics:
tree = lambda coordinates: KDTree(coordinates, metric=metric)
elif metric in BallTree.valid_metrics:
elif metric in balltree_valid_metrics:
tree = lambda coordinates: BallTree(coordinates, metric=metric)
elif callable(metric):
warnings.warn(
Expand All @@ -323,8 +332,8 @@ def build_best_tree(coordinates, metric):
else:
raise KeyError(
f"Metric {metric} not found in set of available types."
f"BallTree metrics: {BallTree.valid_metrics}, and"
f"scikit KDTree metrics: {KDTree.valid_metrics}."
f"BallTree metrics: {balltree_valid_metrics}, and"
f"scikit KDTree metrics: {kdtree_valid_metrics}."
)
except ModuleNotFoundError as e:
if metric not in ("l2", "euclidean"):
Expand Down
4 changes: 2 additions & 2 deletions pointpats/pointpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ def knn_other(self, other, k=1):
if k < 1:
raise ValueError('k must be at least 1')
try:
nn = self.tree.query(other.points, k=k)
nn = self.tree.query(np.asarray(other.points), k=k)
except:
nn = self.tree.query(other, k=k)
nn = self.tree.query(np.asarray(other), k=k)
return nn[1], nn[0]

def explode(self, mark):
Expand Down