counters
counters ¤
Reset-aware helpers for monotonic production counters.
Many industrial counters -- parts produced, scrap, NOK parts, cumulative
energy -- are expected to increase monotonically. In the field they
frequently reset back to zero (or to a lower value) at a shift change, a
part change, or a controller (PLC) restart. A naive last - first
calculation then silently loses all of the production accumulated in the
window where the reset happened.
These helpers treat any decrease in the counter as a reset and count the
post-reset value as new production, mirroring the approach in
:class:ts_shape.events.production.part_tracking.PartProductionEvents.
Use them wherever a quantity is derived from a cumulative counter so the
behavior is consistent across the library.
Example::
from ts_shape.utils.counters import counter_quantity
# A shift where the counter reset once: 100, 300, 500, 0, 150
values = pd.Series([100, 300, 500, 0, 150])
counter_quantity(values) # 550 (400 + 150)
counter_quantity(values, handle_resets=False) # 50 (plain last - first)
Non-numeric readings are treated as gaps; a
:class:~ts_shape.errors.DataQualityWarning reports how many there were.
counter_increments ¤
counter_increments(values: Series) -> pd.Series
Per-reading production increments from a (possibly resetting) counter.
A decrease between consecutive readings is interpreted as a reset: the
counter is assumed to have dropped to (at most) zero and then climbed to
its current value, so the new value is the production that happened after
the reset. The first reading has no predecessor and therefore contributes
0 -- it establishes the baseline rather than counting as production.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Series
|
Counter readings, ordered in time. Non-numeric entries are
coerced to NaN and treated as gaps (with a
:class: |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A float Series, index-aligned with |
Series
|
since the previous reading. |
counter_quantity ¤
counter_quantity(
values: Series, *, handle_resets: bool = True
) -> float
Total production represented by a counter series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Series
|
Counter readings, ordered in time. |
required |
handle_resets
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
float
|
The total quantity as a float. Empty input returns |
counter_by_group ¤
counter_by_group(
values: Series,
keys: Series | list[Series],
*,
handle_resets: bool = True
) -> pd.DataFrame
Counter quantity and resets per group (shift, day, operator, ...).
Computing :func:counter_quantity separately inside each group loses
the production between the last reading of one group and the first
reading of the next: every group boundary drops one increment. Here
the reset-aware increments are computed once over the whole signal and
each one is booked to the group of the reading that reports it, so the
group quantities add up to the signal's total.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Series
|
Counter readings of one signal, ordered in time. |
required |
keys
|
Series | list[Series]
|
Group label(s) per reading, index-aligned with |
required |
handle_resets
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame indexed by the group keys with |
DataFrame
|
|
count_resets ¤
count_resets(values: Series) -> int
Number of resets (strictly decreasing steps) in a counter series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Series
|
Counter readings, ordered in time. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The count of readings that are lower than their predecessor. |
counter_per_window ¤
counter_per_window(
values: Series,
window: str,
*,
handle_resets: bool = True
) -> pd.Series
Counter quantity per time window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Series
|
Counter readings with a |
required |
window
|
str
|
Resample frequency (e.g. |
required |
handle_resets
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
Series
|
A Series indexed by window start, clipped at |
Series
|
|