Skip to main content
Version: Next

ts_shape.events.production.batch_tracking

Batch / recipe production tracking.

Detect batch boundaries from a string signal that carries the current batch ID, compute duration statistics, per-batch yield, and batch transition matrices.

Classes
NameDescription
BatchTrackingEventsTrack batch/recipe production from a batch-ID string signal.

BatchTrackingEvents​

classsource
BatchTrackingEvents(
dataframe: pd.DataFrame,
batch_uuid: str,
*,
event_uuid: str = "prod:batch",
value_column: str = "value_string",
time_column: str = "systime"
) -> None

Bases: Base

Track batch/recipe production from a batch-ID string signal.

A "batch" is defined as a contiguous period where the batch-ID value remains constant. A new batch begins when the value changes.

Example usage

batches = BatchTrackingEvents(df, batch_uuid='batch_id_signal')

detected = batches.detect_batches() stats = batches.batch_duration_statistics() yields = batches.batch_yield('part_counter') transitions = batches.batch_transition_matrix()

Functions
NameDescription
detect_batchesDetect batch start/end from value changes in the batch-ID signal.
batch_duration_statisticsCompute duration statistics grouped by batch type (batch_id).
batch_yieldCompute production quantity for each detected batch.
batch_transition_matrixCount which batch follows which.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame with timeseries data.required
batch_uuidstrUUID of the batch-ID signal.required
event_uuidstrUUID to tag derived events with.'prod:batch'
value_columnstrColumn holding the batch ID string.'value_string'
time_columnstrName of timestamp column.'systime'

detect_batches​

methodsource
detect_batches() -> pd.DataFrame

Detect batch start/end from value changes in the batch-ID signal.

Returns
TypeDescription
DataFrameDataFrame with columns:
DataFrame- batch_id: The batch identifier string.
DataFrame- start: Timestamp of the first sample in the batch.
DataFrame- end: Timestamp of the last sample in the batch.
DataFrame- duration_seconds: Duration of the batch.
DataFrame- sample_count: Number of data points in the batch.
DataFrame- uuid: Event UUID.
DataFrame- source_uuid: Source signal UUID.

batch_duration_statistics​

methodsource
batch_duration_statistics() -> pd.DataFrame

Compute duration statistics grouped by batch type (batch_id).

Returns
TypeDescription
DataFrameDataFrame with columns:
DataFrame- batch_id
DataFrame- count: deprecated twin of batch_count.
DataFrame- batch_count: Number of occurrences of this batch type.
DataFrame- min_duration_seconds
DataFrame- avg_duration_seconds
DataFrame- max_duration_seconds
DataFrame- total_duration_seconds
DataFrameCanonical twin columns: batch_count (after count); it holds the same
DataFramevalues as the deprecated column right before it, which is removed
DataFramein the next major release.

batch_yield​

methodsource
batch_yield(
counter_uuid: str,
*,
value_column_counter: str = "value_integer",
handle_resets: bool = True
) -> pd.DataFrame

Compute production quantity for each detected batch.

Uses a monotonic counter signal. With handle_resets=True the reset-aware increments are computed once over the whole counter and each is booked to the batch that was running when it was produced: a reading reports the production since the previous reading, so it belongs to the batch whose ID was last seen strictly before it. A batch therefore owns the counter readings after its first sample up to and including the next batch-ID change, the production between the last sample of one batch and the first sample of the next counts for the earlier batch, and the batch quantities add up to the counter total (readings before the first batch-ID sample, after the last one, or while the batch ID is empty belong to no batch).

Parameters
NameTypeDescriptionDefault
counter_uuidstrUUID of the monotonic part counter.required
value_column_counterstrColumn holding counter values.'value_integer'
handle_resetsboolWhen True (default), treat a decreasing counter as a reset and count the production after the reset instead of losing it, so quantity is the reset-aware total for the batch. The result gains a resets column with the number of resets observed in each batch. When False, quantity is simply max(0, last - first) of the readings between the batch's first and last sample (0 for fewer than two readings): a batch containing a reset reports too little and production between batches is lost.True
Returns
TypeDescription
DataFrameDataFrame with columns:
DataFrame- batch_id
DataFrame- start
DataFrame- end
DataFrame- duration_seconds
DataFrame- quantity: Parts produced during the batch (see above).
DataFrame- uuid
DataFrame- source_uuid
DataFrame- resets (only when handle_resets=True)

batch_transition_matrix​

methodsource
batch_transition_matrix(
*, layout: Literal["long", "wide"] = "long"
) -> pd.DataFrame

Count which batch follows which.

Parameters
NameTypeDescriptionDefault
layoutLiteral['long', 'wide']"long" (default) returns one row per observed transition. "wide" returns the pivoted matrix.'long'
Returns
TypeDescription
DataFrameWith layout="long", a DataFrame with columns from_state,
DataFrameto_state and transition_count (int), one row per observed
DataFrame(from_state, to_state) pair, sorted by from_state and
DataFrameto_state. With fewer than two batches the result is empty and
DataFramekeeps these columns.
DataFrameWith layout="wide", a pivot table where index = from_batch,
DataFramecolumns = to_batch, values = transition count, plus a total
DataFramecolumn. The batch columns depend on the data, so with fewer than
DataFrametwo batches the result is an empty frame with only the total
DataFramecolumn (index named from_batch).
Raises
TypeDescription
ValueErrorIf layout is not "long" or "wide".