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%20warnings%0A%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%20pandas%20as%20pd%0A%0A%20%20%20%20import%20flexsignal%20as%20fs%0A%20%20%20%20from%20flexsignal.core%20import%20FLAG_DESCRIPTIONS%2C%20QUALITY_FLAGS%2C%20Flag%2C%20flag_names%0A%20%20%20%20from%20flexsignal.testing%20import%20synthetic%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20FLAG_DESCRIPTIONS%2C%0A%20%20%20%20%20%20%20%20Flag%2C%0A%20%20%20%20%20%20%20%20QUALITY_FLAGS%2C%0A%20%20%20%20%20%20%20%20flag_names%2C%0A%20%20%20%20%20%20%20%20fs%2C%0A%20%20%20%20%20%20%20%20mdates%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20pd%2C%0A%20%20%20%20%20%20%20%20plt%2C%0A%20%20%20%20%20%20%20%20synthetic%2C%0A%20%20%20%20%20%20%20%20warnings%2C%0A%20%20%20%20)%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%201.%20A%20first%20look%20at%20plant%20data%0A%0A%20%20%20%20flex-signal%20cleans%2C%20corrects%2C%20fills%20and%20forecasts%20plant%20timeseries%20for%20the%0A%20%20%20%20flex-pse%20ecosystem.%20These%20tutorials%20build%20up%20from%20the%20basics%3A%0A%0A%20%20%20%20%7C%20%23%20%7C%20Tutorial%20%7C%20What%20you%20learn%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%20**1**%20%7C%20**A%20first%20look**%20(this%20one)%20%7C%20%60SignalFrame%60%2C%20units%2C%20quality%20flags%2C%20and%20the%20synthetic%20test%20data%20%7C%0A%20%20%20%20%7C%202%20%7C%20Cleaning%20one%20stream%20%7C%20Each%20cleaning%20step%2C%20how%20to%20tune%20it%2C%20and%20why%20order%20matters%20%7C%0A%20%20%20%20%7C%203%20%7C%20Pipelines%20and%20specs%20%7C%20Saving%20a%20cleaning%20recipe%20as%20JSON%2C%20fitting%20steps%2C%20provenance%20%7C%0A%20%20%20%20%7C%204%20%7C%20Several%20streams%20%7C%20Aligning%20sources%2C%20masking%20operating%20periods%2C%20conditional%20rules%20%7C%0A%0A%20%20%20%20Later%20releases%20add%20tutorials%20for%20calibration%2C%20model-based%20imputation%2C%0A%20%20%20%20flex-pse%20models%20and%20forecasting.%0A%0A%20%20%20%20Every%20tutorial%20uses%20**synthetic%20data**.%20Because%20we%20inject%20each%20fault%0A%20%20%20%20ourselves%2C%20we%20know%20exactly%20where%20it%20is%2C%20so%20we%20can%20check%20whether%20a%20cleaning%0A%20%20%20%20step%20caught%20it.%0A%0A%20%20%20%20**Reading%20the%20code.**%20%60mo%60%20is%20%5Bmarimo%5D(https%3A%2F%2Fmarimo.io)%2C%20the%20notebook%0A%20%20%20%20these%20tutorials%20run%20in.%20It%20is%20used%20only%20to%20show%20results%3A%20%60mo.md%60%20renders%0A%20%20%20%20text%2C%20%60mo.vstack%60%20and%20%60mo.hstack%60%20lay%20outputs%20out%2C%20and%20%60mo.ui%60%20makes%20the%0A%20%20%20%20sliders%20and%20drop-downs.%20Each%20such%20line%20has%20a%20%22Display%20only%22%20or%20%22Notebook%0A%20%20%20%20control%22%20comment.%20As%20in%20Jupyter%2C%20a%20bare%20expression%20on%20a%20cell's%20last%20line%2C%0A%20%20%20%20such%20as%20%60df.head(8)%60%2C%20is%20displayed.%20In%20your%20own%20scripts%2C%20print%20those%0A%20%20%20%20results%20or%20leave%20them%20out%3B%20everything%20else%20is%20ordinary%20flex-signal%20and%0A%20%20%20%20pandas%20code.%0A%20%20%20%20%22%22%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%20The%20synthetic%20plant%0A%0A%20%20%20%20%60synthetic.plant()%60%20makes%20a%20week%20of%205-minute%20data%20from%20a%20simple%20pumping%0A%20%20%20%20station%3A%0A%0A%20%20%20%20-%20**%60flow%60**%20(m%C2%B3%2Fh)%3A%20about%20100%2C%20with%20a%20daily%20and%20a%20weekly%20cycle.%20It%20is%200%0A%20%20%20%20%20%20while%20the%20pump%20is%20off.%0A%20%20%20%20-%20**%60pressure%60**%20(bar)%3A%20rises%20with%20flow.%0A%20%20%20%20-%20**%60temperature%60**%20(%C2%B0C)%3A%20a%20daily%20cycle.%0A%20%20%20%20-%20**%60pump_on%60**%20(0%2F1)%3A%20whether%20the%20pump%20runs.%0A%20%20%20%20-%20**%60reference_flow%60**%20(m%C2%B3%2Fh)%3A%20a%20precise%20meter%20that%20only%20reports%20for%20two%0A%20%20%20%20%20%20hours%20a%20day.%20Tutorial%204%20and%20the%20calibration%20tutorial%20use%20it.%0A%0A%20%20%20%20A%20seed%20makes%20it%20repeatable%3A%20the%20same%20seed%20always%20gives%20the%20same%20data.%0A%0A%20%20%20%20To%20start%20the%20way%20you%20would%20with%20real%20data%2C%20we%20take%20the%20case%20out%20as%20a%0A%20%20%20%20**plain%20pandas%20DataFrame**%2C%20shaped%20like%20a%20historian%20export%3A%20a%20%60timestamp%60%0A%20%20%20%20column%2C%20then%20one%20column%20per%20sensor.%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%20synthetic.plant(seed%3D7%2C%20pump_off_periods%3D2)%0A%20%20%20%20df%20%3D%20case.observed.reset_index(names%3D%22timestamp%22)%0A%20%20%20%20df.head(8)%0A%20%20%20%20return%20case%2C%20df%0A%0A%0A%40app.cell%0Adef%20_(df%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%22%22%0A%20%20%20%20Nothing%20here%20is%20specific%20to%20flex-signal%3A%20%60df%60%20is%20an%20ordinary%0A%20%20%20%20%60%7Btype(df).__name__%7D%60%20with%20%7Blen(df)%7D%20rows.%20The%20timestamps%20are%20already%20in%0A%20%20%20%20UTC%20(%60%7Bdf%5B%22timestamp%22%5D.dtype%7D%60)%2C%20and%20each%20sensor%20is%20a%20float%20column.%20NaN%0A%20%20%20%20means%20%22no%20reading%22.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20mo)%3A%0A%20%20%20%20%23%20Notebook%20control%20(marimo)%3A%20moving%20it%20reruns%20every%20cell%20that%20uses%20it.%0A%20%20%20%20stream_picker%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%5Bc%20for%20c%20in%20df.columns%20if%20c%20!%3D%20%22timestamp%22%5D%2C%0A%20%20%20%20%20%20%20%20value%3D%22flow%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Stream%20to%20plot%22%2C%0A%20%20%20%20)%0A%20%20%20%20stream_picker%0A%20%20%20%20return%20(stream_picker%2C)%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20df%2C%20plt%2C%20stream_picker%2C%20style)%3A%0A%20%20%20%20_name%20%3D%20stream_picker.value%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.fill_between(%0A%20%20%20%20%20%20%20%20df%5B%22timestamp%22%5D%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%3Ddf%5B%22pump_on%22%5D%20%3D%3D%200%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%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(df%5B%22timestamp%22%5D%2C%20df%5B_name%5D%2C%20color%3DCOLORS%5B%22signal%22%5D%2C%20lw%3D0.8)%0A%20%20%20%20style(_ax%2C%20_name)%0A%20%20%20%20_ax.legend(frameon%3DFalse%2C%20loc%3D%22lower%20left%22%2C%20bbox_to_anchor%3D(0.75%2C%201.0))%0A%20%20%20%20_ax.set_title(f%22One%20week%20of%20%7B_name%7D%22%2C%20loc%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%20A%20SignalFrame%3A%20values%2C%20flags%20and%20units%0A%0A%20%20%20%20flex-signal%20keeps%20data%20in%20a%20**%60SignalFrame%60**.%20It%20holds%20three%20things%20that%0A%20%20%20%20always%20travel%20together%3A%0A%0A%20%20%20%20-%20**values**%3A%20one%20float%20column%20per%20stream%2C%20on%20a%20UTC%20time%20index%3B%0A%20%20%20%20-%20**flags**%3A%20one%20integer%20per%20value%2C%20recording%20what%20happened%20to%20it%3B%0A%20%20%20%20-%20**metadata**%3A%20units%20and%20descriptions%20per%20stream.%0A%0A%20%20%20%20No%20step%20ever%20deletes%20data.%20A%20step%20that%20rejects%20a%20value%20sets%20it%20to%20NaN%20and%0A%20%20%20%20records%20why%20in%20the%20flags%2C%20so%20you%20can%20always%20see%20what%20was%20removed%20and%20why.%0A%0A%20%20%20%20%23%23%23%20Building%20one%20from%20a%20DataFrame%0A%0A%20%20%20%20%60SignalFrame.from_frame%60%20turns%20a%20plain%20DataFrame%20into%20a%20SignalFrame.%20You%0A%20%20%20%20tell%20it%3A%0A%0A%20%20%20%20-%20**%60time_column%60**%3A%20which%20column%20holds%20the%20timestamps.%20They%20become%20the%0A%20%20%20%20%20%20index.%20Leave%20it%20out%20if%20the%20timestamps%20are%20already%20the%20index.%0A%20%20%20%20-%20**%60units%60**%3A%20the%20units%20of%20each%20stream%2C%20in%20the%20pint%20syntax%20flex-pse%20uses%0A%20%20%20%20%20%20(%60%22m%5E3%2Fhr%22%60%2C%20%60%22degC%22%60).%20%60pump_on%60%20is%20a%200%2F1%20state%2C%20so%20it%20has%20none.%0A%20%20%20%20-%20**%60timezone%60**%3A%20the%20time%20zone%20of%20timestamps%20that%20have%20none%20(see%20below).%0A%0A%20%20%20%20It%20checks%20the%20data%20(real%20timestamps%2C%20no%20duplicates%2C%20numeric%20columns)%2C%0A%20%20%20%20sorts%20it%20by%20time%2C%20and%20flags%20every%20NaN%20as%20%60MISSING%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20fs)%3A%0A%20%20%20%20sf%20%3D%20fs.SignalFrame.from_frame(%0A%20%20%20%20%20%20%20%20df%2C%0A%20%20%20%20%20%20%20%20time_column%3D%22timestamp%22%2C%0A%20%20%20%20%20%20%20%20units%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22flow%22%3A%20%22m%5E3%2Fhr%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22pressure%22%3A%20%22bar%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22temperature%22%3A%20%22degC%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22reference_flow%22%3A%20%22m%5E3%2Fhr%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20)%0A%20%20%20%20sf%0A%20%20%20%20return%20(sf%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20sf)%3A%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(f%22**Units**%20(%60sf.units%60)%3A%20%60%7Bsf.units%7D%60%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22**Values**%20(%60sf.values%60)%2C%20one%20float%20column%20per%20stream%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20sf.values.head(4)%2C%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**Flags**%20(%60sf.flags%60)%2C%20one%20integer%20per%20value.%200%20means%20nothing%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22has%20happened%20to%20the%20value.%20%60reference_flow%60%20shows%201%20(%60MISSING%60)%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22that%20meter%20only%20reports%20between%2009%3A00%20and%2011%3A00.%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%20sf.flags.head(4)%2C%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%60sf.to_pandas()%60%20gives%20you%20a%20plain%20DataFrame%20back%2C%20and%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%60sf.to_pandas(include_flags%3DTrue)%60%20adds%20a%20%60%3Cstream%3E__flags%60%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22column%20per%20stream.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%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(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**A%20shortcut%20for%20the%20synthetic%20data.**%20%60case.signal()%60%20does%20exactly%20what%0A%20%20%20%20the%20cell%20above%20does%2C%20with%20the%20case's%20own%20units.%20The%20later%20tutorials%20use%0A%20%20%20%20it%20to%20keep%20the%20code%20short.%0A%20%20%20%20%22%22%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%23%20Timestamps%20without%20a%20time%20zone%0A%0A%20%20%20%20Our%20synthetic%20timestamps%20carry%20a%20time%20zone%20(UTC)%2C%20so%20%60from_frame%60%20needed%0A%20%20%20%20no%20more.%20Many%20historian%20exports%20give%20local%20wall-clock%20times%20with%20no%20time%0A%20%20%20%20zone.%20flex-signal%20then%20takes%20them%20as%20**this%20machine's%20local%20time%20zone**%2C%0A%20%20%20%20which%20is%20usually%20right%20when%20the%20data%20comes%20from%20the%20plant%20you%20are%20working%0A%20%20%20%20on%2C%20and%20warns%20you%20that%20it%20did%20so.%20Internally%20every%20timestamp%20is%20still%20UTC.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fs%2C%20mo%2C%20pd%2C%20warnings)%3A%0A%20%20%20%20historian_export%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22timestamp%22%3A%20pd.date_range(%222025-01-06%2008%3A00%22%2C%20periods%3D4%2C%20freq%3D%2215min%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22flow%22%3A%20%5B101.2%2C%20103.8%2C%20float(%22nan%22)%2C%20104.1%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20%23%20Record%20the%20warning%20so%20the%20notebook%20can%20show%20it.%20In%20a%20script%2C%20Python%0A%20%20%20%20%23%20prints%20it%20for%20you%2C%20and%20you%20would%20just%20call%20from_frame.%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20assumed%20%3D%20fs.SignalFrame.from_frame(historian_export%2C%20time_column%3D%22timestamp%22)%0A%0A%20%20%20%20own%20%3D%20fs.SignalFrame.from_frame(%0A%20%20%20%20%20%20%20%20historian_export%2C%0A%20%20%20%20%20%20%20%20time_column%3D%22timestamp%22%2C%0A%20%20%20%20%20%20%20%20timezone%3D%22America%2FLos_Angeles%22%2C%0A%20%20%20%20%20%20%20%20units%3D%7B%22flow%22%3A%20%22m%5E3%2Fhr%22%7D%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%20f%22Without%20%60timezone%3D%60%2C%20on%20this%20machine%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22(%60fs.local_timezone()%60%20is%20%60%7Bfs.local_timezone()%7D%60)%3A%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3E%20%7Btype(caught%5B0%5D.message).__name__%7D%3A%20%7Bcaught%5B0%5D.message%7D%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22so%2008%3A00%20local%20became%20%60%7Bassumed.index%5B0%5D%7D%60.%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%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Pass%20%60timezone%3D%60%20when%20the%20data%20comes%20from%20somewhere%20else%2C%20or%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22when%20the%20result%20must%20be%20the%20same%20on%20every%20machine.%20With%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'%60timezone%3D%22America%2FLos_Angeles%22%60%2C%2008%3A00%20becomes%2016%3A00%20UTC%2C%20and%20'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22the%20NaN%20is%20flagged%20%60MISSING%60%20(flag%20value%201)%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%20own.to_pandas(include_flags%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20return%20caught%2C%20own%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%20Quality%20flags%0A%0A%20%20%20%20Each%20flag%20is%20one%20bit%2C%20so%20a%20value%20can%20carry%20several%20at%20once.%20Cleaning%20steps%0A%20%20%20%20set%20the%20**quality**%20flags%20when%20they%20remove%20a%20value.%20Other%20steps%20set%20flags%0A%20%20%20%20that%20only%20record%20what%20they%20did%2C%20such%20as%20filling%20a%20gap%20or%20resampling.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(FLAG_DESCRIPTIONS%2C%20QUALITY_FLAGS%2C%20pd)%3A%0A%20%20%20%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%22flag%22%3A%20flag.name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22bit%22%3A%20int(flag)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22removes%20the%20value%22%3A%20%22yes%22%20if%20flag%20%26%20QUALITY_FLAGS%20else%20%22%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22meaning%22%3A%20meaning%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22set%20by%22%3A%20set_by%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%20flag%2C%20(meaning%2C%20set_by)%20in%20FLAG_DESCRIPTIONS.items()%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%20flag_names%2C%20mo)%3A%0A%20%20%20%20combined%20%3D%20Flag.STUCK%20%7C%20Flag.IMPUTED_SHORT%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%20Flags%20combine%20with%20%60%7C%60.%20A%20value%20that%20was%20frozen%2C%20removed%2C%20and%20then%20filled%0A%20%20%20%20by%20interpolation%20carries%20%60Flag.STUCK%20%7C%20Flag.IMPUTED_SHORT%60%2C%20stored%20as%20the%0A%20%20%20%20integer%20**%7Bint(combined)%7D**.%20%60flag_names(%7Bint(combined)%7D)%60%20turns%20it%20back%20into%0A%20%20%20%20%60%7Bflag_names(int(combined))%7D%60.%0A%20%20%20%20%22%22%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%20Faults%20with%20known%20answers%0A%0A%20%20%20%20The%20%60with_*%60%20methods%20inject%20faults%20and%20record%20exactly%20where.%20Each%20returns%0A%20%20%20%20a%20new%20case%2C%20so%20they%20chain%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(case%2C%20pd)%3A%0A%20%20%20%20faulty%20%3D%20(%0A%20%20%20%20%20%20%20%20case.with_out_of_range(%22flow%22%2C%20count%3D4)%20%20%23%20-999%20error%20codes%0A%20%20%20%20%20%20%20%20.with_stuck(%22flow%22%2C%20length%3D36)%20%20%23%20a%20sensor%20frozen%20for%203%20hours%0A%20%20%20%20%20%20%20%20.with_spikes(%22flow%22%2C%20count%3D6%2C%20size%3D40)%20%20%23%20isolated%20one-sample%20spikes%0A%20%20%20%20%20%20%20%20.with_gaps(%22flow%22%2C%20count%3D4%2C%20length%3D2)%20%20%23%2010-minute%20dropouts%0A%20%20%20%20)%0A%20%20%20%20fault_kinds%20%3D%20%5B%22out_of_range%22%2C%20%22stuck%22%2C%20%22spike%22%2C%20%22gap%22%5D%0A%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22fault%22%3A%20fault_kinds%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22samples%20affected%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int(faulty.fault_mask(kind%2C%20%22flow%22).sum())%20for%20kind%20in%20fault_kinds%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%0A%20%20%20%20)%0A%20%20%20%20return%20fault_kinds%2C%20faulty%0A%0A%0A%40app.cell%0Adef%20_(COLORS%2C%20fault_kinds%2C%20faulty%2C%20mdates%2C%20pd%2C%20plt%2C%20style)%3A%0A%20%20%20%20_fig%2C%20_axes%20%3D%20plt.subplots(1%2C%204%2C%20figsize%3D(11%2C%203.0)%2C%20sharey%3DTrue)%0A%20%20%20%20_observed%2C%20_clean%20%3D%20faulty.observed%5B%22flow%22%5D%2C%20faulty.clean%5B%22flow%22%5D%0A%20%20%20%20for%20_ax%2C%20_kind%20in%20zip(_axes%2C%20fault_kinds%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20_mask%20%3D%20faulty.fault_mask(_kind%2C%20%22flow%22)%0A%20%20%20%20%20%20%20%20_first%20%3D%20_mask%5B_mask%5D.index%5B0%5D%0A%20%20%20%20%20%20%20%20_span%20%3D%20slice(_first%20-%20pd.Timedelta(%222h%22)%2C%20_first%20%2B%20pd.Timedelta(%224h%22))%0A%20%20%20%20%20%20%20%20_ax.plot(_clean%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_ax.plot(_observed%5B_span%5D%2C%20color%3DCOLORS%5B%22signal%22%5D%2C%20lw%3D0.9%2C%20label%3D%22observed%22)%0A%20%20%20%20%20%20%20%20if%20_kind%20%3D%3D%20%22gap%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.fill_between(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_mask%5B_span%5D.index%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20where%3D_mask%5B_span%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20transform%3D_ax.get_xaxis_transform()%2C%0A%20%20%20%20%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%20%20%20%20%20alpha%3D0.25%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20step%3D%22mid%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20-999%20is%20far%20off%20the%20scale%2C%20so%20draw%20it%20at%20the%20bottom%20edge.%0A%20%20%20%20%20%20%20%20%20%20%20%20_hits%20%3D%20_observed%5B_span%5D%5B_mask%5B_span%5D%5D.clip(lower%3D45)%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_hits.index%2C%20_hits%2C%20%22o%22%2C%20ms%3D4%2C%20color%3DCOLORS%5B%22fault%22%5D%2C%20label%3D%22fault%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.set_ylim(40%2C%20170)%0A%20%20%20%20%20%20%20%20_ax.set_title(_kind.replace(%22_%22%2C%20%22%20%22)%2C%20loc%3D%22left%22%2C%20fontsize%3D10)%0A%20%20%20%20%20%20%20%20_ax.xaxis.set_major_locator(mdates.HourLocator(interval%3D2))%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_kind%20%3D%3D%20%22out_of_range%22%20else%20None)%0A%20%20%20%20_fig.legend(%0A%20%20%20%20%20%20%20%20*_axes%5B0%5D.get_legend_handles_labels()%2C%0A%20%20%20%20%20%20%20%20frameon%3DFalse%2C%0A%20%20%20%20%20%20%20%20ncol%3D3%2C%0A%20%20%20%20%20%20%20%20loc%3D%22upper%20right%22%2C%0A%20%20%20%20%20%20%20%20fontsize%3D9%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.suptitle(%0A%20%20%20%20%20%20%20%20%22Each%20fault%2C%20zoomed%20in%20(UTC%3B%20-999%20codes%20drawn%20at%20the%20bottom%20edge)%22%2C%0A%20%20%20%20%20%20%20%20x%3D0.01%2C%0A%20%20%20%20%20%20%20%20ha%3D%22left%22%2C%0A%20%20%20%20%20%20%20%20fontsize%3D10%2C%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_(case%2C%20caught%2C%20faulty%2C%20fs%2C%20own%2C%20sf)%3A%0A%20%20%20%20%23%20These%20checks%20run%20when%20CI%20runs%20the%20tutorial%20as%20a%20script.%0A%20%20%20%20assert%20case.signal().equals(sf)%20%20%23%20the%20shortcut%20builds%20the%20same%20frame%0A%20%20%20%20assert%20%5Bw.category%20for%20w%20in%20caught%5D%20%3D%3D%20%5Bfs.NaiveTimestampWarning%5D%0A%20%20%20%20assert%20str(own.index%5B0%5D)%20%3D%3D%20%222025-01-06%2016%3A00%3A00%2B00%3A00%22%0A%20%20%20%20assert%20sf.flags.drop(columns%3D%22reference_flow%22).to_numpy().max()%20%3D%3D%200%0A%20%20%20%20assert%20sf.flag_counts()%5B%22reference_flow%22%5D%5B%22flags%22%5D%20%3D%3D%20%7B%0A%20%20%20%20%20%20%20%20%22MISSING%22%3A%20sf.values%5B%22reference_flow%22%5D.isna().sum()%0A%20%20%20%20%7D%0A%20%20%20%20assert%20own.flag_counts()%5B%22flow%22%5D%5B%22flags%22%5D%20%3D%3D%20%7B%22MISSING%22%3A%201%7D%0A%20%20%20%20assert%20int(faulty.fault_mask(%22gap%22%2C%20%22flow%22).sum())%20%3D%3D%208%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%20In%20**tutorial%202**%20we%20remove%20these%20faults%20one%20step%20at%20a%20time%2C%20and%20score%0A%20%20%20%20each%20step%20against%20the%20injected%20faults.%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
f9228a76354611656d4d28c259e44369