unsupervised.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. __all__ = ["extract_candidates"]
  2. import numpy as np
  3. import stumpy
  4. def extract_candidates(
  5. ear_ts: np.ndarray,
  6. window_length:int=100,
  7. max_matches:int=10
  8. ) -> list[np.ndarray]:
  9. """
  10. Extracts candidate motifs from the given time series using the Matrix Profile algorithm.
  11. Parameters:
  12. - ear_ts (np.ndarray): The input time series.
  13. - window_length (int): The length of the sliding window used for candidates extraction. Default is 100. Should be based on the FPS of the video.
  14. - max_matches (int): The maximum number of candidates to extract. Default is 10.
  15. Returns:
  16. - candidates (list[np.ndarray]): A list of candidate candidates extracted from the time series.
  17. """
  18. # input validation
  19. if not isinstance(ear_ts, np.ndarray):
  20. raise TypeError("ear_ts must be a numpy array")
  21. if ear_ts.ndim != 1:
  22. raise ValueError("ear_ts must be a 1D array")
  23. if not isinstance(window_length, int):
  24. raise TypeError("window_length must be an integer")
  25. if not isinstance(max_matches, int):
  26. raise TypeError("max_matches must be an integer")
  27. mp = stumpy.stump(ear_ts, window_length) # matrix profile
  28. _, candidates_idx = stumpy.motifs(ear_ts, mp[:, 0], max_matches=max_matches)
  29. return [ear_ts[idx:idx+window_length] for idx in (candidates_idx[0] + window_length // 2)]