Skip to main content
Version: Next

ts_shape.events.quality.anomaly_classification

Classes
NameDescription
AnomalyClassificationEventsQuality: Anomaly Classification

AnomalyClassificationEvents​

classsource
AnomalyClassificationEvents(
dataframe: pd.DataFrame,
signal_uuid: str,
*,
event_uuid: str = "quality:anomaly_type",
value_column: str = "value_double",
time_column: str = "systime"
) -> None

Bases: Base

Quality: Anomaly Classification

Classify anomalies in a numeric signal by type: spike, drift, oscillation, flatline, or level shift.

Methods:

  • classify_anomalies: Detect and classify anomalous windows.
  • detect_flatline: Signal stuck at constant value.
  • detect_oscillation: Periodic instability detection.
  • detect_drift: Short-term slope-based drift events.
Functions
NameDescription
detect_flatlineDetect intervals where the signal is stuck at a constant value.
detect_oscillationDetect windows with excessive zero-crossings of the detrended signal.
detect_driftDetect windows where the rolling slope exceeds a threshold.
classify_anomaliesDetect anomalous windows and classify by type.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameLong-format timeseries frame (one row per sample, uuid identifies the signal).required
signal_uuidstrUUID of the signal to analyze.required
event_uuidstrUUID stamped on emitted events.'quality:anomaly_type'
value_columnstrColumn holding the signal values.'value_double'
time_columnstrTimestamp column.'systime'

detect_flatline​

methodsource
detect_flatline(
min_duration: str = "1min",
tolerance: float = 1e-06,
*,
interval_end: str = "next_change",
max_gap: str | pd.Timedelta | None = None
) -> pd.DataFrame

Detect intervals where the signal is stuck at a constant value.

A run starts at its first sample (the value was already stuck there) and continues while each sample is within tolerance of the run's first value (its anchor): |v - anchor| <= tolerance. Comparing consecutive samples instead would let a slow ramp (each step within tolerance) pass as one long flatline. Samples at the same timestamp (duplicates) add no duration: a run whose samples all share one timestamp counts as a single sample.

Parameters
NameTypeDescriptionDefault
min_durationstrMinimum duration to qualify as a flatline.'1min'
tolerancefloatLargest distance from the run's first value that still counts as flat.1e-06
interval_endstr"next_change" (default) ends a run at the next differing sample, so a historian with a deadband -- one row, then nothing until the value moves -- still reports a stuck sensor. A single-sample run counts only when the gap after it is longer than the signal's median sampling interval. "last_sample" ends a run at its own last sample and needs at least two samples.'next_change'
max_gapstr | Timedelta | NoneLongest time between two samples a run may span (e.g. "15min"). A longer gap is "no data": the run before it ends at its last sample and a new run starts after it, even if the value is unchanged. None (default): no limit.None
Returns
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, uuid, source_uuid, duration, stuck_value. duration is a Timedelta; duration_seconds is the same span in float seconds.

detect_oscillation​

methodsource
detect_oscillation(
window: str = "1min", min_crossings: int = 6
) -> pd.DataFrame

Detect windows with excessive zero-crossings of the detrended signal.

Windows are half-open, t0 + k*window, t0 + (k+1)*window) from the first sample t0, and cover every sample: a sample on a window edge belongs to the later window, and the trailing partial window is evaluated too (with at least 3 samples).

A zero-crossing is a sign change of the value minus the line joining the window's first and last value. A value exactly on the line carries the last non-zero sign (the window's first and last value always lie on it), so 1, 0, -1 is one crossing and touching the line (1, 0, 1) is none.

Parameters
NameTypeDescriptionDefault
window[strWindow size for analysis.'1min'
min_crossingsintMinimum zero-crossings to flag oscillation.6
Returns
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, uuid, source_uuid, crossing_count, amplitude.

detect_drift​

methodsource
detect_drift(
window: str = "1h", min_slope: float = 0.01, slope_per: str = "second"
) -> pd.DataFrame

Detect windows where the rolling slope exceeds a threshold.

A least-squares line is fitted to each non-overlapping half-open window t0 + k*window, t0 + (k+1)*window) (t0 the first sample) with at least 3 samples; the trailing partial window is evaluated too, and its end is the full window end. With the default slope_per="second" the x-axis is elapsed time, so slope and min_slope are in signal units per second regardless of the sampling rate (and irregular sampling is handled correctly). With slope_per="sample" the x-axis is the sample index, so slope is in signal units per sample and the meaning of min_slope depends on the sampling rate.

NaN values are left out of the fit. A window whose fit fails (e.g. all of its samples share one timestamp) is skipped with a :class:~ts_shape.errors.DataQualityWarning.

Parameters
NameTypeDescriptionDefault
window[strWindow size for slope computation.'1h'
min_slopefloatMinimum absolute slope to flag, in signal units per sample or per second (see slope_per).0.01
slope_perstr"second" (default) or "sample".'second'
Returns
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, uuid, source_uuid, slope, direction.

classify_anomalies​

methodsource
classify_anomalies(
window: str = "10min", n_sigma: float = 3.0, slope_per: str = "second"
) -> pd.DataFrame

Detect anomalous windows and classify by type.

Types: spike, drift, oscillation, flatline, level_shift.

Windows are half-open, t0 + k*window, t0 + (k+1)*window) from the first sample t0, and cover every sample, the trailing partial window included (a window needs at least 3 samples).

A window is a drift when its least-squares slope exceeds 0.1 × the signal's overall std per sample. With the default slope_per="second" the slope is fitted against elapsed time and compared with 0.1 × std divided by the signal's median sampling interval (seconds): irregular sampling is handled correctly and the slope reported in details is in signal units per second. With slope_per="sample" the slope is fitted against the sample index, so the test depends on the sampling rate; on regularly sampled data both settings flag the same windows.

A window is an oscillation when its detrended values change sign more than 0.4 × samples times, counted as in :meth:detect_oscillation (zeros carry the last non-zero sign).

Parameters
NameTypeDescriptionDefault
window[strWindow size for analysis.'10min'
n_sigmafloatZ-score threshold for spike detection.3.0
slope_perstr"second" (default) or "sample".'second'
Returns
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, uuid, source_uuid, anomaly_type, severity, details.