Skip to main content
Version: Next

ts_shape.events.production.routing_traceability

Routing-based traceability using ID + state/handover signals.

In this pattern a single UUID carries the current item identifier (serial code, order number, etc.) and a separate UUID carries a state / routing signal whose value encodes which process step or station the item is at.

The state_map parameter defines the mapping from signal values to station/step names. This is required because the signal values alone don't tell the module what they mean — they could be PLC step numbers, recipe phases, station codes, or anything else.

Example::

State signal value 10 → "Heating"
State signal value 20 → "Holding"
State signal value 30 → "Cooling"

Or::

State signal value 1 → "Welding"
State signal value 2 → "Painting"
State signal value 3 → "Assembly"

This module correlates both signals to reconstruct the full routing path.

Classes
NameDescription
RoutingTraceabilityEventsTrace item routing using an ID signal paired with a state/routing signal.

RoutingTraceabilityEvents​

classsource
RoutingTraceabilityEvents(
dataframe: pd.DataFrame,
id_uuid: str,
routing_uuid: str,
*,
state_map: dict[int | float | str, str] | None = None,
station_map: dict[int | float | str, str] | None = None,
event_uuid: str = "prod:routing_trace",
value_column_id: str = "value_string",
value_column_routing: str = "value_integer",
time_column: str = "systime",
exclude: Exclude = None
) -> None

Bases: Base

Trace item routing using an ID signal paired with a state/routing signal.

Two UUIDs work together:

  • id_uuid: string signal carrying the current identifier (serial number, order ID, batch code, etc.).
  • routing_uuid: signal whose value encodes the current process step or station.

A state_map translates signal values to human-readable station or step names. Without it, raw values are used as labels.

Example usage::

# PLC step numbers mapped to process steps
trace = RoutingTraceabilityEvents(
df,
id_uuid='serial_code_signal',
routing_uuid='step_chain_signal',
state_map={
10: 'Heating',
20: 'Holding',
30: 'Cooling',
40: 'Discharge',
},
)

# Station handover signal mapped to stations
trace = RoutingTraceabilityEvents(
df,
id_uuid='serial_code_signal',
routing_uuid='handover_signal',
state_map={1: 'Welding', 2: 'Painting', 3: 'Assembly'},
)

timeline = trace.build_routing_timeline()
lead = trace.lead_time()
stats = trace.station_statistics()
paths = trace.routing_paths()
Functions
NameDescription
build_routing_timelineCorrelate the ID signal with the state/routing signal to build a timeline.
lead_timeCompute end-to-end lead time per item.
station_statisticsCompute dwell-time statistics per station/step across all items.
routing_pathsAnalyze routing path frequencies -- which station sequences are most common.
to_intervalsThe routing timeline as an object interval table (one row per visit).
Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame with timeseries data.required
id_uuidstrUUID of the signal carrying item identifiers.required
routing_uuidstrUUID of the state/routing signal.required
state_mapdict[int | float | str, str] | NoneMapping from signal value to station/step name. e.g. {10: 'Heating', 20: 'Holding', 30: 'Cooling'} or {1: 'Welding', 2: 'Painting', 3: 'Assembly'} Values can be int, float, or str keys. If not provided, raw signal values are used as labels.None
station_mapdict[int | float | str, str] | NoneDeprecated alias for state_map; passing it emits a :class:DeprecationWarning and it will be removed in the next major release. When both are passed, state_map wins and a UserWarning is emitted.None
event_uuidstrUUID to tag derived events with.'prod:routing_trace'
value_column_idstrColumn holding the item ID string.'value_string'
value_column_routingstrColumn holding the state/routing value.'value_integer'
time_columnstrName of timestamp column.'systime'
excludeExcludeReadings of the ID signal that are not item IDs, read like :attr:ts_shape.eventlog.ObjectSpec.exclude: "misreads" (:func:ts_shape.utils.id_codes.is_misread: NOREAD, #N/A, 0, 65535, blanks, ...), a collection of values, or a callable returning a boolean mask over the readings in time order. Between two readings of the same item an excluded reading is a gap (the item is held across it); anywhere else it is an empty reading (no item). None (default) keeps every reading; "misreads" is planned to become the default in the next major release.None

build_routing_timeline​

methodsource
build_routing_timeline(
*,
infer_dwell_from_next: bool = False,
interval_end: IntervalEnd = "last_sample"
) -> pd.DataFrame

Correlate the ID signal with the state/routing signal to build a timeline.

For each sample of the routing signal, the current item ID is determined via backward-fill merge (most recent ID at that timestamp). Contiguous intervals where the same (item_id, state) pair holds are grouped into single events. A routing sample without an item (none read yet, a missing or empty ID reading) or without a routing value is no visit and ends the interval before it; rows without a timestamp are left out.

Parameters
NameTypeDescriptionDefault
infer_dwell_from_nextboolWhen False (default), an interval's end is the last routing sample of the (item, state) segment, so a change-encoded routing signal (one row per change) reports a dwell of 0. When True, each visit's end is set to the item's next routing arrival, giving the honest arrival-here → arrival-at-next-state dwell. The last visit per item stays open.False
interval_endIntervalEndWhere a segment ends. "last_sample" (default): at its last routing sample. "next_change": at the next routing sample with another (item, state) pair, or with no item yet, so the state is held until it changes (like :func:ts_shape.eventlog.object_intervals); the last segment is held until the last timestamp of the two signals. The change is seen at a routing sample: an item change on the ID signal alone ends the segment at the next routing sample. infer_dwell_from_next is applied afterwards.'last_sample'
Returns
TypeDescription
DataFrameDataFrame with columns:
DataFrame- item_id: Identifier string.
DataFrame- routing_value: Raw signal value.
DataFrame- station_name: Resolved station/step name from state_map.
DataFrame- start: First timestamp of interval.
DataFrame- end: Last timestamp of interval (see interval_end).
DataFrame- duration_seconds: Time at this state.
DataFrame- sample_count: Number of routing samples in interval.
DataFrame- station_sequence: Visit order per item (1-based).
DataFrame- uuid: Event UUID.
DataFrame- source_uuid: "<id_uuid>,<routing_uuid>" (the two correlated input signals).

lead_time​

methodsource
lead_time() -> pd.DataFrame

Compute end-to-end lead time per item.

Returns
TypeDescription
DataFrameDataFrame with columns:
DataFrame- item_id
DataFrame- first_station: Name of first station/step visited.
DataFrame- last_station: Name of last station/step visited.
DataFrame- first_seen: Earliest timestamp.
DataFrame- last_seen: Latest timestamp.
DataFrame- lead_time_seconds: Total elapsed time.
DataFrame- stations_visited: Number of distinct stations/steps.
DataFrame- routing_path: Ordered station names joined by " -> ".
DataFrame- uuid: Event UUID.
DataFrame- start: Same as first_seen.
DataFrame- end: Same as last_seen.
DataFrame- duration_seconds: Same as lead_time_seconds (float seconds).
DataFrame- source_uuid: "<id_uuid>,<routing_uuid>".

station_statistics​

methodsource
station_statistics(
*,
infer_dwell_from_next: bool = False,
interval_end: IntervalEnd = "last_sample"
) -> pd.DataFrame

Compute dwell-time statistics per station/step across all items.

Parameters
NameTypeDescriptionDefault
infer_dwell_from_nextboolForwarded to :meth:build_routing_timeline; pass True for change-encoded routing signals so dwell is measured to the item's next routing arrival instead of collapsing to zero.False
interval_endIntervalEndForwarded to :meth:build_routing_timeline.'last_sample'
Returns
TypeDescription
DataFrameDataFrame with columns:
DataFrame- station_name
DataFrame- routing_value
DataFrame- item_count: Number of distinct items seen.
DataFrame- min_dwell_seconds
DataFrame- avg_dwell_seconds
DataFrame- max_dwell_seconds
DataFrame- total_dwell_seconds

routing_paths​

methodsource
routing_paths() -> pd.DataFrame

Analyze routing path frequencies -- which station sequences are most common.

Returns
TypeDescription
DataFrameDataFrame with columns:
DataFrame- routing_path: Ordered station names joined by " -> ".
DataFrame- item_count: Number of items that followed this path.
DataFrame- avg_lead_time_seconds: Average lead time for items on this path.
DataFrame- min_lead_time_seconds
DataFrame- max_lead_time_seconds

to_intervals​

methodsource
to_intervals(
*, object_type: str = "serial", interval_end: IntervalEnd = "last_sample"
) -> pd.DataFrame

The routing timeline as an object interval table (one row per visit).

The result has the columns of :func:ts_shape.eventlog.object_intervals plus station, so it feeds :func:~ts_shape.eventlog.presence_events, :func:~ts_shape.eventlog.object_kpis, :func:~ts_shape.eventlog.wip_over_time, :func:~ts_shape.eventlog.attach_counts, :func:~ts_shape.eventlog.relate and :func:~ts_shape.eventlog.link_events_to_objects directly.

Parameters
NameTypeDescriptionDefault
object_typestrObject type of the items ("serial", "work_order", "batch", ...).'serial'
interval_endIntervalEndForwarded to :meth:build_routing_timeline.'last_sample'
Returns
TypeDescription
DataFrameDataFrame with columns:
DataFrame- oid: The item ID (text).
DataFrame- type: object_type.
DataFrame- start / end: The visit, in UTC (naive input is read as UTC).
DataFrame- duration_seconds
DataFrame- uuid: The ID signal (id_uuid).
DataFrame- source_uuid: "<id_uuid>,<routing_uuid>", as in the timeline.
DataFrame- station: The station / step name from state_map; the analysis functions use it as the station (to :func:~ts_shape.eventlog.relate it is an object attribute, recorded at each visit).
DataFrameOrdered by item, then start. No visit gives an empty frame with
DataFramethese columns.