From 208b6f6272f88a5a7b91f82ed1f609ce1ea871c0 Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Wed, 31 Jan 2024 08:50:15 -0500 Subject: [PATCH 1/6] Declare variables so cell can run as standalone --- data_processing.ipynb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_processing.ipynb b/data_processing.ipynb index 2385dba..ad95637 100644 --- a/data_processing.ipynb +++ b/data_processing.ipynb @@ -843,6 +843,8 @@ "\n", "# Select subject to show\n", "subject = 'sub-05'\n", + "os.chdir(os.path.join(path_data, subject, \"fmap\"))\n", + "file_mask = f\"{subject}_acq-anatCP_TB1TFL_mask-shimming.nii.gz\"\n", "\n", "# Defining crop limits for resulting figure\n", "xmin = 20\n", From 62acbeb06bc8044426572b9dd7bcc6accdb9d2d4 Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Wed, 31 Jan 2024 10:24:49 -0500 Subject: [PATCH 2/6] Display B1+ efficiency value inside the figure Fixes #82 --- data_processing.ipynb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/data_processing.ipynb b/data_processing.ipynb index ad95637..e8e3d80 100644 --- a/data_processing.ipynb +++ b/data_processing.ipynb @@ -846,6 +846,12 @@ "os.chdir(os.path.join(path_data, subject, \"fmap\"))\n", "file_mask = f\"{subject}_acq-anatCP_TB1TFL_mask-shimming.nii.gz\"\n", "\n", + "# Load the statistics from the CSV file\n", + "stats_df = pd.read_csv(os.path.join(path_results, 'stats_b1plus.csv'))\n", + "\n", + "# Filter for the specific subject\n", + "subject_stats = stats_df[stats_df['Subject'] == subject]\n", + "\n", "# Defining crop limits for resulting figure\n", "xmin = 20\n", "xmax = 110\n", @@ -911,6 +917,17 @@ " splot.set_title(shim_mode, size=font_size)\n", " splot.axis('off')\n", "\n", + " # Find the statistics for the current shim mode\n", + " shim_stats = subject_stats[subject_stats['Shim_Mode'] == shim_mode]\n", + " if not shim_stats.empty:\n", + " mean_val = shim_stats.iloc[0]['Average']\n", + " std_val = shim_stats.iloc[0]['Standard_Deviation']\n", + " cv = std_val / mean_val * 100 # Coefficient of variation in percentage\n", + " annotation_text = f\"{mean_val:.2f} nT/V\\n{cv:.2f}%\"\n", + " splot.annotate(annotation_text, (0.05, 0.95), xycoords='axes fraction', \n", + " fontsize=10, color='white', \n", + " verticalalignment='top', horizontalalignment='left')\n", + "\n", "plt.tight_layout()\n", "plt.subplots_adjust(wspace=0.1, hspace=0, right=0.9)\n", "\n", From 24b3f00e88ebf7e198ddcd2e35dee8019f0d102c Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Wed, 31 Jan 2024 10:29:54 -0500 Subject: [PATCH 3/6] Refactoring --- data_processing.ipynb | 110 ++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 59 deletions(-) diff --git a/data_processing.ipynb b/data_processing.ipynb index e8e3d80..d28ed42 100644 --- a/data_processing.ipynb +++ b/data_processing.ipynb @@ -867,66 +867,58 @@ "font_size = 14\n", "axes=axes.flatten()\n", "\n", - "# Plotter loop (avoiding the generation of an ungly 4D data structure)\n", + "# First, plot the anatomical image with an overlay of the mask\n", + "\n", + "# Load data\n", + "CP_anat=nib.load(f\"{subject}_acq-anatCP_TB1TFL.nii.gz\")\n", + "CP_SC=nib.load(file_mask)\n", + "CP_nTpV=nib.load(f\"{subject}_acq-CP_TB1map.nii.gz\")\n", + "\n", + "# Defining mask based on the magnitude image intensity threshold\n", + "cslice=CP_anat.shape[2] // 2 -2 #shows the SC seg best\n", + "threshold=300\n", + "mask=CP_anat.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", + "mask=np.where(mask > threshold, 1, 0)\n", + "\n", + "# Cropping anat, SC, B1+\n", + "CP_anat=CP_anat.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", + "CP_anat=CP_anat*mask\n", + "CP_SC=CP_SC.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", + "CP_nTpV=CP_nTpV.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", + "CP_nTpV=CP_nTpV*mask\n", + "\n", + "# All opacity overalys look ugly: workaround, set the anat slice to a max value where the segmentation exists\n", + "CP_anat[CP_SC>0.5]=4000;\n", + "\n", + "# Plotting anat overlayed with SC\n", + "splot=axes[0]\n", + "splot.imshow((CP_anat.T), cmap='gray', origin='lower',vmin=0,vmax=2000)#, interpolation='spline36')\n", + "splot.set_title('SC overlay', size=font_size)\n", + "splot.axis('off')\n", + "\n", + "# Then, plot each B1+ map, with an overlay of the mean and CV inside the cord\n", "for i,shim_mode in enumerate(shim_modes):\n", - " if i==0: # Grabbing the CP mode anat to use for displaying the SC and cropping noise for the B1+ map\n", - " \n", - " # Load data\n", - " CP_anat=nib.load(f\"{subject}_acq-anat{shim_mode}_TB1TFL.nii.gz\")\n", - " CP_SC=nib.load(file_mask)\n", - " CP_nTpV=nib.load(f\"{subject}_acq-{shim_mode}_TB1map.nii.gz\")\n", - " \n", - " # Defining mask based on the magnitude image intensity threshold\n", - " cslice=CP_anat.shape[2] // 2 -2 #shows the SC seg best\n", - " threshold=300\n", - " mask=CP_anat.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", - " mask=np.where(mask > threshold, 1, 0)\n", - " \n", - " # Cropping anat, SC, B1+\n", - " CP_anat=CP_anat.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", - " CP_anat=CP_anat*mask\n", - " CP_SC=CP_SC.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", - " CP_nTpV=CP_nTpV.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", - " CP_nTpV=CP_nTpV*mask\n", - " \n", - " # All opacity overalys look ugly: workaround, set the anat slice to a max value where the segmentation exists\n", - " CP_anat[CP_SC>0.5]=4000;\n", - " \n", - " # Plotting anat overlayed with SC\n", - " splot=axes[i]\n", - " splot.imshow((CP_anat.T), cmap='gray', origin='lower',vmin=0,vmax=2000)#, interpolation='spline36')\n", - " splot.set_title('SC overlay', size=font_size)\n", - " splot.axis('off')\n", - " \n", - " # Plotting the B1+ map\n", - " splot=axes[i+1]\n", - " splot.imshow((CP_nTpV.T), cmap='viridis', origin='lower',vmin=dynmin,vmax=dynmax)#, interpolation='spline36')\n", - " splot.set_title(shim_mode, size=font_size)\n", - " splot.axis('off')\n", - "\n", - " \n", - " else:\n", - " # Load data\n", - " B1map=nib.load(f\"{subject}_acq-{shim_mode}_TB1map.nii.gz\")\n", - " B1map=B1map.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", - " B1map=B1map*mask\n", - " \n", - " # Plot\n", - " splot=axes[i+1]\n", - " im = splot.imshow((B1map.T), cmap='viridis', origin='lower',vmin=dynmin,vmax=dynmax)#, interpolation='spline36')\n", - " splot.set_title(shim_mode, size=font_size)\n", - " splot.axis('off')\n", - "\n", - " # Find the statistics for the current shim mode\n", - " shim_stats = subject_stats[subject_stats['Shim_Mode'] == shim_mode]\n", - " if not shim_stats.empty:\n", - " mean_val = shim_stats.iloc[0]['Average']\n", - " std_val = shim_stats.iloc[0]['Standard_Deviation']\n", - " cv = std_val / mean_val * 100 # Coefficient of variation in percentage\n", - " annotation_text = f\"{mean_val:.2f} nT/V\\n{cv:.2f}%\"\n", - " splot.annotate(annotation_text, (0.05, 0.95), xycoords='axes fraction', \n", - " fontsize=10, color='white', \n", - " verticalalignment='top', horizontalalignment='left')\n", + " # Load data\n", + " B1map=nib.load(f\"{subject}_acq-{shim_mode}_TB1map.nii.gz\")\n", + " B1map=B1map.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n", + " B1map=B1map*mask\n", + "\n", + " # Plot\n", + " splot=axes[i+1]\n", + " im = splot.imshow((B1map.T), cmap='viridis', origin='lower',vmin=dynmin,vmax=dynmax)#, interpolation='spline36')\n", + " splot.set_title(shim_mode, size=font_size)\n", + " splot.axis('off')\n", + "\n", + " # Find the statistics for the current shim mode\n", + " shim_stats = subject_stats[subject_stats['Shim_Mode'] == shim_mode]\n", + " if not shim_stats.empty:\n", + " mean_val = shim_stats.iloc[0]['Average']\n", + " std_val = shim_stats.iloc[0]['Standard_Deviation']\n", + " cv = std_val / mean_val * 100 # Coefficient of variation in percentage\n", + " annotation_text = f\"{mean_val:.2f} nT/V\\n{cv:.2f}%\"\n", + " splot.annotate(annotation_text, (0.05, 0.95), xycoords='axes fraction', \n", + " fontsize=10, color='white', \n", + " verticalalignment='top', horizontalalignment='left')\n", "\n", "plt.tight_layout()\n", "plt.subplots_adjust(wspace=0.1, hspace=0, right=0.9)\n", From d11a5aab063fdcb3e6c80a8fb21f85440f6c5967 Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Wed, 31 Jan 2024 11:01:38 -0500 Subject: [PATCH 4/6] Changed title of subplot --- data_processing.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_processing.ipynb b/data_processing.ipynb index d28ed42..d0358fd 100644 --- a/data_processing.ipynb +++ b/data_processing.ipynb @@ -893,7 +893,7 @@ "# Plotting anat overlayed with SC\n", "splot=axes[0]\n", "splot.imshow((CP_anat.T), cmap='gray', origin='lower',vmin=0,vmax=2000)#, interpolation='spline36')\n", - "splot.set_title('SC overlay', size=font_size)\n", + "splot.set_title('Anat', size=font_size)\n", "splot.axis('off')\n", "\n", "# Then, plot each B1+ map, with an overlay of the mean and CV inside the cord\n", From 7ad69c75cd54bed74d256e81153549d9b4ef3d0e Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Wed, 31 Jan 2024 11:50:19 -0500 Subject: [PATCH 5/6] Save fig at 300dpi --- data_processing.ipynb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_processing.ipynb b/data_processing.ipynb index d0358fd..7156f55 100644 --- a/data_processing.ipynb +++ b/data_processing.ipynb @@ -439,6 +439,7 @@ "\n", "# Adjust the layout so labels and titles do not overlap\n", "plt.tight_layout()\n", + "plt.savefig(os.path.join(path_results, 'fig_gre_csf-sc_ratio.png'), dpi=300, format='png')\n", "plt.show()" ] }, @@ -734,6 +735,7 @@ "\n", "# Adjust the layout so labels and titles do not overlap\n", "plt.tight_layout()\n", + "plt.savefig(os.path.join(path_results, 'fig_b1plus.png'), dpi=300, format='png')\n", "plt.show()" ] }, From 7bc92fe355c7113eaf221a96f2749d0d166605e3 Mon Sep 17 00:00:00 2001 From: jcohenadad Date: Wed, 31 Jan 2024 11:51:23 -0500 Subject: [PATCH 6/6] Save B1+ map at 300dpi --- data_processing.ipynb | 1 + 1 file changed, 1 insertion(+) diff --git a/data_processing.ipynb b/data_processing.ipynb index 7156f55..d176545 100644 --- a/data_processing.ipynb +++ b/data_processing.ipynb @@ -936,6 +936,7 @@ "# cbar_ax = fig.add_axes([0.95, 0.5, 0.04, 0.4])\n", "# cbar = plt.colorbar(im, cax=cbar_ax)\n", "cbar_ax.set_title('nT/V', size=12)\n", + "plt.savefig(os.path.join(path_results, 'fig_b1plus_map.png'), dpi=300, format='png')\n", "plt.show\n" ] },