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

[Fix] version sklearn valid_metrics #861

Merged
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
10 changes: 8 additions & 2 deletions neurokit2/complexity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import sklearn.metrics
import sklearn.neighbors
from packaging import version

from .utils_complexity_embedding import complexity_embedding

Expand Down Expand Up @@ -109,11 +110,16 @@ def _get_count(
# Get neighbors count
# -------------------
# Sanity checks
if distance not in sklearn.neighbors.KDTree.valid_metrics + ["range"]:
sklearn_version = version.parse(sklearn.__version__)
if sklearn_version >= version.parse("1.3.0"):
valid_metrics = sklearn.neighbors.KDTree.valid_metrics() + ["range"]
else:
valid_metrics = sklearn.neighbors.KDTree.valid_metrics + ["range"]
if distance not in valid_metrics:
raise ValueError(
"The given metric (%s) is not valid."
"The valid metric names are: %s"
% (distance, sklearn.neighbors.KDTree.valid_metrics + ["range"])
% (distance, valid_metrics)
)

if fuzzy is True:
Expand Down
13 changes: 9 additions & 4 deletions tests/tests_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import numpy as np
import pandas as pd
from pyentrp import entropy as pyentrp
from sklearn.neighbors import KDTree
import sklearn.neighbors
from packaging import version

# import EntropyHub
import neurokit2 as nk
Expand Down Expand Up @@ -340,7 +341,11 @@ def entropy_embed(x, order=3, delay=1):


def entropy_app_samp_entropy(x, order, metric="chebyshev", approximate=True):
_all_metrics = KDTree.valid_metrics
sklearn_version = version.parse(sklearn.__version__)
if sklearn_version >= version.parse("1.3.0"):
_all_metrics = sklearn.neighbors.KDTree.valid_metrics()
else:
_all_metrics = sklearn.neighbors.KDTree.valid_metrics
if metric not in _all_metrics:
raise ValueError(
"The given metric (%s) is not valid. The valid " # pylint: disable=consider-using-f-string
Expand All @@ -356,14 +361,14 @@ def entropy_app_samp_entropy(x, order, metric="chebyshev", approximate=True):
else:
emb_data1 = _emb_data1[:-1]
count1 = (
KDTree(emb_data1, metric=metric)
sklearn.neighbors.KDTree(emb_data1, metric=metric)
.query_radius(emb_data1, r, count_only=True)
.astype(np.float64)
)
# compute phi(order + 1, r)
emb_data2 = entropy_embed(x, order + 1, 1)
count2 = (
KDTree(emb_data2, metric=metric)
sklearn.neighbors.KDTree(emb_data2, metric=metric)
.query_radius(emb_data2, r, count_only=True)
.astype(np.float64)
)
Expand Down