ts_shape.events.engineering.threshold_monitoring
Classes
| Name | Description |
|---|---|
ThresholdMonitoringEvents | Engineering: Threshold Monitoring |
ThresholdMonitoringEvents
ThresholdMonitoringEvents(
dataframe: pd.DataFrame,
signal_uuid: str,
*,
event_uuid: str = "eng:threshold",
value_column: str = "value_double",
time_column: str = "systime"
) -> None
Bases: Base
Engineering: Threshold Monitoring
Multi-level threshold monitoring with hysteresis for numeric signals.
Methods:
- multi_level_threshold: Intervals exceeding configurable warning/alarm/critical levels.
- threshold_with_hysteresis: Alarm entry/exit with separate high/low thresholds.
- time_above_threshold: Time and percentage above a threshold per window.
- threshold_exceedance_trend: Track exceedance frequency over time.
Example
>>> import pandas as pd
>>> from ts_shape import ThresholdMonitoringEvents
>>> df = pd.DataFrame({
... "systime": pd.date_range("2025-01-01 08:00", periods=6, freq="1min"),
... "uuid": "temp",
... "value_double": [70.0, 82.0, 91.0, 85.0, 75.0, 70.0],
... })
>>> monitor = ThresholdMonitoringEvents(df, "temp")
>>> monitor.multi_level_threshold({"warning": 80.0, "alarm": 90.0})[
... ["start", "end", "level"]]
start end level
0 2025-01-01 08:02:00 2025-01-01 08:03:00 alarm
1 2025-01-01 08:01:00 2025-01-01 08:04:00 warning
>>> monitor.threshold_with_hysteresis(upper_limit=80.0, lower_limit=76.0)[
... ["start", "end", "duration_seconds"]]
start end duration_seconds
0 2025-01-01 08:01:00 2025-01-01 08:04:00 180.0
Functions
| Name | Description |
|---|---|
multi_level_threshold | Detect intervals where signal exceeds configurable threshold levels. |
threshold_with_hysteresis | Alarm intervals with hysteresis to prevent chattering. |
time_above_threshold | Per window: total time and percentage above threshold. |
threshold_exceedance_trend | Track exceedance frequency and duration trend over time. |
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | Long-format timeseries frame (one row per sample, uuid identifies the signal). | required |
signal_uuid | str | UUID of the signal to analyze. | required |
event_uuid | str | UUID stamped on emitted events. | 'eng:threshold' |
value_column | str | Column holding the signal values. | 'value_double' |
time_column | str | Timestamp column. | 'systime' |
multi_level_threshold
multi_level_threshold(
levels: dict[str, float],
direction: str = "above",
*,
interval_end: str = "next_change"
) -> pd.DataFrame
Detect intervals where signal exceeds configurable threshold levels.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
levels | dict[str, float] | Dict mapping level names to threshold values. e.g. {'warning': 80, 'alarm': 90, 'critical': 95} | required |
direction | str | 'above' or 'below'. | 'above' |
interval_end | str | "next_change" (default) ends an interval at the next sample (the transition), so the interval spans the time the condition held and change-encoded signals -- one row per change -- keep their duration. "last_sample" ends it at its last flagged sample. See :func:ts_shape.utils.intervals.interval_end_times. | 'next_change' |
Returns
| Type | Description |
|---|---|
DataFrame | DataFrame with columns: start, end, duration_seconds, uuid, |
DataFrame | source_uuid, duration (Timedelta), level, peak_value. |
threshold_with_hysteresis
threshold_with_hysteresis(
upper_limit: float,
lower_limit: float,
*,
max_gap: str | pd.Timedelta | None = None
) -> pd.DataFrame
Alarm intervals with hysteresis to prevent chattering.
Enter alarm when signal crosses upper_limit, exit when it drops
below lower_limit.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
upper_limit | float | Upper threshold to enter alarm state. | required |
lower_limit | float | Lower threshold to exit alarm state. | required |
max_gap | str | Timedelta | None | Longest time between two samples an alarm may span (e.g. "15min"). A longer gap is "no data": an open alarm ends at its last sample before the gap, and after the gap the signal must reach upper_limit again to start a new one. None (default): no limit. | None |
Returns
| Type | Description |
|---|---|
DataFrame | DataFrame with columns: start, end, duration_seconds, uuid, |
DataFrame | source_uuid, duration (Timedelta), peak_value. |
time_above_threshold
time_above_threshold(
threshold: float, window: str = "1h", *, interval_end: str = "next_change"
) -> pd.DataFrame
Per window: total time and percentage above threshold.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
threshold | float | Value threshold. | required |
window | str | Resample window. | '1h' |
interval_end | str | "next_change" (default) holds each value until the next sample and measures the real time above the threshold inside each window (pct_above is relative to the window length). A window without a sample of its own that a held value covers is reported too. The time after the last sample is not counted. "last_sample" estimates the time as the fraction of samples above the threshold times the window length, which is only right for uniform sampling; it reports only windows that contain samples. | 'next_change' |
Returns
| Type | Description |
|---|---|
DataFrame | DataFrame with columns: start, end, duration_seconds, time_above, |
DataFrame | pct_above, exceedance_count. end is start + window, |
DataFrame | calendar-correct: on tz-aware data a daily window on a DST change |
DataFrame | lasts 23 h or 25 h, and time_above / pct_above are |
DataFrame | measured against that real duration_seconds. |
DataFrame | exceedance_count counts the samples that start an exceedance |
DataFrame | (at or above threshold while the previous sample was below); |
DataFrame | a signal that is already above at its first sample starts one |
DataFrame | there. |
DataFrame | time_above is in seconds. |
DataFrame | Canonical twin columns: time_above_seconds (after |
DataFrame | time_above), time_above_pct (after pct_above); each holds the |
DataFrame | same values as the deprecated column right before it, which is removed in |
DataFrame | the next major release. |
threshold_exceedance_trend
threshold_exceedance_trend(
threshold: float, window: str = "1D"
) -> pd.DataFrame
Track exceedance frequency and duration trend over time.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
threshold | float | Value threshold. | required |
window | str | Resample window (e.g. '1D' for daily). | '1D' |
Returns
| Type | Description |
|---|---|
DataFrame | DataFrame with columns: start, end, duration_seconds, |
DataFrame | exceedance_count, total_duration, total_duration_seconds, |
DataFrame | trend_direction. end is start + window; |
DataFrame | total_duration_seconds is the time above threshold in the |
DataFrame | window, in seconds. |
DataFrame | Canonical twin columns: total_duration_seconds (after |
DataFrame | total_duration); it holds the same values as the deprecated |
DataFrame | column right before it, which is removed in the next major |
DataFrame | release. |