.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/tensor/plot_0_3D_diff_tensor_mri.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_tensor_plot_0_3D_diff_tensor_mri.py: Diffusion tensor MRI, 3D{6} dataset ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 6-14 The following is an example of a 3D{6} diffusion tensor MRI dataset with three spatial dimensions, :math:`d=3`, and one, :math:`p=1`, dependent-variable with six components. For illustration, we have reduced the size of the dataset. The complete diffusion tensor MRI dataset, in the CSDM format, is available `online `_. The original dataset [#f1]_ is also available. Let's import the CSDM data-file and look at its data structure. .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python import csdmpy as cp domain = "https://www.ssnmr.org/sites/default/files/CSDM" filename = f"{domain}/tensor/human_brain/brain_MRI_reduced_example.csdf" diff_mri = cp.load(filename) .. GENERATED FROM PYTHON SOURCE LINES 21-23 There are three linear dimensions in this dataset, corresponding to the x, y, and z spatial dimensions, .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: Python x = diff_mri.dimensions print(x[0].label, x[1].label, x[2].label) .. rst-class:: sphx-glr-script-out .. code-block:: none x y z .. GENERATED FROM PYTHON SOURCE LINES 27-30 and one six-component dependent variables holding the diffusion tensor components. Because the diffusion tensor is a symmetric second-rank tensor, we only need six tensor components. The components of the tensor are ordered as .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python y = diff_mri.dependent_variables print(y[0].component_labels) .. rst-class:: sphx-glr-script-out .. code-block:: none ['dxx', 'dxy', 'dxz', 'dyy', 'dyz', 'dzz'] .. GENERATED FROM PYTHON SOURCE LINES 34-36 The symmetric matrix information is also found with the :attr:`~csdmpy.DependentVariable.quantity_type` attribute, .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python print(y[0].quantity_type) .. rst-class:: sphx-glr-script-out .. code-block:: none symmetric_matrix_3 .. GENERATED FROM PYTHON SOURCE LINES 39-40 which implies a 3x3 symmetric matrix. .. GENERATED FROM PYTHON SOURCE LINES 42-48 **Visualize the dataset** In the following, we visualize the isotropic diffusion coefficient, that is, the average of the :math:`d_{xx}`, :math:`d_{yy}`, and :math:`d_{zz}` tensor components. Since it's a three-dimensional dataset, we'll visualize the projections onto the three dimensions. .. GENERATED FROM PYTHON SOURCE LINES 48-55 .. code-block:: Python # the isotropic diffusion coefficient. # component at index 0 = dxx # component at index 3 = dyy # component at index 5 = dzz isotropic_diffusion = (y[0].components[0] + y[0].components[3] + y[0].components[5]) / 3 .. GENERATED FROM PYTHON SOURCE LINES 56-58 In the following, we use certain features of the csdmpy module. Please refer to :ref:`generating_csdm_objects` for further details. .. GENERATED FROM PYTHON SOURCE LINES 58-66 .. code-block:: Python # Create a new csdm object from the isotropic diffusion coefficient array. new_csdm = cp.as_csdm(isotropic_diffusion, quantity_type="scalar") # Add the dimensions from `diff_mri` object to the `new_csdm` object. for i, dim in enumerate(x): new_csdm.dimensions[i] = dim .. GENERATED FROM PYTHON SOURCE LINES 67-69 Now, we can plot the projections of the isotropic diffusion coefficients along the respective dimensions as .. GENERATED FROM PYTHON SOURCE LINES 69-79 .. code-block:: Python import matplotlib.pyplot as plt # projection along the x-axis. plt.figure(figsize=(5, 4)) ax = plt.subplot(projection="csdm") cb = ax.imshow(new_csdm.sum(axis=0), cmap="gray_r", origin="upper", aspect="auto") plt.colorbar(cb, ax=ax) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/tensor/images/sphx_glr_plot_0_3D_diff_tensor_mri_001.png :alt: plot 0 3D diff tensor mri :srcset: /auto_examples/tensor/images/sphx_glr_plot_0_3D_diff_tensor_mri_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-89 .. code-block:: Python # projection along the y-axis. plt.figure(figsize=(5, 4)) ax = plt.subplot(projection="csdm") cb = ax.imshow(new_csdm.sum(axis=1), cmap="gray_r", origin="upper", aspect="auto") plt.colorbar(cb, ax=ax) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/tensor/images/sphx_glr_plot_0_3D_diff_tensor_mri_002.png :alt: plot 0 3D diff tensor mri :srcset: /auto_examples/tensor/images/sphx_glr_plot_0_3D_diff_tensor_mri_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-99 .. code-block:: Python # projection along the z-axis. plt.figure(figsize=(5, 4)) ax = plt.subplot(projection="csdm") cb = ax.imshow(new_csdm.sum(axis=2), cmap="gray_r", origin="upper", aspect="auto") plt.colorbar(cb, ax=ax) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/tensor/images/sphx_glr_plot_0_3D_diff_tensor_mri_003.png :alt: plot 0 3D diff tensor mri :srcset: /auto_examples/tensor/images/sphx_glr_plot_0_3D_diff_tensor_mri_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. rubric:: Citation .. [#f1] Diffusion tensor MRI `data `_; 2000. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.241 seconds) .. _sphx_glr_download_auto_examples_tensor_plot_0_3D_diff_tensor_mri.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_0_3D_diff_tensor_mri.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_0_3D_diff_tensor_mri.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_