Skip to content

Commit b66a580

Browse files
committed
DOC: Document the PET notebook
Document the PET notebook: add markdown cells explaining the steps.
1 parent 19af931 commit b66a580

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

docs/notebooks/pet_motion_estimation.ipynb

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
{
22
"cells": [
33
{
4-
"cell_type": "code",
5-
"execution_count": 1,
6-
"id": "4111cc1b-5123-407a-8ec6-56d65bc94bc4",
74
"metadata": {},
5+
"cell_type": "markdown",
6+
"source": [
7+
"# Positron emission tomography (PET) data realignment example\n",
8+
"\n",
9+
"This example shows how to estimate the head motion of a PET dataset using\n",
10+
"`NiFreeze`.\n",
11+
"\n",
12+
"The notebook uses the `sub-02` dataset that was generated synthetically from\n",
13+
"a real PET dataset by adding random motion. The dataset\n",
14+
"can be installed from [GIN G-node](https://gin.g-node.org/nipreps-data/tests-nifreeze):\n",
15+
"\n",
16+
"```\n",
17+
"$ datalad install -g https://gin.g-node.org/nipreps-data/tests-nifreeze.git\n",
18+
"```\n",
19+
"\n",
20+
"after which the environment variable `TEST_DATA_HOME` will need to be set to\n",
21+
"point to the corresponding folder."
22+
],
23+
"id": "e8a4c20508512623"
24+
},
25+
{
26+
"metadata": {},
27+
"cell_type": "code",
828
"outputs": [],
29+
"execution_count": 1,
930
"source": [
1031
"from os import getenv\n",
1132
"from pathlib import Path\n",
1233
"\n",
1334
"from nifreeze.data.pet import PET\n",
1435
"\n",
15-
"# Install test data from gin.g-node.org:\n",
16-
"# $ datalad install -g https://gin.g-node.org/nipreps-data/tests-nifreeze.git\n",
17-
"# and point the environment variable TEST_DATA_HOME to the corresponding folder\n",
1836
"DATA_PATH = Path(getenv(\"TEST_DATA_HOME\", str(Path.home() / \"nifreeze-tests\")))\n",
1937
"WORKDIR = Path.home() / \"tmp\" / \"nifreezedev\" / \"pet_data\"\n",
2038
"WORKDIR.mkdir(parents=True, exist_ok=True)\n",
@@ -30,7 +48,8 @@
3048
")\n",
3149
"\n",
3250
"pet_dataset = PET.load(pet_file, json_file)"
33-
]
51+
],
52+
"id": "a258948070bbc0c"
3453
},
3554
{
3655
"cell_type": "code",
@@ -53,6 +72,17 @@
5372
"pet_dataset"
5473
]
5574
},
75+
{
76+
"metadata": {},
77+
"cell_type": "markdown",
78+
"source": [
79+
"## Model fitting and motion correction\n",
80+
"\n",
81+
"The relevant structure to model PET data is `nifreeze.model.PETModel`. We\n",
82+
"instantiate it by providing it with the loaded PET dataset."
83+
],
84+
"id": "22dd60dcdd65f09d"
85+
},
5686
{
5787
"cell_type": "code",
5888
"execution_count": 4,
@@ -417,9 +447,13 @@
417447
"id": "d541ae17-8bf8-457a-8aeb-986a4324434a",
418448
"metadata": {},
419449
"outputs": [],
420-
"source": [
421-
"model.fit_predict(None)"
422-
]
450+
"source": "model.fit_predict(None)"
451+
},
452+
{
453+
"metadata": {},
454+
"cell_type": "markdown",
455+
"source": "Let's now ask the model for a prediction at the `midframe[2]` time point. By calling `model.fit_predict` the model is fitted using all the available data, and a prediction is requested on the time provided as the argument. The B-Spline PET model employs uses a B-Spline-based interpolation method to estimate the data for the requested frame.",
456+
"id": "fa33b648ed70d336"
423457
},
424458
{
425459
"cell_type": "code",
@@ -432,6 +466,15 @@
432466
"predicted = model.fit_predict(pet_dataset.midframe[index])"
433467
]
434468
},
469+
{
470+
"metadata": {},
471+
"cell_type": "markdown",
472+
"source": [
473+
"We now save the uncorrected and corrected data so that we can visualize\n",
474+
"the difference."
475+
],
476+
"id": "c4985826e2d8bc54"
477+
},
435478
{
436479
"cell_type": "code",
437480
"execution_count": 8,
@@ -452,6 +495,15 @@
452495
"nifti_img_after.to_filename(output_path_after)"
453496
]
454497
},
498+
{
499+
"metadata": {},
500+
"cell_type": "markdown",
501+
"source": [
502+
"Let's now visualize a number of axial, sagittal and coronal slices of the\n",
503+
"uncorrected and corrected data."
504+
],
505+
"id": "7dfb887dcc76aa65"
506+
},
455507
{
456508
"cell_type": "code",
457509
"execution_count": 10,
@@ -2241,6 +2293,19 @@
22412293
")"
22422294
]
22432295
},
2296+
{
2297+
"metadata": {},
2298+
"cell_type": "markdown",
2299+
"source": [
2300+
"## Motion estimation\n",
2301+
"\n",
2302+
"We now want to have an estimate of the motion that the model corrects. We will\n",
2303+
"need to instantiate the `nifreeze.estimator.PETMotionEstimator`, which will\n",
2304+
"take an instance of the model. We will call `run` to get the parameters of the\n",
2305+
"affine transform estimation."
2306+
],
2307+
"id": "95b6939bea2d1394"
2308+
},
22442309
{
22452310
"cell_type": "code",
22462311
"execution_count": 13,
@@ -2427,6 +2492,15 @@
24272492
"affines"
24282493
]
24292494
},
2495+
{
2496+
"metadata": {},
2497+
"cell_type": "markdown",
2498+
"source": [
2499+
"Let's now visualize the estimated motion: we will plot the translation and\n",
2500+
"rotation components in the affine transform for each axis."
2501+
],
2502+
"id": "991990094e4a07f3"
2503+
},
24302504
{
24312505
"cell_type": "code",
24322506
"execution_count": 16,

0 commit comments

Comments
 (0)