import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20json%0A%0A%20%20%20%20%23%20marimo%20(%60mo%60)%20is%20only%20for%20the%20notebook%3A%20prose%2C%20layout%20and%20interactive%0A%20%20%20%20%23%20controls.%20Lines%20marked%20%22Display%20only%22%20or%20%22Notebook%20control%22%20are%20not%0A%20%20%20%20%23%20flex-signal%3B%20in%20your%20own%20script%2C%20print%20the%20result%20or%20leave%20them%20out.%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20flexsignal%20as%20fs%0A%20%20%20%20from%20flexsignal.core%20import%20Flag%0A%20%20%20%20from%20flexsignal.testing%20import%20synthetic%0A%0A%20%20%20%20return%20Flag%2C%20fs%2C%20json%2C%20mo%2C%20np%2C%20pd%2C%20plt%2C%20synthetic%0A%0A%0A%40app.cell%0Adef%20_(plt)%3A%0A%20%20%20%20%23%20One%20palette%20and%20axis%20style%20for%20every%20tutorial%2C%20so%20the%20plots%20read%20alike.%0A%20%20%20%20COLORS%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22signal%22%3A%20%22%232b6cb0%22%2C%0A%20%20%20%20%20%20%20%20%22truth%22%3A%20%22%23a0aec0%22%2C%0A%20%20%20%20%20%20%20%20%22fault%22%3A%20%22%23dd6b20%22%2C%0A%20%20%20%20%20%20%20%20%22filled%22%3A%20%22%232f855a%22%2C%0A%20%20%20%20%20%20%20%20%22excluded%22%3A%20%22%23edf2f7%22%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20def%20style(ax%2C%20ylabel%3DNone)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Light%20grid%2C%20no%20box%2C%20muted%20ticks.%22%22%22%0A%20%20%20%20%20%20%20%20ax.grid(axis%3D%22y%22%2C%20color%3D%22%23e2e8f0%22%2C%20linewidth%3D0.8)%0A%20%20%20%20%20%20%20%20ax.set_axisbelow(True)%0A%20%20%20%20%20%20%20%20for%20side%20in%20(%22top%22%2C%20%22right%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ax.spines%5Bside%5D.set_visible(False)%0A%20%20%20%20%20%20%20%20for%20side%20in%20(%22left%22%2C%20%22bottom%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ax.spines%5Bside%5D.set_color(%22%23a0aec0%22)%0A%20%20%20%20%20%20%20%20ax.tick_params(colors%3D%22%234a5568%22%2C%20labelsize%3D9)%0A%20%20%20%20%20%20%20%20if%20ylabel%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ax.set_ylabel(ylabel%2C%20color%3D%22%234a5568%22)%0A%20%20%20%20%20%20%20%20return%20ax%0A%0A%20%20%20%20plt.rcParams%5B%22figure.dpi%22%5D%20%3D%20110%0A%20%20%20%20return%20COLORS%2C%20style%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%204.%20Several%20streams%3A%20aligning%2C%20masking%20and%20conditional%20rules%0A%0A%20%20%20%20Real%20cleaning%20rarely%20involves%20one%20stream%20on%20its%20own.%20Data%20comes%20from%0A%20%20%20%20several%20systems%20with%20different%20clocks%2C%20some%20periods%20shouldn't%20be%20judged%0A%20%20%20%20at%20all%2C%20and%20many%20rules%20involve%20more%20than%20one%20stream%3A%20%22this%20reading%20is%0A%20%20%20%20wrong%20*when*%20the%20pump%20is%20running%20and%20the%20pressure%20disagrees%20with%20the%0A%20%20%20%20flow%22.%0A%0A%20%20%20%20This%20tutorial%20works%20through%20those%20cases%20on%20two%20sources%3A%0A%0A%20%20%20%20-%20**SCADA**%3A%20%60flow%60%2C%20%60pressure%60%20and%20%60pump_on%60%2C%20every%205%20minutes.%20The%20pump%0A%20%20%20%20%20%20stops%20twice%20during%20the%20week.%0A%20%20%20%20-%20**A%20reference%20meter**%3A%20a%20precise%20flow%20meter%20downstream%2C%20with%20its%20own%0A%20%20%20%20%20%20logger.%20Water%20takes%20**10%20minutes**%20to%20reach%20it%2C%20and%20its%20logger%20stamps%0A%20%20%20%20%20%20readings%20**2%20minutes%20past**%20each%205-minute%20mark.%20It%20only%20reports%20for%20two%0A%20%20%20%20%20%20hours%20a%20day.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pd%2C%20synthetic)%3A%0A%20%20%20%20case%20%3D%20(%0A%20%20%20%20%20%20%20%20synthetic.plant(seed%3D3%2C%20pump_off_periods%3D2)%0A%20%20%20%20%20%20%20%20.with_spikes(%22flow%22%2C%20count%3D4)%0A%20%20%20%20%20%20%20%20.with_out_of_range(%22pressure%22%2C%20count%3D3%2C%20value%3D3.0)%20%20%23%20plausible%2C%20but%20wrong%0A%20%20%20%20)%0A%20%20%20%20scada%20%3D%20case.observed%5B%5B%22flow%22%2C%20%22pressure%22%2C%20%22pump_on%22%5D%5D%0A%20%20%20%20reference%20%3D%20case.observed%5B%5B%22reference_flow%22%5D%5D.dropna()%0A%20%20%20%20%23%2010%20minutes%20of%20transport%20delay%2C%20plus%20the%20logger's%202-minute%20offset.%0A%20%20%20%20reference%20%3D%20reference.set_axis(reference.index%20%2B%20pd.Timedelta(%2212min%22))%0A%20%20%20%20return%20case%2C%20reference%2C%20scada%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20reference%2C%20scada)%3A%0A%20%20%20%20%23%20Display%20only%20(marimo)%3A%20show%20these%20outputs%20side%20by%20side.%0A%20%20%20%20mo.hstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22**SCADA**%22)%2C%20scada.head(4)%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22**Reference%20meter**%22)%2C%20reference.head(4)%5D)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20widths%3D%22equal%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Two%20named%20inputs%0A%0A%20%20%20%20A%20spec%20can%20declare%20**input%20slots**.%20The%20caller%20fills%20each%20slot%20by%20name%2C%0A%20%20%20%20and%20flex-signal%20joins%20them%20on%20time.%20A%20joined%20row%20where%20a%20stream%20had%20no%0A%20%20%20%20sample%20is%20flagged%20%60MISSING%60%20and%20%60NO_SAMPLE%60%3A%20the%20row%20exists%20only%20because%0A%20%20%20%20the%20other%20source%20has%20a%20sample%20at%20that%20time.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20reference%2C%20scada)%3A%0A%20%20%20%20INPUTS%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%22kind%22%3A%20%22frame%22%2C%20%22name%22%3A%20%22scada%22%2C%20%22description%22%3A%20%225-minute%20SCADA%20export%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22kind%22%3A%20%22frame%22%2C%20%22name%22%3A%20%22reference%22%2C%20%22description%22%3A%20%22reference%20meter%20log%22%7D%2C%0A%20%20%20%20%5D%0A%20%20%20%20DATA%20%3D%20%7B%22scada%22%3A%20scada%2C%20%22reference%22%3A%20reference%7D%0A%0A%20%20%20%20def%20run(*steps)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Run%20steps%20on%20both%20inputs%20and%20return%20the%20SignalFrame.%22%22%22%0A%20%20%20%20%20%20%20%20return%20fs.run(%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.Pipeline(list(steps)%2C%20name%3D%22demo%22%2C%20inputs%3DINPUTS).spec%2C%20DATA%0A%20%20%20%20%20%20%20%20).signal%0A%0A%20%20%20%20joined%20%3D%20run()%0A%20%20%20%20joined.to_pandas(include_flags%3DTrue).loc%5B%222025-01-06%2009%3A00%22%3A%222025-01-06%2009%3A25%22%5D%0A%20%20%20%20return%20DATA%2C%20INPUTS%2C%20joined%2C%20run%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Step%201%3A%20%60Align%60%20puts%20every%20stream%20on%20one%20grid%0A%0A%20%20%20%20%60Align%60%20builds%20a%20regular%20grid%20and%20gives%20each%20stream%20a%20value%20at%20each%20grid%0A%20%20%20%20point.%20It%20keeps%20a%20sample%20that%20is%20exactly%20on%20the%20grid%2C%20and%20otherwise%20takes%0A%20%20%20%20the%20nearest%20sample%20within%20%60tolerance%60.%20Moved%20values%20are%20flagged%0A%20%20%20%20%60RESAMPLED%60.%0A%0A%20%20%20%20%60tolerance%60%20sets%20how%20far%20a%20sample%20may%20be%20moved.%20The%20reference%20meter's%0A%20%20%20%20readings%20are%202%20minutes%20off%20the%20grid%2C%20so%20%60tolerance%3D%222min%22%60%20places%20each%0A%20%20%20%20one%20at%20exactly%20one%20grid%20point.%20With%20a%20looser%20tolerance%2C%20a%20reading%20at%20the%0A%20%20%20%20edge%20of%20its%20reporting%20window%20could%20also%20be%20copied%20to%20the%20next%20grid%20point.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20mo%2C%20reference%2C%20run)%3A%0A%20%20%20%20aligned%20%3D%20run(fs.select.Align(freq%3D%225min%22%2C%20tolerance%3D%222min%22))%0A%20%20%20%20_counts%20%3D%20aligned.flag_counts()%5B%22reference_flow%22%5D%0A%20%20%20%20%23%20Display%20only%20(marimo)%3A%20render%20the%20result%20as%20formatted%20text.%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22The%20grid%20has%20**%7Blen(aligned)%7D**%20points.%20The%20reference%20meter's%20%22%0A%20%20%20%20%20%20%20%20f%22**%7Blen(reference)%7D**%20readings%20land%20on%20**%7B_counts%5B'valid'%5D%7D**%20of%20them%2C%20each%20%22%0A%20%20%20%20%20%20%20%20f%22flagged%20%60RESAMPLED%60%3B%20its%20other%20points%20are%20%60MISSING%60.%22%0A%20%20%20%20)%0A%20%20%20%20return%20(aligned%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Step%202%3A%20find%20the%20delay%2C%20then%20%60Shift%60%20it%20out%0A%0A%20%20%20%20The%20reference%20meter%20sees%20the%20same%20water%2010%20minutes%20later.%20In%20practice%0A%20%20%20%20you'd%20have%20to%20find%20that%20delay%2C%20and%20the%20data%20can%20tell%20you.%20Slide%20the%0A%20%20%20%20reference%20back%20by%20different%20amounts%20and%20compare%20it%20with%20the%20SCADA%20flow%3A%0A%20%20%20%20the%20difference%20is%20smallest%20at%20the%20right%20delay.%20Clean%20the%20spikes%20first%20so%0A%20%20%20%20they%20don't%20dominate%20the%20comparison.%0A%0A%20%20%20%20The%20minimum%20is%20shallow%2C%20because%20flow%20changes%20slowly%3A%2010%20minutes%20of%20delay%0A%20%20%20%20shifts%20it%20by%20only%20about%201%20m%C2%B3%2Fh%2C%20the%20same%20as%20the%20meter%20noise.%20A%20week%20of%0A%20%20%20%20data%20is%20enough%20to%20see%20it%2C%20but%20check%20the%20answer%20is%20physically%20sensible%2C%0A%20%20%20%20such%20as%20the%20pipe%20volume%20divided%20by%20the%20flow%2C%20before%20relying%20on%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20np%2C%20pd%2C%20run)%3A%0A%20%20%20%20_clean%20%3D%20run(%0A%20%20%20%20%20%20%20%20fs.select.Align(freq%3D%225min%22%2C%20tolerance%3D%222min%22)%2C%0A%20%20%20%20%20%20%20%20fs.clean.Hampel(%22flow%22%2C%20window%3D%221h%22%2C%20n_sigma%3D6%2C%20min_scale%3D1.0)%2C%0A%20%20%20%20).values%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_minutes%20in%20range(-20%2C%2025%2C%205)%3A%0A%20%20%20%20%20%20%20%20_diff%20%3D%20_clean%5B%22flow%22%5D%20-%20_clean%5B%22reference_flow%22%5D.shift(-_minutes%20%2F%2F%205)%0A%20%20%20%20%20%20%20%20_rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22delay%20%5Bmin%5D%22%3A%20_minutes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22RMS%20difference%22%3A%20float(np.sqrt(np.nanmean(_diff**2)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20lag_scan%20%3D%20pd.DataFrame(_rows)%0A%20%20%20%20best_delay%20%3D%20int(lag_scan.loc%5Blag_scan%5B%22RMS%20difference%22%5D.idxmin()%2C%20%22delay%20%5Bmin%5D%22%5D)%0A%20%20%20%20return%20best_delay%2C%20lag_scan%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20best_delay%2C%20lag_scan%2C%20plt%2C%20style)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6%2C%202.6))%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20lag_scan%5B%22delay%20%5Bmin%5D%22%5D%2C%0A%20%20%20%20%20%20%20%20lag_scan%5B%22RMS%20difference%22%5D%2C%0A%20%20%20%20%20%20%20%20%22o-%22%2C%0A%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22signal%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.axvline(best_delay%2C%20color%3DCOLORS%5B%22fault%22%5D%2C%20ls%3D%22--%22%2C%20lw%3D1)%0A%20%20%20%20_ax.text(%0A%20%20%20%20%20%20%20%20best_delay%20%2B%201%2C%0A%20%20%20%20%20%20%20%20lag_scan%5B%22RMS%20difference%22%5D.max()%20*%200.9%2C%0A%20%20%20%20%20%20%20%20f%22%7Bbest_delay%7D%20min%22%2C%0A%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22fault%22%5D%2C%0A%20%20%20%20%20%20%20%20fontsize%3D9%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.set_xlabel(%22assumed%20delay%20of%20the%20reference%20meter%20%5Bmin%5D%22%2C%20color%3D%22%234a5568%22)%0A%20%20%20%20style(_ax%2C%20%22RMS%20difference%20%5Bm%C2%B3%2Fh%5D%22)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60Shift(%22reference_flow%22%2C%20by%3D%22-10min%22)%60%20moves%20the%20reference%20readings%2010%0A%20%20%20%20minutes%20earlier%2C%20back%20to%20when%20that%20water%20passed%20the%20pump.%20Shift%20needs%20a%0A%20%20%20%20regular%20grid%2C%20so%20it%20comes%20after%20%60Align%60.%0A%0A%20%20%20%20%23%23%20Step%203%3A%20%60Mask%60%20the%20periods%20you%20shouldn't%20judge%0A%0A%20%20%20%20While%20the%20pump%20is%20off%2C%20the%20flow%20meter%20reads%20noise%20around%20zero.%20Half%20of%0A%20%20%20%20those%20readings%20dip%20below%200%2C%20so%20a%20plain%20%60Range(min%3D0)%60%20removes%20them%20as%0A%20%20%20%20faults.%20They%20aren't%20faults%3A%20there's%20simply%20nothing%20to%20measure.%0A%0A%20%20%20%20%60Mask(keep_when%3D%22pump_on%20%3D%3D%201%22)%60%20sets%20the%20pump-off%20values%20aside%20**before**%0A%20%20%20%20cleaning.%20They're%20flagged%20%60EXCLUDED%60%2C%20which%20means%20deliberately%20left%20out%2C%0A%20%20%20%20not%20bad%2C%20and%20%60Interpolate%60%20never%20fills%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20case%2C%20fs%2C%20pd%2C%20run)%3A%0A%20%20%20%20_align%20%3D%20fs.select.Align(freq%3D%225min%22%2C%20tolerance%3D%222min%22)%0A%20%20%20%20_range%20%3D%20fs.clean.Range(%22flow%22%2C%20min%3D0%2C%20max%3D500)%0A%20%20%20%20_mask%20%3D%20fs.select.Mask(keep_when%3D%22pump_on%20%3D%3D%201%22%2C%20streams%3D%5B%22flow%22%2C%20%22pressure%22%5D)%0A%20%20%20%20_fill%20%3D%20fs.impute.Interpolate(%22flow%22%2C%20max_gap%3D%2215min%22)%0A%20%20%20%20_pump_off%20%3D%20case.observed%5B%22pump_on%22%5D%20%3D%3D%200%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_label%2C%20_steps%20in%20(%0A%20%20%20%20%20%20%20%20(%22range%20only%22%2C%20(_align%2C%20_range%2C%20_fill))%2C%0A%20%20%20%20%20%20%20%20(%22mask%2C%20then%20range%22%2C%20(_align%2C%20_mask%2C%20_range%2C%20_fill))%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20run(*_steps)%0A%20%20%20%20%20%20%20%20_rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22steps%22%3A%20_label%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pump-off%20values%20removed%20as%20faults%22%3A%20int(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(_out.has_flag(Flag.OUT_OF_RANGE)%5B%22flow%22%5D%20%26%20_pump_off).sum()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pump-off%20values%20excluded%22%3A%20int(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(_out.has_flag(Flag.EXCLUDED)%5B%22flow%22%5D%20%26%20_pump_off).sum()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pump-off%20values%20filled%22%3A%20int(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(_out.has_flag(Flag.IMPUTED_SHORT)%5B%22flow%22%5D%20%26%20_pump_off).sum()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20mask_comparison%20%3D%20pd.DataFrame(_rows).set_index(%22steps%22)%0A%20%20%20%20mask_comparison%0A%20%20%20%20return%20(mask_comparison%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20With%20%60Range%60%20alone%2C%20pump-off%20readings%20that%20dip%20below%200%20are%20removed%20as%0A%20%20%20%20faults%2C%20and%20%60Interpolate%60%20then%20fills%20the%20short%20holes%20this%20leaves%20in%20what%0A%20%20%20%20is%20really%20an%20idle%20period.%20With%20the%20mask%20first%2C%20all%20of%20them%20are%20set%20aside%0A%20%20%20%20and%20none%20are%20filled.%0A%0A%20%20%20%20%23%23%20Step%204%3A%20a%20rule%20over%20several%20streams%2C%20and%20why%20order%20still%20matters%0A%0A%20%20%20%20Pressure%20follows%20flow%20here%2C%20at%20about%204%20%2B%200.01%20%C3%97%20flow%20bar.%20Three%20pressure%0A%20%20%20%20readings%20are%20wrong%20but%20plausible%2C%20at%203.0%20bar%2C%20so%20no%20fixed%20range%20would%0A%20%20%20%20catch%20them.%20Instead%20we%20use%20a%20rule%20that%20compares%20the%20two%20streams.%20%60Expression%60%0A%20%20%20%20takes%20any%20condition%20over%20stream%20names%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20abs(pressure%20-%20(4%20%2B%200.01%20*%20flow))%20%3E%200.15%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20rule%20assumes%20%60flow%60%20is%20right.%20Run%20it%20**before**%20cleaning%20flow%2C%20and%0A%20%20%20%20every%20flow%20spike%20makes%20a%20good%20pressure%20reading%20look%20inconsistent.%20Run%20it%0A%20%20%20%20**after**%2C%20and%20the%20spikes%20are%20already%20NaN%2C%20and%20a%20comparison%20with%20NaN%20is%0A%20%20%20%20never%20true%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20case%2C%20fs%2C%20pd%2C%20run)%3A%0A%20%20%20%20_align%20%3D%20fs.select.Align(freq%3D%225min%22%2C%20tolerance%3D%222min%22)%0A%20%20%20%20_mask%20%3D%20fs.select.Mask(keep_when%3D%22pump_on%20%3D%3D%201%22%2C%20streams%3D%5B%22flow%22%2C%20%22pressure%22%5D)%0A%20%20%20%20_flow%20%3D%20fs.clean.Hampel(%22flow%22%2C%20window%3D%221h%22%2C%20n_sigma%3D6%2C%20min_scale%3D1.0)%0A%20%20%20%20_rule%20%3D%20fs.clean.Expression(%0A%20%20%20%20%20%20%20%20%22pressure%22%2C%20invalid_when%3D%22abs(pressure%20-%20(4%20%2B%200.01%20*%20flow))%20%3E%200.15%22%0A%20%20%20%20)%0A%20%20%20%20_truth%20%3D%20case.fault_mask(%22out_of_range%22%2C%20%22pressure%22)%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_label%2C%20_steps%20in%20(%0A%20%20%20%20%20%20%20%20(%22rule%2C%20then%20clean%20flow%22%2C%20(_align%2C%20_mask%2C%20_rule%2C%20_flow))%2C%0A%20%20%20%20%20%20%20%20(%22clean%20flow%2C%20then%20rule%22%2C%20(_align%2C%20_mask%2C%20_flow%2C%20_rule))%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_flagged%20%3D%20run(*_steps).has_flag(Flag.MANUAL)%5B%22pressure%22%5D%0A%20%20%20%20%20%20%20%20_rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22order%22%3A%20_label%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22bad%20pressure%20caught%22%3A%20int((_flagged%20%26%20_truth).sum())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22good%20pressure%20removed%22%3A%20int((_flagged%20%26%20~_truth).sum())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20rule_comparison%20%3D%20pd.DataFrame(_rows).set_index(%22order%22)%0A%20%20%20%20rule_comparison%0A%20%20%20%20return%20(rule_comparison%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Step%205%3A%20%60Combine%60%20the%20best%20of%20both%20meters%0A%0A%20%20%20%20Where%20the%20trusted%20reference%20meter%20reports%2C%20use%20it.%20Elsewhere%2C%20use%20the%0A%20%20%20%20SCADA%20meter.%20%60Combine(how%3D%22coalesce%22)%60%20takes%20the%20first%20stream%20with%20a%20value%0A%20%20%20%20at%20each%20time%2C%20in%20the%20order%20you%20list%20them.%20The%20new%20stream%20is%20flagged%0A%20%20%20%20%60DERIVED%60%2C%20and%20each%20value%20keeps%20the%20flags%20of%20the%20stream%20it%20came%20from%2C%20so%0A%20%20%20%20you%20can%20still%20tell%20the%20sources%20apart.%0A%0A%20%20%20%20%23%23%20Step%206%3A%20%60Resample%60%20and%20%60Window%60%20for%20reporting%0A%0A%20%20%20%20Last%2C%20%60Resample%60%20averages%20to%20hourly%20values.%20A%20bin's%20flags%20are%20the%20OR%20of%0A%20%20%20%20its%20samples'%20flags%2C%20so%20an%20hour%20that%20contains%20an%20excluded%20or%20removed%0A%20%20%20%20value%20says%20so.%20%60Window%60%20picks%20out%20the%20two%20days%20around%20the%20pump%20stops.%0A%20%20%20%20(%60Window(last%3D%222D%22)%60%20would%20keep%20the%20most%20recent%20two%20days%20instead.)%0A%0A%20%20%20%20%23%23%20The%20whole%20recipe%0A%0A%20%20%20%20Here%20it%20is%20as%20one%20pipeline%2C%20saved%20as%20JSON%20and%20run%20from%20the%20JSON%2C%20with%20the%0A%20%20%20%20data%20bound%20to%20the%20two%20slots%20by%20name%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DATA%2C%20INPUTS%2C%20best_delay%2C%20fs%2C%20json)%3A%0A%20%20%20%20pipeline%20%3D%20fs.Pipeline(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.select.Align(freq%3D%225min%22%2C%20tolerance%3D%222min%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.select.Shift(%22reference_flow%22%2C%20by%3Df%22-%7Bbest_delay%7Dmin%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.select.Mask(keep_when%3D%22pump_on%20%3D%3D%201%22%2C%20streams%3D%5B%22flow%22%2C%20%22pressure%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.clean.Range(%22flow%22%2C%20min%3D0%2C%20max%3D500)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.clean.Hampel(%22flow%22%2C%20window%3D%221h%22%2C%20n_sigma%3D6%2C%20min_scale%3D1.0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.clean.Expression(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pressure%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20invalid_when%3D%22abs(pressure%20-%20(4%20%2B%200.01%20*%20flow))%20%3E%200.15%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22pressure_follows_flow%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.impute.Interpolate(%5B%22flow%22%2C%20%22pressure%22%5D%2C%20max_gap%3D%2215min%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.select.Combine(output%3D%22best_flow%22%2C%20streams%3D%5B%22reference_flow%22%2C%20%22flow%22%5D)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20name%3D%22pump_station%22%2C%0A%20%20%20%20%20%20%20%20inputs%3DINPUTS%2C%0A%20%20%20%20)%0A%20%20%20%20spec_text%20%3D%20json.dumps(fs.dump_spec(pipeline.spec)%2C%20indent%3D2)%0A%20%20%20%20station%20%3D%20fs.run(spec_text%2C%20DATA)%0A%20%20%20%20station_python%20%3D%20fs.run(pipeline.spec%2C%20DATA)%0A%20%20%20%20same_from_json%20%3D%20station.signal.equals(station_python.signal)%0A%20%20%20%20return%20same_from_json%2C%20spec_text%2C%20station%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20same_from_json%2C%20spec_text)%3A%0A%20%20%20%20_title%20%3D%20(%0A%20%20%20%20%20%20%20%20f%22The%20spec%3A%20%7Blen(spec_text.splitlines())%7D%20lines%20of%20JSON.%20%22%0A%20%20%20%20%20%20%20%20f%22Same%20result%20from%20JSON%20as%20from%20Python%3A%20%7Bsame_from_json%7D%22%0A%20%20%20%20)%0A%20%20%20%20%23%20Display%20only%20(marimo)%3A%20put%20the%20output%20in%20a%20collapsible%20section.%0A%20%20%20%20mo.accordion(%7B_title%3A%20mo.md(f%22%60%60%60json%5Cn%7Bspec_text%7D%5Cn%60%60%60%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20fs%2C%20mo%2C%20pd%2C%20station)%3A%0A%20%20%20%20_best%20%3D%20station.signal%0A%20%20%20%20_from_reference%20%3D%20(%0A%20%20%20%20%20%20%20%20_best.values%5B%22reference_flow%22%5D.notna()%20%26%20_best.values%5B%22best_flow%22%5D.notna()%0A%20%20%20%20)%0A%20%20%20%20report%20%3D%20fs.run(%0A%20%20%20%20%20%20%20%20fs.Pipeline(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fs.select.Resample(freq%3D%221h%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fs.select.Window(start%3D%222025-01-07%22%2C%20end%3D%222025-01-09%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22report%22%2C%0A%20%20%20%20%20%20%20%20).spec%2C%0A%20%20%20%20%20%20%20%20_best.select(%5B%22flow%22%2C%20%22pressure%22%2C%20%22best_flow%22%5D)%2C%0A%20%20%20%20).signal%0A%20%20%20%20_hours_flagged%20%3D%20pd.Series(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3A%20int(report.has_flag(flag)%5B%22best_flow%22%5D.sum())%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20name%2C%20flag%20in%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22hours%20with%20excluded%20values%22%2C%20Flag.EXCLUDED)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22hours%20with%20removed%20values%22%2C%20Flag.OUTLIER%20%7C%20Flag.OUT_OF_RANGE)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22hours%20with%20filled%20values%22%2C%20Flag.IMPUTED_SHORT)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20name%3D%22count%22%2C%0A%20%20%20%20)%0A%20%20%20%20%23%20Display%20only%20(marimo)%3A%20stack%20these%20outputs%20one%20above%20the%20other.%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%60best_flow%60%20comes%20from%20the%20reference%20meter%20at%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7Bint(_from_reference.sum())%7D**%20of%20%7Blen(_best)%7D%20points%2C%20and%20from%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22SCADA%20elsewhere.%20The%20hourly%20report%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22has%20**%7Blen(report)%7D**%20rows%3A%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_hours_flagged.to_frame()%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20return%20(report%2C)%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20Flag%2C%20case%2C%20plt%2C%20report%2C%20station%2C%20style)%3A%0A%20%20%20%20_sf%20%3D%20station.signal%0A%20%20%20%20_fig%2C%20_axes%20%3D%20plt.subplots(2%2C%201%2C%20figsize%3D(9%2C%204.4)%2C%20sharex%3DFalse)%0A%20%20%20%20_ax%20%3D%20_axes%5B0%5D%0A%20%20%20%20_off%20%3D%20case.observed%5B%22pump_on%22%5D%20%3D%3D%200%0A%20%20%20%20_ax.fill_between(%0A%20%20%20%20%20%20%20%20case.observed.index%2C%0A%20%20%20%20%20%20%20%200%2C%0A%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%20where%3D_off%2C%0A%20%20%20%20%20%20%20%20transform%3D_ax.get_xaxis_transform()%2C%0A%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22excluded%22%5D%2C%0A%20%20%20%20%20%20%20%20label%3D%22pump%20off%20(excluded)%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(_sf.values%5B%22best_flow%22%5D%2C%20color%3DCOLORS%5B%22signal%22%5D%2C%20lw%3D0.8%2C%20label%3D%22best_flow%22)%0A%20%20%20%20_ref%20%3D%20_sf.values%5B%22reference_flow%22%5D.dropna()%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20_ref.index%2C%0A%20%20%20%20%20%20%20%20_ref%2C%0A%20%20%20%20%20%20%20%20%22.%22%2C%0A%20%20%20%20%20%20%20%20ms%3D3%2C%0A%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22filled%22%5D%2C%0A%20%20%20%20%20%20%20%20label%3D%22from%20the%20reference%20meter%22%2C%0A%20%20%20%20)%0A%20%20%20%20style(_ax%2C%20%22flow%20%5Bm%C2%B3%2Fh%5D%22).set_title(%0A%20%20%20%20%20%20%20%20%22Cleaned%2C%20combined%20flow%20for%20the%20week%22%2C%20loc%3D%22left%22%2C%20fontsize%3D10%0A%20%20%20%20)%0A%20%20%20%20_ax.legend(%0A%20%20%20%20%20%20%20%20frameon%3DFalse%2C%20ncol%3D3%2C%20fontsize%3D8%2C%20loc%3D%22lower%20left%22%2C%20bbox_to_anchor%3D(0%2C%201.08)%0A%20%20%20%20)%0A%20%20%20%20_ax%20%3D%20_axes%5B1%5D%0A%20%20%20%20_ax.fill_between(%0A%20%20%20%20%20%20%20%20report.index%2C%0A%20%20%20%20%20%20%20%200%2C%0A%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%20where%3Dreport.has_flag(Flag.EXCLUDED)%5B%22best_flow%22%5D%2C%0A%20%20%20%20%20%20%20%20transform%3D_ax.get_xaxis_transform()%2C%0A%20%20%20%20%20%20%20%20step%3D%22post%22%2C%0A%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22fault%22%5D%2C%0A%20%20%20%20%20%20%20%20alpha%3D0.2%2C%0A%20%20%20%20%20%20%20%20label%3D%22hour%20includes%20excluded%20values%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.step(%0A%20%20%20%20%20%20%20%20report.index%2C%0A%20%20%20%20%20%20%20%20report.values%5B%22best_flow%22%5D%2C%0A%20%20%20%20%20%20%20%20where%3D%22post%22%2C%0A%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22signal%22%5D%2C%0A%20%20%20%20%20%20%20%20lw%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20style(_ax%2C%20%22hourly%20mean%22).set_title(%0A%20%20%20%20%20%20%20%20%22The%20hourly%20report%2C%207-8%20January%22%2C%20loc%3D%22left%22%2C%20fontsize%3D10%0A%20%20%20%20)%0A%20%20%20%20_ax.legend(frameon%3DFalse%2C%20fontsize%3D8%2C%20loc%3D%22lower%20right%22)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20Flag%2C%0A%20%20%20%20aligned%2C%0A%20%20%20%20best_delay%2C%0A%20%20%20%20joined%2C%0A%20%20%20%20mask_comparison%2C%0A%20%20%20%20reference%2C%0A%20%20%20%20rule_comparison%2C%0A%20%20%20%20same_from_json%2C%0A)%3A%0A%20%20%20%20%23%20These%20checks%20run%20when%20CI%20runs%20the%20tutorial%20as%20a%20script.%0A%20%20%20%20assert%20joined.has_flag(Flag.NO_SAMPLE)%5B%22reference_flow%22%5D.sum()%20%3E%200%0A%20%20%20%20assert%20aligned.flag_counts()%5B%22reference_flow%22%5D%5B%22valid%22%5D%20%3D%3D%20len(reference)%0A%20%20%20%20assert%20best_delay%20%3D%3D%2010%0A%20%20%20%20assert%20mask_comparison.loc%5B%22range%20only%22%2C%20%22pump-off%20values%20removed%20as%20faults%22%5D%20%3E%200%0A%20%20%20%20assert%20mask_comparison.loc%5B%22mask%2C%20then%20range%22%5D.tolist()%5B0%5D%20%3D%3D%200%0A%20%20%20%20assert%20mask_comparison.loc%5B%22mask%2C%20then%20range%22%2C%20%22pump-off%20values%20filled%22%5D%20%3D%3D%200%0A%20%20%20%20assert%20rule_comparison.loc%5B%22rule%2C%20then%20clean%20flow%22%2C%20%22good%20pressure%20removed%22%5D%20%3E%200%0A%20%20%20%20assert%20rule_comparison.loc%5B%22clean%20flow%2C%20then%20rule%22%5D.tolist()%20%3D%3D%20%5B3%2C%200%5D%0A%20%20%20%20assert%20same_from_json%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What's%20next%0A%0A%20%20%20%20These%20are%20the%20pieces%20of%20the%20problem%20flex-signal%20was%20built%20for%3A%0A%20%20%20%20*correct%20stream%20A%20using%20a%20trusted%20reference%20B%2C%20but%20only%20when%20C%20and%20D%20are%0A%20%20%20%200%20and%20B%20is%20positive*.%20You%20can%20now%20align%20A%20and%20B%2C%20shift%20out%20the%20delay%2C%0A%20%20%20%20and%20pick%20out%20the%20periods%20where%20the%20condition%20holds.%0A%0A%20%20%20%20The%20next%20release%20adds%20the%20step%20that%20**fits%20the%20correction**%3A%20%60calibrate%60%2C%0A%20%20%20%20plus%20model-based%20imputation%20for%20long%20gaps.%20Its%20tutorials%20pick%20up%20from%0A%20%20%20%20here.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
080888d7a71a910753c42ab62e831bfe