Skip to content

Commit 59936a1

Browse files
authored
Merge pull request #89 from shimming-toolbox/jca/82-b1-value-in-plots
Display B1+ efficiency value inside the figure
2 parents 537854d + 7bc92fe commit 59936a1

File tree

1 file changed

+62
-48
lines changed

1 file changed

+62
-48
lines changed

data_processing.ipynb

+62-48
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@
439439
"\n",
440440
"# Adjust the layout so labels and titles do not overlap\n",
441441
"plt.tight_layout()\n",
442+
"plt.savefig(os.path.join(path_results, 'fig_gre_csf-sc_ratio.png'), dpi=300, format='png')\n",
442443
"plt.show()"
443444
]
444445
},
@@ -734,6 +735,7 @@
734735
"\n",
735736
"# Adjust the layout so labels and titles do not overlap\n",
736737
"plt.tight_layout()\n",
738+
"plt.savefig(os.path.join(path_results, 'fig_b1plus.png'), dpi=300, format='png')\n",
737739
"plt.show()"
738740
]
739741
},
@@ -843,6 +845,14 @@
843845
"\n",
844846
"# Select subject to show\n",
845847
"subject = 'sub-05'\n",
848+
"os.chdir(os.path.join(path_data, subject, \"fmap\"))\n",
849+
"file_mask = f\"{subject}_acq-anatCP_TB1TFL_mask-shimming.nii.gz\"\n",
850+
"\n",
851+
"# Load the statistics from the CSV file\n",
852+
"stats_df = pd.read_csv(os.path.join(path_results, 'stats_b1plus.csv'))\n",
853+
"\n",
854+
"# Filter for the specific subject\n",
855+
"subject_stats = stats_df[stats_df['Subject'] == subject]\n",
846856
"\n",
847857
"# Defining crop limits for resulting figure\n",
848858
"xmin = 20\n",
@@ -859,55 +869,58 @@
859869
"font_size = 14\n",
860870
"axes=axes.flatten()\n",
861871
"\n",
862-
"# Plotter loop (avoiding the generation of an ungly 4D data structure)\n",
863-
"for i,shim_mode in enumerate(shim_modes):\n",
864-
" if i==0: # Grabbing the CP mode anat to use for displaying the SC and cropping noise for the B1+ map\n",
865-
" \n",
866-
" # Load data\n",
867-
" CP_anat=nib.load(f\"{subject}_acq-anat{shim_mode}_TB1TFL.nii.gz\")\n",
868-
" CP_SC=nib.load(file_mask)\n",
869-
" CP_nTpV=nib.load(f\"{subject}_acq-{shim_mode}_TB1map.nii.gz\")\n",
870-
" \n",
871-
" # Defining mask based on the magnitude image intensity threshold\n",
872-
" cslice=CP_anat.shape[2] // 2 -2 #shows the SC seg best\n",
873-
" threshold=300\n",
874-
" mask=CP_anat.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
875-
" mask=np.where(mask > threshold, 1, 0)\n",
876-
" \n",
877-
" # Cropping anat, SC, B1+\n",
878-
" CP_anat=CP_anat.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
879-
" CP_anat=CP_anat*mask\n",
880-
" CP_SC=CP_SC.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
881-
" CP_nTpV=CP_nTpV.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
882-
" CP_nTpV=CP_nTpV*mask\n",
883-
" \n",
884-
" # All opacity overalys look ugly: workaround, set the anat slice to a max value where the segmentation exists\n",
885-
" CP_anat[CP_SC>0.5]=4000;\n",
886-
" \n",
887-
" # Plotting anat overlayed with SC\n",
888-
" splot=axes[i]\n",
889-
" splot.imshow((CP_anat.T), cmap='gray', origin='lower',vmin=0,vmax=2000)#, interpolation='spline36')\n",
890-
" splot.set_title('SC overlay', size=font_size)\n",
891-
" splot.axis('off')\n",
892-
" \n",
893-
" # Plotting the B1+ map\n",
894-
" splot=axes[i+1]\n",
895-
" splot.imshow((CP_nTpV.T), cmap='viridis', origin='lower',vmin=dynmin,vmax=dynmax)#, interpolation='spline36')\n",
896-
" splot.set_title(shim_mode, size=font_size)\n",
897-
" splot.axis('off')\n",
872+
"# First, plot the anatomical image with an overlay of the mask\n",
898873
"\n",
899-
" \n",
900-
" else:\n",
901-
" # Load data\n",
902-
" B1map=nib.load(f\"{subject}_acq-{shim_mode}_TB1map.nii.gz\")\n",
903-
" B1map=B1map.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
904-
" B1map=B1map*mask\n",
905-
" \n",
906-
" # Plot\n",
907-
" splot=axes[i+1]\n",
908-
" im = splot.imshow((B1map.T), cmap='viridis', origin='lower',vmin=dynmin,vmax=dynmax)#, interpolation='spline36')\n",
909-
" splot.set_title(shim_mode, size=font_size)\n",
910-
" splot.axis('off')\n",
874+
"# Load data\n",
875+
"CP_anat=nib.load(f\"{subject}_acq-anatCP_TB1TFL.nii.gz\")\n",
876+
"CP_SC=nib.load(file_mask)\n",
877+
"CP_nTpV=nib.load(f\"{subject}_acq-CP_TB1map.nii.gz\")\n",
878+
"\n",
879+
"# Defining mask based on the magnitude image intensity threshold\n",
880+
"cslice=CP_anat.shape[2] // 2 -2 #shows the SC seg best\n",
881+
"threshold=300\n",
882+
"mask=CP_anat.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
883+
"mask=np.where(mask > threshold, 1, 0)\n",
884+
"\n",
885+
"# Cropping anat, SC, B1+\n",
886+
"CP_anat=CP_anat.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
887+
"CP_anat=CP_anat*mask\n",
888+
"CP_SC=CP_SC.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
889+
"CP_nTpV=CP_nTpV.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
890+
"CP_nTpV=CP_nTpV*mask\n",
891+
"\n",
892+
"# All opacity overalys look ugly: workaround, set the anat slice to a max value where the segmentation exists\n",
893+
"CP_anat[CP_SC>0.5]=4000;\n",
894+
"\n",
895+
"# Plotting anat overlayed with SC\n",
896+
"splot=axes[0]\n",
897+
"splot.imshow((CP_anat.T), cmap='gray', origin='lower',vmin=0,vmax=2000)#, interpolation='spline36')\n",
898+
"splot.set_title('Anat', size=font_size)\n",
899+
"splot.axis('off')\n",
900+
"\n",
901+
"# Then, plot each B1+ map, with an overlay of the mean and CV inside the cord\n",
902+
"for i,shim_mode in enumerate(shim_modes):\n",
903+
" # Load data\n",
904+
" B1map=nib.load(f\"{subject}_acq-{shim_mode}_TB1map.nii.gz\")\n",
905+
" B1map=B1map.get_fdata()[xmin:xmax,ymin:ymax, cslice]\n",
906+
" B1map=B1map*mask\n",
907+
"\n",
908+
" # Plot\n",
909+
" splot=axes[i+1]\n",
910+
" im = splot.imshow((B1map.T), cmap='viridis', origin='lower',vmin=dynmin,vmax=dynmax)#, interpolation='spline36')\n",
911+
" splot.set_title(shim_mode, size=font_size)\n",
912+
" splot.axis('off')\n",
913+
"\n",
914+
" # Find the statistics for the current shim mode\n",
915+
" shim_stats = subject_stats[subject_stats['Shim_Mode'] == shim_mode]\n",
916+
" if not shim_stats.empty:\n",
917+
" mean_val = shim_stats.iloc[0]['Average']\n",
918+
" std_val = shim_stats.iloc[0]['Standard_Deviation']\n",
919+
" cv = std_val / mean_val * 100 # Coefficient of variation in percentage\n",
920+
" annotation_text = f\"{mean_val:.2f} nT/V\\n{cv:.2f}%\"\n",
921+
" splot.annotate(annotation_text, (0.05, 0.95), xycoords='axes fraction', \n",
922+
" fontsize=10, color='white', \n",
923+
" verticalalignment='top', horizontalalignment='left')\n",
911924
"\n",
912925
"plt.tight_layout()\n",
913926
"plt.subplots_adjust(wspace=0.1, hspace=0, right=0.9)\n",
@@ -923,6 +936,7 @@
923936
"# cbar_ax = fig.add_axes([0.95, 0.5, 0.04, 0.4])\n",
924937
"# cbar = plt.colorbar(im, cax=cbar_ax)\n",
925938
"cbar_ax.set_title('nT/V', size=12)\n",
939+
"plt.savefig(os.path.join(path_results, 'fig_b1plus_map.png'), dpi=300, format='png')\n",
926940
"plt.show\n"
927941
]
928942
},

0 commit comments

Comments
 (0)