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.
| Name | Description |
|---|---|
RoutingTraceabilityEvents | Trace item routing using an ID signal paired with a state/routing signal. |
RoutingTraceabilityEvents
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()
| Name | Description |
|---|---|
build_routing_timeline | Correlate the ID signal with the state/routing signal to build a timeline. |
lead_time | Compute end-to-end lead time per item. |
station_statistics | Compute dwell-time statistics per station/step across all items. |
routing_paths | Analyze routing path frequencies -- which station sequences are most common. |
to_intervals | The routing timeline as an object interval table (one row per visit). |
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | Input DataFrame with timeseries data. | required |
id_uuid | str | UUID of the signal carrying item identifiers. | required |
routing_uuid | str | UUID of the state/routing signal. | required |
state_map | dict[int | float | str, str] | None | Mapping 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_map | dict[int | float | str, str] | None | Deprecated 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_uuid | str | UUID to tag derived events with. | 'prod:routing_trace' |
value_column_id | str | Column holding the item ID string. | 'value_string' |
value_column_routing | str | Column holding the state/routing value. | 'value_integer' |
time_column | str | Name of timestamp column. | 'systime' |
exclude | Exclude | Readings 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
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.
| Name | Type | Description | Default |
|---|---|---|---|
infer_dwell_from_next | bool | When 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_end | IntervalEnd | Where 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' |
| Type | Description |
|---|---|
DataFrame | DataFrame 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
lead_time() -> pd.DataFrame
Compute end-to-end lead time per item.
| Type | Description |
|---|---|
DataFrame | DataFrame 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
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.
| Name | Type | Description | Default |
|---|---|---|---|
infer_dwell_from_next | bool | Forwarded 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_end | IntervalEnd | Forwarded to :meth:build_routing_timeline. | 'last_sample' |
| Type | Description |
|---|---|
DataFrame | DataFrame 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
routing_paths() -> pd.DataFrame
Analyze routing path frequencies -- which station sequences are most common.
| Type | Description |
|---|---|
DataFrame | DataFrame 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
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.
| Name | Type | Description | Default |
|---|---|---|---|
object_type | str | Object type of the items ("serial", "work_order", "batch", ...). | 'serial' |
interval_end | IntervalEnd | Forwarded to :meth:build_routing_timeline. | 'last_sample' |
| Type | Description |
|---|---|
DataFrame | DataFrame 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). |
DataFrame | Ordered by item, then start. No visit gives an empty frame with |
DataFrame | these columns. |