Skip to main content
Version: 0.0.98

ts_shape.features.pattern_recognition

Classes
NameDescription
PatternRecognitionPattern Recognition for univariate timeseries.

PatternRecognition​

classsource

Bases: Base

Pattern Recognition for univariate timeseries.

Discover motifs, discords, and template matches using Matrix Profile and Dynamic Time Warping approaches.

Every method reads one signal: rows are put in time order (when the frame has time_column), and a frame with several uuids needs signal_uuid= to pick one. Positions in the results (index_a, start_index, ...) count samples of that time-ordered signal. A window that contains a missing value is skipped.

Methods:

  • discover_motifs: Find top-k recurring subsequence patterns.
  • discover_discords: Find top-k anomalous subsequences.
  • similarity_search: Find subsequences most similar to a query (DTW).
  • template_match: Find all occurrences of a reference template.
  • compute_distance_profile: Distance from query to every subsequence.
Functions
NameDescription
discover_motifsFind the top-k recurring subsequence patterns (motifs).
discover_discordsFind the top-k anomalous subsequences (discords).
similarity_searchFind the top-k most similar subsequences to a query using DTW.
template_matchFind all occurrences of a template pattern in the time series.
compute_distance_profileCompute distance from query to every subsequence of same length.

discover_motifs​

classmethodsource
discover_motifs(
dataframe: pd.DataFrame,
value_column: str = "value_double",
window_size: int = 50,
top_k: int = 5,
exclusion_zone: int | None = None,
time_column: str = "systime",
*,
signal_uuid: str | None = None
) -> pd.DataFrame

Find the top-k recurring subsequence patterns (motifs).

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrColumn containing numeric values.'value_double'
window_sizeintLength of subsequences to compare.50
top_kintNumber of motif pairs to return.5
exclusion_zoneint | NoneIndices to exclude around matches. Defaults to window_size // 2. Also applied between motifs: no member of a returned pair starts within exclusion_zone of a member of a better-ranked pair, so later motifs are not the first one shifted by a sample or two.None
time_columnstrColumn containing timestamps.'systime'
signal_uuidstr | NoneSignal to analyze. Required when the frame holds several uuids (ValueError otherwise); optional for a single-signal frame (formerly uuid).None
Returns
TypeDescription
DataFrameDataFrame with motif_rank, index_a, index_b, distance, time_a, time_b (the time columns only when the frame has time_column; they keep its timezone). Empty, with these columns, when the signal is shorter than window_size.
Raises
TypeDescription
ColumnNotFoundErrorIf value_column is missing.
ValueErrorIf the frame holds several uuids and signal_uuid is not given.

discover_discords​

classmethodsource
discover_discords(
dataframe: pd.DataFrame,
value_column: str = "value_double",
window_size: int = 50,
top_k: int = 5,
exclusion_zone: int | None = None,
time_column: str = "systime",
*,
signal_uuid: str | None = None
) -> pd.DataFrame

Find the top-k anomalous subsequences (discords).

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrColumn containing numeric values.'value_double'
window_sizeintLength of subsequences to compare.50
top_kintNumber of discords to return.5
exclusion_zoneint | NoneIndices to exclude around matches. Defaults to window_size // 2.None
time_columnstrColumn containing timestamps.'systime'
signal_uuidstr | NoneSignal to analyze. Required when the frame holds several uuids (ValueError otherwise); optional for a single-signal frame (formerly uuid).None
Returns
TypeDescription
DataFrameDataFrame with discord_rank, start_index, distance, start_time (the time column only when the frame has time_column). Windows with a missing value are never discords. Empty, with these columns, when the signal is shorter than window_size.
Raises
TypeDescription
ColumnNotFoundErrorIf value_column is missing.
ValueErrorIf the frame holds several uuids and signal_uuid is not given.
classmethodsource
similarity_search(
dataframe: pd.DataFrame,
query: np.ndarray,
value_column: str = "value_double",
top_k: int = 5,
normalize: bool = True,
time_column: str = "systime",
*,
signal_uuid: str | None = None
) -> pd.DataFrame

Find the top-k most similar subsequences to a query using DTW.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
queryndarrayQuery pattern as numpy array.required
value_columnstrColumn containing numeric values.'value_double'
top_kintNumber of matches to return.5
normalizeboolWhether to z-normalize before comparison.True
time_columnstrColumn containing timestamps.'systime'
signal_uuidstr | NoneSignal to analyze. Required when the frame holds several uuids (ValueError otherwise); optional for a single-signal frame (formerly uuid).None
Returns
TypeDescription
DataFrameDataFrame with rank, start_index, dtw_distance, start_time (the time column only when the frame has time_column). Empty, with these columns, when the signal is shorter than the query.
Raises
TypeDescription
ColumnNotFoundErrorIf value_column is missing.
ValueErrorIf the frame holds several uuids and signal_uuid is not given, or query is empty or contains NaN.

template_match​

classmethodsource
template_match(
dataframe: pd.DataFrame,
template: np.ndarray,
value_column: str = "value_double",
threshold: float | None = None,
normalize: bool = True,
time_column: str = "systime",
*,
signal_uuid: str | None = None
) -> pd.DataFrame

Find all occurrences of a template pattern in the time series.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
templatendarrayReference pattern as numpy array.required
value_columnstrColumn containing numeric values.'value_double'
thresholdfloat | NoneMaximum distance to consider a match. None = adaptive (mean minus two standard deviations of the distance profile).None
normalizeboolWhether to z-normalize before comparison. With False the distance is the plain Euclidean distance, so the level and scale of the template matter.True
time_columnstrColumn containing timestamps.'systime'
signal_uuidstr | NoneSignal to analyze when the frame holds several uuids (formerly uuid).None
Returns
TypeDescription
DataFrameDataFrame with start_index, distance, start_time, end_time (the time columns only when the frame has time_column). Empty, with these columns, when nothing matches.
Raises
TypeDescription
ColumnNotFoundErrorIf value_column is missing.
ValueErrorIf the frame holds several uuids and signal_uuid is not given, or template is empty or contains NaN.

compute_distance_profile​

classmethodsource
compute_distance_profile(
dataframe: pd.DataFrame,
query: np.ndarray,
value_column: str = "value_double",
metric: str = "euclidean",
normalize: bool = True,
*,
time_column: str = "systime",
signal_uuid: str | None = None
) -> np.ndarray

Compute distance from query to every subsequence of same length.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
queryndarrayQuery subsequence.required
value_columnstrColumn containing numeric values.'value_double'
metricstr'euclidean' (FFT-based) or 'dtw'.'euclidean'
normalizeboolWhether to z-normalize. With False the Euclidean distance is the plain distance of the raw values.True
time_columnstrRows are put in the order of this column (when the frame has it).'systime'
signal_uuidstr | NoneSignal to analyze when the frame holds several uuids (formerly uuid).None
Returns
TypeDescription
ndarray1D numpy array of distances, one per window start; NaN for a window with a missing value. Empty when the signal is shorter than the query.
Raises
TypeDescription
ColumnNotFoundErrorIf value_column is missing.
ValueErrorFor an unknown metric, several uuids without signal_uuid, or a query that is empty or contains NaN.