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

Fixed #966 Add Named Attributes to Matrix Profile #972

Merged
merged 17 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 stumpy/aamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,4 @@ def aamp(T_A, m, T_B=None, ignore_trivial=True, p=2.0, k=1):

core._check_P(out[:, 0])

return mparray(out, m=m, k=k)
return mparray(out, m, k, config.STUMPY_EXCL_ZONE_DENOM)
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion stumpy/aamped.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,4 @@ def aamped(client, T_A, m, T_B=None, ignore_trivial=True, p=2.0, k=1):

core._check_P(out[:, 0])

return mparray(out, m=m, k=k)
return mparray(out, m, k, config.STUMPY_EXCL_ZONE_DENOM)
2 changes: 1 addition & 1 deletion stumpy/gpu_aamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,4 @@ def gpu_aamp(T_A, m, T_B=None, ignore_trivial=True, device_id=0, p=2.0, k=1):

core._check_P(out[:, 0])

return mparray(out, m=m, k=k)
return mparray(out, m, k, config.STUMPY_EXCL_ZONE_DENOM)
2 changes: 1 addition & 1 deletion stumpy/gpu_stump.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,4 @@ def gpu_stump(

core._check_P(out[:, 0])

return mparray(out, m=m, k=k)
return mparray(out, m, k, config.STUMPY_EXCL_ZONE_DENOM)
21 changes: 13 additions & 8 deletions stumpy/mparray.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class mparray(np.ndarray):
The base class

input_array : ndarray
The input `numpy` array to be sublcassed
The input `numpy` array to be subclassed

m : int
Window size
Expand All @@ -34,14 +34,14 @@ class mparray(np.ndarray):
indices is returned. When `k > 1`, the output has exactly `k` columns
consisting of the top-k matrix profile indices.

left_P_ : numpy.ndarray
The left (top-1) matrix profile for `T`

left_I_ : numpy.ndarray
The updated left (top-1) matrix profile indices for `T`
The left (top-1) matrix profile indicesfor `T`
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved

right_I_ : numpy.ndarray
The right (top-1) matrix profile indices for `T`
"""

def __new__(cls, input_array, m, k):
def __new__(cls, input_array, m, k, excl_zone_denom):
"""
Create the ndarray instance of our type, given the usual
ndarray input arguments. This will call the standard
Expand All @@ -54,19 +54,23 @@ def __new__(cls, input_array, m, k):
The base class

input_array : ndarray
The input `numpy` array to be sublcassed
The input `numpy` array to be subclassed

m : int
Window size

k : int
The number of top `k` smallest distances used to construct the
matrix profile.
NimaSarajpoor marked this conversation as resolved.
Show resolved Hide resolved

excl_zone_denom : int
The denominator used in computing the exclusion zone
"""
obj = np.asarray(input_array).view(cls)
obj._m = m
obj._k = k
# All new attributes will also needed to be added to the `__array_finalize__`
obj._excl_zone_denom = excl_zone_denom
# All new attributes will also need to be added to the `__array_finalize__`
# function below so that "new-from-template" objects (e.g., an array slice)
# will also contain the same new attributes
return obj
Expand All @@ -86,6 +90,7 @@ def __array_finalize__(self, obj):
# of an `mparray` will also inherit the attributes from the parent `mparray`
self._m = getattr(obj, "_m", None)
self._k = getattr(obj, "_k", None)
self._excl_zone_denom = getattr(obj, "_excl_zone_denom", None)

def _P(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion stumpy/stump.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,4 @@ def stump(

core._check_P(out[:, 0])

return mparray(out, m=m, k=k)
return mparray(out, m, k, config.STUMPY_EXCL_ZONE_DENOM)
2 changes: 1 addition & 1 deletion stumpy/stumped.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,4 @@ def stumped(

core._check_P(out[:, 0])

return mparray(out, m=m, k=k)
return mparray(out, m, k, config.STUMPY_EXCL_ZONE_DENOM)
8 changes: 5 additions & 3 deletions tests/test_mparray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd
import pytest

from stumpy import aamp, stump
from stumpy import aamp, config, stump
from stumpy.mparray import mparray

test_data = [
Expand All @@ -27,13 +27,15 @@ def test_mparray_init(T_A, T_B):
m = 3
k = 2
arr = stump(T_B, m, ignore_trivial=True, k=k)
mp = mparray(arr, m, k)
mp = mparray(arr, m, k, config.STUMPY_EXCL_ZONE_DENOM)
assert mp._m == m
assert mp._k == k
assert mp._excl_zone_denom == config.STUMPY_EXCL_ZONE_DENOM

slice_mp = mp[1:, :] # Initialize "new-from-template"
assert slice_mp._m == m
assert slice_mp._k == k
assert mp._excl_zone_denom == config.STUMPY_EXCL_ZONE_DENOM


@pytest.mark.parametrize("T_A, T_B", test_data)
Expand Down Expand Up @@ -77,7 +79,7 @@ def test_mparray_self_join(T_A, T_B, k):

@pytest.mark.parametrize("T_A, T_B", test_data)
@pytest.mark.parametrize("k", kNN)
def test_mparrau_A_B_join(T_A, T_B, k):
def test_mparray_A_B_join(T_A, T_B, k):
m = 3
ref_mp = naive.stump(T_A, m, T_B=T_B, k=k)
comp_mp = stump(T_A, m, T_B, ignore_trivial=False, k=k)
Expand Down
Loading