MCPcopy
hub / github.com/stumpy-dev/stumpy / prescraamp

Function prescraamp

tests/naive.py:1975–2046  ·  view source on GitHub ↗
(T_A, m, T_B, s, exclusion_zone=None, p=2.0, k=1)

Source from the content-addressed store, hash-verified

1973
1974
1975def prescraamp(T_A, m, T_B, s, exclusion_zone=None, p=2.0, k=1):
1976 distance_matrix = aamp_distance_matrix(T_A, T_B, m, p)
1977
1978 l = T_A.shape[0] - m + 1 # matrix profile length
1979 w = T_B.shape[0] - m + 1 # distance profile length
1980
1981 P = np.full((l, k), np.inf, dtype=np.float64)
1982 I = np.full((l, k), -1, dtype=np.int64)
1983
1984 for i in np.random.permutation(range(0, l, s)):
1985 distance_profile = distance_matrix[i]
1986 if exclusion_zone is not None:
1987 apply_exclusion_zone(distance_profile, i, exclusion_zone, np.inf)
1988
1989 nn_idx = np.argmin(distance_profile)
1990 if distance_profile[nn_idx] < P[i, -1] and nn_idx not in I[i]:
1991 pos = np.searchsorted(P[i], distance_profile[nn_idx], side="right")
1992 P[i] = np.insert(P[i], pos, distance_profile[nn_idx])[:-1]
1993 I[i] = np.insert(I[i], pos, nn_idx)[:-1]
1994
1995 if P[i, 0] == np.inf:
1996 I[i, 0] = -1
1997 continue
1998
1999 j = nn_idx
2000 for g in range(1, min(s, l - i, w - j)):
2001 d = distance_matrix[i + g, j + g]
2002 # Do NOT optimize the `condition` in the following if statement
2003 # and similar ones in this naive function. This is to ensure
2004 # we are avoiding duplicates in each row of I.
2005 if d < P[i + g, -1] and (j + g) not in I[i + g]:
2006 pos = np.searchsorted(P[i + g], d, side="right")
2007 P[i + g] = np.insert(P[i + g], pos, d)[:-1]
2008 I[i + g] = np.insert(I[i + g], pos, j + g)[:-1]
2009 if (
2010 exclusion_zone is not None
2011 and d < P[j + g, -1]
2012 and (i + g) not in I[j + g]
2013 ):
2014 pos = np.searchsorted(P[j + g], d, side="right")
2015 P[j + g] = np.insert(P[j + g], pos, d)[:-1]
2016 I[j + g] = np.insert(I[j + g], pos, i + g)[:-1]
2017
2018 for g in range(1, min(s, i + 1, j + 1)):
2019 d = distance_matrix[i - g, j - g]
2020 if d < P[i - g, -1] and (j - g) not in I[i - g]:
2021 pos = np.searchsorted(P[i - g], d, side="right")
2022 P[i - g] = np.insert(P[i - g], pos, d)[:-1]
2023 I[i - g] = np.insert(I[i - g], pos, j - g)[:-1]
2024 if (
2025 exclusion_zone is not None
2026 and d < P[j - g, -1]
2027 and (i - g) not in I[j - g]
2028 ):
2029 pos = np.searchsorted(P[j - g], d, side="right")
2030 P[j - g] = np.insert(P[j - g], pos, d)[:-1]
2031 I[j - g] = np.insert(I[j - g], pos, i - g)[:-1]
2032

Callers

nothing calls this directly

Calls 2

aamp_distance_matrixFunction · 0.85
apply_exclusion_zoneFunction · 0.70

Tested by

no test coverage detected