import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%0A%20%20%20%20return%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd)%3A%0A%20%20%20%20%23%20Generate%20a%20reproducible%2C%20self-contained%20longitudinal%20neuroimaging%20dataset%0A%20%20%20%20%23%208%20subjects%2C%2010%20timepoints%2C%202%20experimental%20conditions%20(Stimulus%20vs%20Cue)%2C%202%20brain%20regions%20(Frontal%2C%20Parietal)%0A%20%20%20%20rng%20%3D%20np.random.default_rng(42)%0A%0A%20%20%20%20subjects%20%3D%20%5Bf%22s%7Bi%3A02d%7D%22%20for%20i%20in%20range(1%2C%209)%5D%0A%20%20%20%20timepoints%20%3D%20list(range(10))%0A%20%20%20%20events%20%3D%20%5B%22cue%22%2C%20%22stim%22%5D%0A%20%20%20%20regions%20%3D%20%5B%22Frontal%22%2C%20%22Parietal%22%5D%0A%0A%20%20%20%20records%20%3D%20%5B%5D%0A%20%20%20%20for%20_sub%20in%20subjects%3A%0A%20%20%20%20%20%20%20%20%23%20Subject-specific%20baseline%20shift%0A%20%20%20%20%20%20%20%20_sub_offset%20%3D%20rng.normal(0.0%2C%200.05)%0A%20%20%20%20%20%20%20%20for%20_tp%20in%20timepoints%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_ev%20in%20events%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20_reg%20in%20regions%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Synthetic%20hemodynamic%20response%20curve%20(gamma-like%20impulse%20response)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_t_peak%20%3D%204.0%20if%20_ev%20%3D%3D%20%22stim%22%20else%202.5%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_amp%20%3D%200.25%20if%20_ev%20%3D%3D%20%22stim%22%20else%200.12%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_reg_scale%20%3D%201.2%20if%20_reg%20%3D%3D%20%22Frontal%22%20else%200.8%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_signal_mean%20%3D%20_amp%20*%20_reg_scale%20*%20(_tp%20%2F%20_t_peak)%20*%20np.exp(1.0%20-%20(_tp%20%2F%20_t_peak))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_noise%20%3D%20rng.normal(0.0%2C%200.03)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_signal%20%3D%20float(_signal_mean%20%2B%20_sub_offset%20%2B%20_noise)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20records.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22subject%22%3A%20_sub%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22timepoint%22%3A%20_tp%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22event%22%3A%20_ev%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22region%22%3A%20_reg%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22signal%22%3A%20_signal%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20df_long%20%3D%20pd.DataFrame(records)%0A%20%20%20%20n_rows_long%20%3D%20len(df_long)%0A%20%20%20%20return%20(df_long%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(df_long)%3A%0A%20%20%20%20_head_md%20%3D%20df_long.head(6).to_markdown(index%3DFalse)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df_long)%3A%0A%20%20%20%20%23%201.%20Standard%20Pivot%20Table%3A%20Mean%20signal%20across%20timepoints%20and%20events%0A%20%20%20%20df_pivoted_event%20%3D%20df_long.pivot_table(%0A%20%20%20%20%20%20%20%20index%3D%22timepoint%22%2C%0A%20%20%20%20%20%20%20%20columns%3D%22event%22%2C%0A%20%20%20%20%20%20%20%20values%3D%22signal%22%2C%0A%20%20%20%20%20%20%20%20aggfunc%3D%22mean%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%202.%20Multi-Index%20Pivot%3A%20Mean%20signal%20indexed%20by%20timepoint%20and%20partitioned%20across%20(region%2C%20event)%0A%20%20%20%20df_pivoted_multi%20%3D%20df_long.pivot_table(%0A%20%20%20%20%20%20%20%20index%3D%22timepoint%22%2C%0A%20%20%20%20%20%20%20%20columns%3D%5B%22region%22%2C%20%22event%22%5D%2C%0A%20%20%20%20%20%20%20%20values%3D%22signal%22%2C%0A%20%20%20%20%20%20%20%20aggfunc%3D%22mean%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%203.%20Standard%20Deviation%20Pivot%20(Dispersion%20across%20subjects)%0A%20%20%20%20df_pivoted_std%20%3D%20df_long.pivot_table(%0A%20%20%20%20%20%20%20%20index%3D%22timepoint%22%2C%0A%20%20%20%20%20%20%20%20columns%3D%22event%22%2C%0A%20%20%20%20%20%20%20%20values%3D%22signal%22%2C%0A%20%20%20%20%20%20%20%20aggfunc%3D%22std%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%204.%20Verification%3A%20Melt%20the%20pivoted%20table%20back%20to%20long%20format%0A%20%20%20%20df_melted_back%20%3D%20df_pivoted_event.reset_index().melt(%0A%20%20%20%20%20%20%20%20id_vars%3D%22timepoint%22%2C%0A%20%20%20%20%20%20%20%20value_vars%3D%5B%22cue%22%2C%20%22stim%22%5D%2C%0A%20%20%20%20%20%20%20%20var_name%3D%22event%22%2C%0A%20%20%20%20%20%20%20%20value_name%3D%22signal_mean%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20df_melted_back%2C%20df_pivoted_event%2C%20df_pivoted_multi%2C%20df_pivoted_std%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(df_pivoted_event%2C%20go%2C%20mo)%3A%0A%20%20%20%20%23%20Visualize%20the%20pivoted%20matrix%20as%20an%20annotated%20heatmap%0A%20%20%20%20_z_vals%20%3D%20df_pivoted_event.values%0A%20%20%20%20_x_cols%20%3D%20list(df_pivoted_event.columns)%0A%20%20%20%20_y_rows%20%3D%20%5Bf%22t%20%3D%20%7Bt%7D%22%20for%20t%20in%20df_pivoted_event.index%5D%0A%0A%20%20%20%20_fig%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20data%3Dgo.Heatmap(%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3D_z_vals%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_x_cols%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_y_rows%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorscale%3D%22Plasma%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorbar%3Ddict(title%3D%22Mean%20Signal%22%2C%20len%3D0.8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5B%5Bf%22%7Bv%3A.4f%7D%22%20for%20v%20in%20row%5D%20for%20row%20in%20_z_vals%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20texttemplate%3D%22%25%7Btext%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textfont%3Ddict(size%3D12%2C%20color%3D%22%23ffffff%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22Timepoint%3A%20%25%7By%7D%3Cbr%3EEvent%3A%20%25%7Bx%7D%3Cbr%3EMean%20Signal%3A%20%25%7Bz%3A.4f%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Pivot%20Table%20Heatmap%3A%20Mean%20BOLD%20Signal%20by%20Timepoint%20and%20Event%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Event%20Condition%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Timepoint%22%2C%0A%20%20%20%20%20%20%20%20yaxis_autorange%3D%22reversed%22%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D80%2C%20r%3D40%2C%20t%3D60%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Pivoted%202D%20Matrix%20Representation%0A%0A%20%20%20%20Pivoting%20aggregates%20the%20longitudinal%20subject%20records%20into%20a%20compact%202D%20summary%20matrix%20where%20temporal%20dynamics%20and%20condition%20differences%20can%20be%20evaluated%20instantaneously.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(df_pivoted_event%2C%20df_pivoted_std%2C%20go%2C%20mo)%3A%0A%20%20%20%20%23%20Time-series%20response%20curve%20with%20standard%20error%20bands%0A%20%20%20%20_timepoints%20%3D%20df_pivoted_event.index.values%0A%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%0A%20%20%20%20_colors%20%3D%20%7B%22stim%22%3A%20%22%233b82f6%22%2C%20%22cue%22%3A%20%22%23f59e0b%22%7D%0A%0A%20%20%20%20for%20_ev%20in%20%5B%22stim%22%2C%20%22cue%22%5D%3A%0A%20%20%20%20%20%20%20%20_mean%20%3D%20df_pivoted_event%5B_ev%5D.values%0A%20%20%20%20%20%20%20%20_std%20%3D%20df_pivoted_std%5B_ev%5D.values%0A%20%20%20%20%20%20%20%20_color%20%3D%20_colors%5B_ev%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Mean%20line%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_timepoints%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_mean%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22%7B_ev.capitalize()%7D%20Condition%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D_color%2C%20width%3D3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3Df%22%3Cb%3E%7B_ev.capitalize()%7D%3C%2Fb%3E%3Cbr%3Et%20%3D%20%25%7B%7Bx%7D%7D%3Cbr%3EMean%3A%20%25%7B%7By%3A.4f%7D%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Upper%20bound%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_timepoints%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_mean%20%2B%20_std%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Lower%20bound%20with%20fill%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_timepoints%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_mean%20-%20_std%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(width%3D0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fill%3D%22tonexty%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fillcolor%3Df%22rgba%7Btuple(list(int(_color.lstrip('%23')%5Bi%3Ai%2B2%5D%2C%2016)%20for%20i%20in%20(0%2C%202%2C%204))%20%2B%20%5B0.15%5D)%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22%7B_ev.capitalize()%7D%20(%2B%2F-%201%20SD)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Hemodynamic%20Response%20Trajectory%3A%20Stimulus%20vs%20Cue%20across%20Timepoints%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Timepoint%20(Scan%20Interval)%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Hemodynamic%20Signal%20Amplitude%22%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D420%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D60%2C%20r%3D40%2C%20t%3D60%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.80%2C%20y%3D0.95%2C%20bgcolor%3D%22rgba(255%2C255%2C255%2C0.8)%22%2C%20bordercolor%3D%22%23cbd5e1%22%2C%20borderwidth%3D1)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Longitudinal%20Trajectory%20Derived%20from%20Pivoted%20Columns%0A%0A%20%20%20%20Having%20events%20as%20dedicated%20matrix%20columns%20allows%20direct%20arithmetic%20operations%20between%20conditions%2C%20such%20as%20the%20differential%20contrast%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5CDelta(t)%20%3D%20P(t%2C%20%5Ctext%7Bstim%7D)%20-%20P(t%2C%20%5Ctext%7Bcue%7D)%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(df_pivoted_multi%2C%20go%2C%20make_subplots%2C%20mo)%3A%0A%20%20%20%20%23%20Multi-index%20pivot%20visualization%20across%20brain%20regions%20and%20events%0A%20%20%20%20_regions%20%3D%20%5B%22Frontal%22%2C%20%22Parietal%22%5D%0A%20%20%20%20_events%20%3D%20%5B%22stim%22%2C%20%22cue%22%5D%0A%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5Bf%22Brain%20Region%3A%20%7Breg%7D%22%20for%20reg%20in%20_regions%5D%2C%0A%20%20%20%20%20%20%20%20shared_yaxes%3DTrue%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.08%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_colors%20%3D%20%7B%22stim%22%3A%20%22%233b82f6%22%2C%20%22cue%22%3A%20%22%23f59e0b%22%7D%0A%0A%20%20%20%20for%20_col_idx%2C%20_reg%20in%20enumerate(_regions%2C%20start%3D1)%3A%0A%20%20%20%20%20%20%20%20for%20_ev%20in%20_events%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_y_series%20%3D%20df_pivoted_multi%5B(_reg%2C%20_ev)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_y_series.index.values%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_y_series.values%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22%7B_ev.capitalize()%7D%20(%7B_reg%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D_colors%5B_ev%5D%2C%20width%3D2.5%2C%20dash%3D%22solid%22%20if%20_reg%20%3D%3D%20%22Frontal%22%20else%20%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D7)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3Df%22%3Cb%3E%7B_reg%7D%20-%20%7B_ev%7D%3C%2Fb%3E%3Cbr%3Et%20%3D%20%25%7B%7Bx%7D%7D%3Cbr%3ESignal%3A%20%25%7B%7By%3A.4f%7D%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%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%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20col%3D_col_idx%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_fig.update_xaxes(title_text%3D%22Timepoint%22%2C%20row%3D1%2C%20col%3D_col_idx)%0A%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Signal%20Amplitude%22%2C%20row%3D1%2C%20col%3D1)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Hierarchical%20Multi-Index%20Pivot%3A%20Region%20x%20Event%20Factorial%20Decomposition%22%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D400%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D60%2C%20r%3D40%2C%20t%3D60%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Hierarchical%20Multi-Index%20Pivoting%0A%0A%20%20%20%20Pandas%20supports%20hierarchical%20indexing%20(Cartesian%20product%20of%20multiple%20row%20and%20column%20factors)%3A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20df.pivot_table(index%3D%22timepoint%22%2C%20columns%3D%5B%22region%22%2C%20%22event%22%5D%2C%20values%3D%22signal%22%2C%20aggfunc%3D%22mean%22)%0A%20%20%20%20%60%60%60%0A%20%20%20%20This%20corresponds%20to%20folding%20a%20relational%20table%20into%20a%203D%20tensor%20%24%5Cmathcal%7BT%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BT%20%5Ctimes%20R%20%5Ctimes%20E%7D%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(df_melted_back%2C%20mo)%3A%0A%20%20%20%20_top_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20The%20Inverse%20Transformation%3A%20Melting%20Back%20to%20Tidy%20Data%0A%0A%20%20%20%20To%20pass%20pivoted%20summaries%20into%20visualization%20libraries%20or%20regression%20models%2C%20we%20apply%20%60melt%60%3A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20df_pivoted.reset_index().melt(%0A%20%20%20%20%20%20%20%20id_vars%3D%22timepoint%22%2C%0A%20%20%20%20%20%20%20%20value_vars%3D%5B%22cue%22%2C%20%22stim%22%5D%2C%0A%20%20%20%20%20%20%20%20var_name%3D%22event%22%2C%0A%20%20%20%20%20%20%20%20value_name%3D%22signal%22%2C%0A%20%20%20%20)%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20_table_md%20%3D%20mo.md(df_melted_back.head(8).to_markdown(index%3DFalse))%0A%0A%20%20%20%20_bottom_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Algebraic%20Invariant%20Audit%0A%0A%20%20%20%20Let%20%24%5Cmathbf%7BP%7D%20%3D%20%5Cmathcal%7BP%7D(%5Cmathcal%7BD%7D)%24%20and%20%24%5Cmathcal%7BD%7D'%20%3D%20%5Cmathcal%7BM%7D(%5Cmathbf%7BP%7D)%24.%0A%20%20%20%20Every%20row%20%24(t%2C%20e%2C%20s)%24%20in%20%24%5Cmathcal%7BD%7D'%24%20exactly%20satisfies%20%24s%20%3D%20%5Cfrac%7B1%7D%7B%7CS(t%2C%20e)%7C%7D%20%5Csum_%7Bv%20%5Cin%20S(t%2C%20e)%7D%20v%24%2C%20verifying%20mathematical%20consistency.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%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
82b29d3ba93a28ddf31d754f8b6ef602