Skip to main content
Version: Next

ts_shape.events.production.machine_state

Classes
NameDescription
MachineStateEventsProduction: Machine State

MachineStateEvents​

classsource
MachineStateEvents(
dataframe: pd.DataFrame,
run_state_uuid: str,
*,
event_uuid: str = "prod:run_idle",
value_column: str = "value_bool",
time_column: str = "systime",
value_range: tuple[float | None, float | None] | None = None,
run_states: Collection[Any] | None = None,
specs: Mapping[str, SignalSpec] | None = None
) -> None

Bases: Base

Production: Machine State

Detect run/idle transitions and intervals from a boolean state signal.

  • MachineStateEvents: Run/idle state intervals and transitions.
    • detect_run_idle: Intervalize run/idle states with optional min duration filter.
    • transition_events: Point events on state changes (idle->run, run->idle).
Example
>>> import pandas as pd, ts_shape
>>> t = pd.date_range("2025-01-01 08:00", periods=6, freq="10min")
>>> df = pd.DataFrame({"systime": t, "uuid": "run",
... "value_bool": [True, True, False, True, True, False]})
>>> out = ts_shape.MachineStateEvents(df, "run").detect_run_idle()
>>> out[["start", "state", "duration_seconds"]]
start state duration_seconds
0 2025-01-01 08:00:00 run 1200.0
1 2025-01-01 08:20:00 idle 600.0
2 2025-01-01 08:30:00 run 1200.0
3 2025-01-01 08:50:00 idle 0.0
Functions
NameDescription
detect_run_idleReturn intervals labeled as 'run' or 'idle'.
transition_eventsReturn point events at state transitions.
detect_rapid_transitionsIdentify suspicious rapid state changes.
state_quality_metricsReturn quality metrics for the state data.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameLong-format timeseries frame (one row per sample, uuid identifies the signal).required
run_state_uuidstrUUID of the run-state signal; must be present in a non-empty dataframe.required
event_uuidstrUUID stamped on emitted events.'prod:run_idle'
value_columnstrColumn holding the run state.'value_bool'
time_columnstrTimestamp column.'systime'
value_rangetuple[float | None, float | None] | None(lower, upper) inclusive bounds; a value inside counts as running (either bound may be None). None (default) casts the value to bool.None
run_statesCollection[Any] | NoneValues that count as running (e.g. {"RUN", "AUTO"} for a text state column); every other known value is idle. None (default) uses value_range or the bool cast. A text state column without run_states or value_range raises a DataQualityWarning: the bool cast counts every non-empty text, including "IDLE", as running.None
specsMapping str, [SignalSpec ] | NoneOptional signal semantics (see :mod:ts_shape.signals). When the run-state signal has a spec, interval closing and gap handling follow it.None

detect_run_idle​

methodsource
detect_run_idle(
min_duration: str = "0s",
*,
interval_end: str | None = None,
data_end: pd.Timestamp | None = None
) -> pd.DataFrame

Return intervals labeled as 'run' or 'idle'.

  • min_duration: discard intervals shorter than this duration.
  • interval_end: how to close each interval. "next_change" closes each interval at the next transition instant, which measures both dense and change-encoded (is_delta) signals. "last_sample" ends an interval at its last sample; on a change-encoded signal (one row per transition) every interval then has zero duration. Default (None): follow the signal's spec when specs= was given (held states close at the next change, and gaps longer than the spec's max_gap are "no data"), otherwise "next_change".
  • data_end: optional bound for the final, still-open interval when intervals close at the next change (e.g. the end of the data window).
  • A missing state value (None, NaN, pd.NA) is unknown, not idle: the time from such a row to the next known row is in neither a run nor an idle interval (like ReliabilityEvents' "no data"). Columns: start, end, uuid, source_uuid, is_delta, state, duration_seconds

transition_events​

methodsource
transition_events() -> pd.DataFrame

Return point events at state transitions.

Rows with a missing state are skipped: a transition is a change between two known states.

Columns: systime, uuid, source_uuid, is_delta, transition ('idle_to_run'|'run_to_idle'), time_since_last_transition_seconds

detect_rapid_transitions​

methodsource
detect_rapid_transitions(
threshold: str = "5s", min_count: int = 3
) -> pd.DataFrame

Identify suspicious rapid state changes.

  • threshold: time window to look for rapid transitions
  • min_count: minimum number of transitions within threshold to be considered rapid Returns: DataFrame with systime (= start, the first transition of the burst), uuid (event uuid), source_uuid (run-state uuid), start, end, transition_count, duration_seconds

state_quality_metrics​

methodsource
state_quality_metrics(
*,
interval_end: str | None = None,
data_end: pd.Timestamp | None = None,
as_frame: bool = True
) -> pd.DataFrame | dict[str, Any]

Return quality metrics for the state data.

Parameters
NameTypeDescriptionDefault
interval_endstr | NoneForwarded to :meth:detect_run_idle. Default None resolves exactly as in :meth:detect_run_idle: follow the signal's spec when specs= was given, otherwise "next_change" (run/idle durations are measured to the next transition).None
data_endTimestamp | NoneForwarded to :meth:detect_run_idle; optional bound for the final, still-open interval.None
as_frameboolWhen True (default), return a one-row DataFrame (see below). as_frame=False returns the dictionary form; it is deprecated, emits a DeprecationWarning and will be removed in the next major release.True
Returns
TypeDescription
DataFrame | dict[str, Any]With as_frame=True (default), a one-row summary DataFrame with
DataFrame | dict[str, Any]columns start, end, duration_seconds (the span of the state
DataFrame | dict[str, Any]signal; NaT / 0 without data), uuid (event uuid), source_uuid
DataFrame | dict[str, Any](run-state uuid), followed by the metrics below.
DataFrame | dict[str, Any]Canonical twin columns: avg_run_duration_seconds (after
DataFrame | dict[str, Any]avg_run_duration), avg_idle_duration_seconds (after
DataFrame | dict[str, Any]avg_idle_duration), run_idle_factor (after
DataFrame | dict[str, Any]run_idle_ratio, which is not a 0-1 fraction); each holds the
DataFrame | dict[str, Any]same values as the deprecated column right before it, which is
DataFrame | dict[str, Any]removed in the next major release. The dictionary form keeps the
DataFrame | dict[str, Any]old keys.
DataFrame | dict[str, Any]With as_frame=False (deprecated), a dictionary with:
DataFrame | dict[str, Any]- total_transitions: total number of state transitions
DataFrame | dict[str, Any]- avg_run_duration: average duration of run states in seconds; NaN when there is no run interval
DataFrame | dict[str, Any]- avg_idle_duration: average duration of idle states in seconds; NaN when there is no idle interval
DataFrame | dict[str, Any]Missing state values are unknown time, in neither average nor the
DataFrame | dict[str, Any]ratio.
DataFrame | dict[str, Any]- run_idle_ratio: ratio of total run time to total idle time; NaN when there is no idle time (including no intervals at all), since the ratio is undefined
DataFrame | dict[str, Any]- data_gaps_detected: number of data gaps found
DataFrame | dict[str, Any]- rapid_transitions_detected: number of rapid transition events