|
27 | 27 | "source": [
|
28 | 28 | "This notebook is designed to reproduce the processing pipeline and visualize the results for the paper titled \"On the Impact of B0 Shimming Algorithms on Single Voxel Magnetic Resonance Spectroscopy,\" authored by Behrouz Vejdani Afkham and Eva Alonso-Ortiz.\n",
|
29 | 29 | "\n",
|
30 |
| - "The data for this paper is available online on OSF and will be downloaded at the beginning to reproduce the results. The content of the notebook is as follows:" |
| 30 | + "The data for this paper is available online on OSF and will be downloaded in the notebook to reproduce the results.\n", |
| 31 | + "\n", |
| 32 | + "**Note**: While installing new packages, the user may be prompted to restart the session to use them. Please restart the session before proceeding to run the next cells." |
31 | 33 | ]
|
32 | 34 | },
|
33 | 35 | {
|
|
58 | 60 | },
|
59 | 61 | {
|
60 | 62 | "cell_type": "code",
|
61 |
| - "execution_count": 1, |
| 63 | + "execution_count": null, |
62 | 64 | "metadata": {
|
63 | 65 | "id": "gy_EGEgylTI_"
|
64 | 66 | },
|
65 | 67 | "outputs": [],
|
66 | 68 | "source": [
|
67 |
| - "%%capture\n", |
68 | 69 | "%cd /content\n",
|
69 | 70 | "!git clone --recurse-submodules https://git.fmrib.ox.ac.uk/fsl/fsl_mrs.git\n",
|
70 | 71 | "%cd /content/fsl_mrs\n",
|
71 |
| - "!pip install .\n", |
72 |
| - "\n", |
73 |
| - "%cd /content\n", |
74 |
| - "!git clone --single-branch --branch gradient_optimizer https://github.com/shimming-toolbox/shimming-toolbox.git\n", |
75 |
| - "%cd /content/shimming-toolbox/\n", |
76 |
| - "!pip install .\n" |
| 72 | + "!pip install ." |
77 | 73 | ]
|
78 | 74 | },
|
79 | 75 | {
|
80 | 76 | "cell_type": "code",
|
81 |
| - "execution_count": null, |
| 77 | + "source": [ |
| 78 | + "%cd /content\n", |
| 79 | + "!git clone --single-branch --branch gradient_optimizer https://github.com/shimming-toolbox/shimming-toolbox.git\n", |
| 80 | + "%cd /content/shimming-toolbox/\n", |
| 81 | + "!pip install ." |
| 82 | + ], |
82 | 83 | "metadata": {
|
83 |
| - "id": "5VxegHqgzUTm" |
| 84 | + "id": "V3IxN-qd-NAT" |
84 | 85 | },
|
85 |
| - "outputs": [], |
86 |
| - "source": [ |
87 |
| - "# Restart the runtime to resolve the compatibilty issues between the packages\n", |
88 |
| - "import os\n", |
89 |
| - "os.kill(os.getpid(), 9)" |
90 |
| - ] |
| 86 | + "execution_count": null, |
| 87 | + "outputs": [] |
91 | 88 | },
|
92 | 89 | {
|
93 | 90 | "cell_type": "markdown",
|
|
106 | 103 | },
|
107 | 104 | "outputs": [],
|
108 | 105 | "source": [
|
109 |
| - "%%capture\n", |
110 | 106 | "%cd /content\n",
|
111 | 107 | "import os\n",
|
112 | 108 | "os.environ[\"LD_PRELOAD\"] = \"\";\n",
|
|
156 | 152 | "from shimmingtoolbox.prepare_fieldmap import prepare_fieldmap\n",
|
157 | 153 | "from shimmingtoolbox.load_nifti import read_nii\n",
|
158 | 154 | "from shimmingtoolbox.cli import b0shim\n",
|
159 |
| - "from shimmingtoolbox.masking.threshold import threshold as mask_threshold\n", |
160 | 155 | "\n",
|
161 | 156 | "try:\n",
|
162 | 157 | " import seaborn as sns\n",
|
|
349 | 344 | "source": [
|
350 | 345 | "### Display field maps for Siemens and Grad methods for a representative subject.\n",
|
351 | 346 | "\n",
|
352 |
| - "fig = plt.figure(figsize=(12, 5), facecolor='white')\n", |
| 347 | + "fig = plt.figure(figsize=(10, 3), facecolor='white')\n", |
353 | 348 | "\n",
|
354 | 349 | "mask_mrs_sub3 = nib.load(current_path + '/derivatives/sub-3/mask_mrs.nii.gz').get_fdata()\n",
|
355 | 350 | "mask_mrs_sub3[mask_mrs_sub3 != 1] = np.nan\n",
|
356 | 351 | "fieldmap_Grad_sub3 = nib.load(current_path+\"/derivatives/sub-3/fmap/sub-3_acq-grad-after-Shim-fieldmap.nii.gz\").get_fdata()*mask_mrs_sub3\n",
|
357 | 352 | "fieldmap_Siemens_sub3 = nib.load(current_path+\"/derivatives/sub-3/fmap/sub-3_acq-siemens-after-Shim-fieldmap.nii.gz\").get_fdata()*mask_mrs_sub3\n",
|
358 | 353 | "magnitude = nib.load(current_path+\"/sub-3/fmap/sub-3_acq-grad-before-Shim_magnitude1.nii.gz\").get_fdata()\n",
|
359 | 354 | "\n",
|
360 |
| - "plt.suptitle(' Comparison of fieldmaps (Hz) after shimming', fontsize=16, fontweight='bold')\n", |
| 355 | + "plt.suptitle(' Comparison of the measured fieldmaps [Hz] after shimming', fontsize=14, fontweight='bold')\n", |
361 | 356 | "# Define gridspec\n",
|
362 |
| - "gs = gridspec.GridSpec(1, 2)\n", |
| 357 | + "gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])\n", |
363 | 358 | "\n",
|
364 | 359 | "# Plot a sagittal cut for Grad method\n",
|
365 | 360 | "ax1 = plt.subplot(gs[0])\n",
|
366 |
| - "ax1.imshow(np.rot90(magnitude[30,:,:]), cmap='gray')\n", |
367 |
| - "ax1.imshow(np.rot90(fieldmap_Grad_sub3[30,:,:]), vmin=-20, vmax=20, cmap='jet')\n", |
| 361 | + "ax1.imshow(np.rot90(magnitude[31,:,:]), cmap='gray')\n", |
| 362 | + "img1= ax1.imshow(np.rot90(fieldmap_Grad_sub3[31,:,:]), vmin=-20, vmax=20, cmap='jet')\n", |
368 | 363 | "\n",
|
369 | 364 | "ax1.set_xticks([]) # Turn off x-axis ticks\n",
|
370 | 365 | "ax1.set_yticks([]) # Turn off y-axis ticks\n",
|
371 | 366 | "ax1.set_title('Grad', fontsize=12)\n",
|
| 367 | + "cbar = plt.colorbar(img1, ax= ax1, fraction=0.025, pad=0.03)\n", |
| 368 | + "cbar.ax.tick_params(labelsize=12) # Set font size for color bar\n", |
372 | 369 | "\n",
|
373 | 370 | "# Plot a sagittal cut for the Siemens method\n",
|
374 | 371 | "ax2 = plt.subplot(gs[1])\n",
|
375 |
| - "ax2.imshow(np.rot90(magnitude[30,:,:]), cmap='gray')\n", |
376 |
| - "img = ax2.imshow(np.rot90(fieldmap_Siemens_sub3[30,:,:]), vmin=-20, vmax=20, cmap='jet')\n", |
| 372 | + "ax2.imshow(np.rot90(magnitude[31,:,:]), cmap='gray')\n", |
| 373 | + "img2 = ax2.imshow(np.rot90(fieldmap_Siemens_sub3[31,:,:]), vmin=-20, vmax=20, cmap='jet')\n", |
377 | 374 | "\n",
|
378 | 375 | "ax2.set_xticks([]) # Turn off x-axis ticks\n",
|
379 | 376 | "ax2.set_yticks([]) # Turn off y-axis ticks\n",
|
380 | 377 | "ax2.set_title('Siemens', fontsize=12)\n",
|
381 | 378 | "\n",
|
382 |
| - "cbar = plt.colorbar(img, ax=ax2) # Add color bar\n", |
383 |
| - "cbar.ax.tick_params(labelsize=20) # Set font size for color bar\n", |
| 379 | + "# cbar = plt.colorbar(img, ax=ax2) # Add color bar\n", |
| 380 | + "cbar = plt.colorbar(img2, ax=ax2, fraction=0.025, pad=0.03)\n", |
| 381 | + "cbar.ax.tick_params(labelsize=12) # Set font size for color bar\n", |
384 | 382 | "\n",
|
385 | 383 | "# Save the figure with 300 dpi\n",
|
386 | 384 | "plt.savefig('fieldmaps_after_shimming.png', dpi=300)\n",
|
387 |
| - "plt.show()" |
| 385 | + "plt.show()\n", |
| 386 | + "print(magnitude.shape)" |
388 | 387 | ]
|
389 | 388 | },
|
390 | 389 | {
|
|
501 | 500 | },
|
502 | 501 | {
|
503 | 502 | "cell_type": "code",
|
504 |
| - "execution_count": null, |
| 503 | + "execution_count": 34, |
505 | 504 | "metadata": {
|
506 | 505 | "id": "VMkUSY6K0T7j"
|
507 | 506 | },
|
|
755 | 754 | "else:\n",
|
756 | 755 | " print(\"Fail to reject the null hypothesis. The provided data follow a normal distribution.\")\n",
|
757 | 756 | "\n",
|
758 |
| - "comparison_methods= ['PI','Grad', 'LSq', 'QuadProg']\n", |
| 757 | + "comparison_methods = ['PI','Grad','QuadProg','LSq']\n", |
759 | 758 | "Uncor_pvals = []\n",
|
| 759 | + "\n", |
760 | 760 | "# levene test\n",
|
761 | 761 | "for method in comparison_methods:\n",
|
762 | 762 | " data = eval(f\"all_{method}_data_mrs_mask_shimmed\")\n",
|
763 |
| - " statistic, p_value = stats.levene(all_Siemens_data_mrs_mask_shimmed, data)\n", |
764 |
| - " Uncor_pvals.append(p_value)\n", |
765 |
| - "print(\"Uncorrected p-values are: \", Uncor_pvals)\n", |
| 763 | + " if np.var(data)< np.var(all_Siemens_data_mrs_mask_shimmed):\n", |
| 764 | + " statistic, p_value = stats.levene(all_Siemens_data_mrs_mask_shimmed, data)\n", |
| 765 | + " Uncor_pvals.append(p_value)\n", |
| 766 | + " else:\n", |
| 767 | + " comparison_methods.remove(method)\n", |
| 768 | + " print(f\"The variance of the {method} is higher than Siemens and excluded from further comparisons.\")\n", |
766 | 769 | "\n",
|
| 770 | + "print(\"Uncorrected p-values are: \", Uncor_pvals)\n", |
767 | 771 | "# Perform Multiple comparison correction with step-down method using Bonferroni adjustments\n",
|
768 | 772 | "Corrected_pvals = smm.multipletests(Uncor_pvals, alpha=0.01, method='holm', is_sorted=False, returnsorted=False)\n",
|
769 | 773 | "print(\"Corrected p-values are: \" , Corrected_pvals[1])\n",
|
770 | 774 | "\n",
|
771 | 775 | "for pval, method in zip(Corrected_pvals[1], comparison_methods):\n",
|
772 | 776 | " if pval < 0.01:\n",
|
773 |
| - " print(f\"Reject the null hypothesis. The variance between the Siemens and {method} groups are significantly different.\")\n", |
| 777 | + " print(f\"Reject the null hypothesis. The variance between the Siemens and {method} methods are significantly different.\")\n", |
774 | 778 | " else:\n",
|
775 |
| - " print(f\"Fail to reject the null hypothesis. There is no significant difference in variances between the Siemens and {method} groups.\")\n", |
776 |
| - "\n" |
| 779 | + " print(f\"Fail to reject the null hypothesis. There is no significant difference in variances between the Siemens and {method} methods.\")\n" |
777 | 780 | ]
|
778 | 781 | },
|
779 | 782 | {
|
|
0 commit comments