Skip to content
Open

Ipmi #25

Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
carpentry: 'IDEAS'

# Overall title for pages.
title: 'Medical-Image-Registration-Short-Course'
title: 'MPHY0025 IPMI Registration Practical Exercises Instructions'

# Date the lesson was created (YYYY-MM-DD, this is empty by default)
created: '2024-05-13'
Expand Down
18,051 changes: 0 additions & 18,051 deletions episodes/fig/CT-PET-VI-02.svg

This file was deleted.

Binary file removed episodes/fig/Figure_1.png
Binary file not shown.
Binary file removed episodes/fig/colour-map-editor.png
Binary file not shown.
Binary file removed episodes/fig/final_output.png
Binary file not shown.
Binary file removed episodes/fig/final_results_vid.mp4
Binary file not shown.
Binary file removed episodes/fig/image-info.png
Binary file not shown.
6,817 changes: 0 additions & 6,817 deletions episodes/fig/itk-snap-16-32-bits.svg

This file was deleted.

Binary file removed episodes/fig/itk-snap-additional-image.png
Binary file not shown.
Binary file removed episodes/fig/itk-snap-calculating-registrations.png
Binary file not shown.
8,173 changes: 0 additions & 8,173 deletions episodes/fig/itk-snap-colour-overlay-as-semi-transparent-image.svg

This file was deleted.

4,964 changes: 0 additions & 4,964 deletions episodes/fig/itk-snap-colour-overlay-as-separate-image.svg

This file was deleted.

Binary file removed episodes/fig/itk-snap-image-manual-registration.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed episodes/fig/itk-snap-layer-inspector-info-tab.png
Binary file not shown.
Binary file not shown.
372 changes: 0 additions & 372 deletions episodes/fig/itk-snap-main-window.svg

This file was deleted.

133 changes: 0 additions & 133 deletions episodes/fig/itk-snap-opening-dicoms.svg

This file was deleted.

Binary file not shown.
Binary file removed episodes/fig/itk-snap-save-ct-for-pet.png
Binary file not shown.
Binary file removed episodes/fig/itk-snap-thumbnails.png
Binary file not shown.
Binary file removed episodes/fig/itk-snap-tools-reorient-image.png
Binary file not shown.
4,593 changes: 0 additions & 4,593 deletions episodes/fig/itk-snap-viewing-dicoms.svg

This file was deleted.

Binary file removed episodes/fig/itk-snap_ct_for_pet_cropped_nii_gz.png
Binary file not shown.
Binary file removed episodes/fig/layers-panel.png
Binary file not shown.
Binary file removed episodes/fig/level1.png
Diff not rendered.
Binary file removed episodes/fig/level2.png
Diff not rendered.
Binary file removed episodes/fig/level3.png
Diff not rendered.
Binary file removed episodes/fig/new-itk-snap-window.png
Diff not rendered.
Binary file removed episodes/fig/open-additional-image.png
Diff not rendered.
Binary file removed episodes/fig/overlay-wizard.png
Diff not rendered.
Binary file removed episodes/fig/p4-comp-jac.png
Diff not rendered.
Binary file removed episodes/fig/p4-def-field-comp.png
Diff not rendered.
Binary file removed episodes/fig/p4-def-field-zoom.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output-numlev1.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output-sigelastic0.5.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output-sigelastic0.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output-sigfluid0.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output2-numlev1.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output2-sigelastic0.5.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output2-sigelastic0.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output2-sigfluid0.png
Diff not rendered.
Binary file removed episodes/fig/p4-reg-output2.png
Diff not rendered.
File renamed without changes
File renamed without changes
Binary file added episodes/fig/practical4-comp-def-field-jac.png
Binary file added episodes/fig/practical4-comp-final.png
Binary file added episodes/fig/practical4-def-field-zoom.png
Binary file added episodes/fig/practical4-defparams-end.png
Binary file added episodes/fig/practical4-defparams-final.png
Binary file added episodes/fig/practical4-elastic0-final.png
Binary file added episodes/fig/practical4-elastic05-final-jac.png
Binary file added episodes/fig/practical4-elastic05-final.png
Binary file added episodes/fig/practical4-fig-controls.png
Binary file added episodes/fig/practical4-fluid0-end.png
Binary file added episodes/fig/practical4-fluid0-final.png
Binary file added episodes/fig/practical4-numlev1-final.png
Binary file removed episodes/fig/registration_animation.gif
Diff not rendered.
Binary file removed episodes/fig/registration_animation_comp.gif
Diff not rendered.
Diff not rendered.
Binary file removed episodes/fig/registration_animation_elastic0.gif
Diff not rendered.
Binary file removed episodes/fig/registration_animation_fluid0.gif
Diff not rendered.
Binary file removed episodes/fig/registration_animation_level1.gif
Diff not rendered.
Binary file removed episodes/fig/rotated_images.gif
Diff not rendered.
Binary file added episodes/fig/setup-vscode-ipmi-reg-folder.png
Binary file removed episodes/fig/vscode-ideas-reg.png
Diff not rendered.
Binary file removed episodes/fig/vscode-python-env.png
Diff not rendered.
Binary file removed episodes/fig/vscode-select-kernel.png
Diff not rendered.
Binary file removed episodes/fig/vscode.png
Diff not rendered.
427 changes: 427 additions & 0 deletions episodes/files/demonsReg.py

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions episodes/files/ipmi_reg_python_env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: ideas-reg
channels:
- conda-forge
- defaults
dependencies:
- ipykernel=6.29.5
- matplotlib=3.9.2
- nibabel=5.3.2
- numpy=2.1.3
- scikit-image=0.24.0
- scipy=1.14.1
272 changes: 272 additions & 0 deletions episodes/files/practical1_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Practical 1: An introduction to medical imaging data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Visualising images with ITK-SNAP\n",
"\n",
"#### This section is completed using ITK-SNAP.\n",
"\n",
"## 2. Viewing and understanding the NIfTI header with NiBabel\n",
"\n",
"### 2.1. Reading and displaying the NIfTI header"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# import packages\n",
"import numpy as np\n",
"import nibabel as nib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load the nifti file saved in section 1\n",
"ct_for_pet_nii = nib.load(\"data/practical1/CT_for_PET.nii.gz\")\n",
"\n",
"#this returns a Nifti1Image object\n",
"print(type(ct_for_pet_nii))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# display the size/shape of the image\n",
"print(ct_for_pet_nii.shape)\n",
"\n",
"# display the data type of the image\n",
"print(ct_for_pet_nii.get_data_dtype())\n",
"\n",
"# display the affine matrix for the image\n",
"print(ct_for_pet_nii.affine)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# print the header\n",
"print(ct_for_pet_nii.header)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.2 Specifying the affine transform"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# print the matrix represented by the qform\n",
"print(ct_for_pet_nii.get_qform())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Modifying NIfTI images and headers with NiBabel\n",
"\n",
"### 3.1. Changing the data type and qform code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# read the image data of the Nifti1Image object\n",
"# it gets loaded as a numpy array and casted to float64\n",
"ct_for_pet_img = ct_for_pet_nii.get_fdata()\n",
"\n",
"# check the type of the array\n",
"print(type(ct_for_pet_img))\n",
"\n",
"# check the type of the array elements\n",
"print(ct_for_pet_img.dtype)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# change the data type to float32\n",
"ct_for_pet_nii.set_data_dtype(np.float32)\n",
"\n",
"# set the qform code to unkown (0)\n",
"ct_for_pet_nii.set_qform(None, code='unknown')\n",
"\n",
"# check the header has been updated\n",
"print(ct_for_pet_nii.header)\n",
"\n",
"# save the float32 image to disk\n",
"nib.save(ct_for_pet_nii, 'data/practical1/CT_for_PET_float32.nii.gz')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Check data types with NiBabel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load the new image and check the data type is float32\n",
"ct_for_pet_float32_nii = nib.load(\"data/practical1/CT_for_PET_float32.nii.gz\")\n",
"print(ct_for_pet_float32_nii.get_data_dtype())\n",
"\n",
"# load the original image and check the data type is still int16\n",
"ct_for_pet_orig_nii = nib.load(\"data/practical1/CT_for_PET.nii.gz\")\n",
"print(ct_for_pet_orig_nii.get_data_dtype())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.2. Cropping images"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create a new array containing a copy of the desired slices\n",
"x_first = 91\n",
"x_last = 390\n",
"y_first = 131\n",
"y_last = 375\n",
"z_first = 21\n",
"z_last = 155\n",
"ct_for_pet_cropped_img = ct_for_pet_img[x_first:x_last+1, y_first:y_last+1, z_first:z_last+1].copy()\n",
"\n",
"# check the size of the arrays containing the original image and the cropped image\n",
"print(ct_for_pet_img.shape)\n",
"print(ct_for_pet_cropped_img.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create a new Nifti1Image object using the header and affine from the uncropped image\n",
"ct_for_pet_cropped_nii = nib.nifti1.Nifti1Image(ct_for_pet_cropped_img, ct_for_pet_nii.affine, ct_for_pet_nii.header)\n",
"\n",
"# check the shape and header of the new Nifti1Image object\n",
"print(ct_for_pet_cropped_nii.shape)\n",
"print(ct_for_pet_cropped_nii.header)\n",
"\n",
"# save the cropped image\n",
"nib.save(ct_for_pet_cropped_nii, \"data/practical1/CT_for_PET_cropped.nii.gz\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate the world coordinates of first voxel in the cropped image\n",
"cropped_origin = ct_for_pet_nii.affine @ np.array([x_first, y_first, z_first, 1])\n",
"print(cropped_origin)\n",
"\n",
"# use these to update the corresponding values in the affine matrix for the cropped image\n",
"ct_for_pet_cropped_nii.affine[0,3] = cropped_origin[0]\n",
"ct_for_pet_cropped_nii.affine[1,3] = cropped_origin[1]\n",
"ct_for_pet_cropped_nii.affine[2,3] = cropped_origin[2]\n",
"print(ct_for_pet_cropped_nii.affine)\n",
"\n",
"# save the cropped image - this also updates the sform values in the header using the updated affine\n",
"print(ct_for_pet_cropped_nii.header.get_sform())\n",
"nib.save(ct_for_pet_cropped_nii, \"data/practical1/CT_for_PET_cropped_aligned.nii.gz\")\n",
"print(ct_for_pet_cropped_nii.header.get_sform())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# we can do the same as above using nibabel's slicer attribute\n",
"ct_for_pet_slicer_nii = ct_for_pet_nii.slicer[x_first:x_last+1, y_first:y_last+1, z_first:z_last+1]\n",
"print(ct_for_pet_slicer_nii.header)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.3. Aligning data"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Add you code here to align the centre of the inhale_BH_CT with the centre of CT_for_PET\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ideas-reg",
"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.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading