import%20marimo%0A%0A__generated_with%20%3D%20%220.18.0%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%20numpy%20as%20np%0A%20%20%20%20from%20dipy.core.gradients%20import%20gradient_table%0A%20%20%20%20from%20dipy.data%20import%20get_fnames%2C%20get_sphere%0A%20%20%20%20from%20dipy.direction%20import%20peaks_from_model%0A%20%20%20%20from%20dipy.io.gradients%20import%20read_bvals_bvecs%0A%20%20%20%20from%20dipy.io.image%20import%20load_nifti%0A%20%20%20%20from%20dipy.reconst.gqi%20import%20GeneralizedQSamplingModel%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20GeneralizedQSamplingModel%2C%0A%20%20%20%20%20%20%20%20get_fnames%2C%0A%20%20%20%20%20%20%20%20get_sphere%2C%0A%20%20%20%20%20%20%20%20gradient_table%2C%0A%20%20%20%20%20%20%20%20load_nifti%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20peaks_from_model%2C%0A%20%20%20%20%20%20%20%20read_bvals_bvecs%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Reconstruct%20with%20Generalized%20Q-Sampling%20Imaging%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(r%22%22%22%0A%20%20%20%20%23%23%20Load%20the%20data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(get_fnames)%3A%0A%20%20%20%20%23%20Load%20the%20raw%20diffusion%20MRI%20data%20(4D%20NIfTI)%2C%20b-values%2C%20and%20b-vectors%20from%20the%20built-in%20DSI%20dataset%0A%20%20%20%20q_fraw%2C%20q_fbval%2C%20q_fbvec%20%3D%20get_fnames(name%3D%22taiwan_ntu_dsi%22)%0A%20%20%20%20return%20q_fbval%2C%20q_fbvec%2C%20q_fraw%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20gradient_table%2C%0A%20%20%20%20load_nifti%2C%0A%20%20%20%20np%2C%0A%20%20%20%20q_fbval%2C%0A%20%20%20%20q_fbvec%2C%0A%20%20%20%20q_fraw%2C%0A%20%20%20%20read_bvals_bvecs%2C%0A)%3A%0A%20%20%20%20%23%20Load%20the%204D%20image%20data%2C%20its%20affine%20transformation%20matrix%2C%20and%20voxel%20dimensions%20(in%20mm)%0A%20%20%20%20q_data%2C%20q_affine%2C%20q_voxel_size%20%3D%20load_nifti(q_fraw%2C%20return_voxsize%3DTrue)%0A%0A%20%20%20%20%23%20Load%20b-values%20(diffusion%20weighting%20strengths)%20and%20b-vectors%20(gradient%20directions)%0A%20%20%20%20q_bvals%2C%20q_bvecs%20%3D%20read_bvals_bvecs(q_fbval%2C%20q_fbvec)%0A%0A%20%20%20%20%23%20Normalize%20b-vectors%20to%20unit%20length%20(skip%20the%20first%20row%2C%20which%20corresponds%20to%20the%20b%3D0%20volume%20and%20should%20remain%20%5B0%2C0%2C0%5D)%0A%20%20%20%20%23%20This%20ensures%20accurate%20directional%20modeling%20in%20diffusion%20reconstruction%0A%20%20%20%20q_bvecs%5B1%3A%5D%20%3D%20q_bvecs%5B1%3A%5D%20%2F%20np.sqrt(np.sum(q_bvecs%5B1%3A%5D%20*%20q_bvecs%5B1%3A%5D%2C%20axis%3D1))%5B%3A%2C%20None%5D%0A%0A%20%20%20%20%23%20Create%20a%20DIPY%20gradient%20table%20object%20that%20combines%20b-values%20and%20normalized%20b-vectors%20for%20use%20in%20reconstruction%20models%0A%20%20%20%20q_gtab%20%3D%20gradient_table(q_bvals%2C%20bvecs%3Dq_bvecs)%0A%0A%20%20%20%20%23%20Print%20the%20shape%20of%20the%20diffusion%20data%20(x%2C%20y%2C%20z%2C%20number_of_volumes)%20for%20verification%0A%20%20%20%20print(f%22data.shape%20%7Bq_data.shape%7D%22)%0A%20%20%20%20return%20q_data%2C%20q_gtab%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Initialize%20the%20Qball%20model%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(GeneralizedQSamplingModel%2C%20q_gtab)%3A%0A%20%20%20%20gqmodel%20%3D%20GeneralizedQSamplingModel(q_gtab%2C%20sampling_length%3D3)%0A%20%20%20%20return%20(gqmodel%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Only%20select%201%20slice%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(q_data)%3A%0A%20%20%20%20q_dataslice%20%3D%20q_data%5B%3A%2C%20%3A%2C%20q_data.shape%5B2%5D%20%2F%2F%202%5D%0A%0A%20%20%20%20q_mask%20%3D%20q_dataslice%5B...%2C%200%5D%20%3E%2050%0A%20%20%20%20return%20q_dataslice%2C%20q_mask%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Fit%20the%20slice%20to%20the%20Qball%20model%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(gqmodel%2C%20q_dataslice%2C%20q_mask)%3A%0A%20%20%20%20q_gqfit%20%3D%20gqmodel.fit(q_dataslice%2C%20mask%3Dq_mask)%0A%20%20%20%20return%20(q_gqfit%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Get%20an%20ODF%20(orientation%20distribution%20function)%20representation%20sphere%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(get_sphere)%3A%0A%20%20%20%20sphere%20%3D%20get_sphere(name%3D%22repulsion724%22)%0A%20%20%20%20return%20(sphere%2C)%0A%0A%0A%40app.cell%0Adef%20_(q_gqfit%2C%20sphere)%3A%0A%20%20%20%20q_ODF%20%3D%20q_gqfit.odf(sphere)%0A%0A%20%20%20%20print(f%22ODF.shape%20%7Bq_ODF.shape%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Get%20the%20main%20peaks%20from%20the%20ODF%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(gqmodel%2C%20peaks_from_model%2C%20q_dataslice%2C%20q_mask%2C%20sphere)%3A%0A%20%20%20%20gqpeaks%20%3D%20peaks_from_model(%0A%20%20%20%20%20%20%20%20model%3Dgqmodel%2C%0A%20%20%20%20%20%20%20%20data%3Dq_dataslice%2C%0A%20%20%20%20%20%20%20%20sphere%3Dsphere%2C%0A%20%20%20%20%20%20%20%20relative_peak_threshold%3D0.5%2C%0A%20%20%20%20%20%20%20%20min_separation_angle%3D25%2C%0A%20%20%20%20%20%20%20%20mask%3Dq_mask%2C%0A%20%20%20%20%20%20%20%20return_odf%3DFalse%2C%0A%20%20%20%20%20%20%20%20normalize_peaks%3DTrue%2C%0A%20%20%20%20)%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
57ce57c12a85ce27d4ca52fe4350eb56