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

align

select

Align

no

Put every stream on one regular time grid.

combine

select

Combine

no

Build a new stream from several, e.g. two redundant meters.

expression

clean

Expression

no

Reject values wherever a user-written condition holds.

hampel

clean

Hampel

no

Reject spikes with a Hampel filter: a rolling median plus a robust spread.

interpolate

impute

Interpolate

no

Fill short interior gaps by interpolating between the valid values either side.

mask

select

Mask

no

Keep data only where a condition holds; exclude the rest.

range

clean

Range

no

Reject values outside fixed limits, e.g. a negative flow or an error code.

rate_of_change

clean

RateOfChange

no

Reject values that change faster than the process physically can.

rename

select

Rename

no

Rename streams. Metadata and flags move with them.

resample

select

Resample

no

Aggregate every stream into fixed time bins.

shift

select

Shift

no

Shift streams in time, e.g. to undo a known transport delay.

stuck

clean

Stuck

no

Reject flat-lined runs, the signature of a frozen sensor or a held last value.

tukey

clean

Tukey

yes

Reject values beyond Tukey’s fences, learned from the training data.

window

select

Window

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 by method, and is flagged RESAMPLED. With no valid sample within tolerance, the point is NaN and flagged MISSING. The grid is anchored at multiples of freq from 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

kind

'align'

'align'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

freq

str

required

Grid spacing, e.g. 1min.

method

'nearest' | 'previous' | 'linear'

'nearest'

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.

tolerance

str | None

None

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).

how

'outer' | 'inner'

'outer'

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 freq from the Unix epoch. A bin’s flags are the OR of its samples’ flags, plus RESAMPLED when it has a value. A bin with no samples at all is flagged MISSING.

__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

kind

'resample'

'resample'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

freq

str

required

Bin width, e.g. 15min or 1h.

agg

'mean' | 'median' | 'min' | 'max' | 'sum' | 'first' | 'last'

'mean'

How to aggregate the valid samples in a bin.

min_count

int

1

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

kind

'window'

'window'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

start

str | None

None

First time kept (inclusive). Naive means UTC.

end

str | None

None

End of the window (exclusive). Naive means UTC.

last

str | None

None

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 flagged EXCLUDED, which marks them as deliberately left out, not as bad data. Imputers never fill them. With drop_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

kind

'mask'

'mask'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

keep_when

str

required

Condition over stream names, e.g. ‘pump_on == 1 and flow > 0’.

streams

list[str] | None

None

Streams to exclude values from (default: all).

drop_rows

bool

False

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

kind

'rename'

'rename'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

mapping

dict[str, str]

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. For coalesce, 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

kind

'combine'

'combine'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

output

str

required

Name of the new stream.

streams

list[str]

required

The streams to combine.

how

'coalesce' | 'mean' | 'sum' | 'min' | 'max'

'coalesce'

How to combine the values at each time.

units

str | None

None

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 by moves values later. It needs a regular index, and by must be a whole number of steps. Grid points left without a value are flagged MISSING.

__init__(streams: str | Sequence[str] | None = None, /, **data: Any)

Accept streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)

field

type

default

description

kind

'shift'

'shift'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

streams

list[str]

required

The streams this step acts on.

by

str

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 streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)

field

type

default

description

kind

'range'

'range'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

streams

list[str]

required

The streams this step acts on.

min

float | Quantity | None

None

Smallest allowed value, as a number or {value, units}.

max

float | Quantity | None

None

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 time exceeds max_rate per per, the value is flagged RATE_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 streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)

field

type

default

description

kind

'rate_of_change'

'rate_of_change'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

streams

list[str]

required

The streams this step acts on.

max_rate

float

required

Largest allowed change per per.

per

str

'1h'

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 lasting min_duration or longer is flagged STUCK. Runs at a value listed in exclude_values are 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 streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)

field

type

default

description

kind

'stuck'

'stuck'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

streams

list[str]

required

The streams this step acts on.

min_duration

str

required

Shortest flat run that counts as stuck, e.g. 1h.

tolerance

float

0.0

Largest change between samples that still counts as flat.

exclude_values

list[float]

()

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 OUTLIER if it lies more than n_sigma robust 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, so n_sigma is 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_scale to 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 streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)

field

type

default

description

kind

'hampel'

'hampel'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

streams

list[str]

required

The streams this step acts on.

window

str

required

Width of the centred window, e.g. 1h.

n_sigma

float

3.0

How many robust standard deviations count as an outlier.

min_periods

int

3

Fewest valid samples a window needs before it can flag.

min_scale

float

0.0

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.

fit records each stream’s quartiles Q1 and Q3. Values below Q1 - k·IQR or above Q3 + k·IQR are flagged OUTLIER, 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 streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)

field

type

default

description

kind

'tukey'

'tukey'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

streams

list[str]

required

The streams this step acts on.

k

float

1.5

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 with flag, MANUAL by default.

__init__(streams: str | Sequence[str] | None = None, /, **data: Any)

Accept streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)

field

type

default

description

kind

'expression'

'expression'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

streams

list[str]

required

The streams this step acts on.

invalid_when

str

required

Condition over stream names, e.g. ‘pressure < 0’.

flag

'MANUAL' | 'OUT_OF_RANGE' | 'RATE_OF_CHANGE' | 'OUTLIER' | 'STUCK'

'MANUAL'

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_gap are 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 flagged EXCLUDED or NO_SAMPLE, are never filled. Filled values keep the flags that explain why they were missing, and gain IMPUTED_SHORT.

Methods use numpy and scipy:

__init__(streams: str | Sequence[str] | None = None, /, **data: Any)

Accept streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)

field

type

default

description

kind

'interpolate'

'interpolate'

name

str | None

None

Optional label for this step, shown in provenance and reports. Must be unique within a spec.

streams

list[str]

required

The streams this step acts on.

max_gap

str

required

Longest gap to fill, measured between the valid samples either side, e.g. 15min.

method

'linear' | 'nearest' | 'previous' | 'pchip' | 'akima'

'linear'

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 streams positionally as well as by keyword.

classmethod __new__(*args, **kwargs)