Steps¶
Pipeline steps, grouped by what they do.
select: put streams on a grid, choose windows and rows, reshape.clean: find bad values, set them to NaN, and flag why.impute: fill missing values and flag them as filled.
Every step is a frozen pydantic model, so the object you build in Python is exactly what a JSON spec loads into. Importing this package registers the built-in steps. Later milestones add correct, forecast and derive steps.
kind |
group |
class |
learns state |
summary |
|---|---|---|---|---|
|
select |
no |
Put every stream on one regular time grid. |
|
|
select |
no |
Build a new stream from several, e.g. two redundant meters. |
|
|
clean |
no |
Reject values wherever a user-written condition holds. |
|
|
clean |
no |
Reject spikes with a Hampel filter: a rolling median plus a robust spread. |
|
|
impute |
no |
Fill short interior gaps by interpolating between the valid values either side. |
|
|
select |
no |
Keep data only where a condition holds; exclude the rest. |
|
|
clean |
no |
Reject values outside fixed limits, e.g. a negative flow or an error code. |
|
|
clean |
no |
Reject values that change faster than the process physically can. |
|
|
select |
no |
Rename streams. Metadata and flags move with them. |
|
|
select |
no |
Aggregate every stream into fixed time bins. |
|
|
select |
no |
Shift streams in time, e.g. to undo a known transport delay. |
|
|
clean |
no |
Reject flat-lined runs, the signature of a frozen sensor or a held last value. |
|
|
clean |
yes |
Reject values beyond Tukey’s fences, learned from the training data. |
|
|
select |
no |
Keep only the rows inside a time window. |
Select¶
Selection steps: put streams on a common grid, choose time windows and rows, reshape.
None of these steps judges data quality. They move, aggregate, rename or exclude values, and record what they did in the flags.
- class flexsignal.steps.select.Align¶
Put every stream on one regular time grid.
Each grid point takes the stream’s sample at exactly that time if there is one, even if that sample is missing. (Rows flagged
NO_SAMPLE, which a join of several inputs adds, aren’t samples.) Otherwise it takes a value from nearby valid samples, chosen bymethod, and is flaggedRESAMPLED. With no valid sample withintolerance, the point is NaN and flaggedMISSING. The grid is anchored at multiples offreqfrom the Unix epoch, so the same data always gives the same grid.- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
Grid spacing, e.g. 1min. |
|
|
|
nearest: the closest valid sample; previous: the last valid sample at or before the grid time; linear: time-weighted between the valid samples either side. |
|
|
|
nearest/previous: how far away a sample may be (default: freq). linear: the largest allowed gap between the two samples either side (default: 2 x freq). |
|
|
|
outer: the grid spans every stream’s data; inner: only the time all streams cover. |
- class flexsignal.steps.select.Resample¶
Aggregate every stream into fixed time bins.
Bins are left-closed and labelled by their start, and anchored at multiples of
freqfrom the Unix epoch. A bin’s flags are the OR of its samples’ flags, plusRESAMPLEDwhen it has a value. A bin with no samples at all is flaggedMISSING.- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
Bin width, e.g. 15min or 1h. |
|
|
|
How to aggregate the valid samples in a bin. |
|
|
|
Fewest valid samples a bin needs to get a value. |
- class flexsignal.steps.select.Window¶
Keep only the rows inside a time window.
- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
|
First time kept (inclusive). Naive means UTC. |
|
|
|
End of the window (exclusive). Naive means UTC. |
|
|
|
Keep only this much time before the last timestamp, e.g. 7D. Can’t be combined with start or end. |
- class flexsignal.steps.select.Mask¶
Keep data only where a condition holds; exclude the rest.
Use this to select operating periods, e.g.
keep_when="pump_on == 1". Excluded values become NaN and are flaggedEXCLUDED, which marks them as deliberately left out, not as bad data. Imputers never fill them. Withdrop_rows, the rows are removed instead.- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
Condition over stream names, e.g. ‘pump_on == 1 and flow > 0’. |
|
|
|
Streams to exclude values from (default: all). |
|
|
|
Remove rows where the condition fails, instead of excluding values. |
- class flexsignal.steps.select.Rename¶
Rename streams. Metadata and flags move with them.
- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
Old name -> new name, e.g. {FT_101: flow}. |
- class flexsignal.steps.select.Combine¶
Build a new stream from several, e.g. two redundant meters.
The result is flagged
DERIVED. Forcoalesce, each sample takes the first stream (in order) that has a value, along with that stream’s flags. For the other methods, the result’s flags are the OR of every input’s flags.- __init__(**data: Any) None¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
Name of the new stream. |
|
|
required |
The streams to combine. |
|
|
|
How to combine the values at each time. |
|
|
|
Units of the result. Required if the inputs’ units differ; defaults to their common units. |
- class flexsignal.steps.select.Shift¶
Shift streams in time, e.g. to undo a known transport delay.
A positive
bymoves values later. It needs a regular index, andbymust be a whole number of steps. Grid points left without a value are flaggedMISSING.- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
The streams this step acts on. |
|
|
required |
How far to move the values, e.g. 5min or -5min. |
Clean¶
Cleaning steps: find bad values, set them to NaN, and flag why.
A cleaner never deletes a row or changes a good value. It sets each value it
rejects to NaN and adds its flag (OUT_OF_RANGE, RATE_OF_CHANGE,
OUTLIER, STUCK or MANUAL). Values that are already missing are left
alone.
- class flexsignal.steps.clean.Range¶
Reject values outside fixed limits, e.g. a negative flow or an error code.
Flags rejected values
OUT_OF_RANGE. The limits themselves are allowed.- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
The streams this step acts on. |
|
|
|
Smallest allowed value, as a number or {value, units}. |
|
|
|
Largest allowed value, as a number or {value, units}. |
- class flexsignal.steps.clean.RateOfChange¶
Reject values that change faster than the process physically can.
Each value is compared with the last accepted value. If
|change| / elapsed timeexceedsmax_rateperper, the value is flaggedRATE_OF_CHANGE. A spike is caught without flagging the point after it, because that point is compared with the last good value before the spike. A genuine step change is accepted once enough time has passed for the allowed rate to cover it.- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
The streams this step acts on. |
|
|
required |
Largest allowed change per per. |
|
|
|
Time unit of max_rate, e.g. 1h or 1min. |
- class flexsignal.steps.clean.Stuck¶
Reject flat-lined runs, the signature of a frozen sensor or a held last value.
A run is consecutive valid samples whose values differ by at most
tolerance. Missing samples in between don’t break a run. Every sample of a run lastingmin_durationor longer is flaggedSTUCK. Runs at a value listed inexclude_valuesare left alone; this is useful for a legitimate constant such as 0 when a pump is off.- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
The streams this step acts on. |
|
|
required |
Shortest flat run that counts as stuck, e.g. 1h. |
|
|
|
Largest change between samples that still counts as flat. |
|
|
|
Values at which a flat run is legitimate, e.g. [0]. |
- class flexsignal.steps.clean.Hampel¶
Reject spikes with a Hampel filter: a rolling median plus a robust spread.
Each value is compared with the median of a centred time window. It is flagged
OUTLIERif it lies more thann_sigmarobust standard deviations from that median. The robust standard deviation is 1.4826 × the window’s median absolute deviation; 1.4826 is the fixed factor that makes this match the standard deviation for normal data, son_sigmais the only threshold to tune. See Pearson, R. K. (2002), “Outliers in process modeling and identification”, IEEE Trans. Control Systems Technology 10(1), 55-63, doi:10.1109/87.974338.The window should be several times wider than the spikes being removed, and narrow enough that the signal’s own trend changes little across it.
A spread estimated from a short window can come out much smaller than the real noise, and then ordinary noise gets flagged. This happens most where the signal is flat. Set
min_scaleto the sensor’s noise standard deviation, or its resolution, to put a floor under the spread.- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
The streams this step acts on. |
|
|
required |
Width of the centred window, e.g. 1h. |
|
|
|
How many robust standard deviations count as an outlier. |
|
|
|
Fewest valid samples a window needs before it can flag. |
|
|
|
Floor on the robust standard deviation, in the stream’s units; typically the sensor’s noise level. |
- class flexsignal.steps.clean.Tukey¶
Reject values beyond Tukey’s fences, learned from the training data.
fitrecords each stream’s quartiles Q1 and Q3. Values belowQ1 - k·IQRor aboveQ3 + k·IQRare flaggedOUTLIER, where IQR = Q3 - Q1. Use k = 1.5 for “outliers” and k = 3 for “far out” values (Tukey, J. W. (1977), Exploratory Data Analysis, Addison-Wesley).This suits streams whose normal range doesn’t drift. For a signal with a strong trend or regimes, prefer
hampel.- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
The streams this step acts on. |
|
|
|
Fence width in multiples of the IQR. |
- class flexsignal.steps.clean.Expression¶
Reject values wherever a user-written condition holds.
For example
invalid_when="pressure < 0 or pump_on == 0". Flags the rejected values withflag,MANUALby default.- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
The streams this step acts on. |
|
|
required |
Condition over stream names, e.g. ‘pressure < 0’. |
|
|
|
Which flag to set on rejected values. |
Impute¶
Imputation steps: fill missing values and flag them as filled.
M1 has gap-length-aware interpolation. Model-based imputers (Kalman, regression, PyPOTS) and the tiered imputer arrive in M2 and M5.
- class flexsignal.steps.impute.Interpolate¶
Fill short interior gaps by interpolating between the valid values either side.
A gap is a run of missing values. Its length is the time between the valid sample before it and the valid sample after it. Only gaps no longer than
max_gapare filled: on 5-minute data,max_gap="15min"fills up to two missing samples in a row. Gaps at the start or end of the data, and values flaggedEXCLUDEDorNO_SAMPLE, are never filled. Filled values keep the flags that explain why they were missing, and gainIMPUTED_SHORT.Methods use numpy and scipy:
linear:numpy.interp();nearestandprevious:scipy.interpolate.interp1d;pchip:scipy.interpolate.PchipInterpolator, shape-preserving with no overshoot;
- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶
field |
type |
default |
description |
|---|---|---|---|
|
|
|
|
|
|
|
Optional label for this step, shown in provenance and reports. Must be unique within a spec. |
|
|
required |
The streams this step acts on. |
|
|
required |
Longest gap to fill, measured between the valid samples either side, e.g. 15min. |
|
|
|
Interpolation method. |
Writing a step¶
- class flexsignal.steps.StreamStep¶
A step that acts on named streams.
The streams may be given positionally, as in
Hampel("flow", window="1h").- __init__(streams: str | Sequence[str] | None = None, /, **data: Any)¶
Accept
streamspositionally as well as by keyword.
- classmethod __new__(*args, **kwargs)¶