Skip to main content
Version: 0.0.98

ts_shape.events.production.duty_cycle

Classes
NameDescription
DutyCycleEventsProduction: Duty Cycle Analysis

DutyCycleEvents​

classsource
DutyCycleEvents(
dataframe: pd.DataFrame,
signal_uuid: str,
*,
event_uuid: str = "prod:duty_cycle",
value_column: str = "value_bool",
time_column: str = "systime",
interval_end: str = "next_change",
data_end: pd.Timestamp | None = None,
run_states: Collection[Any] | None = None,
value_range: tuple[float | None, float | None] | None = None
) -> None

Bases: Base

Production: Duty Cycle Analysis

Analyze on/off patterns from a boolean signal: duty cycle percentage, interval listing, transition counts, and excessive cycling detection.

Methods:

  • duty_cycle_by_window: On-time percentage per time window.
  • on_off_intervals: List every on and off interval with duration.
  • cycle_count: Number of on/off transitions per window.
  • excessive_cycling: Flag windows with too many transitions.
Functions
NameDescription
on_off_intervalsList every on and off interval with duration.
duty_cycle_by_windowPercentage of time the signal is True per window.
cycle_countNumber of on/off transitions per window.
excessive_cyclingFlag windows where transition count exceeds threshold.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameLong-format input data.required
signal_uuidstrUUID of the boolean on/off signal (True = on).required
event_uuidstrUUID assigned to generated events.'prod:duty_cycle'
value_columnstrColumn holding the on/off state. A missing value (None, NaN, pd.NA) is unknown, not off: the time from such a row to the next sample is in neither on_time nor off_time, no on/off interval covers it, and it is no transition. Ratios use observed (known-state) time.'value_bool'
time_columnstrTimestamp column.'systime'
interval_endstrHow on/off intervals are closed. "next_change" (default) closes each interval at the next transition, which measures both dense and change-encoded (is_delta) signals. "last_sample" ends each interval at its last sample; on a change-encoded signal (one row per transition) every on/off interval then has zero duration.'next_change'
data_endTimestamp | NoneOptional bound for the final, still-open interval when interval_end="next_change"; :meth:duty_cycle_by_window (time-weighted) and :meth:cycle_count then also report the windows up to data_end. A naive value or text on tz-aware data is local time of the data's timezone.None
run_statesCollection[Any] | NoneValues of the run signal that mean running (e.g. {2} for a mode signal where 2 is automatic, or {"RUN"}); every other known value is not running. None (default) uses value_range or reads the signal as a bool, with a DataQualityWarning when it is not one.None
value_rangetuple[float | None, float | None] | None(lower, upper) inclusive bounds; a value inside counts as running (either bound may be None). Used when run_states is None.None

on_off_intervals​

methodsource
on_off_intervals(*, max_gap: str | pd.Timedelta | None = None) -> pd.DataFrame

List every on and off interval with duration.

Parameters
NameTypeDescriptionDefault
max_gapstr | Timedelta | NoneLongest time between two samples an interval may span (e.g. "15min"). A longer gap is "no data": the interval before it ends at its last sample and a new interval starts after the gap, even if the state is unchanged. None (default): no limit.None
Returns
TypeDescription
DataFrameDataFrame with columns: start, end, duration_seconds (float),
DataFrameuuid (event uuid), source_uuid (signal uuid), state (on/off),
DataFrameduration (Timedelta).

duty_cycle_by_window​

methodsource
duty_cycle_by_window(
window: str = "1h", *, time_weighted: bool = True, max_gap: Any = "auto"
) -> pd.DataFrame

Percentage of time the signal is True per window.

Parameters
NameTypeDescriptionDefault
windowstrResample window.'1h'
time_weightedboolWhen True (default), each sample is weighted by the gap to the next sample, so the duty cycle reflects wall-clock on-time on change-encoded and irregularly sampled signals; on_time and off_time are the covered seconds in the window. When False, the duty cycle is the mean of the boolean samples in the window, which equals the time share only under uniform sampling (On@09:00, Off@09:59 reads 50 %).True
max_gapAnyTime-weighted only. A gap between two samples longer than this ("15min", Timedelta) is a logging gap: its time is unknown_seconds, neither on nor off. "auto" (default) uses the signal's inferred max_gap (five times the sampling period of a sampled signal; no cap for a change-encoded one, whose long gaps are real holds -- pass a value when the export has holes). None applies no cap.'auto'

Each interval between two samples is split at the window boundaries, so on_time + off_time + unknown_seconds never exceeds the window's length (time-weighted).

Samples with a missing value are unknown time: in neither on_time nor off_time (time-weighted) or not averaged (sample mean), and duty_cycle_pct is the on share of the known time (NaN for a window without any). With the constructor's data_end (time-weighted), the last sample's state is held until data_end and the windows up to it are reported.

Returns
TypeDescription
DataFrameDataFrame with columns: start, end (start + window),
DataFrameduration_seconds, uuid, source_uuid, on_time, on_time_seconds,
DataFrameoff_time, off_time_seconds, duty_cycle_pct, unknown_seconds. On
DataFrameand off time are in seconds; unknown_seconds is the time in
DataFramethe window with a missing value or inside a gap longer than
DataFramemax_gap (time-weighted; 0 for the sample mean).
DataFrameCanonical twin columns: on_time_seconds (after on_time),
DataFrameoff_time_seconds (after off_time); each holds the same
DataFramevalues as the deprecated column right before it, which is removed
DataFramein the next major release.

cycle_count​

methodsource
cycle_count(window: str = '1h') -> pd.DataFrame

Number of on/off transitions per window.

A transition is a change between two known states; samples with a missing value are skipped. With the constructor's data_end the windows up to data_end are reported (with zero transitions after the last sample).

Parameters
NameTypeDescriptionDefault
windowstrResample window.'1h'
Returns
TypeDescription
DataFrameDataFrame with columns: start, end (start + window),
DataFrameduration_seconds, uuid, source_uuid, on_count, off_count,
DataFrametotal_transitions.

excessive_cycling​

methodsource
excessive_cycling(
max_transitions: int = 20, window: str = "1h"
) -> pd.DataFrame

Flag windows where transition count exceeds threshold.

Excessive cycling indicates hunting, instability, or wear risk.

Parameters
NameTypeDescriptionDefault
max_transitionsintThreshold for flagging.20
windowstrResample window.'1h'
Returns
TypeDescription
DataFrameDataFrame with columns: start, end (start + window),
DataFrameduration_seconds, uuid, source_uuid, transition_count,
DataFrameavg_on_duration, avg_on_duration_seconds, avg_off_duration,
DataFrameavg_off_duration_seconds. The averages are in seconds.
DataFrameCanonical twin columns: avg_on_duration_seconds (after
DataFrameavg_on_duration), avg_off_duration_seconds (after
DataFrameavg_off_duration); each holds the same values as the
DataFramedeprecated column right before it, which is removed in the next
DataFramemajor release.