SignalFrame and quality flags¶
One container, plain pandas inside¶
Every step takes and returns a SignalFrame. It isn’t a new timeseries library. It’s
four plain parts:
Part |
What it is |
|---|---|
|
A pandas DataFrame, one float64 column per stream, indexed by time |
|
A DataFrame of the same shape: a |
|
Per-stream |
|
One provenance record per step that produced the frame |
sf.values and sf.flags give you ordinary DataFrames, so any pandas, scipy or scikit-learn call works on them
directly. The frame itself is immutable: every step returns a new one.
The time index is always timezone-aware UTC, sorted, unique, and nanosecond resolution. A naive index is taken as this machine’s local time, with a warning, unless you say which time zone it is in (see Time and units).
import flexsignal as fs
from flexsignal.testing import synthetic
sf = synthetic.plant(seed=0).with_gaps("flow", count=2).signal()
sf.values.head() # a plain DataFrame
sf.units["flow"] # 'm^3/hr'
sf.flag_counts()["flow"] # {'rows': 2016, 'valid': 2012, 'flags': {'MISSING': 4}}
Cleaning never deletes data¶
A cleaner doesn’t drop rows or overwrite values with guesses. When it rejects a value, it does two things:
sets the value to NaN;
sets a flag bit saying why, for example
OUTLIERorSTUCK.
An imputer later fills the NaN, keeps the bits that explain why it was missing, and adds IMPUTED_SHORT. A
downstream consumer can always tell:
which values are raw measurements (flags
0);which were removed, and by which rule;
which were filled in, and how.
from flexsignal.core import Flag, flag_names
flag_names(int(Flag.OUTLIER | Flag.IMPUTED_SHORT)) # ['OUTLIER', 'IMPUTED_SHORT']
imputed = sf.has_flag(Flag.IMPUTED_SHORT) # boolean DataFrame
The flags¶
flag |
bit value |
meaning |
set by |
|---|---|---|---|
|
1 |
No value: missing (NaN) in the input, or no sample existed at that time. |
input data, align, resample, shift |
|
2 |
The value was outside its allowed range. |
range |
|
4 |
The value changed faster than physically allowed. |
rate_of_change |
|
8 |
The value was a statistical outlier. |
hampel, tukey |
|
16 |
The value was part of a flat-lined (stuck sensor) run. |
stuck |
|
32 |
A user-written rule marked the value invalid. |
expression |
|
64 |
Deliberately left out by a selection rule; not bad data, and never filled. |
mask |
|
128 |
The value was corrected by a model. |
calibrate (M2) |
|
256 |
A short gap was filled by interpolation. |
interpolate |
|
512 |
A gap was filled by a model. |
model imputers (M2) |
|
1024 |
The value is a forecast, not a measurement. |
forecast (M4) |
|
2048 |
The stream was computed from other streams. |
combine |
|
4096 |
The value was aggregated or moved onto a new time grid. |
align, resample |
|
8192 |
The stream has no sample at this time; the row exists only because another input does. Always set with MISSING. Align, resample and interpolate skip these rows. |
joining several inputs |
EXCLUDED is deliberately different from the quality flags. It marks data a selection rule left out, such as
“only while the pump runs”. That isn’t bad data, and imputers never fill it.
Provenance¶
When a Pipeline runs, it compares each step’s input with its output and appends a record to
the frame’s history:
the step’s index, kind and name;
the spec hash;
the row counts before and after;
streams added and removed;
how many samples changed in each stream.
Steps don’t write these records themselves, so the record is always exact.