Synthetic test data¶
Seeded, plant-like synthetic data with known injected faults.
This is flex-signal’s main source of test, example and benchmark data (PLAN.md §8, “Test data”). Every case keeps three things:
the underlying noise-free signal (
underlying);the fault-free measurement (
clean: the signal plus sensor noise);the observed data with faults injected (
observed), together with an exact boolean mask of where each fault was injected.
Because the ground truth is known, tests can assert that a cleaner flags exactly the injected faults, and that an imputer recovers values to a known accuracy.
Example
>>> from flexsignal.testing import synthetic
>>> case = synthetic.plant(seed=1).with_spikes("flow", count=5)
>>> int(case.fault_mask("spike", "flow").sum())
5
- flexsignal.testing.synthetic.plant(*, start: str = '2025-01-06', periods: int = 2016, freq: str = '5min', seed: int = 0, pump_off_periods: int = 0, reference_hours: tuple[int, int] = (9, 11), noise: Mapping[str, float] | None = None) SyntheticCase¶
Generate a plant-like dataset with no faults yet.
The streams:
flow(m^3/hr): around 100, with a daily cycle (±30, peaking in the afternoon) and a weekly cycle (±5). It is 0 whenever the pump is off.pressure(bar): 4 + 0.01 × flow, so it correlates with flow.temperature(degC): 15, with a daily cycle of ±3.pump_on(0/1): 1, except inpump_off_periodsrandom blocks of 2–6 h.reference_flow(m^3/hr): a trusted, precise meter for the same flow. It reports only betweenreference_hourseach day, and is NaN otherwise. M2’scalibrateuses it.
- Parameters:
start – First timestamp (UTC).
periods – Number of samples.
freq – Sample spacing.
seed – Seed for the noise, pump-off blocks and every later fault injection.
pump_off_periods – How many pump-off blocks to add.
reference_hours –
(first_hour, end_hour), in UTC, of the reference meter’s daily reporting window.noise – Override the sensor-noise standard deviation per stream.
- Returns:
A
SyntheticCasewithobserved == clean.
- class flexsignal.testing.synthetic.SyntheticCase¶
A synthetic dataset with known faults;
with_*methods return a new case.- faults: dict[str, DataFrame]¶
Fault kind → boolean frame that is True where that fault was injected.
- signal() SignalFrame¶
Return the observed data as a SignalFrame, with units.
- fault_mask(kind: str, stream: str | None = None) DataFrame | Series¶
Return where a fault kind was injected.
- Parameters:
kind – e.g.
"spike","stuck","gap","out_of_range".stream – One stream, or None for all streams.
- Returns:
A boolean Series (one stream) or DataFrame. All False if the fault was never injected.
- __init__(underlying: ~pandas.DataFrame, clean: ~pandas.DataFrame, observed: ~pandas.DataFrame, units: dict[str, str | None], noise: dict[str, float], seed: int, faults: dict[str, ~pandas.DataFrame] = <factory>) None¶
- with_spikes(stream: str, *, count: int = 5, size: float = 40.0, spacing: int = 30) SyntheticCase¶
Add isolated one-sample spikes,
sizenoise standard deviations high.Spikes alternate up and down, and are at least
spacingsamples from each other and from earlier faults. Fault kind:"spike".
- with_stuck(stream: str, *, count: int = 1, length: int = 36, spacing: int = 30) SyntheticCase¶
Freeze the sensor: hold the first value of a run for
lengthsamples.Fault kind:
"stuck". The mask covers the whole run, including its first sample, which keeps its true value but is part of the flat line.
- with_gaps(stream: str, *, count: int = 3, length: int = 2, spacing: int = 30) SyntheticCase¶
Remove
countruns oflengthconsecutive samples (set them to NaN).Fault kind:
"gap".
- with_out_of_range(stream: str, *, count: int = 5, value: float = -999.0, spacing: int = 30) SyntheticCase¶
Replace
countsamples with a sentinel such as an error code.Fault kind:
"out_of_range".
- with_drift(stream: str, *, start: int, rate_per_day: float) SyntheticCase¶
Add a linear calibration drift from sample
startonward.Fault kind:
"drift". The mask covers every drifted sample.
- with_step(stream: str, *, start: int, size: float) SyntheticCase¶
Add a constant offset from sample
starton, like a bad recalibration.Fault kind:
"step".
- flexsignal.testing.synthetic.STREAMS: dict[str, dict[str, object]]¶
Streams
plant()generates, with units and sensor-noise standard deviation.
Example steps¶
Ready-made example steps, for tests and docs.
The spec tests check that one_of_every_step() covers every registered step
kind, so adding a step kind means adding an example here.