import%20marimo%0A%0A__generated_with%20%3D%20%220.18.1%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20from%20dipy.data%20import%20dsi_voxels%0A%20%20%20%20from%20dipy.core.gradients%20import%20gradient_table%0A%20%20%20%20from%20dipy.reconst.gqi%20import%20GeneralizedQSamplingModel%0A%20%20%20%20return%20GeneralizedQSamplingModel%2C%20dsi_voxels%2C%20gradient_table%2C%20mo%2C%20np%2C%20plt%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20GQI%20Round-Trip%20Prediction%20Notebook%0A%0A%20%20%20%20This%20notebook%20demonstrates%20**round-trip%20consistency**%20in%20GQI%20reconstruction%3A%0A%20%20%20%20-%20Fit%20GQI%20model%20on%20diffusion%20data%0A%20%20%20%20-%20Predict%20signals%20using%20the%20**same%20gradients**%20used%20for%20fitting%0A%20%20%20%20-%20Compare%20original%20vs%20predicted%20signals%20to%20validate%20reconstruction%20accuracy%0A%0A%20%20%20%20**Key%20Concepts%3A**%0A%20%20%20%20-%20Round-trip%20consistency%20tests%20if%20the%20model%20can%20accurately%20reconstruct%20training%20data%0A%20%20%20%20-%20High%20correlation%20indicates%20good%20model%20fit%0A%20%20%20%20-%20Tests%20both%20single-voxel%20and%20multi-voxel%20scenarios%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%20mo.md(%22%22%22%0A%20%20%20%20%23%23%201.%20Load%20Real%20DSI%20Data%0A%0A%20%20%20%20Using%20the%20same%20small_101D%20DSI%20dataset%20as%20in%20the%20tests%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(dsi_voxels%2C%20np)%3A%0A%20%20%20%20%23%20Load%20real%20DSI%20data%0A%20%20%20%20data%2C%20gtab%20%3D%20dsi_voxels()%0A%20%20%20%20print(f%22Data%20shape%3A%20%7Bdata.shape%7D%22)%0A%20%20%20%20print(f%22Number%20of%20gradients%3A%20%7Blen(gtab.bvals)%7D%22)%0A%20%20%20%20print(f%22b-values%20range%3A%20%5B%7Bgtab.bvals.min()%7D%2C%20%7Bgtab.bvals.max()%7D%5D%22)%0A%20%20%20%20print(f%22Number%20of%20b%3D0%20images%3A%20%7Bnp.sum(gtab.b0s_mask)%7D%22)%0A%20%20%20%20return%20data%2C%20gtab%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Remove%20the%20b0%20volumes%20from%20the%20train%20data%20and%20train%20gtab%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(data%2C%20gradient_table%2C%20gtab%2C%20np)%3A%0A%20%20%20%20non_b0_indices%20%3D%20np.where(~gtab.b0s_mask)%5B0%5D%0A%0A%20%20%20%20train_data%20%3D%20data%5B...%2C%20non_b0_indices%5D%0A%0A%20%20%20%20bvals%20%3D%20gtab.bvals%0A%20%20%20%20bvecs%20%3D%20gtab.bvecs%0A%0A%20%20%20%20train_bvals%20%3D%20bvals%5Bnon_b0_indices%5D%0A%20%20%20%20train_bvecs%20%3D%20bvecs%5Bnon_b0_indices%5D%0A%0A%20%20%20%20train_gtab%20%3D%20gradient_table(bvals%3Dtrain_bvals%2C%20bvecs%3Dtrain_bvecs)%0A%20%20%20%20return%20train_data%2C%20train_gtab%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%202.%20Single%20Voxel%20Round-Trip%20Test%0A%0A%20%20%20%20Test%20reconstruction%20accuracy%20on%20a%20single%20voxel%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(GeneralizedQSamplingModel%2C%20np%2C%20train_data%2C%20train_gtab)%3A%0A%20%20%20%20%23%20Select%20a%20voxel%20with%20good%20signal%0A%20%20%20%20voxel_coord%20%3D%20(0%2C%200%2C%200)%0A%20%20%20%20voxel_data%20%3D%20train_data%5Bvoxel_coord%5D%0A%0A%20%20%20%20print(f%22Selected%20voxel%20at%20%7Bvoxel_coord%7D%22)%0A%20%20%20%20print(f%22Signal%20range%3A%20%5B%7Bvoxel_data.min()%3A.3f%7D%2C%20%7Bvoxel_data.max()%3A.3f%7D%5D%22)%0A%20%20%20%20print(f%22Mean%20signal%3A%20%7Bvoxel_data.mean()%3A.3f%7D%22)%0A%0A%20%20%20%20%23%20Test%20both%20methods%0A%20%20%20%20methods%20%3D%20%5B%22standard%22%2C%20%22gqi2%22%5D%0A%20%20%20%20results%20%3D%20%7B%7D%0A%0A%20%20%20%20for%20_method%20in%20methods%3A%0A%20%20%20%20%20%20%20%20print(f%22%5Cn---%20Testing%20%7B_method%7D%20method%20---%22)%0A%0A%20%20%20%20%20%20%20%20%23%20Fit%20GQI%20model%0A%20%20%20%20%20%20%20%20_gq%20%3D%20GeneralizedQSamplingModel(train_gtab%2C%20method%3D_method%2C%20sampling_length%3D1.2)%0A%20%20%20%20%20%20%20%20voxel_fit%20%3D%20_gq.fit(voxel_data)%0A%0A%20%20%20%20%20%20%20%20%23%20Predict%20on%20same%20gradients%20(round-trip)%0A%20%20%20%20%20%20%20%20_voxel_predicted%20%3D%20voxel_fit.predict(train_gtab)%0A%0A%20%20%20%20%20%20%20%20%23%20Calculate%20metrics%0A%20%20%20%20%20%20%20%20_correlation%20%3D%20np.corrcoef(voxel_data%2C%20_voxel_predicted)%5B0%2C%201%5D%0A%20%20%20%20%20%20%20%20_mae%20%3D%20np.mean(np.abs(voxel_data%20-%20_voxel_predicted))%0A%0A%20%20%20%20%20%20%20%20print(f%22Correlation%3A%20%7B_correlation%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22Mean%20Absolute%20Error%3A%20%7B_mae%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Predicted%20range%3A%20%5B%7B_voxel_predicted.min()%3A.3f%7D%2C%20%7B_voxel_predicted.max()%3A.3f%7D%5D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20results%5B_method%5D%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22original%22%3A%20voxel_data%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22predicted%22%3A%20_voxel_predicted%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22correlation%22%3A%20_correlation%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mae%22%3A%20_mae%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20return%20results%2C%20voxel_coord%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%203.%20Single%20Voxel%20Visualization%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt%2C%20results%2C%20voxel_coord)%3A%0A%20%20%20%20def%20plot_voxel_reconstruction(results%2C%20voxel_coord%2C%20figsize%3D(14%2C%209))%3A%0A%20%20%20%20%20%20%20%20methods%20%3D%20%5B%22standard%22%2C%20%22gqi2%22%5D%0A%20%20%20%20%20%20%20%20method_labels%20%3D%20%5B%22Standard%20GQI%22%2C%20%22GQI2%22%5D%0A%20%20%20%20%20%20%20%20colors%20%3D%20%5B%22%231f77b4%22%2C%20%22%23ff7f0e%22%5D%0A%0A%20%20%20%20%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(2%2C%202%2C%20figsize%3Dfigsize)%0A%20%20%20%20%20%20%20%20fig.suptitle(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Voxel%20%7Bvoxel_coord%7D%20Signal%20Reconstruction%3A%20Original%20vs.%20Predicted%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20fontsize%3D14%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20weight%3D'bold'%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20x%20%3D%20np.arange(len(results%5B%22standard%22%5D%5B%22original%22%5D))%0A%0A%20%20%20%20%20%20%20%20for%20row%2C%20(method%2C%20label%2C%20color)%20in%20enumerate(zip(methods%2C%20method_labels%2C%20colors))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20orig%20%3D%20np.array(results%5Bmethod%5D%5B%22original%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20pred%20%3D%20np.array(results%5Bmethod%5D%5B%22predicted%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20corr%20%3D%20results%5Bmethod%5D%5B%22correlation%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20mae%20%3D%20results%5Bmethod%5D%5B%22mae%22%5D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20---%20Left%3A%20Scatter%20plot%20(0%20to%20max)%20---%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter%20%3D%20axes%5Brow%2C%200%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.scatter(orig%2C%20pred%2C%20alpha%3D0.7%2C%20s%3D25%2C%20color%3Dcolor%2C%20edgecolor%3D'k'%2C%20linewidth%3D0.3)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Compute%20shared%20axis%20limit%3A%20from%200%20to%20max%20of%20both%20signals%0A%20%20%20%20%20%20%20%20%20%20%20%20max_val%20%3D%20max(orig.max()%2C%20pred.max())%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.set_xlim(0%2C%20max_val)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.set_ylim(0%2C%20max_val)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Identity%20line%20(only%20within%20%5B0%2C%20max_val%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.plot(%5B0%2C%20max_val%5D%2C%20%5B0%2C%20max_val%5D%2C%20'k--'%2C%20lw%3D1.5%2C%20label%3D%22Perfect%20fit%22)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Linear%20fit%20(optional%20but%20useful)%0A%20%20%20%20%20%20%20%20%20%20%20%20slope%2C%20intercept%20%3D%20np.polyfit(orig%2C%20pred%2C%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20fit_x%20%3D%20np.array(%5B0%2C%20max_val%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20fit_y%20%3D%20slope%20*%20fit_x%20%2B%20intercept%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.plot(fit_x%2C%20fit_y%2C%20color%3D'red'%2C%20lw%3D1.8%2C%20label%3Df%22Fit%3A%20y%3D%7Bslope%3A.2f%7Dx%2B%7Bintercept%3A.2f%7D%22)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.set_xlabel(%22Original%20Signal%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.set_ylabel(%22Predicted%20Signal%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.set_title(f%22%7Blabel%7D%5CnPearson%20r%20%3D%20%7Bcorr%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.legend(fontsize%3D8)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.grid(True%2C%20alpha%3D0.3)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_scatter.set_aspect('equal'%2C%20adjustable%3D'box')%20%20%23%20square%20axes%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20---%20Right%3A%20Signal%20%2B%20residuals%20---%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_signal%20%3D%20axes%5Brow%2C%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_signal.plot(x%2C%20orig%2C%20'o-'%2C%20color%3D'black'%2C%20label%3D%22Original%22%2C%20markersize%3D4%2C%20linewidth%3D1.2)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_signal.plot(x%2C%20pred%2C%20'o--'%2C%20color%3Dcolor%2C%20label%3D%22Predicted%22%2C%20markersize%3D4%2C%20linewidth%3D1.2)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Residuals%20on%20twin%20axis%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_resid%20%3D%20ax_signal.twinx()%0A%20%20%20%20%20%20%20%20%20%20%20%20residuals%20%3D%20pred%20-%20orig%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_resid.vlines(x%2C%200%2C%20residuals%2C%20colors%3D'gray'%2C%20linestyles%3D'%3A'%2C%20alpha%3D0.6%2C%20linewidth%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_resid.axhline(0%2C%20color%3D'gray'%2C%20linewidth%3D0.8)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_resid.set_ylabel(%22Residual%20(Pred%20%E2%80%93%20Orig)%22%2C%20color%3D'gray')%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_resid.tick_params(axis%3D'y'%2C%20labelcolor%3D'gray')%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_signal.set_xlabel(%22Gradient%20Index%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_signal.set_ylabel(%22Signal%20Intensity%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_signal.set_title(f%22%7Blabel%7D%5CnMAE%20%3D%20%7Bmae%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_signal.legend()%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_signal.grid(True%2C%20alpha%3D0.3)%0A%0A%20%20%20%20%20%20%20%20plt.tight_layout(rect%3D%5B0%2C%200%2C%201%2C%200.95%5D)%0A%20%20%20%20%20%20%20%20return%20fig%0A%0A%20%20%20%20plot_voxel_reconstruction(results%2C%20voxel_coord)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%204.%20Multi-Voxel%20Round-Trip%20Test%0A%0A%20%20%20%20Test%20reconstruction%20accuracy%20across%20multiple%20voxels%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(GeneralizedQSamplingModel%2C%20np%2C%20train_data%2C%20train_gtab)%3A%0A%20%20%20%20%23%20Test%20multi-voxel%20round-trip%20for%20both%20methods%0A%20%20%20%20multi_results%20%3D%20%7B%7D%0A%0A%20%20%20%20for%20_method%20in%20%5B%22standard%22%2C%20%22gqi2%22%5D%3A%0A%20%20%20%20%20%20%20%20print(f%22%5Cn%3D%3D%3D%20Multi-voxel%20%7B_method%7D%20method%20%3D%3D%3D%22)%0A%0A%20%20%20%20%20%20%20%20%23%20Fit%20on%20entire%203D%20dataset%0A%20%20%20%20%20%20%20%20_gq%20%3D%20GeneralizedQSamplingModel(train_gtab%2C%20method%3D_method%2C%20sampling_length%3D1.2)%0A%20%20%20%20%20%20%20%20multi_fit%20%3D%20_gq.fit(train_data)%0A%0A%20%20%20%20%20%20%20%20%23%20Predict%20on%20same%20gradients%0A%20%20%20%20%20%20%20%20multi_predicted%20%3D%20multi_fit.predict(train_gtab)%0A%0A%20%20%20%20%20%20%20%20%23%20Calculate%20voxel-wise%20correlations%0A%20%20%20%20%20%20%20%20correlations%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20total_voxels%20%3D%20train_data.shape%5B0%5D%20*%20train_data.shape%5B1%5D%20*%20train_data.shape%5B2%5D%0A%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(train_data.shape%5B0%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20j%20in%20range(train_data.shape%5B1%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20k%20in%20range(train_data.shape%5B2%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20original_voxel%20%3D%20train_data%5Bi%2C%20j%2C%20k%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20predicted_voxel%20%3D%20multi_predicted%5Bi%2C%20j%2C%20k%5D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Skip%20voxels%20with%20no%20signal%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20np.sum(original_voxel)%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20correlation%20%3D%20np.corrcoef(original_voxel%2C%20predicted_voxel)%5B0%2C%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20correlations.append(correlation)%0A%0A%20%20%20%20%20%20%20%20_correlations%20%3D%20np.array(correlations)%0A%0A%20%20%20%20%20%20%20%20print(f%22Total%20voxels%3A%20%7Btotal_voxels%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22Average%20correlation%3A%20%7Bnp.mean(correlations)%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22Min%20correlation%3A%20%7Bnp.min(_correlations)%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22Max%20correlation%3A%20%7Bnp.max(_correlations)%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22Std%20correlation%3A%20%7Bnp.std(_correlations)%3A.4f%7D%22)%0A%0A%20%20%20%20%20%20%20%20%23%20Global%20metrics%0A%20%20%20%20%20%20%20%20overall_correlation%20%3D%20np.corrcoef(train_data.flatten()%2C%20multi_predicted.flatten())%5B%0A%20%20%20%20%20%20%20%20%20%20%20%200%2C%201%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20overall_mae%20%3D%20np.mean(np.abs(train_data%20-%20multi_predicted))%0A%0A%20%20%20%20%20%20%20%20print(f%22Overall%20correlation%3A%20%7Boverall_correlation%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22Overall%20MAE%3A%20%7Boverall_mae%3A.4f%7D%22)%0A%0A%20%20%20%20%20%20%20%20multi_results%5B_method%5D%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22predicted%22%3A%20multi_predicted%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22correlations%22%3A%20correlations%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22avg_correlation%22%3A%20np.mean(correlations)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22overall_correlation%22%3A%20overall_correlation%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22overall_mae%22%3A%20overall_mae%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20return%20(multi_results%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%205.%20Multi-Voxel%20Results%20Visualization%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(data%2C%20multi_results%2C%20np%2C%20plt)%3A%0A%20%20%20%20def%20plot_multi_voxel_results(multi_results%2C%20data_shape%2C%20figsize%3D(14%2C%2010))%3A%0A%20%20%20%20%20%20%20%20methods%20%3D%20%5B%22standard%22%2C%20%22gqi2%22%5D%0A%20%20%20%20%20%20%20%20method_labels%20%3D%20%5B%22Standard%20GQI%22%2C%20%22GQI2%22%5D%0A%20%20%20%20%20%20%20%20colors%20%3D%20%5B%22%231f77b4%22%2C%20%22%23ff7f0e%22%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Reconstruct%203D%20correlation%20maps%20for%20spatial%20visualization%0A%20%20%20%20%20%20%20%20corr_maps%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20method%20in%20methods%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20correlations_flat%20%3D%20np.array(multi_results%5Bmethod%5D%5B%22correlations%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20corr_map%20%3D%20np.full(data_shape%5B%3A3%5D%2C%20np.nan)%20%20%23%20(X%2C%20Y%2C%20Z)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Refill%20in%20original%20order%0A%20%20%20%20%20%20%20%20%20%20%20%20idx%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20i%20in%20range(data_shape%5B0%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20j%20in%20range(data_shape%5B1%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20k%20in%20range(data_shape%5B2%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20np.sum(data%5Bi%2C%20j%2C%20k%5D)%20%3E%200%3A%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%20%20%20%20corr_map%5Bi%2C%20j%2C%20k%5D%20%3D%20correlations_flat%5Bidx%5D%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%20%20%20%20idx%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20corr_maps%5Bmethod%5D%20%3D%20corr_map%0A%0A%20%20%20%20%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(2%2C%202%2C%20figsize%3Dfigsize)%0A%20%20%20%20%20%20%20%20fig.suptitle(%22Voxel-wise%20Signal%20Reconstruction%20Accuracy%3A%22%2C%20fontsize%3D14%2C%20weight%3D'bold')%0A%0A%20%20%20%20%20%20%20%20for%20row%2C%20(method%2C%20label%2C%20color)%20in%20enumerate(zip(methods%2C%20method_labels%2C%20colors))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20corr_flat%20%3D%20np.array(multi_results%5Bmethod%5D%5B%22correlations%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20corr_map%20%3D%20corr_maps%5Bmethod%5D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%201.%20Histogram%20(left%20column)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_hist%20%3D%20axes%5Brow%2C%200%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_hist.hist(corr_flat%2C%20bins%3D30%2C%20alpha%3D0.7%2C%20color%3Dcolor%2C%20edgecolor%3D'black'%2C%20density%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20mean_corr%20%3D%20np.mean(corr_flat)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_hist.axvline(mean_corr%2C%20color%3D'red'%2C%20linestyle%3D'--'%2C%20linewidth%3D2%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%20%20%20%20label%3Df%22Mean%3A%20%7Bmean_corr%3A.4f%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_hist.set_xlabel(%22Voxel-wise%20Pearson%20Correlation%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_hist.set_ylabel(%22Density%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_hist.set_title(f%22%7Blabel%7D%3A%20Correlation%20Distribution%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_hist.legend()%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_hist.grid(True%2C%20alpha%3D0.3)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%202.%20Central%20slice%20of%20correlation%20map%20(right%20column)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_slice%20%3D%20axes%5Brow%2C%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Choose%20middle%20slice%20along%20the%20largest%20dimension%20for%20visibility%0A%20%20%20%20%20%20%20%20%20%20%20%20mid_i%2C%20mid_j%2C%20mid_k%20%3D%20np.array(data_shape%5B%3A3%5D)%20%2F%2F%202%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Prefer%20axial%20(k)%20if%20reasonable%20size%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20data_shape%5B2%5D%20%3E%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20slice_data%20%3D%20corr_map%5B%3A%2C%20%3A%2C%20mid_k%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%20%3D%20f%22%7Blabel%7D%3A%20Correlation%20Map%20(Axial%20Slice%20z%3D%7Bmid_k%7D)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20data_shape%5B1%5D%20%3E%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20slice_data%20%3D%20corr_map%5B%3A%2C%20mid_j%2C%20%3A%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%20%3D%20f%22%7Blabel%7D%3A%20Correlation%20Map%20(Coronal%20Slice%20y%3D%7Bmid_j%7D)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20slice_data%20%3D%20corr_map%5Bmid_i%2C%20%3A%2C%20%3A%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%20%3D%20f%22%7Blabel%7D%3A%20Correlation%20Map%20(Sagittal%20Slice%20x%3D%7Bmid_i%7D)%22%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20im%20%3D%20ax_slice.imshow(slice_data%2C%20cmap%3D%22viridis%22%2C%20origin%3D%22lower%22%2C%20vmin%3D0%2C%20vmax%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20ax_slice.set_title(title)%0A%20%20%20%20%20%20%20%20%20%20%20%20plt.colorbar(im%2C%20ax%3Dax_slice%2C%20fraction%3D0.046%2C%20pad%3D0.04)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Add%20summary%20stats%20as%20text%0A%20%20%20%20%20%20%20%20%20%20%20%20overall_corr%20%3D%20multi_results%5Bmethod%5D%5B%22overall_correlation%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20overall_mae%20%3D%20multi_results%5Bmethod%5D%5B%22overall_mae%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20stats_text%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22Mean%20corr%3A%20%7Bmean_corr%3A.4f%7D%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22Overall%20corr%3A%20%7Boverall_corr%3A.4f%7D%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22MAE%3A%20%7Boverall_mae%3A.4f%7D%22%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%20ax_hist.text(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%200.02%2C%200.98%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20stats_text%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20transform%3Dax_hist.transAxes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fontsize%3D9%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20verticalalignment%3D'top'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bbox%3Ddict(boxstyle%3D%22round%2Cpad%3D0.3%22%2C%20facecolor%3D%22white%22%2C%20alpha%3D0.8)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20plt.tight_layout()%0A%20%20%20%20%20%20%20%20return%20fig%0A%0A%20%20%20%20plot_multi_voxel_results(multi_results%2C%20data.shape)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%207.%20Summary%20Statistics%0A%0A%20%20%20%20Compare%20performance%20between%20methods%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(multi_results%2C%20results)%3A%0A%20%20%20%20print(%22%3D%3D%3D%20ROUND-TRIP%20PREDICTION%20SUMMARY%20%3D%3D%3D%5Cn%22)%0A%0A%20%20%20%20print(%22Single%20Voxel%20Results%3A%22)%0A%20%20%20%20for%20_method%20in%20%5B%22standard%22%2C%20%22gqi2%22%5D%3A%0A%20%20%20%20%20%20%20%20result%20%3D%20results%5B_method%5D%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%20%7B_method.title()%3A8s%7D%3A%20Correlation%20%3D%20%7Bresult%5B'correlation'%5D%3A.4f%7D%2C%20MAE%20%3D%20%7Bresult%5B'mae'%5D%3A.4f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20print(%22%5CnMulti-Voxel%20Results%3A%22)%0A%20%20%20%20for%20_method%20in%20%5B%22standard%22%2C%20%22gqi2%22%5D%3A%0A%20%20%20%20%20%20%20%20result%20%3D%20multi_results%5B_method%5D%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%20%7B_method.title()%3A8s%7D%3A%20Avg%20Correlation%20%3D%20%7Bresult%5B'avg_correlation'%5D%3A.4f%7D%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Overall%20Correlation%20%3D%20%7Bresult%5B'overall_correlation'%5D%3A.4f%7D%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Overall%20MAE%20%3D%20%7Bresult%5B'overall_mae'%5D%3A.4f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%20%20%20%20%23%20Determine%20which%20method%20performed%20better%0A%20%20%20%20std_corr%20%3D%20multi_results%5B%22standard%22%5D%5B%22avg_correlation%22%5D%0A%20%20%20%20gqi2_corr%20%3D%20multi_results%5B%22gqi2%22%5D%5B%22avg_correlation%22%5D%0A%0A%20%20%20%20if%20std_corr%20%3E%20gqi2_corr%3A%0A%20%20%20%20%20%20%20%20print(f%22%5CnStandard%20method%20performed%20better%20(%7Bstd_corr%3A.4f%7D%20vs%20%7Bgqi2_corr%3A.4f%7D)%22)%0A%20%20%20%20elif%20gqi2_corr%20%3E%20std_corr%3A%0A%20%20%20%20%20%20%20%20print(f%22%5CnGQI2%20method%20performed%20better%20(%7Bgqi2_corr%3A.4f%7D%20vs%20%7Bstd_corr%3A.4f%7D)%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(f%22%5CnBoth%20methods%20performed%20similarly%20(%7Bstd_corr%3A.4f%7D)%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%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
65dfbb656d0127bf745d5ad57d1af65a