Skip to content

Commit cb90840

Browse files
authored
Merge pull request #180 from jhlegarreta/sty/rename-pet-notebook
STY: Use more descriptive name for PET notebook file
2 parents a18cbad + 8d325f5 commit cb90840

File tree

1 file changed

+56
-47
lines changed

1 file changed

+56
-47
lines changed

docs/notebooks/PET_example.ipynb renamed to docs/notebooks/pet_motion_estimation.ipynb

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@
77
"metadata": {},
88
"outputs": [],
99
"source": [
10-
"import numpy as np\n",
11-
"from pathlib import Path\n",
12-
"import shutil\n",
13-
"import warnings\n",
1410
"from os import getenv\n",
11+
"from pathlib import Path\n",
1512
"\n",
16-
"from nifreeze.data import pet\n",
17-
"from nifreeze.model import PETModel\n",
18-
"#from eddymotion.viz import plot_dwi\n",
1913
"import nibabel as nib\n",
20-
"from nifreeze.data.splitting import lovo_split\n",
21-
"from pathlib import Path"
14+
"\n",
15+
"from nifreeze.data import pet\n",
16+
"from nifreeze.model import PETModel"
2217
]
2318
},
2419
{
@@ -38,8 +33,12 @@
3833
"OUTPUT_DIR = WORKDIR / \"motion_estimation\"\n",
3934
"OUTPUT_DIR.mkdir(exist_ok=True, parents=True)\n",
4035
"\n",
41-
"pet_file = DATA_PATH / \"pet_data\" / \"sub-02\" / \"ses-baseline\" / \"pet\" / \"sub-02_ses-baseline_pet.nii.gz\"\n",
42-
"json_file = DATA_PATH / \"pet_data\" / \"sub-02\" / \"ses-baseline\" / \"pet\" / \"sub-02_ses-baseline_pet.json\"\n",
36+
"pet_file = (\n",
37+
" DATA_PATH / \"pet_data\" / \"sub-02\" / \"ses-baseline\" / \"pet\" / \"sub-02_ses-baseline_pet.nii.gz\"\n",
38+
")\n",
39+
"json_file = (\n",
40+
" DATA_PATH / \"pet_data\" / \"sub-02\" / \"ses-baseline\" / \"pet\" / \"sub-02_ses-baseline_pet.json\"\n",
41+
")\n",
4342
"\n",
4443
"pet_dataset = pet.PET.load(pet_file, json_file)"
4544
]
@@ -450,12 +449,12 @@
450449
"source": [
451450
"# before\n",
452451
"nifti_img_before = nib.Nifti1Image(predicted, pet_dataset.affine)\n",
453-
"output_path_before = 'before_mc.nii'\n",
452+
"output_path_before = \"before_mc.nii\"\n",
454453
"nifti_img_before.to_filename(output_path_before)\n",
455454
"\n",
456455
"# after\n",
457456
"nifti_img_after = nib.Nifti1Image(data_test[0], pet_dataset.affine)\n",
458-
"output_path_after = 'after_mc.nii'\n",
457+
"output_path_after = \"after_mc.nii\"\n",
459458
"nifti_img_after.to_filename(output_path_after)"
460459
]
461460
},
@@ -2237,12 +2236,15 @@
22372236
],
22382237
"source": [
22392238
"from niworkflows.viz.notebook import display\n",
2240-
"import os\n",
2241-
"from IPython.display import SVG\n",
22422239
"\n",
22432240
"moving_image = output_path_after\n",
22442241
"fixed_image = output_path_before\n",
2245-
"obj = display(fixed_image, moving_image, fixed_label='PET_before', moving_label='PET', )"
2242+
"obj = display(\n",
2243+
" fixed_image,\n",
2244+
" moving_image,\n",
2245+
" fixed_label=\"PET_before\",\n",
2246+
" moving_label=\"PET\",\n",
2247+
")"
22462248
]
22472249
},
22482250
{
@@ -2271,7 +2273,9 @@
22712273
],
22722274
"source": [
22732275
"# Instantiate with a PETModel or appropriate model instance\n",
2274-
"model = PETModel(dataset=pet_dataset, timepoints=pet_dataset.midframe, xlim=pet_dataset.total_duration)\n",
2276+
"model = PETModel(\n",
2277+
" dataset=pet_dataset, timepoints=pet_dataset.midframe, xlim=pet_dataset.total_duration\n",
2278+
")\n",
22752279
"estimator = PETMotionEstimator(model=model)\n",
22762280
"\n",
22772281
"# Run the estimator\n",
@@ -2452,34 +2456,36 @@
24522456
"import numpy as np\n",
24532457
"import pandas as pd\n",
24542458
"\n",
2459+
"\n",
24552460
"def extract_motion_parameters(affine):\n",
24562461
" \"\"\"Extract translation (mm) and rotation (degrees) parameters from an affine matrix.\"\"\"\n",
24572462
" translation = affine[:3, 3]\n",
24582463
" rotation_rad = np.arctan2(\n",
2459-
" [affine[2, 1], affine[0, 2], affine[1, 0]],\n",
2460-
" [affine[2, 2], affine[0, 0], affine[1, 1]]\n",
2464+
" [affine[2, 1], affine[0, 2], affine[1, 0]], [affine[2, 2], affine[0, 0], affine[1, 1]]\n",
24612465
" )\n",
24622466
" rotation_deg = np.rad2deg(rotation_rad)\n",
2463-
" return (*translation, *rotation_deg)\n",
2467+
" return *translation, *rotation_deg\n",
2468+
"\n",
24642469
"\n",
24652470
"def compute_fd(motion_parameters):\n",
24662471
" \"\"\"Compute Framewise Displacement from motion parameters.\"\"\"\n",
24672472
" translations = motion_parameters[:, :3]\n",
24682473
" rotations_deg = motion_parameters[:, 3:]\n",
24692474
" rotations_rad = np.deg2rad(rotations_deg)\n",
2470-
" \n",
2475+
"\n",
24712476
" # Compute differences between consecutive frames\n",
24722477
" d_translations = np.vstack([np.zeros((1, 3)), np.diff(translations, axis=0)])\n",
24732478
" d_rotations = np.vstack([np.zeros((1, 3)), np.diff(rotations_rad, axis=0)])\n",
2474-
" \n",
2479+
"\n",
24752480
" # Convert rotations from radians to displacement on a sphere (radius 50 mm)\n",
24762481
" radius = 50 # typical head radius in mm\n",
24772482
" rotation_displacement = d_rotations * radius\n",
2478-
" \n",
2483+
"\n",
24792484
" # Compute FD as sum of absolute differences\n",
24802485
" fd = np.sum(np.abs(d_translations) + np.abs(rotation_displacement), axis=1)\n",
24812486
" return fd\n",
24822487
"\n",
2488+
"\n",
24832489
"# Assume 'affines' is the list of affine matrices you computed earlier\n",
24842490
"motion_parameters = []\n",
24852491
"\n",
@@ -2491,16 +2497,18 @@
24912497
"fd = compute_fd(motion_parameters)\n",
24922498
"\n",
24932499
"# Creating a DataFrame for better visualization\n",
2494-
"df_motion = pd.DataFrame({\n",
2495-
" \"Frame\": np.arange(len(fd)),\n",
2496-
" \"Translation X\": motion_parameters[:, 0],\n",
2497-
" \"Translation Y\": motion_parameters[:, 1],\n",
2498-
" \"Translation Z\": motion_parameters[:, 2],\n",
2499-
" \"Rotation X\": motion_parameters[:, 3],\n",
2500-
" \"Rotation Y\": motion_parameters[:, 4],\n",
2501-
" \"Rotation Z\": motion_parameters[:, 5],\n",
2502-
" \"Framewise Displacement (mm)\": fd\n",
2503-
"})\n"
2500+
"df_motion = pd.DataFrame(\n",
2501+
" {\n",
2502+
" \"Frame\": np.arange(len(fd)),\n",
2503+
" \"Translation X\": motion_parameters[:, 0],\n",
2504+
" \"Translation Y\": motion_parameters[:, 1],\n",
2505+
" \"Translation Z\": motion_parameters[:, 2],\n",
2506+
" \"Rotation X\": motion_parameters[:, 3],\n",
2507+
" \"Rotation Y\": motion_parameters[:, 4],\n",
2508+
" \"Rotation Z\": motion_parameters[:, 5],\n",
2509+
" \"Framewise Displacement (mm)\": fd,\n",
2510+
" }\n",
2511+
")"
25042512
]
25052513
},
25062514
{
@@ -2522,33 +2530,34 @@
25222530
"# Set up the matplotlib figure\n",
25232531
"import matplotlib.pyplot as plt\n",
25242532
"import seaborn as sns\n",
2533+
"\n",
25252534
"plt.figure(figsize=(18, 10))\n",
25262535
"\n",
25272536
"# Translation plots\n",
25282537
"plt.subplot(2, 3, 1)\n",
2529-
"sns.lineplot(x='Frame', y='Translation X', data=df_motion)\n",
2530-
"plt.title('Translation X over Frames')\n",
2538+
"sns.lineplot(x=\"Frame\", y=\"Translation X\", data=df_motion)\n",
2539+
"plt.title(\"Translation X over Frames\")\n",
25312540
"\n",
25322541
"plt.subplot(2, 3, 2)\n",
2533-
"sns.lineplot(x='Frame', y='Translation Y', data=df_motion)\n",
2534-
"plt.title('Translation Y over Frames')\n",
2542+
"sns.lineplot(x=\"Frame\", y=\"Translation Y\", data=df_motion)\n",
2543+
"plt.title(\"Translation Y over Frames\")\n",
25352544
"\n",
25362545
"plt.subplot(2, 3, 3)\n",
2537-
"sns.lineplot(x='Frame', y='Translation Z', data=df_motion)\n",
2538-
"plt.title('Translation Z over Frames')\n",
2546+
"sns.lineplot(x=\"Frame\", y=\"Translation Z\", data=df_motion)\n",
2547+
"plt.title(\"Translation Z over Frames\")\n",
25392548
"\n",
25402549
"# Rotation plots\n",
25412550
"plt.subplot(2, 3, 4)\n",
2542-
"sns.lineplot(x='Frame', y='Rotation X', data=df_motion)\n",
2543-
"plt.title('Rotation X over Frames')\n",
2551+
"sns.lineplot(x=\"Frame\", y=\"Rotation X\", data=df_motion)\n",
2552+
"plt.title(\"Rotation X over Frames\")\n",
25442553
"\n",
25452554
"plt.subplot(2, 3, 5)\n",
2546-
"sns.lineplot(x='Frame', y='Rotation Y', data=df_motion)\n",
2547-
"plt.title('Rotation Y over Frames')\n",
2555+
"sns.lineplot(x=\"Frame\", y=\"Rotation Y\", data=df_motion)\n",
2556+
"plt.title(\"Rotation Y over Frames\")\n",
25482557
"\n",
25492558
"plt.subplot(2, 3, 6)\n",
2550-
"sns.lineplot(x='Frame', y='Rotation Z', data=df_motion)\n",
2551-
"plt.title('Rotation Z over Frames')\n",
2559+
"sns.lineplot(x=\"Frame\", y=\"Rotation Z\", data=df_motion)\n",
2560+
"plt.title(\"Rotation Z over Frames\")\n",
25522561
"\n",
25532562
"plt.tight_layout()\n",
25542563
"plt.show()"
@@ -2571,8 +2580,8 @@
25712580
],
25722581
"source": [
25732582
"plt.figure(figsize=(18, 10))\n",
2574-
"sns.lineplot(x='Frame', y='Framewise Displacement (mm)', data=df_motion)\n",
2575-
"plt.title('Framewise Displacement (mm)')\n",
2583+
"sns.lineplot(x=\"Frame\", y=\"Framewise Displacement (mm)\", data=df_motion)\n",
2584+
"plt.title(\"Framewise Displacement (mm)\")\n",
25762585
"\n",
25772586
"plt.tight_layout()\n",
25782587
"plt.show()"

0 commit comments

Comments
 (0)