ts_shape.features.pattern_recognition
| Name | Description |
|---|---|
PatternRecognition | Pattern Recognition for univariate timeseries. |
PatternRecognition
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.
| Name | Description |
|---|---|
discover_motifs | Find the top-k recurring subsequence patterns (motifs). |
discover_discords | Find the top-k anomalous subsequences (discords). |
similarity_search | Find the top-k most similar subsequences to a query using DTW. |
template_match | Find all occurrences of a template pattern in the time series. |
compute_distance_profile | Compute distance from query to every subsequence of same length. |
discover_motifs
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).
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | Input DataFrame. | required |
value_column | str | Column containing numeric values. | 'value_double' |
window_size | int | Length of subsequences to compare. | 50 |
top_k | int | Number of motif pairs to return. | 5 |
exclusion_zone | int | None | Indices 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_column | str | Column containing timestamps. | 'systime' |
signal_uuid | str | None | Signal to analyze. Required when the frame holds several uuids (ValueError otherwise); optional for a single-signal frame (formerly uuid). | None |
| Type | Description |
|---|---|
DataFrame | DataFrame 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. |
| Type | Description |
|---|---|
ColumnNotFoundError | If value_column is missing. |
ValueError | If the frame holds several uuids and signal_uuid is not given. |
discover_discords
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).
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | Input DataFrame. | required |
value_column | str | Column containing numeric values. | 'value_double' |
window_size | int | Length of subsequences to compare. | 50 |
top_k | int | Number of discords to return. | 5 |
exclusion_zone | int | None | Indices to exclude around matches. Defaults to window_size // 2. | None |
time_column | str | Column containing timestamps. | 'systime' |
signal_uuid | str | None | Signal to analyze. Required when the frame holds several uuids (ValueError otherwise); optional for a single-signal frame (formerly uuid). | None |
| Type | Description |
|---|---|
DataFrame | DataFrame 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. |
| Type | Description |
|---|---|
ColumnNotFoundError | If value_column is missing. |
ValueError | If the frame holds several uuids and signal_uuid is not given. |
similarity_search
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.
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | Input DataFrame. | required |
query | ndarray | Query pattern as numpy array. | required |
value_column | str | Column containing numeric values. | 'value_double' |
top_k | int | Number of matches to return. | 5 |
normalize | bool | Whether to z-normalize before comparison. | True |
time_column | str | Column containing timestamps. | 'systime' |
signal_uuid | str | None | Signal to analyze. Required when the frame holds several uuids (ValueError otherwise); optional for a single-signal frame (formerly uuid). | None |
| Type | Description |
|---|---|
DataFrame | DataFrame 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. |
| Type | Description |
|---|---|
ColumnNotFoundError | If value_column is missing. |
ValueError | If the frame holds several uuids and signal_uuid is not given, or query is empty or contains NaN. |
template_match
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.
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | Input DataFrame. | required |
template | ndarray | Reference pattern as numpy array. | required |
value_column | str | Column containing numeric values. | 'value_double' |
threshold | float | None | Maximum distance to consider a match. None = adaptive (mean minus two standard deviations of the distance profile). | None |
normalize | bool | Whether 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_column | str | Column containing timestamps. | 'systime' |
signal_uuid | str | None | Signal to analyze when the frame holds several uuids (formerly uuid). | None |
| Type | Description |
|---|---|
DataFrame | DataFrame 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. |
| Type | Description |
|---|---|
ColumnNotFoundError | If value_column is missing. |
ValueError | If the frame holds several uuids and signal_uuid is not given, or template is empty or contains NaN. |
compute_distance_profile
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.
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | Input DataFrame. | required |
query | ndarray | Query subsequence. | required |
value_column | str | Column containing numeric values. | 'value_double' |
metric | str | 'euclidean' (FFT-based) or 'dtw'. | 'euclidean' |
normalize | bool | Whether to z-normalize. With False the Euclidean distance is the plain distance of the raw values. | True |
time_column | str | Rows are put in the order of this column (when the frame has it). | 'systime' |
signal_uuid | str | None | Signal to analyze when the frame holds several uuids (formerly uuid). | None |
| Type | Description |
|---|---|
ndarray | 1D 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. |
| Type | Description |
|---|---|
ColumnNotFoundError | If value_column is missing. |
ValueError | For an unknown metric, several uuids without signal_uuid, or a query that is empty or contains NaN. |