Skip to content

Instantly share code, notes, and snippets.

@xgrg
Last active November 17, 2020 11:56
Show Gist options
  • Save xgrg/eee39713a740ee202215fc5f23666509 to your computer and use it in GitHub Desktop.
Save xgrg/eee39713a740ee202215fc5f23666509 to your computer and use it in GitHub Desktop.
Generating animated snapshots for Quality Control of coregistration
#! /usr/bin/env python
# Usage: python qc_reg.py path_to_T1 path_to_T2 path_to_GIF
import sys
t1_fp, t2_fp, gif_fp = sys.argv[1:]
def ants_snapshot(t1w_fp, coreg_fp):
""" Create 2 sets of snapshots, one with the background (T1) image,
one with the overlay. Both along the 3 axes (axial, coronal and sagittal).
Returns the paths to the created images. """
from nilearn import plotting
from nilearn import image
import tempfile
# compute a threshold for the overlay based on range of intensity values
data = image.load_img(coreg_fp).get_fdata()
val_range = abs(data.min()) + abs(data.max())
thresh = val_range / 6
paths = []
paths_orig = []
for each in 'xyz':
_, path = tempfile.mkstemp(suffix='.jpg')
paths_orig.append(path)
im = plotting.plot_anat(t1w_fp,
black_bg=True,
bg_img=None,
display_mode=each,
draw_cross=False)
im.savefig(path)
_, path = tempfile.mkstemp(suffix='.jpg')
paths.append(path)
im.add_overlay(coreg_fp,
threshold=thresh,
cmap=plotting.cm.black_red)
im.savefig(path)
return paths, paths_orig
print('Processing: %s over %s' % (t2_fp, t1_fp))
p1, p2 = ants_snapshot(t1_fp, t2_fp)
# Preparing the previous outputs for __montage__
paths = {'x': [p1[0]], 'y':[p1[1]], 'z': [p1[2]]}
paths_orig = {'x': [p2[0]], 'y': [p2[1]], 'z': [p2[2]]}
from nisnap.utils.montage import __montage__
__montage__(paths, paths_orig, 'xyz', 50, True, True, savefig=gif_fp)
print('Saving %s' % gif_fp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment