HighVSLowMotion.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import os
  2. import nibabel as nib
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.axes_grid1 import make_axes_locatable
  6. from matplotlib.colors import Normalize
  7. from matplotlib.gridspec import GridSpec
  8. # Define the directory path where the NIfTI and PNG files are located
  9. path = r"C:\Users\aswen\Desktop\Code\2023_Kalantari_AIDAqc\outputs\files_4figs" # Replace with your actual directory path
  10. # File names for NIfTI images (assuming they represent motion data)
  11. motion_best_filename = "MotionBest.nii.gz"
  12. motion_worst_filename = "MotionWorst.nii.gz"
  13. # Load the NIfTI files
  14. motion_best = nib.load(os.path.join(path, motion_best_filename))
  15. motion_worst = nib.load(os.path.join(path, motion_worst_filename))
  16. # Get the NIfTI data as NumPy arrays
  17. motion_best_data = motion_best.get_fdata()
  18. motion_worst_data = motion_worst.get_fdata()
  19. # Rotate the data arrays 90 degrees counterclockwise
  20. motion_best_data = np.rot90(motion_best_data, k=-1)
  21. motion_worst_data = np.rot90(motion_worst_data, k=1)
  22. # Average over the 4th dimension and select the middle slice
  23. motion_best_data = np.mean(motion_best_data, axis=3)
  24. motion_worst_data = np.mean(motion_worst_data, axis=3)
  25. slice_index1 = motion_best_data.shape[2] // 2
  26. slice_index2 = motion_worst_data.shape[2] // 2
  27. motion_best_data = motion_best_data[:, :, slice_index1]
  28. motion_best_data = np.rot90(np.rot90(motion_best_data, k=1), k=1)
  29. motion_worst_data = motion_worst_data[:, :, slice_index2]
  30. # Normalize the data to the range of 0 to 1
  31. norm = Normalize(vmin=motion_best_data.min(), vmax=motion_best_data.max())
  32. motion_best_data = norm(motion_best_data)
  33. motion_worst_data = norm(motion_worst_data)
  34. # Create a figure with GridSpec
  35. cm = 1/2.54 # centimeters in inches
  36. fig = plt.figure(figsize=(9*cm, 6.68*cm), dpi=300)
  37. gs = GridSpec(1, 2, width_ratios=[1, 1])
  38. # Create the axes for the "Motion Data (Best)" NIfTI image
  39. ax0 = plt.subplot(gs[0])
  40. im0 = ax0.imshow(motion_best_data, cmap='viridis', origin='lower', vmin=0, vmax=1)
  41. ax0.set_title("rsfMRI low motion", fontsize=10, fontweight='bold', fontname='Times New Roman')
  42. ax0.set_xticks([])
  43. ax0.set_yticks([])
  44. # Create the axes for the "Motion Data (Worst)" NIfTI image
  45. ax1 = plt.subplot(gs[1])
  46. im1 = ax1.imshow(motion_worst_data, cmap='viridis', origin='lower', vmin=0, vmax=1)
  47. ax1.set_title("rsfMRI high motion", fontsize=10, fontweight='bold', fontname='Times New Roman')
  48. ax1.set_xticks([])
  49. ax1.set_yticks([])
  50. # Add a single colorbar for the NIfTI images with more ticks
  51. divider0 = make_axes_locatable(ax1)
  52. cax0 = divider0.append_axes("right", size="5%", pad=0.05)
  53. cbar0 = plt.colorbar(im0, cax=cax0, ticks=np.linspace(0, 1, 6)) # Adjust the number of ticks (6 in this case)
  54. cbar0.ax.tick_params(labelsize=8)
  55. # Adjust the layout for better visibility
  56. plt.tight_layout()
  57. # Display the figure
  58. plt.show()