spinal-cord-7t / coil-qc-code

7T Spinal Cord Coil QC Analysis Code
0 stars 0 forks source link

Add section that generate a GIF animation to assess the quality of co-registration #80

Open jcohenadad opened 1 week ago

jcohenadad commented 1 week ago

I tried a couple of things, without success. Below my (messy) code for posterity:

Terminal output ```python import imageio import matplotlib.animation as animation from IPython.display import Image, display def get_mid_sagittal_slice(image): mid_sagittal_index = image.shape[0] // 2 return image[mid_sagittal_index, :, :] def create_gif(image1, image2, output_path, duration=0.5): images = [] fig, ax = plt.subplots() def add_image_to_list(image): ax.clear() ax.imshow(image, cmap='gray') ax.axis('off') fig.canvas.draw() image_from_plot = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) images.append(image_from_plot.reshape(fig.canvas.get_width_height()[::-1] + (3,))) add_image_to_list(image1) add_image_to_list(image2) imageio.mimsave(output_path, images, duration=duration) plt.close(fig) # def create_gif(images, output_path, duration=0.5): # fig, ax = plt.subplots() # frames = [] # for img in images: # ax.imshow(img, cmap='gray') # ax.axis('off') # fig.canvas.draw() # frame = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) # frames.append(frame.reshape(fig.canvas.get_width_height()[::-1] + (3,))) # imageio.mimsave(output_path, frames, duration=duration) subject_ref = 'sub-CRMBM' file_names = ["TFLTB1map"] animations = [] with open('sub-MGH_TFLTB1map_registration.gif','rb') as f: display(Image(data=f.read(), format='png')) fig, axes = plt.subplots(1, len(subjects)-1, figsize=(20, 4)) for idx, subject in enumerate([s for s in subjects if s != subject_ref]): print(f"👉 PROCESSING: {subject}") for file_name in file_names: file_path = os.path.join(path_data, subject, "fmap", f"{subject}_{file_name}_reg.nii.gz") ref_file_path = os.path.join(path_data, subject_ref, "fmap", f"{subject_ref}_{file_name}.nii.gz") # Load NIfTI files subject_img = nib.load(file_path).get_fdata() ref_img = nib.load(ref_file_path).get_fdata() # Get mid-sagittal slices subject_slice = get_mid_sagittal_slice(subject_img) ref_slice = get_mid_sagittal_slice(ref_img) # Create GIF output_path = os.path.join(path_data, f'{subject}_{file_name}_registration.gif') create_gif(subject_slice, ref_slice, output_path) # # Display the GIF # with open(output_path, 'rb') as file: # display(Image(data=file.read(), format='gif')) # # Display the first frame of the GIF for reference # axes[idx].imshow(subject_slice, cmap='gray') # axes[idx].set_title(f'{subject} vs {subject_ref}') # axes[idx].axis('off') import matplotlib.image as mpimg img = mpimg.imread("sub-MGH_TFLTB1map_registration.gif") plt.imshow(img) # from IPython.display import HTML # HTML('') plt.tight_layout() plt.show() ```

Relevant links:

So I will end up simply outputting the syntax to call FSLeyes to verify the quality of registration:

fsleyes sub-CRMBM/fmap/sub-CRMBM_TFLTB1map.nii.gz /Users/julien/code/coil-qc-code/data-phantom/sub-MGH/fmap/sub-MGH_TFLTB1map_reg.nii.gz /Users/julien/code/coil-qc-code/data-phantom/sub-MNI/fmap/sub-MNI_TFLTB1map_reg.nii.gz /Users/julien/code/coil-qc-code/data-phantom/sub-MPI/fmap/sub-MPI_TFLTB1map_reg.nii.gz /Users/julien/code/coil-qc-code/data-phantom/sub-MSSM/fmap/sub-MSSM_TFLTB1map_reg.nii.gz /Users/julien/code/coil-qc-code/data-phantom/sub-NTNU/fmap/sub-NTNU_TFLTB1map_reg.nii.gz /Users/julien/code/coil-qc-code/data-phantom/sub-UCL/fmap/sub-UCL_TFLTB1map_reg.nii.gz
jcohenadad commented 1 week ago

Alternatively, we could consider using https://brainsprite.github.io/auto_examples/index.html

(keeping this issue open until someone implements the brainsprite option)