Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDR #183

Closed
wants to merge 27 commits into from
Closed

MDR #183

Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d029d25
"moco" folder created. Parameter files added plus first MDR implement…
Dec 31, 2021
0af0b82
mdr-library added to requirements and WIP integration
Jan 10, 2022
e94f6b4
Merge branch 'mdr_moco' of https://github.com/JSousa-UoL/ukat into md…
Jan 18, 2022
0df1543
Saving WIP. "moco" folder structure set.
Jan 19, 2022
e0ed2a9
Class MotionCorrectionm created and Jupyter Notebooks developed
Jan 20, 2022
83a5279
Unit tests work in progress. Issues with Elastix.
Jan 25, 2022
39e1764
Fixes in Elastix parameters and fitting functions. Still a WIP.
Jan 27, 2022
5897bb6
Progress in unit testing and Custom elastix parameters added
Jan 28, 2022
16b7730
SimpleITK removed from requirements.txt
JSousa-UoL Feb 7, 2022
ed26c47
Saving WIP March 2022
Mar 14, 2022
482664e
WIP 18th March
Mar 18, 2022
f06d265
Jupyter notebooks merged into 1 notebook named motion_correction.ipynb
Mar 22, 2022
a3cc6b9
MDR_library version 0.1.10
Mar 25, 2022
fc86e3f
Updated notebooks for analysis
Mar 29, 2022
bb341f4
Notebooks updated for scanners
Apr 2, 2022
b8761a0
mdr-library replaced with mdreg
Apr 10, 2022
30d9d7a
Docstrings added to the modules in "moco" foder
Apr 12, 2022
2016218
PEP8 fixes
Apr 12, 2022
7e69fa5
Merge branch 'dev' into mdr_moco
JSousa-UoL Apr 12, 2022
db0d6b4
Custom_Rigid added to constant model and some export improvements
Apr 13, 2022
20e4d30
PEP8 fix in mdr
Apr 13, 2022
6f4bd1f
Unit tests written. Still need to correct some of them
Apr 14, 2022
49c8f5f
PEP8 fixes
Apr 14, 2022
d07c0c3
Finishing unit tests
Apr 14, 2022
1955e55
PEP8 fixes and all outputs in the tutorial notebook displayed
Apr 15, 2022
35a5a74
Adjusting expected values decimals in the unit tests and tolerance ad…
Apr 15, 2022
58a13da
mdreg upgraded to 0.3.3
Apr 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
dipy>=1.2.0
itk-elastix>=0.13.0
matplotlib>=3.1.3
mdr-library==0.1.6
nibabel>=3.0.2
notebook>=6.0.1
numpy>=1.18.1
numpy>=1.21.2
pandas>=1.3.5
renalsegmentor>=1.3.3
scikit-image>=0.16.2
scipy>=1.4.1
SimpleITK>=1.2.4
scikit-learn>=0.24.2
scipy>=1.7.3
scikit-learn>=1.0.2
tabulate>=0.8.9
tqdm>=4.48
248 changes: 248 additions & 0 deletions tutorials/diffusion_moco.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# Diffusion motion correction tutorial"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Start by importing the required libraries and defining some settings:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from ukat.data import fetch\n",
"from ukat.mapping.diffusion import ADC, DTI\n",
"from ukat.moco.mdr import MotionCorrection\n",
"\n",
"# Ensure figures are rendered in the notebook\n",
"%matplotlib inline\n",
"\n",
"# Initialise output path for the Model-Driven Registration process\n",
"directory = 'diffusion_motion_correction_output'\n",
"os.makedirs(directory, exist_ok=True)\n",
"OUTPUT_DIR = os.path.join(os.getcwd(), directory)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Fetch test data\n",
"pixel_array, affine, bvals, bvecs = fetch.dwi_philips()\n",
"# Pre-processing as preparation for the Model-Driven Registration process\n",
"list_input_parameters = [affine, bvals, None, False]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 15878/15878 [00:02<00:00, 6786.63it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:43<00:00, 2.07s/it]\n",
"100%|██████████| 15906/15906 [00:02<00:00, 6177.13it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:39<00:00, 2.02s/it]\n",
"100%|██████████| 15898/15898 [00:02<00:00, 6033.26it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:41<00:00, 2.05s/it]\n",
"100%|██████████| 16059/16059 [00:02<00:00, 6306.56it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:41<00:00, 2.05s/it]\n",
"100%|██████████| 16029/16029 [00:02<00:00, 6742.23it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:36<00:00, 1.98s/it]\n",
"100%|██████████| 16042/16042 [00:02<00:00, 6809.34it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:50<00:00, 2.15s/it]\n",
"100%|██████████| 15965/15965 [00:02<00:00, 6079.89it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:39<00:00, 2.02s/it]\n",
"100%|██████████| 15676/15676 [00:02<00:00, 5486.48it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:43<00:00, 2.06s/it]\n",
"100%|██████████| 15130/15130 [00:02<00:00, 6129.13it/s]\n",
"Co-registration progress: 100%|██████████| 79/79 [02:47<00:00, 2.12s/it]\n"
]
}
],
"source": [
"# Model-driven registration\n",
"registration = MotionCorrection(pixel_array, affine, 'DWI_Moco', list_input_parameters, convergence=5, multithread=True, log=False)\n",
"registration.run()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Get the coregistered input pixel_array\n",
"moco_array = registration.get_coregistered()\n",
"# Save motion corrected diffusion sequence to NIfTI\n",
"registration.to_nifti(output_directory=OUTPUT_DIR, base_file_name='DWI_motion_corrected', maps=['coregistered', 'fitted', 'deformation_field'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 57399/57399 [00:08<00:00, 6998.09it/s]\n",
"100%|██████████| 57103/57103 [00:08<00:00, 6745.59it/s]\n"
]
}
],
"source": [
"# Generate a mask based on the intensity of the b0 volume. This will reduce computation times.\n",
"mask = pixel_array[..., 0] > 20000\n",
"\n",
"# Calculate maps from the original diffusion sequence using ADC methods and save as niftis\n",
"adc_mapper = ADC(pixel_array, affine, bvals, mask=mask, ukrin_b=True)\n",
"adc_mapper.to_nifti(output_directory=OUTPUT_DIR, base_file_name='diffusion_original', maps='all')\n",
"\n",
"# Calculate maps from the original diffusion sequence using DTI methods and save as niftis\n",
"dti_mapper = DTI(pixel_array, affine, bvals, bvecs, mask=mask, ukrin_b=True)\n",
"dti_mapper.to_nifti(output_directory=OUTPUT_DIR, base_file_name='diffusion_original', maps=['md', 'fa'])\n",
"\n",
"# Calculate maps from the motion corrected diffusion sequence using ADC methods and save as niftis\n",
"adc_mapper_moco = ADC(moco_array, affine, bvals, mask=mask, ukrin_b=True)\n",
"adc_mapper.to_nifti(output_directory=OUTPUT_DIR, base_file_name='diffusion_moco', maps='all')\n",
"\n",
"# Calculate maps from the motion corrected diffusion sequence using DTI methods and save as niftis\n",
"dti_mapper_moco = DTI(moco_array, affine, bvals, bvecs, mask=mask, ukrin_b=True)\n",
"dti_mapper.to_nifti(output_directory=OUTPUT_DIR, base_file_name='diffusion_moco', maps=['md', 'fa'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Display the central slice of each map\n",
"fig, ax = plt.subplots(2, 2, figsize=(8, 6))\n",
"\n",
"# Display a central slice of the ADC map\n",
"im = ax[0, 0].imshow(np.rot90(adc_mapper.adc[:, :, adc_mapper.shape[2]//2]), cmap='inferno', clim=(0.001, 0.003))\n",
"cb = fig.colorbar(im, ax=ax[0, 0])\n",
"cb.set_label('ADC ($mm^2/s$)')\n",
"ax[0, 0].axis('off')\n",
"\n",
"# Display a central slice of the MD map\n",
"im = ax[0, 1].imshow(np.rot90(dti_mapper.md[:, :, dti_mapper.shape[2]//2]), cmap='inferno', clim=(0.001, 0.003))\n",
"cb = fig.colorbar(im, ax=ax[0, 1])\n",
"cb.set_label('MD ($mm^2/s$)')\n",
"ax[0, 1].axis('off')\n",
"\n",
"# Display a central slice of the FA map\n",
"im = ax[1, 0].imshow(np.rot90(dti_mapper.fa[:, :, dti_mapper.shape[2]//2]), cmap='viridis', clim=(0.1, 0.8))\n",
"cb = fig.colorbar(im, ax=ax[1, 0])\n",
"cb.set_label('FA')\n",
"ax[1, 0].axis('off')\n",
"\n",
"# Display a central slice of the color FA map\n",
"im = ax[1, 1].imshow(np.rot90(dti_mapper.color_fa[:, :, dti_mapper.shape[2]//2, :]))\n",
"ax[1, 1].axis('off')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Display the central slice of each map\n",
"fig, ax = plt.subplots(2, 2, figsize=(8, 6))\n",
"\n",
"# Display a central slice of the ADC map\n",
"im = ax[0, 0].imshow(np.rot90(adc_mapper_moco.adc[:, :, adc_mapper_moco.shape[2]//2]), cmap='inferno', clim=(0.001, 0.003))\n",
"cb = fig.colorbar(im, ax=ax[0, 0])\n",
"cb.set_label('ADC ($mm^2/s$)')\n",
"ax[0, 0].axis('off')\n",
"\n",
"# Display a central slice of the MD map\n",
"im = ax[0, 1].imshow(np.rot90(dti_mapper_moco.md[:, :, dti_mapper_moco.shape[2]//2]), cmap='inferno', clim=(0.001, 0.003))\n",
"cb = fig.colorbar(im, ax=ax[0, 1])\n",
"cb.set_label('MD ($mm^2/s$)')\n",
"ax[0, 1].axis('off')\n",
"\n",
"# Display a central slice of the FA map\n",
"im = ax[1, 0].imshow(np.rot90(dti_mapper_moco.fa[:, :, dti_mapper_moco.shape[2]//2]), cmap='viridis', clim=(0.1, 0.8))\n",
"cb = fig.colorbar(im, ax=ax[1, 0])\n",
"cb.set_label('FA')\n",
"ax[1, 0].axis('off')\n",
"\n",
"# Display a central slice of the color FA map\n",
"im = ax[1, 1].imshow(np.rot90(dti_mapper_moco.color_fa[:, :, dti_mapper_moco.shape[2]//2, :]))\n",
"ax[1, 1].axis('off')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading