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%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.dates%20as%20mdates%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%20QUALITY_FLAGS%2C%20Flag%0A%20%20%20%20from%20flexsignal.testing%20import%20synthetic%0A%0A%20%20%20%20return%20Flag%2C%20QUALITY_FLAGS%2C%20fs%2C%20mdates%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%202.%20Cleaning%20one%20stream%0A%0A%20%20%20%20A%20flow%20meter%20on%20the%20synthetic%20plant%20has%20four%20problems%3A%20%60-999%60%20error%20codes%2C%0A%20%20%20%20a%20sensor%20that%20froze%20for%20three%20hours%2C%20spikes%2C%20and%20short%20dropouts.%20Here%20we%0A%20%20%20%20apply%20flex-signal's%20cleaning%20steps%20one%20at%20a%20time%20and%20score%20each%20one%20against%0A%20%20%20%20the%20faults%20we%20injected%3A%0A%0A%20%20%20%20-%20**caught**%3A%20injected%20faults%20the%20step%20removed%3B%0A%20%20%20%20-%20**missed**%3A%20injected%20faults%20it%20left%20in%3B%0A%20%20%20%20-%20**false%20alarms**%3A%20good%20values%20it%20removed.%0A%0A%20%20%20%20Then%20we%20put%20the%20steps%20together%2C%20and%20see%20why%20their%20order%20matters.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(synthetic)%3A%0A%20%20%20%20case%20%3D%20(%0A%20%20%20%20%20%20%20%20synthetic.plant(seed%3D7)%0A%20%20%20%20%20%20%20%20.with_out_of_range(%22flow%22%2C%20count%3D4)%0A%20%20%20%20%20%20%20%20.with_stuck(%22flow%22%2C%20length%3D36)%0A%20%20%20%20%20%20%20%20.with_spikes(%22flow%22%2C%20count%3D6%2C%20size%3D40)%0A%20%20%20%20%20%20%20%20.with_gaps(%22flow%22%2C%20count%3D4%2C%20length%3D2)%0A%20%20%20%20%20%20%20%20.with_gaps(%22flow%22%2C%20count%3D1%2C%20length%3D12)%20%20%23%20one%20long%2C%201-hour%20dropout%0A%20%20%20%20)%0A%20%20%20%20raw%20%3D%20case.signal()%0A%20%20%20%20return%20case%2C%20raw%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20pd%2C%20raw)%3A%0A%20%20%20%20def%20clean_with(*steps%2C%20data%3Draw)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Run%20a%20few%20steps%20and%20return%20the%20cleaned%20SignalFrame.%22%22%22%0A%20%20%20%20%20%20%20%20return%20fs.run(fs.Pipeline(list(steps)).spec%2C%20data).signal%0A%0A%20%20%20%20def%20score(result%2C%20flag%2C%20truth%2C%20stream%3D%22flow%22)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Compare%20the%20values%20a%20step%20flagged%20with%20where%20faults%20were%20injected.%22%22%22%0A%20%20%20%20%20%20%20%20flagged%20%3D%20result.has_flag(flag)%5Bstream%5D%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22caught%22%3A%20int((flagged%20%26%20truth).sum())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22missed%22%3A%20int((~flagged%20%26%20truth).sum())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22false%20alarms%22%3A%20int((flagged%20%26%20~truth).sum())%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20def%20score_table(rows)%3A%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(rows).set_index(%22step%22)%0A%0A%20%20%20%20return%20clean_with%2C%20score%2C%20score_table%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%60Range%60%20removes%20impossible%20values%0A%0A%20%20%20%20A%20meter%20can't%20read%20below%200%20or%20far%20above%20its%20span%2C%20so%20anything%20outside%0A%20%20%20%20the%20physical%20range%20is%20an%20error%20code%20or%20a%20fault.%20%60Range%60%20takes%20the%20limits%0A%20%20%20%20in%20the%20stream's%20own%20units%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%20clean_with%2C%20fs%2C%20score%2C%20score_table)%3A%0A%20%20%20%20in_range%20%3D%20clean_with(fs.clean.Range(%22flow%22%2C%20min%3D0%2C%20max%3D500))%0A%20%20%20%20score_table(%0A%20%20%20%20%20%20%20%20%5B%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%22step%22%3A%20%22range%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20**score(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20in_range%2C%20Flag.OUT_OF_RANGE%2C%20case.fault_mask(%22out_of_range%22%2C%20%22flow%22)%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%5D%0A%20%20%20%20)%0A%20%20%20%20return%20(in_range%2C)%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20in_range%2C%20plt%2C%20raw%2C%20style)%3A%0A%20%20%20%20_fig%2C%20_axes%20%3D%20plt.subplots(2%2C%201%2C%20figsize%3D(9%2C%204)%2C%20sharex%3DTrue)%0A%20%20%20%20_axes%5B0%5D.plot(raw.values%5B%22flow%22%5D%2C%20color%3DCOLORS%5B%22fault%22%5D%2C%20lw%3D0.8)%0A%20%20%20%20style(_axes%5B0%5D%2C%20%22raw%22).set_title(%0A%20%20%20%20%20%20%20%20%22The%20-999%20codes%20dwarf%20the%20real%20signal%22%2C%20loc%3D%22left%22%2C%20fontsize%3D10%0A%20%20%20%20)%0A%20%20%20%20_axes%5B1%5D.plot(in_range.values%5B%22flow%22%5D%2C%20color%3DCOLORS%5B%22signal%22%5D%2C%20lw%3D0.8)%0A%20%20%20%20style(_axes%5B1%5D%2C%20%22after%20range%22).set_title(%0A%20%20%20%20%20%20%20%20%22After%20Range%3A%20the%20signal%20is%20visible%20again%22%2C%20loc%3D%22left%22%2C%20fontsize%3D10%0A%20%20%20%20)%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%23%23%20Step%202%3A%20%60Stuck%60%20finds%20a%20frozen%20sensor%0A%0A%20%20%20%20A%20frozen%20sensor%20repeats%20its%20last%20reading.%20Real%20flow%20always%20has%20some%20noise%2C%0A%20%20%20%20so%20a%20run%20of%20identical%20values%20lasting%20longer%20than%20%60min_duration%60%20is%20a%20fault.%0A%20%20%20%20%60tolerance%60%20sets%20how%20close%20values%20must%20be%20to%20count%20as%20identical.%20The%0A%20%20%20%20default%2C%200%2C%20means%20exactly%20equal.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20case%2C%20clean_with%2C%20fs%2C%20score%2C%20score_table)%3A%0A%20%20%20%20stuck_removed%20%3D%20clean_with(%0A%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%20fs.clean.Stuck(%22flow%22%2C%20min_duration%3D%221h%22)%2C%0A%20%20%20%20)%0A%20%20%20%20score_table(%0A%20%20%20%20%20%20%20%20%5B%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%22step%22%3A%20%22stuck%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20**score(stuck_removed%2C%20Flag.STUCK%2C%20case.fault_mask(%22stuck%22%2C%20%22flow%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%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%20Step%203%3A%20%60RateOfChange%60%2C%20and%20why%20order%20matters%0A%0A%20%20%20%20%60RateOfChange%60%20removes%20values%20that%20jump%20faster%20than%20the%20process%20can%0A%20%20%20%20change.%20Here%20%60max_rate%3D100%60%20m%C2%B3%2Fh%20per%20hour%2C%20which%20is%20about%208%20m%C2%B3%2Fh%20per%0A%20%20%20%205-minute%20sample.%20The%20real%20flow%20changes%20by%20at%20most%20about%201%20per%20sample%2C%20and%0A%20%20%20%20a%20spike%20by%2040.%0A%0A%20%20%20%20Each%20value%20is%20compared%20with%20the%20**last%20accepted**%20value.%20So%20if%0A%20%20%20%20%60RateOfChange%60%20runs%20**before**%20%60Stuck%60%2C%20it%20compares%20the%20first%20good%20samples%0A%20%20%20%20after%20the%20frozen%20run%20with%20the%20frozen%20value.%20The%20real%20flow%20has%20drifted%20by%0A%20%20%20%20then%2C%20so%20the%20good%20samples%20look%20like%20a%20jump%20and%20are%20removed.%20Run%20%60Stuck%60%0A%20%20%20%20first%20and%20those%20samples%20are%20kept%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%20clean_with%2C%20fs%2C%20pd%2C%20score)%3A%0A%20%20%20%20_range%20%3D%20fs.clean.Range(%22flow%22%2C%20min%3D0%2C%20max%3D500)%0A%20%20%20%20_stuck%20%3D%20fs.clean.Stuck(%22flow%22%2C%20min_duration%3D%221h%22)%0A%20%20%20%20_rate%20%3D%20fs.clean.RateOfChange(%22flow%22%2C%20max_rate%3D100%2C%20per%3D%221h%22)%0A%20%20%20%20rate_first%20%3D%20clean_with(_range%2C%20_rate%2C%20_stuck)%0A%20%20%20%20stuck_first%20%3D%20clean_with(_range%2C%20_stuck%2C%20_rate)%0A%20%20%20%20_spikes%20%3D%20case.fault_mask(%22spike%22%2C%20%22flow%22)%0A%20%20%20%20order_scores%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%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%22range%2C%20rate_of_change%2C%20stuck%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20**score(rate_first%2C%20Flag.RATE_OF_CHANGE%2C%20_spikes)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%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%22range%2C%20stuck%2C%20rate_of_change%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20**score(stuck_first%2C%20Flag.RATE_OF_CHANGE%2C%20_spikes)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20).set_index(%22order%22)%0A%20%20%20%20order_scores%0A%20%20%20%20return%20order_scores%2C%20rate_first%2C%20stuck_first%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20Flag%2C%20case%2C%20mdates%2C%20pd%2C%20plt%2C%20rate_first%2C%20stuck_first%2C%20style)%3A%0A%20%20%20%20_stuck_mask%20%3D%20case.fault_mask(%22stuck%22%2C%20%22flow%22)%0A%20%20%20%20_end%20%3D%20_stuck_mask%5B_stuck_mask%5D.index%5B-1%5D%0A%20%20%20%20_span%20%3D%20slice(_end%20-%20pd.Timedelta(%221h%22)%2C%20_end%20%2B%20pd.Timedelta(%221h%22))%0A%20%20%20%20_fig%2C%20_axes%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%202.8)%2C%20sharey%3DTrue)%0A%20%20%20%20for%20_ax%2C%20_result%2C%20_title%20in%20(%0A%20%20%20%20%20%20%20%20(_axes%5B0%5D%2C%20rate_first%2C%20%22rate_of_change%20before%20stuck%22)%2C%0A%20%20%20%20%20%20%20%20(_axes%5B1%5D%2C%20stuck_first%2C%20%22stuck%20before%20rate_of_change%22)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20case.observed%5B%22flow%22%5D%5B_span%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22truth%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20lw%3D2.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3D%22observed%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20_result.values%5B%22flow%22%5D%5B_span%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22o-%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ms%3D3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20lw%3D0.9%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22signal%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3D%22kept%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_wrong%20%3D%20_result.has_flag(Flag.RATE_OF_CHANGE)%5B%22flow%22%5D%5B_span%5D%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20case.observed%5B%22flow%22%5D%5B_span%5D%5B_wrong%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22x%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ms%3D8%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mew%3D2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22fault%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3D%22removed%20as%20a%20jump%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.xaxis.set_major_formatter(mdates.DateFormatter(%22%25H%3A%25M%22))%0A%20%20%20%20%20%20%20%20style(_ax%2C%20%22flow%20%5Bm%C2%B3%2Fh%5D%22%20if%20_ax%20is%20_axes%5B0%5D%20else%20None).set_title(%0A%20%20%20%20%20%20%20%20%20%20%20%20_title%2C%20loc%3D%22left%22%2C%20fontsize%3D10%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_axes%5B0%5D.legend(frameon%3DFalse%2C%20fontsize%3D8%2C%20loc%3D%22lower%20left%22)%0A%20%20%20%20_fig.suptitle(%22The%20end%20of%20the%20frozen%20run%22%2C%20x%3D0.01%2C%20ha%3D%22left%22%2C%20fontsize%3D10)%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%23%23%20Step%204%3A%20%60Hampel%60%20catches%20what's%20left%0A%0A%20%20%20%20A%20Hampel%20filter%20compares%20each%20value%20with%20the%20**median**%20of%20a%20window%0A%20%20%20%20centred%20on%20it%2C%20and%20removes%20it%20if%20it%20is%20more%20than%20%60n_sigma%60%20robust%20standard%0A%20%20%20%20deviations%20away.%20The%20robust%20standard%20deviation%20is%201.4826%20%C3%97%20the%20median%0A%20%20%20%20absolute%20deviation.%20Medians%20ignore%20the%20spikes%20themselves%2C%20so%20the%20filter%0A%20%20%20%20isn't%20pulled%20off%20by%20the%20faults%20it%20is%20looking%20for.%0A%0A%20%20%20%20On%20a%20flat%20stretch%2C%20a%20short%20window%20can%20underestimate%20the%20noise%2C%20and%20then%0A%20%20%20%20ordinary%20noise%20gets%20flagged.%20%60min_scale%60%20puts%20a%20floor%20under%20the%20spread.%0A%20%20%20%20Set%20it%20to%20the%20meter's%20noise%20level%2C%20which%20is%201%20m%C2%B3%2Fh%20here.%0A%0A%20%20%20%20Below%2C%20Hampel%20runs%20on%20its%20own%20after%20%60Range%60%20and%20%60Stuck%60%2C%20so%20it%20has%20to%20find%0A%20%20%20%20the%20spikes%20by%20itself.%20Move%20the%20controls%20and%20watch%20the%20score%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20%23%20Notebook%20control%20(marimo)%3A%20moving%20it%20reruns%20every%20cell%20that%20uses%20it.%0A%20%20%20%20n_sigma%20%3D%20mo.ui.slider(2%2C%2010%2C%20step%3D0.5%2C%20value%3D3%2C%20label%3D%22n_sigma%22%2C%20show_value%3DTrue)%0A%20%20%20%20window%20%3D%20mo.ui.dropdown(%5B%2230min%22%2C%20%221h%22%2C%20%222h%22%2C%20%224h%22%5D%2C%20value%3D%221h%22%2C%20label%3D%22window%22)%0A%20%20%20%20use_floor%20%3D%20mo.ui.switch(value%3DFalse%2C%20label%3D%22min_scale%20%3D%201.0%20(the%20noise%20level)%22)%0A%20%20%20%20mo.hstack(%5Bn_sigma%2C%20window%2C%20use_floor%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20n_sigma%2C%20use_floor%2C%20window%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20case%2C%20clean_with%2C%20fs%2C%20n_sigma%2C%20score%2C%20use_floor%2C%20window)%3A%0A%20%20%20%20hampel_step%20%3D%20fs.clean.Hampel(%0A%20%20%20%20%20%20%20%20%22flow%22%2C%0A%20%20%20%20%20%20%20%20window%3Dwindow.value%2C%0A%20%20%20%20%20%20%20%20n_sigma%3Dn_sigma.value%2C%0A%20%20%20%20%20%20%20%20min_scale%3D1.0%20if%20use_floor.value%20else%200.0%2C%0A%20%20%20%20)%0A%20%20%20%20hampel_result%20%3D%20clean_with(%0A%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%20fs.clean.Stuck(%22flow%22%2C%20min_duration%3D%221h%22)%2C%0A%20%20%20%20%20%20%20%20hampel_step%2C%0A%20%20%20%20)%0A%20%20%20%20hampel_score%20%3D%20score(hampel_result%2C%20Flag.OUTLIER%2C%20case.fault_mask(%22spike%22%2C%20%22flow%22))%0A%20%20%20%20return%20hampel_result%2C%20hampel_score%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20Flag%2C%20case%2C%20hampel_result%2C%20hampel_score%2C%20mo%2C%20plt%2C%20style)%3A%0A%20%20%20%20_flagged%20%3D%20hampel_result.has_flag(Flag.OUTLIER)%5B%22flow%22%5D%0A%20%20%20%20_spikes%20%3D%20case.fault_mask(%22spike%22%2C%20%22flow%22)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(9%2C%202.8))%0A%20%20%20%20_ax.plot(case.observed%5B%22flow%22%5D.clip(lower%3D0)%2C%20color%3DCOLORS%5B%22truth%22%5D%2C%20lw%3D0.8)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20case.observed%5B%22flow%22%5D%5B_flagged%20%26%20_spikes%5D%2C%0A%20%20%20%20%20%20%20%20%22o%22%2C%0A%20%20%20%20%20%20%20%20ms%3D6%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%22spike%20caught%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20case.observed%5B%22flow%22%5D%5B_flagged%20%26%20~_spikes%5D%2C%0A%20%20%20%20%20%20%20%20%22x%22%2C%0A%20%20%20%20%20%20%20%20ms%3D6%2C%0A%20%20%20%20%20%20%20%20mew%3D2%2C%0A%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22fault%22%5D%2C%0A%20%20%20%20%20%20%20%20label%3D%22false%20alarm%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20case.observed%5B%22flow%22%5D%5B~_flagged%20%26%20_spikes%5D%2C%0A%20%20%20%20%20%20%20%20%22s%22%2C%0A%20%20%20%20%20%20%20%20ms%3D6%2C%0A%20%20%20%20%20%20%20%20mfc%3D%22none%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%20label%3D%22spike%20missed%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.set_ylim(40%2C%20170)%0A%20%20%20%20style(_ax%2C%20%22flow%20%5Bm%C2%B3%2Fh%5D%22)%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.0)%0A%20%20%20%20)%0A%20%20%20%20_fig.tight_layout()%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%20f%22**caught%20%7Bhampel_score%5B'caught'%5D%7D%20of%206%20spikes%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bhampel_score%5B'false%20alarms'%5D%7D%20false%20alarms**%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_fig%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20case%2C%20clean_with%2C%20fs%2C%20pd%2C%20score)%3A%0A%20%20%20%20%23%20The%20same%20comparison%20as%20a%20table%2C%20for%20a%20few%20settings.%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_sigma%20in%20(3%2C%204%2C%206)%3A%0A%20%20%20%20%20%20%20%20for%20_floor%20in%20(0.0%2C%201.0)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_result%20%3D%20clean_with(%0A%20%20%20%20%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%20%20%20%20%20fs.clean.Stuck(%22flow%22%2C%20min_duration%3D%221h%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fs.clean.Hampel(%22flow%22%2C%20window%3D%221h%22%2C%20n_sigma%3D_sigma%2C%20min_scale%3D_floor)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20_rows.append(%0A%20%20%20%20%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%20%20%20%20%22n_sigma%22%3A%20_sigma%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22min_scale%22%3A%20_floor%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20**score(_result%2C%20Flag.OUTLIER%2C%20case.fault_mask(%22spike%22%2C%20%22flow%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20hampel_sweep%20%3D%20pd.DataFrame(_rows)%0A%20%20%20%20hampel_sweep%0A%20%20%20%20return%20(hampel_sweep%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%20Every%20setting%20catches%20all%20six%20spikes%2C%20which%20are%2040%20noise%20levels%20high.%0A%20%20%20%20What%20changes%20is%20the%20false%20alarms.%20A%20smaller%20%60n_sigma%60%20or%20no%20floor%20removes%0A%20%20%20%20good%20data.%20%60n_sigma%3D6%60%20with%20%60min_scale%3D1.0%60%20removes%20nothing%20it%20shouldn't.%0A%20%20%20%20With%20real%20data%20you%20don't%20have%20the%20answer%20key%2C%20so%20start%20strict%2C%20like%20this%2C%0A%20%20%20%20and%20loosen%20it%20only%20if%20you%20can%20see%20faults%20getting%20through.%0A%0A%20%20%20%20%23%23%20Step%205%3A%20%60Interpolate%60%20fills%20short%20gaps%2C%20and%20only%20short%20ones%0A%0A%20%20%20%20After%20cleaning%2C%20the%20removed%20values%20and%20the%20original%20dropouts%20are%20all%0A%20%20%20%20NaN.%20%60Interpolate%60%20fills%20a%20gap%20only%20if%20it%20is%20no%20longer%20than%20%60max_gap%60%2C%0A%20%20%20%20measured%20between%20the%20good%20samples%20either%20side.%20Filling%20a%20long%20gap%20with%20a%0A%20%20%20%20straight%20line%20would%20invent%20data.%20Filled%20values%20are%20flagged%0A%20%20%20%20%60IMPUTED_SHORT%60%2C%20so%20they're%20never%20mistaken%20for%20measurements.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20%23%20Notebook%20control%20(marimo)%3A%20moving%20it%20reruns%20every%20cell%20that%20uses%20it.%0A%20%20%20%20max_gap%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20%5B%2210min%22%2C%20%2215min%22%2C%20%2230min%22%2C%20%221h%22%2C%20%222h%22%5D%2C%20value%3D%2215min%22%2C%20label%3D%22max_gap%22%0A%20%20%20%20)%0A%20%20%20%20max_gap%0A%20%20%20%20return%20(max_gap%2C)%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20case%2C%20clean_with%2C%20fs%2C%20max_gap%2C%20mo%2C%20np)%3A%0A%20%20%20%20filled%20%3D%20clean_with(%0A%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%20fs.clean.Stuck(%22flow%22%2C%20min_duration%3D%221h%22)%2C%0A%20%20%20%20%20%20%20%20fs.clean.RateOfChange(%22flow%22%2C%20max_rate%3D100%2C%20per%3D%221h%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%20%20%20%20fs.impute.Interpolate(%22flow%22%2C%20max_gap%3Dmax_gap.value)%2C%0A%20%20%20%20)%0A%20%20%20%20_was_filled%20%3D%20filled.has_flag(Flag.IMPUTED_SHORT)%5B%22flow%22%5D%0A%20%20%20%20_still_missing%20%3D%20filled.values%5B%22flow%22%5D.isna()%0A%20%20%20%20_error%20%3D%20filled.values%5B%22flow%22%5D%5B_was_filled%5D%20-%20case.clean%5B%22flow%22%5D%5B_was_filled%5D%0A%20%20%20%20%23%20Display%20only%20(marimo)%3A%20render%20the%20result%20as%20formatted%20text.%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20With%20%60max_gap%3D%22%7Bmax_gap.value%7D%22%60%3A%20**%7Bint(_was_filled.sum())%7D**%20values%20filled%2C%0A%20%20%20%20**%7Bint(_still_missing.sum())%7D**%20left%20as%20NaN.%20The%20filled%20values%20differ%20from%20the%0A%20%20%20%20fault-free%20measurement%20by%20%7Bnp.sqrt(np.mean(_error**2))%3A.2f%7D%20m%C2%B3%2Fh%20(RMS)%3B%20the%0A%20%20%20%20meter's%20own%20noise%20is%201.0.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%20(filled%2C)%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20Flag%2C%20case%2C%20filled%2C%20mdates%2C%20pd%2C%20plt%2C%20style)%3A%0A%20%20%20%20%23%20Label%20each%20run%20of%20injected%20gap%20samples%2C%20and%20find%20a%20short%20one%20and%20the%20long%20one.%0A%20%20%20%20_gaps%20%3D%20case.fault_mask(%22gap%22%2C%20%22flow%22)%0A%20%20%20%20_run_id%20%3D%20(~_gaps).cumsum()%5B_gaps%5D%0A%20%20%20%20_run_starts%20%3D%20_run_id.groupby(_run_id).apply(lambda%20run%3A%20run.index%5B0%5D)%0A%20%20%20%20_run_lengths%20%3D%20_run_id.value_counts().sort_index()%0A%20%20%20%20_short%20%3D%20_run_starts%5B_run_lengths.index%5B_run_lengths%20%3D%3D%202%5D%5B0%5D%5D%0A%20%20%20%20_long%20%3D%20_run_starts%5B_run_lengths.idxmax()%5D%0A%20%20%20%20_fig%2C%20_axes%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%202.8))%0A%20%20%20%20for%20_ax%2C%20_start%2C%20_title%20in%20(%0A%20%20%20%20%20%20%20%20(_axes%5B0%5D%2C%20_short%2C%20%22a%2010-minute%20dropout%3A%20filled%22)%2C%0A%20%20%20%20%20%20%20%20(_axes%5B1%5D%2C%20_long%2C%20%22the%201-hour%20dropout%3A%20left%20as%20NaN%22)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_span%20%3D%20slice(_start%20-%20pd.Timedelta(%221h%22)%2C%20_start%20%2B%20pd.Timedelta(%222h%22))%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20case.clean%5B%22flow%22%5D%5B_span%5D%2C%20color%3DCOLORS%5B%22truth%22%5D%2C%20lw%3D2.5%2C%20label%3D%22fault-free%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20filled.values%5B%22flow%22%5D%5B_span%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22o-%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ms%3D2.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20lw%3D0.8%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22signal%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3D%22cleaned%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_imp%20%3D%20filled.has_flag(Flag.IMPUTED_SHORT)%5B%22flow%22%5D%5B_span%5D%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20filled.values%5B%22flow%22%5D%5B_span%5D%5B_imp%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22o%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ms%3D5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22filled%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3D%22filled%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.xaxis.set_major_formatter(mdates.DateFormatter(%22%25H%3A%25M%22))%0A%20%20%20%20%20%20%20%20style(_ax%2C%20%22flow%20%5Bm%C2%B3%2Fh%5D%22%20if%20_ax%20is%20_axes%5B0%5D%20else%20None).set_title(%0A%20%20%20%20%20%20%20%20%20%20%20%20_title%2C%20loc%3D%22left%22%2C%20fontsize%3D10%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_axes%5B0%5D.legend(frameon%3DFalse%2C%20fontsize%3D8)%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%23%23%20All%20together%0A%0A%20%20%20%20The%20full%20chain%2C%20in%20the%20order%20that%20works%3A%0A%0A%20%20%20%201.%20%60Range%60%20first%2C%20so%20error%20codes%20can't%20distort%20the%20statistics%20later%20steps%20compute%3B%0A%20%20%20%202.%20%60Stuck%60%20before%20%60RateOfChange%60%2C%20so%20the%20end%20of%20a%20frozen%20run%20isn't%20taken%20for%20a%20jump%3B%0A%20%20%20%203.%20%60RateOfChange%60%20and%20%60Hampel%60%20for%20spikes.%20They%20overlap%2C%20which%20is%20fine%3A%20a%0A%20%20%20%20%20%20%20value%20removed%20by%20one%20isn't%20seen%20by%20the%20next%3B%0A%20%20%20%204.%20%60Interpolate%60%20last%2C%20once%20every%20bad%20value%20is%20gone.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(QUALITY_FLAGS%2C%20case%2C%20clean_with%2C%20fs%2C%20pd)%3A%0A%20%20%20%20final%20%3D%20clean_with(%0A%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%20fs.clean.Stuck(%22flow%22%2C%20min_duration%3D%221h%22)%2C%0A%20%20%20%20%20%20%20%20fs.clean.RateOfChange(%22flow%22%2C%20max_rate%3D100%2C%20per%3D%221h%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%20%20%20%20fs.impute.Interpolate(%22flow%22%2C%20max_gap%3D%2215min%22)%2C%0A%20%20%20%20)%0A%20%20%20%20removed%20%3D%20final.has_flag(QUALITY_FLAGS)%5B%22flow%22%5D%0A%20%20%20%20injected%20%3D%20(%0A%20%20%20%20%20%20%20%20case.fault_mask(%22out_of_range%22%2C%20%22flow%22)%0A%20%20%20%20%20%20%20%20%7C%20case.fault_mask(%22stuck%22%2C%20%22flow%22)%0A%20%20%20%20%20%20%20%20%7C%20case.fault_mask(%22spike%22%2C%20%22flow%22)%0A%20%20%20%20)%0A%20%20%20%20pd.Series(final.flag_counts()%5B%22flow%22%5D%5B%22flags%22%5D%2C%20name%3D%22values%22).to_frame()%0A%20%20%20%20return%20final%2C%20injected%2C%20removed%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20Flag%2C%20final%2C%20plt%2C%20raw%2C%20style)%3A%0A%20%20%20%20_flow%20%3D%20final.values%5B%22flow%22%5D%0A%20%20%20%20_filled%20%3D%20final.has_flag(Flag.IMPUTED_SHORT)%5B%22flow%22%5D%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(9%2C%203.0))%0A%20%20%20%20_ax.fill_between(%0A%20%20%20%20%20%20%20%20_flow.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_flow.isna()%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%22fault%22%5D%2C%0A%20%20%20%20%20%20%20%20alpha%3D0.2%2C%0A%20%20%20%20%20%20%20%20step%3D%22mid%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22still%20NaN%20(longer%20than%20max_gap)%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20raw.values%5B%22flow%22%5D.clip(lower%3D40)%2C%0A%20%20%20%20%20%20%20%20color%3DCOLORS%5B%22fault%22%5D%2C%0A%20%20%20%20%20%20%20%20lw%3D0.8%2C%0A%20%20%20%20%20%20%20%20label%3D%22raw%20(-999%20drawn%20at%2040)%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(_flow%2C%20color%3DCOLORS%5B%22signal%22%5D%2C%20lw%3D0.8%2C%20label%3D%22cleaned%22)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20_flow%5B_filled%5D.index%2C%0A%20%20%20%20%20%20%20%20_flow%5B_filled%5D%2C%0A%20%20%20%20%20%20%20%20%22o%22%2C%0A%20%20%20%20%20%20%20%20ms%3D4%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%3Df%22filled%20(%7Bint(_filled.sum())%7D%20values)%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.set_ylim(35%2C%20170)%0A%20%20%20%20style(_ax%2C%20%22flow%20%5Bm%C2%B3%2Fh%5D%22)%0A%20%20%20%20_ax.legend(%0A%20%20%20%20%20%20%20%20frameon%3DFalse%2C%20ncol%3D4%2C%20fontsize%3D8%2C%20loc%3D%22lower%20left%22%2C%20bbox_to_anchor%3D(0%2C%201.0)%0A%20%20%20%20)%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_(Flag%2C%20final%2C%20mo)%3A%0A%20%20%20%20_missing%20%3D%20final.values%5B%22flow%22%5D.isna()%0A%20%20%20%20_runs%20%3D%20(_missing%20!%3D%20_missing.shift()).cumsum()%5B_missing%5D%0A%20%20%20%20_gaps%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22%7Blen(_run)%20*%205%7D%20minutes%20from%20%7B_run.index%5B0%5D%3A%25a%20%25H%3A%25M%7D%22%0A%20%20%20%20%20%20%20%20for%20_%2C%20_run%20in%20_runs.groupby(_runs)%0A%20%20%20%20%5D%0A%20%20%20%20_stuck%20%3D%20int(final.has_flag(Flag.STUCK)%5B%22flow%22%5D.sum())%0A%20%20%20%20%23%20Display%20only%20(marimo)%3A%20render%20the%20result%20as%20formatted%20text.%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20The%20green%20dots%20are%20the%20values%20%60Interpolate%60%20filled%3A%20the%20error%20codes%2C%20the%0A%20%20%20%20spikes%20and%20the%20short%20dropouts%2C%20each%20one%20or%20two%20samples%20long.%20Two%20gaps%20stay%0A%20%20%20%20empty%20on%20purpose%2C%20because%20they%20are%20longer%20than%20%60max_gap%3D%2215min%22%60%3A%0A%20%20%20%20%7B%22%20and%20%22.join(_gaps)%7D.%20One%20is%20the%201-hour%20dropout%3B%20the%20other%20is%20the%0A%20%20%20%20%7B_stuck%7D-sample%20frozen%20run%20that%20%60Stuck%60%20removed.%20Guessing%20across%20hours%20of%0A%20%20%20%20flow%20is%20not%20a%20job%20for%20interpolation.%20The%20imputers%20in%20a%20later%20tutorial%20fill%0A%20%20%20%20long%20gaps%20from%20the%20other%20streams%20and%20the%20daily%20pattern.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20case%2C%20final%2C%20hampel_sweep%2C%20injected%2C%20order_scores%2C%20removed)%3A%0A%20%20%20%20%23%20These%20checks%20run%20when%20CI%20runs%20the%20tutorial%20as%20a%20script.%0A%20%20%20%20assert%20removed.equals(injected)%2C%20%22removed%20exactly%20the%20injected%20faults%22%0A%20%20%20%20%23%20Only%20the%20stuck%20run%20and%20the%201-hour%20dropout%20stay%20empty%3B%20everything%20else%20is%20filled.%0A%20%20%20%20_left%20%3D%20final.values%5B%22flow%22%5D.isna()%0A%20%20%20%20assert%20_left%5Bcase.fault_mask(%22stuck%22%2C%20%22flow%22)%5D.all()%20%20%23%20the%203-hour%20frozen%20run%0A%20%20%20%20assert%20not%20(_left%20%26%20~case.any_fault(%22flow%22)).any()%20%20%23%20no%20good%20value%20left%20empty%0A%20%20%20%20assert%20int(_left.sum())%20%3D%3D%2036%20%2B%2012%20%20%23%20plus%20the%201-hour%20dropout%2C%20nothing%20else%0A%20%20%20%20assert%20int(final.has_flag(Flag.IMPUTED_SHORT)%5B%22flow%22%5D.sum())%20%3D%3D%204%20%2B%206%20%2B%208%0A%20%20%20%20assert%20order_scores%5B%22false%20alarms%22%5D.tolist()%5B0%5D%20%3E%200%0A%20%20%20%20assert%20order_scores%5B%22false%20alarms%22%5D.tolist()%5B1%5D%20%3D%3D%200%0A%20%20%20%20_strict%20%3D%20hampel_sweep.query(%22n_sigma%20%3D%3D%206%20and%20min_scale%20%3D%3D%201.0%22)%0A%20%20%20%20assert%20_strict%5B%22false%20alarms%22%5D.item()%20%3D%3D%200%20and%20_strict%5B%22caught%22%5D.item()%20%3D%3D%206%0A%20%20%20%20assert%20(hampel_sweep%5B%22caught%22%5D%20%3D%3D%206).all()%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%20Next%0A%0A%20%20%20%20The%20steps%20above%20are%20already%20a%20recipe.%20**Tutorial%203**%20turns%20it%20into%20a%0A%20%20%20%20pipeline%20you%20can%20save%20as%20JSON%2C%20rerun%20anywhere%2C%20and%20fit%20on%20training%20data.%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
5024fa3eddff7614ec2e9ae30f66b3fc