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: call KNNMixin._dtw1d when independent=True #251

Merged
merged 1 commit into from
Apr 13, 2024
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 sequentia/models/knn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def _dtwi(self: KNNMixin, A: FloatArray, B: FloatArray) -> float:

def dtw(a: FloatArray, b: FloatArray) -> float:
"""Windowed DTW wrapper function."""
return self._dtw(a, b, window=window)
return self._dtw1d(a, b, window=window)

return np.sum([dtw(A[:, i], B[:, i]) for i in range(A.shape[1])])

Expand Down
20 changes: 17 additions & 3 deletions tests/unit/test_models/knn/test_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,32 @@ def assert_fit(clf: KNNClassifier, /, *, data: SequentialDataset) -> None:

@pytest.mark.parametrize("k", [1, 2, 5])
@pytest.mark.parametrize("weighting", [None, lambda x: np.exp(-x)])
@pytest.mark.parametrize("independent", [False, True])
def test_classifier_e2e(
helpers: t.Any,
request: SubRequest,
k: int,
weighting: t.Callable | None,
dataset: SequentialDataset,
random_state: np.random.RandomState,
*,
k: int,
weighting: t.Callable | None,
independent: bool,
) -> None:
clf = KNNClassifier(k=k, weighting=weighting, random_state=random_state)
clf = KNNClassifier(
k=k,
weighting=weighting,
independent=independent,
random_state=random_state,
)

assert clf.k == k
assert clf.weighting == weighting
assert clf.independent == independent

if independent:
assert clf._dtw().__name__ == "_dtwi"
else:
assert clf._dtw().__name__ == "_dtwd"

data = dataset.copy()
data._X = data._X[:, :1] # only use one feature
Expand Down