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%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%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%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.io%20import%20read_parquet%0A%20%20%20%20from%20flexsignal.testing%20import%20synthetic%0A%0A%20%20%20%20return%20Flag%2C%20Path%2C%20fs%2C%20json%2C%20mo%2C%20pd%2C%20plt%2C%20read_parquet%2C%20synthetic%2C%20tempfile%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%203.%20Pipelines%2C%20specs%20and%20fitted%20state%0A%0A%20%20%20%20In%20tutorial%202%20we%20ran%20cleaning%20steps%20by%20hand.%20A%20**pipeline**%20is%20the%20same%0A%20%20%20%20steps%20written%20down%20once%2C%20as%20data.%20That%20gives%20you%3A%0A%0A%20%20%20%20-%20a%20**spec**%3A%20plain%20JSON%20you%20can%20save%2C%20review%20in%20a%20pull%20request%2C%20or%20hand%0A%20%20%20%20%20%20to%20flex-orchestrator%20to%20run%3B%0A%20%20%20%20-%20a%20**hash**%20of%20the%20spec%2C%20so%20every%20result%20says%20exactly%20which%20recipe%20made%20it%3B%0A%20%20%20%20-%20**provenance**%3A%20a%20record%20of%20what%20each%20step%20changed%3B%0A%20%20%20%20-%20**fitted%20state**%20for%20steps%20that%20learn%20from%20training%20data%2C%20saved%20and%0A%20%20%20%20%20%20reloaded%20without%20pickle.%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)%0A%20%20%20%20raw%20%3D%20case.signal()%0A%20%20%20%20return%20(raw%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%20A%20pipeline%20is%20a%20list%20of%20steps%0A%0A%20%20%20%20These%20are%20the%20steps%20tutorial%202%20ended%20with.%20Each%20step%20has%20a%20%60kind%60%2C%20and%0A%20%20%20%20you%20can%20also%20give%20it%20a%20%60name%60%2C%20which%20shows%20up%20in%20the%20provenance.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs)%3A%0A%20%20%20%20pipe%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.clean.Range(%22flow%22%2C%20min%3D0%2C%20max%3D500%2C%20name%3D%22sentinels%22)%2C%0A%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%20fs.clean.RateOfChange(%22flow%22%2C%20max_rate%3D100%2C%20per%3D%221h%22)%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.impute.Interpolate(%22flow%22%2C%20max_gap%3D%2215min%22)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20name%3D%22flow_cleanup%22%2C%0A%20%20%20%20%20%20%20%20description%3D%22Remove%20error%20codes%2C%20frozen%20runs%20and%20spikes%3B%20fill%20short%20gaps.%22%2C%0A%20%20%20%20)%0A%20%20%20%20pipe%0A%20%20%20%20return%20(pipe%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%20The%20spec%20is%20JSON%0A%0A%20%20%20%20%60fs.dump_spec%60%20turns%20the%20pipeline%20into%20a%20plain%20dict.%20Options%20left%20at%0A%20%20%20%20their%20defaults%20are%20omitted%2C%20so%20the%20JSON%20shows%20only%20the%20choices%20you%20made.%0A%20%20%20%20%60schema_version%60%20says%20which%20spec%20format%20it%20is%20written%20in%2C%20so%20a%20future%0A%20%20%20%20flex-signal%20can%20upgrade%20an%20old%20file.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20json%2C%20mo%2C%20pipe)%3A%0A%20%20%20%20spec_json%20%3D%20json.dumps(fs.dump_spec(pipe.spec)%2C%20indent%3D2)%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%60%60%60json%5Cn%7Bspec_json%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%20(spec_json%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%20The%20JSON%20is%20the%20pipeline%3A%20loading%20it%20back%20and%20running%20it%20gives%0A%20%20%20%20**bit-identical**%20results%2C%20flags%20and%20all.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20mo%2C%20pipe%2C%20raw%2C%20spec_json)%3A%0A%20%20%20%20result%20%3D%20fs.run(pipe.spec%2C%20raw)%0A%20%20%20%20from_json%20%3D%20fs.run(spec_json%2C%20raw)%0A%20%20%20%20identical%20%3D%20result.signal.equals(from_json.signal)%0A%20%20%20%20%23%20Display%20only%20(marimo)%3A%20render%20the%20result%20as%20formatted%20text.%0A%20%20%20%20mo.md(f%22Python%20pipeline%20and%20JSON%20spec%20give%20identical%20output%3A%20**%7Bidentical%7D**%22)%0A%20%20%20%20return%20identical%2C%20result%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%20The%20spec%20hash%20names%20the%20recipe%0A%0A%20%20%20%20%60spec_hash%60%20is%20a%20SHA-256%20of%20the%20spec's%20canonical%20JSON.%20The%20same%20recipe%0A%20%20%20%20always%20has%20the%20same%20hash%2C%20and%20any%20change%20to%20it%2C%20even%20one%20parameter%2C%0A%20%20%20%20gives%20a%20new%20hash.%20Every%20result%20carries%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20pd%2C%20pipe%2C%20result)%3A%0A%20%20%20%20_looser%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%20*pipe.spec.steps%5B%3A3%5D%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%3D4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20*pipe.spec.steps%5B4%3A%5D%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20name%3D%22flow_cleanup%22%2C%0A%20%20%20%20)%0A%20%20%20%20hash_table%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%22spec%22%3A%20%22flow_cleanup%22%2C%20%22spec_hash%22%3A%20fs.spec_hash(pipe.spec)%5B%3A16%5D%20%2B%20%22%E2%80%A6%22%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%22spec%22%3A%20%22the%20result%20of%20running%20it%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22spec_hash%22%3A%20result.spec_hash%5B%3A16%5D%20%2B%20%22%E2%80%A6%22%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%22spec%22%3A%20%22same%2C%20but%20Hampel%20n_sigma%3D4%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22spec_hash%22%3A%20fs.spec_hash(_looser.spec)%5B%3A16%5D%20%2B%20%22%E2%80%A6%22%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)%0A%20%20%20%20hash_table%0A%20%20%20%20return%20(hash_table%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%20Provenance%3A%20what%20each%20step%20did%0A%0A%20%20%20%20Every%20result%20keeps%20one%20record%20per%20step.%20The%20pipeline%20writes%20these%0A%20%20%20%20records%20by%20comparing%20each%20step's%20input%20and%20output%2C%20so%20a%20step%20can't%0A%20%20%20%20forget%20to%20report%20what%20it%20changed.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pd%2C%20result)%3A%0A%20%20%20%20pd.DataFrame(result.provenance)%5B%0A%20%20%20%20%20%20%20%20%5B%22index%22%2C%20%22kind%22%2C%20%22name%22%2C%20%22samples_changed%22%2C%20%22rows_before%22%2C%20%22rows_after%22%5D%0A%20%20%20%20%5D%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%20Mistakes%20are%20caught%20when%20the%20spec%20is%20loaded%0A%0A%20%20%20%20A%20spec%20is%20checked%20before%20any%20data%20is%20touched.%20An%20error%20names%20the%20exact%0A%20%20%20%20field%2C%20like%20%60steps.3.n_sigma%60%2C%20and%20says%20what's%20wrong%2C%20so%20an%20orchestrator%0A%20%20%20%20or%20a%20form%20can%20point%20at%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20mo)%3A%0A%20%20%20%20_bad_specs%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22a%20negative%20threshold%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22schema_version%22%3A%20fs.SCHEMA_VERSION%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22typo%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22steps%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22kind%22%3A%20%22hampel%22%2C%20%22streams%22%3A%20%5B%22flow%22%5D%2C%20%22window%22%3A%20%221h%22%2C%20%22n_sigma%22%3A%20-1%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22a%20misspelled%20step%20kind%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22schema_version%22%3A%20fs.SCHEMA_VERSION%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22typo%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22steps%22%3A%20%5B%7B%22kind%22%3A%20%22hampl%22%2C%20%22streams%22%3A%20%5B%22flow%22%5D%2C%20%22window%22%3A%20%221h%22%7D%5D%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22a%20duration%20that%20isn't%20one%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22schema_version%22%3A%20fs.SCHEMA_VERSION%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22typo%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22steps%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22kind%22%3A%20%22stuck%22%2C%20%22streams%22%3A%20%5B%22flow%22%5D%2C%20%22min_duration%22%3A%20%22an%20hour%22%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%7D%0A%20%20%20%20_messages%20%3D%20%5B%5D%0A%20%20%20%20for%20_what%2C%20_spec%20in%20_bad_specs.items()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20fs.load_spec(_spec)%0A%20%20%20%20%20%20%20%20except%20fs.SignalConfigError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_messages.append(f%22**%7B_what%7D**%5Cn%5Cn%3E%20%7B_error%7D%22)%0A%20%20%20%20spec_errors%20%3D%20_messages%0A%20%20%20%20%23%20Display%20only%20(marimo)%3A%20render%20the%20result%20as%20formatted%20text.%0A%20%20%20%20mo.md(%22%5Cn%5Cn%22.join(spec_errors))%0A%20%20%20%20return%20(spec_errors%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%20Steps%20that%20learn%3A%20fit%20on%20training%20data%2C%20apply%20to%20new%20data%0A%0A%20%20%20%20Most%20cleaning%20steps%20are%20rules.%20A%20few%20**learn**%20from%20data.%20%60Tukey%60%20sets%0A%20%20%20%20its%20limits%20from%20the%20spread%20of%20the%20training%20data%3A%20anything%20more%20than%0A%20%20%20%20%60k%60%20interquartile%20ranges%20outside%20the%20middle%20half%20is%20an%20outlier.%20This%20is%0A%20%20%20%20useful%20when%20you%20don't%20know%20a%20meter's%20physical%20limits.%0A%0A%20%20%20%20What%20matters%20is%20**which**%20data%20it%20learns%20from.%20Below%2C%20a%20meter%20is%0A%20%20%20%20recalibrated%20badly%20halfway%20through%20the%20test%20week%2C%20and%20reads%2070%20m%C2%B3%2Fh%20too%0A%20%20%20%20high%20from%20then%20on%3A%0A%0A%20%20%20%20-%20**fitted%20on%20a%20clean%20training%20week**%2C%20the%20limits%20come%20from%20normal%0A%20%20%20%20%20%20operation%2C%20and%20the%20offset%20readings%20fall%20outside%20them%3B%0A%20%20%20%20-%20**fitted%20on%20the%20faulty%20test%20week%20itself**%2C%20the%20bad%20data%20widens%20the%0A%20%20%20%20%20%20limits%20and%20hides%20itself.%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%20train%20%3D%20synthetic.plant(seed%3D1).signal().select(%5B%22flow%22%5D)%0A%20%20%20%20test_case%20%3D%20synthetic.plant(seed%3D2%2C%20start%3D%222025-01-13%22).with_step(%0A%20%20%20%20%20%20%20%20%22flow%22%2C%20start%3D1008%2C%20size%3D70.0%0A%20%20%20%20)%0A%20%20%20%20test%20%3D%20test_case.signal().select(%5B%22flow%22%5D)%0A%20%20%20%20return%20test%2C%20test_case%2C%20train%0A%0A%0A%40app.cell%0Adef%20_(Flag%2C%20fs%2C%20pd%2C%20test%2C%20test_case%2C%20train)%3A%0A%20%20%20%20tukey%20%3D%20fs.Pipeline(%5Bfs.clean.Tukey(%22flow%22%2C%20k%3D1.5)%5D%2C%20name%3D%22tukey_flow%22)%0A%20%20%20%20fitted%20%3D%20tukey.fit(train)%0A%20%20%20%20on_new_data%20%3D%20fitted.transform(test)%0A%20%20%20%20refitted%20%3D%20tukey.fit(test)%0A%20%20%20%20on_itself%20%3D%20refitted.transform(test)%0A%0A%20%20%20%20_offset%20%3D%20test_case.fault_mask(%22step%22%2C%20%22flow%22)%0A%20%20%20%20tukey_comparison%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%22fitted%20on%22%3A%20_label%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22lower%20limit%22%3A%20_fit.states%5B0%5D%5B%22fences%22%5D%5B%22flow%22%5D%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22upper%20limit%22%3A%20_fit.states%5B0%5D%5B%22fences%22%5D%5B%22flow%22%5D%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22offset%20readings%20removed%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.OUTLIER)%5B%22flow%22%5D%20%26%20_offset).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%22good%20readings%20removed%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.OUTLIER)%5B%22flow%22%5D%20%26%20~_offset).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%20%20%20%20for%20_label%2C%20_fit%2C%20_out%20in%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22clean%20training%20week%22%2C%20fitted%2C%20on_new_data)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20faulty%20week%20itself%22%2C%20refitted%2C%20on_itself)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20).round(1)%0A%20%20%20%20tukey_comparison%0A%20%20%20%20return%20fitted%2C%20on_new_data%2C%20tukey_comparison%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20fitted%2C%20on_new_data%2C%20plt%2C%20style%2C%20test)%3A%0A%20%20%20%20_low%2C%20_high%20%3D%20fitted.states%5B0%5D%5B%22fences%22%5D%5B%22flow%22%5D%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(%0A%20%20%20%20%20%20%20%20test.values%5B%22flow%22%5D%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%22test%20week%2C%20as%20measured%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(on_new_data.values%5B%22flow%22%5D%2C%20color%3DCOLORS%5B%22signal%22%5D%2C%20lw%3D0.8%2C%20label%3D%22kept%22)%0A%20%20%20%20for%20_limit%20in%20(_low%2C%20_high)%3A%0A%20%20%20%20%20%20%20%20_ax.axhline(_limit%2C%20color%3DCOLORS%5B%22truth%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%20test.index%5B10%5D%2C%0A%20%20%20%20%20%20%20%20_high%20%2B%203%2C%0A%20%20%20%20%20%20%20%20%22limits%20fitted%20on%20the%20training%20week%22%2C%0A%20%20%20%20%20%20%20%20fontsize%3D8%2C%0A%20%20%20%20%20%20%20%20color%3D%22%234a5568%22%2C%0A%20%20%20%20)%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%3D2%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(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Tukey%20removes%20only%20the%20offset%20readings%20above%20its%20upper%20limit.%20The%0A%20%20%20%20offset%20readings%20near%20the%20bottom%20of%20the%20daily%20cycle%20still%20look%20plausible.%0A%20%20%20%20A%20rule%20that%20only%20looks%20at%20single%20values%20can't%20tell%20those%20are%20wrong.%20The%0A%20%20%20%20calibration%20tutorial%20(coming%20with%20the%20next%20release)%20deals%20with%20them%20by%0A%20%20%20%20comparing%20against%20a%20trusted%20reference%20meter.%0A%0A%20%20%20%20The%20learned%20limits%20are%20the%20step's%20**state**%3A%20plain%20JSON%2C%20kept%20apart%20from%0A%20%20%20%20the%20spec.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fitted%2C%20mo)%3A%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%60fitted.states%60%20%E2%86%92%20%60%7Bfitted.states%7D%60%22)%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%20Saving%20a%20fitted%20pipeline%0A%0A%20%20%20%20%60save%60%20writes%20the%20spec%20and%20each%20step's%20state%20as%20JSON%20files%2C%20with%20no%0A%20%20%20%20pickle%2C%20so%20a%20saved%20pipeline%20is%20safe%20to%20load%20and%20easy%20to%20read.%20%60load%60%0A%20%20%20%20checks%20the%20spec%20hash%2C%20so%20a%20state%20can't%20be%20paired%20with%20the%20wrong%20recipe.%0A%20%20%20%20For%20passing%20a%20fitted%20pipeline%20between%20processes%2C%20%60to_payload%60%20gives%20the%0A%20%20%20%20same%20content%20as%20one%20JSON-able%20dict.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Path%2C%20fitted%2C%20fs%2C%20json%2C%20mo%2C%20tempfile%2C%20test)%3A%0A%20%20%20%20workdir%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22flexsignal-tutorial-%22))%0A%20%20%20%20saved_to%20%3D%20fitted.save(workdir%20%2F%20%22tukey_flow%22)%0A%20%20%20%20reloaded%20%3D%20fs.FittedPipeline.load(saved_to)%0A%20%20%20%20same_after_reload%20%3D%20reloaded.transform(test).equals(fitted.transform(test))%0A%20%20%20%20_payload%20%3D%20fitted.to_payload()%0A%20%20%20%20same_after_payload%20%3D%20(%0A%20%20%20%20%20%20%20%20fs.FittedPipeline.from_payload(json.loads(json.dumps(_payload)))%0A%20%20%20%20%20%20%20%20.transform(test)%0A%20%20%20%20%20%20%20%20.equals(fitted.transform(test))%0A%20%20%20%20)%0A%20%20%20%20_files%20%3D%20sorted(%0A%20%20%20%20%20%20%20%20p.relative_to(saved_to).as_posix()%20for%20p%20in%20saved_to.rglob(%22*.json%22)%0A%20%20%20%20)%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%20%22Files%20written%3A%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%2B%20%22%5Cn%22.join(f%22-%20%60%7Bname%7D%60%22%20for%20name%20in%20_files)%0A%20%20%20%20%20%20%20%20%2B%20f%22%5Cn%5CnSame%20result%20after%20%60load%60%3A%20**%7Bsame_after_reload%7D**%3B%20%22%0A%20%20%20%20%20%20%20%20f%22after%20a%20JSON%20round-trip%20of%20%60to_payload()%60%3A%20**%7Bsame_after_payload%7D**.%22%0A%20%20%20%20)%0A%20%20%20%20return%20same_after_payload%2C%20same_after_reload%2C%20workdir%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%20Inputs%20and%20outputs%20in%20the%20spec%0A%0A%20%20%20%20A%20spec%20can%20also%20say%20where%20data%20comes%20from%20and%20where%20results%20go.%20A%0A%20%20%20%20%60frame%60%20input%20is%20a%20named%20slot%20that%20the%20caller%20fills%20at%20run%20time.%20A%0A%20%20%20%20%60parquet%60%20output%20keeps%20values%2C%20flags%2C%20units%20and%20provenance%20together%20in%0A%20%20%20%20one%20file.%20Relative%20paths%20resolve%20against%20the%20spec%20file's%20folder%2C%20so%20a%0A%20%20%20%20folder%20holding%20a%20spec%20and%20its%20data%20can%20be%20moved%20as%20a%20whole.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20json%2C%20mo%2C%20pipe%2C%20raw%2C%20read_parquet%2C%20workdir)%3A%0A%20%20%20%20_spec%20%3D%20fs.dump_spec(pipe.spec)%0A%20%20%20%20_spec%5B%22inputs%22%5D%20%3D%20%5B%7B%22kind%22%3A%20%22frame%22%2C%20%22name%22%3A%20%22historian%22%7D%5D%0A%20%20%20%20_spec%5B%22outputs%22%5D%20%3D%20%5B%7B%22kind%22%3A%20%22parquet%22%2C%20%22path%22%3A%20%22flow_clean.parquet%22%7D%5D%0A%20%20%20%20_spec_file%20%3D%20workdir%20%2F%20%22flow_cleanup.json%22%0A%20%20%20%20_spec_file.write_text(json.dumps(_spec%2C%20indent%3D2))%0A%0A%20%20%20%20file_run%20%3D%20fs.run(_spec_file%2C%20%7B%22historian%22%3A%20raw%7D)%0A%20%20%20%20from_disk%20%3D%20read_parquet(workdir%20%2F%20%22flow_clean.parquet%22)%0A%20%20%20%20disk_round_trip%20%3D%20from_disk.equals(file_run.signal)%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%22Wrote%20%60%7Bfile_run.outputs%5B0%5D%7D%60.%20Read%20back%20with%20%60read_parquet%60%2C%20it%20is%20%22%0A%20%20%20%20%20%20%20%20f%22identical%20to%20the%20result%2C%20flags%20and%20history%20included%3A%20**%7Bdisk_round_trip%7D**%22%0A%20%20%20%20)%0A%20%20%20%20return%20(disk_round_trip%2C)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20disk_round_trip%2C%0A%20%20%20%20hash_table%2C%0A%20%20%20%20identical%2C%0A%20%20%20%20result%2C%0A%20%20%20%20same_after_payload%2C%0A%20%20%20%20same_after_reload%2C%0A%20%20%20%20spec_errors%2C%0A%20%20%20%20tukey_comparison%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%20identical%20and%20same_after_reload%20and%20same_after_payload%20and%20disk_round_trip%0A%20%20%20%20assert%20hash_table%5B%22spec_hash%22%5D%5B0%5D%20%3D%3D%20hash_table%5B%22spec_hash%22%5D%5B1%5D%0A%20%20%20%20assert%20hash_table%5B%22spec_hash%22%5D%5B0%5D%20!%3D%20hash_table%5B%22spec_hash%22%5D%5B2%5D%0A%20%20%20%20assert%20len(spec_errors)%20%3D%3D%203%0A%20%20%20%20assert%20%22steps.0.n_sigma%22%20in%20spec_errors%5B0%5D%0A%20%20%20%20_trained%2C%20_self%20%3D%20tukey_comparison.to_dict(%22records%22)%0A%20%20%20%20assert%20_trained%5B%22offset%20readings%20removed%22%5D%20%3E%20_self%5B%22offset%20readings%20removed%22%5D%0A%20%20%20%20assert%20_trained%5B%22good%20readings%20removed%22%5D%20%3D%3D%200%0A%20%20%20%20assert%20%5Br%5B%22kind%22%5D%20for%20r%20in%20result.provenance%5D%5B0%5D%20%3D%3D%20%22range%22%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%20So%20far%20there%20has%20been%20one%20stream.%20**Tutorial%204**%20works%20with%20several%3A%0A%20%20%20%20lining%20up%20sources%20with%20different%20clocks%2C%20cleaning%20only%20while%20the%20pump%0A%20%20%20%20runs%2C%20and%20rules%20that%20involve%20more%20than%20one%20stream.%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
4f4d8b3ff8d2560b27fa355c26c41ba5