Skip to main content
Version: Next

ts_shape.events.engineering.threshold_monitoring

Classes
NameDescription
ThresholdMonitoringEventsEngineering: Threshold Monitoring

ThresholdMonitoringEvents​

classsource
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
NameDescription
multi_level_thresholdDetect intervals where signal exceeds configurable threshold levels.
threshold_with_hysteresisAlarm intervals with hysteresis to prevent chattering.
time_above_thresholdPer window: total time and percentage above threshold.
threshold_exceedance_trendTrack exceedance frequency and duration trend over time.
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.'eng:threshold'
value_columnstrColumn holding the signal values.'value_double'
time_columnstrTimestamp column.'systime'

multi_level_threshold​

methodsource
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
NameTypeDescriptionDefault
levelsdict[str, float]Dict mapping level names to threshold values. e.g. {'warning': 80, 'alarm': 90, 'critical': 95}required
directionstr'above' or 'below'.'above'
interval_endstr"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
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, uuid,
DataFramesource_uuid, duration (Timedelta), level, peak_value.

threshold_with_hysteresis​

methodsource
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
NameTypeDescriptionDefault
upper_limitfloatUpper threshold to enter alarm state.required
lower_limitfloatLower threshold to exit alarm state.required
max_gapstr | Timedelta | NoneLongest 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
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, uuid,
DataFramesource_uuid, duration (Timedelta), peak_value.

time_above_threshold​

methodsource
time_above_threshold(
threshold: float, window: str = "1h", *, interval_end: str = "next_change"
) -> pd.DataFrame

Per window: total time and percentage above threshold.

Parameters
NameTypeDescriptionDefault
thresholdfloatValue threshold.required
windowstrResample window.'1h'
interval_endstr"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
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds, time_above,
DataFramepct_above, exceedance_count. end is start + window,
DataFramecalendar-correct: on tz-aware data a daily window on a DST change
DataFramelasts 23 h or 25 h, and time_above / pct_above are
DataFramemeasured against that real duration_seconds.
DataFrameexceedance_count counts the samples that start an exceedance
DataFrame(at or above threshold while the previous sample was below);
DataFramea signal that is already above at its first sample starts one
DataFramethere.
DataFrametime_above is in seconds.
DataFrameCanonical twin columns: time_above_seconds (after
DataFrametime_above), time_above_pct (after pct_above); each holds the
DataFramesame values as the deprecated column right before it, which is removed in
DataFramethe next major release.

threshold_exceedance_trend​

methodsource
threshold_exceedance_trend(
threshold: float, window: str = "1D"
) -> pd.DataFrame

Track exceedance frequency and duration trend over time.

Parameters
NameTypeDescriptionDefault
thresholdfloatValue threshold.required
windowstrResample window (e.g. '1D' for daily).'1D'
Returns
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds,
DataFrameexceedance_count, total_duration, total_duration_seconds,
DataFrametrend_direction. end is start + window;
DataFrametotal_duration_seconds is the time above threshold in the
DataFramewindow, in seconds.
DataFrameCanonical twin columns: total_duration_seconds (after
DataFrametotal_duration); it holds the same values as the deprecated
DataFramecolumn right before it, which is removed in the next major
DataFramerelease.