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

Function prescrump

tests/naive.py:1790–1883  ·  view source on GitHub ↗
(
    T_A,
    m,
    T_B,
    s,
    exclusion_zone=None,
    k=1,
    T_A_subseq_isconstant=None,
    T_B_subseq_isconstant=None,
)

Source from the content-addressed store, hash-verified

1788
1789
1790def prescrump(
1791 T_A,
1792 m,
1793 T_B,
1794 s,
1795 exclusion_zone=None,
1796 k=1,
1797 T_A_subseq_isconstant=None,
1798 T_B_subseq_isconstant=None,
1799):
1800 T_A_subseq_isconstant = rolling_isconstant(T_A, m, T_A_subseq_isconstant)
1801 T_B_subseq_isconstant = rolling_isconstant(T_B, m, T_B_subseq_isconstant)
1802
1803 dist_matrix = distance_matrix(T_A, T_B, m)
1804 dist_matrix[np.isnan(dist_matrix)] = np.inf
1805 for i in range(dist_matrix.shape[0]):
1806 for j in range(dist_matrix.shape[1]):
1807 if np.isfinite(dist_matrix[i, j]):
1808 if T_A_subseq_isconstant[i] and T_B_subseq_isconstant[j]:
1809 dist_matrix[i, j] = 0.0
1810 elif T_A_subseq_isconstant[i] or T_B_subseq_isconstant[j]:
1811 dist_matrix[i, j] = np.sqrt(m)
1812 else: # pragma: no cover
1813 pass
1814
1815 l = T_A.shape[0] - m + 1 # matrix profile length
1816 w = T_B.shape[0] - m + 1 # distance profile length
1817
1818 P = np.full((l, k), np.inf, dtype=np.float64)
1819 I = np.full((l, k), -1, dtype=np.int64)
1820
1821 for i in np.random.permutation(range(0, l, s)):
1822 distance_profile = dist_matrix[i]
1823 if exclusion_zone is not None:
1824 apply_exclusion_zone(distance_profile, i, exclusion_zone, np.inf)
1825
1826 nn_idx = np.argmin(distance_profile)
1827 if distance_profile[nn_idx] < P[i, -1] and nn_idx not in I[i]:
1828 pos = np.searchsorted(P[i], distance_profile[nn_idx], side="right")
1829 P[i] = np.insert(P[i], pos, distance_profile[nn_idx])[:-1]
1830 I[i] = np.insert(I[i], pos, nn_idx)[:-1]
1831
1832 if P[i, 0] == np.inf:
1833 I[i, 0] = -1
1834 continue
1835
1836 j = nn_idx
1837 for g in range(1, min(s, l - i, w - j)):
1838 d = dist_matrix[i + g, j + g]
1839 # Do NOT optimize the `condition` in the following if statement
1840 # and similar ones in this naive function. This is to ensure
1841 # we are avoiding duplicates in each row of I.
1842 if d < P[i + g, -1] and (j + g) not in I[i + g]:
1843 pos = np.searchsorted(P[i + g], d, side="right")
1844 P[i + g] = np.insert(P[i + g], pos, d)[:-1]
1845 I[i + g] = np.insert(I[i + g], pos, j + g)[:-1]
1846 if (
1847 exclusion_zone is not None

Callers

nothing calls this directly

Calls 3

distance_matrixFunction · 0.85
rolling_isconstantFunction · 0.70
apply_exclusion_zoneFunction · 0.70

Tested by

no test coverage detected