<jupyter_start><jupyter_text>Introduction to Diffusion MRI data_*Much of the material in both the current notebook and the upcoming notebooks are adapted from the lessons on [neuroimaging analysis with Python](https://github.com/datacarpentry/python-neuroimaging-lesson)._ Diffusion Weighted Imaging (DWI)Diffusion imaging probes the random, microscopic motion of water protons by employing MRI sequences which are sensitive to the geometry and environmental organization surrounding the water protons. This is a popular technique for studying the white matter of the brain. The diffusion within biological structures, such as the brain, are often restricted due to barriers (eg. cell membranes), resulting in a preferred direction of diffusion (anisotropy). A typical diffusion MRI scan will acquire multiple volumes that are sensitive to a particular diffusion direction and result in diffusion-weighted images (DWI). Diffusion that exhibits directionality in the same direction result in an attenuated signal. With further processing (to be discussed later in the lesson), the acquired images can provide measurements which are related to the microscopic changes and estimate white matter trajectories. Images with no diffusion weighting are also acquired as part of the acquisition protocol.![fiber_configurations](images/DiffusionDirections.png) \Diffusion along X, Y, and Z directions  b-values & b-vectorsIn addition to the acquired diffusion images, two files are collected as part of the diffusion dataset. These files correspond to the gradient amplitude (b-values) and directions (b-vectors) of the diffusion measurement and are named with the extensiosn `.bval` and `.bvec` respectively. The b-value is the diffusion-sensitizing factor, and reflects the timing & strength of the gradients used to acquire the diffusion-weighted images. The b-vector corresponds to the direction of the diffusion sensitivity. Together these two files define the diffusion MRI measurement as a set of gradient directions and corresponding amplitudes.  DatasetFor the rest of this tutorial, we will make use of a subset of publicly available dataset, ds000030, from [openneuro.org](https://openneuro.org/datasets/ds000030) The dataset is structured according to the Brain Imaging Data Structure ([BIDS](https://bids-specification.readthedocs.io/en/etable/)).  Below is a tree diagram showing the folder structure of a single MR subject and session within ds000221. This was obtained by using the bash command `tree`.`!tree ../../data/ds000221````../../data/ds000221├── .bidsignore├── CHANGES├── dataset_description.json├── participants.tsv├── README├── derivatives/├── sub-010001/└── sub-010002/    ├── ses-01/    │    ├── anat    │    │    ├── sub-010002_ses-01_acq-lowres_FLAIR.json    │    │    ├── sub-010002_ses-01_acq-lowres_FLAIR.nii.gz    │    │    ├── sub-010002_ses-01_acq-mp2rage_defacemask.nii.gz    │    │    ├── sub-010002_ses-01_acq-mp2rage_T1map.nii.gz    │    │    ├── sub-010002_ses-01_acq-mp2rage_T1w.nii.gz    │    │    ├── sub-010002_ses-01_inv-1_mp2rage.json    │    │    ├── sub-010002_ses-01_inv-1_mp2rage.nii.gz    │    │    ├── sub-010002_ses-01_inv-2_mp2rage.json    │    │    ├── sub-010002_ses-01_inv-2_mp2rage.nii.gz    │    │    ├── sub-010002_ses-01_T2w.json    │    │    └── sub-010002_ses-01_T2w.nii.gz    │    ├── dwi    │    │    ├── sub-010002_ses-01_dwi.bval    │    │    │── sub-010002_ses-01_dwi.bvec    │    │    │── sub-010002_ses-01_dwi.json    │    │    └── sub-010002_ses-01_dwi.nii.gz    │    ├── fmap    │    │    ├── sub-010002_ses-01_acq-GEfmap_run-01_magnitude1.json    │    │    ├── sub-010002_ses-01_acq-GEfmap_run-01_magnitude1.nii.gz    │    │    ├── sub-010002_ses-01_acq-GEfmap_run-01_magnitude2.json    │    │    ├── sub-010002_ses-01_acq-GEfmap_run-01_magnitude2.nii.gz    │    │    ├── sub-010002_ses-01_acq-GEfmap_run-01_phasediff.json    │    │    ├── sub-010002_ses-01_acq-GEfmap_run-01_phasediff.nii.gz    │    │    ├── sub-010002_ses-01_acq-SEfmapBOLDpost_dir-AP_epi.json    │    │    ├── sub-010002_ses-01_acq-SEfmapBOLDpost_dir-AP_epi.nii.gz    │    │    ├── sub-010002_ses-01_acq-SEfmapBOLDpost_dir-PA_epi.json    │    │    ├── sub-010002_ses-01_acq-SEfmapBOLDpost_dir-PA_epi.nii.gz    │    │    ├── sub-010002_ses-01_acq-sefmapBOLDpre_dir-AP_epi.json    │    │    ├── sub-010002_ses-01_acq-sefmapBOLDpre_dir-AP_epi.nii.gz    │    │    ├── sub-010002_ses-01_acq-sefmapBOLDpre_dir-PA_epi.json    │    │    ├── sub-010002_ses-01_acq-sefmapBOLDpre_dir-PA_epi.nii.gz    │    │    ├── sub-010002_ses-01_acq-SEfmapBOLDpost_dir-AP_epi.json    │    │    ├── sub-010002_ses-01_acq-SEfmapBOLDpost_dir-AP_epi.nii.gz    │    │    ├── sub-010002_ses-01_acq-SEfmapBOLDpost_dir-PA_epi.json    │    │    ├── sub-010002_ses-01_acq-SEfmapBOLDpost_dir-PA_epi.nii.gz    │    │    ├── sub-010002_ses-01_acq-SEfmapDWI_dir-AP_epi.json    │    │    ├── sub-010002_ses-01_acq-SEfmapDWI_dir-AP_epi.nii.gz    │    │    ├── sub-010002_ses-01_acq-SEfmapDWI_dir-PA_epi.json    │    │    └── sub-010002_ses-01_acq-SEfmapDWI_dir-PA_epi.nii.gz    │    └── fmap    │    │    ├── sub-010002_ses-01_task-rest_acq-AP_run-01_bold.json    │    │    └── sub-010002_ses-01_task-rest_acq-AP_run-01_bold.nii.gz    └── ses-02/    ```  Querying a BIDS Dataset[`pybids`](https://bids-standard.github.io/pybids/) is a Python API for querying, summarizing and manipulating the BIDS folder structure. We will make use of `pybids` to query the necessary files. Lets first pull the metadata from its associated JSON file using the `get_metadata()` function for the first run.<jupyter_code>?BIDSLayout
from bids.layout import BIDSLayout

layout = BIDSLayout("../../../data/ds000221", validate=False)
dwi = layout.get(subject='010006', suffix='dwi', extension='nii.gz', return_type='file')[0]
layout.get_metadata(dwi)<jupyter_output><empty_output><jupyter_text>[`dipy`](http://dipy.org)For this lesson, we will use the `Dipy` (Diffusion Imaging in Python) package for processing and analysing diffusion MRI. Why `dipy`? - Fully free and open source- Implemented in Python. Easy to understand, and easy to use.- Implementations of many state-of-the art algorithms- High performance. Many algorithms implemented in [`cython`](http://cython.org/) Installing `dipy`The easiest way to install `Dipy` is to use `pip`! Additionally, `Dipy` makes use of the FURY library for visualization. We will also install this using `pip`!We can install it by entering the following in a terminal `pip install dipy`. We will do so using Jupyter Magic in the following cell!<jupyter_code>! pip install dipy
! pip install fury<jupyter_output><empty_output><jupyter_text>Defining a measurement: `GradientTable``Dipy` has a built-in function that allows us to read in `bval` and `bvec` files named `read_bvals_bvecs` under the `dipy.io.gradients` module. Let's first grab the path to our gradient directions and amplitude files and load them into memory.<jupyter_code>bvec = layout.get_bvec(dwi)
bval = layout.get_bval(dwi)
from dipy.io.gradients import read_bvals_bvecs
from dipy.core.gradients import gradient_table

gt_bvals, gt_bvecs = read_bvals_bvecs(bval, bvec)<jupyter_output><empty_output><jupyter_text>There is a also simple `GradientTable` object implemented in the `dipy.core.gradients` module. The input to the `GradientTable` should be our the values for our gradient directions and amplitudes we just read.<jupyter_code>gtab = gradient_table(gt_bvals, gt_bvecs)<jupyter_output><empty_output><jupyter_text>We will need this gradient table later on to process our data and generate diffusion tensor images (DTI)! There is also a built in function for gradient tables, `b0s_mask` that can be used to separate difussion weighted measurements from non-diffusion weighted measurements (b=0s/mm^2). Try to extract the vector corresponding to diffusion weighted measurements in the following cell!<jupyter_code>gtab.bvecs[~gtab.b0s_mask]<jupyter_output><empty_output><jupyter_text>It is also important to know where our diffusion weighting free measurements are as we need them for registration in our preprocessing, (our next notebook). The `b0s_mask` shows that this is the first volume of our dataset.<jupyter_code>gtab.b0s_mask<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10132105199369773, 'fraction_numerical': 0.04993334141316204, 'mean_word_length': 4.167188478396994, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '496692', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 3229, 'n_tokens_neox': 2847, 'n_words': 856, 'path': 'code/introduction/solutions/introduction_solutions.ipynb', 'repo_name': 'zkamvar/SDC-BIDS-dMRI', 'size': '8251'}