|
439 | 439 | "\n",
|
440 | 440 | "# Adjust the layout so labels and titles do not overlap\n",
|
441 | 441 | "plt.tight_layout()\n",
|
| 442 | + "plt.savefig(os.path.join(path_results, 'fig_gre_csf-sc_ratio.png'), dpi=300, format='png')\n", |
442 | 443 | "plt.show()"
|
443 | 444 | ]
|
444 | 445 | },
|
|
734 | 735 | "\n",
|
735 | 736 | "# Adjust the layout so labels and titles do not overlap\n",
|
736 | 737 | "plt.tight_layout()\n",
|
| 738 | + "plt.savefig(os.path.join(path_results, 'fig_b1plus.png'), dpi=300, format='png')\n", |
737 | 739 | "plt.show()"
|
738 | 740 | ]
|
739 | 741 | },
|
|
843 | 845 | "\n",
|
844 | 846 | "# Select subject to show\n",
|
845 | 847 | "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", |
846 | 856 | "\n",
|
847 | 857 | "# Defining crop limits for resulting figure\n",
|
848 | 858 | "xmin = 20\n",
|
|
859 | 869 | "font_size = 14\n",
|
860 | 870 | "axes=axes.flatten()\n",
|
861 | 871 | "\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", |
898 | 873 | "\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", |
911 | 924 | "\n",
|
912 | 925 | "plt.tight_layout()\n",
|
913 | 926 | "plt.subplots_adjust(wspace=0.1, hspace=0, right=0.9)\n",
|
|
923 | 936 | "# cbar_ax = fig.add_axes([0.95, 0.5, 0.04, 0.4])\n",
|
924 | 937 | "# cbar = plt.colorbar(im, cax=cbar_ax)\n",
|
925 | 938 | "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", |
926 | 940 | "plt.show\n"
|
927 | 941 | ]
|
928 | 942 | },
|
|
0 commit comments