JDG Lab Notebook

Making registration QC reports

Overview

The following are some notes on making quality control (QC) reports of various kinds for imaging data analyses.

The task of making QC and similar report type documents basically splits into to problems:

  1. Making the diagnostic images in question
  2. Compiling them into adequately digestible and detailed documents

The following notes consider python-based solutions to each of these. The focus for 1. is primarily assessing registration quality, which is is basically a question of overlaying MRI images and visualizing as a set of 2D slices. For this we shall use the nilearn library, which has a very nice set of simple visualization functions that are well-suited to this task.

For 2. I have a rather heterogenous set of notes centred around constructing report documents either a) directly with matplotlib, or b) through jupyter notebooks. There are pros and cons to each of these.

We deal with each of these two problems in turn.

Notebook Setup

Define some variables

# define system-specific filepaths etc
%run ~/set_localenv_vars.py

nb_name = 'making_registration_qc_reports'

# this is where I look for things
outdir = '%s/notebooks/%s' %(le['data_dir'],nb_name)

# this is where things actually live
outdir_loc = '%s/%s' %('/mnt/titania-hecuba/mcintosh_lab/john/Data/notebooks', nb_name)
import os; 
if not os.path.isdir(outdir_loc): os.makedirs(outdir_loc)
if not os.path.isdir(outdir): os.system('ln -s %s %s' %(outdir_loc,outdir))

# folder on scinet where analyses are run from and written to

# stuff for workdocs-cloudfiles

aws_key = 'drowssaperucesyreva'
aws_secret = '?teytidettopsuoyevah'  

Importage

# Generic Stuff

import os,sys,pandas as pd,numpy as np


# Visualization Stuff

%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from IPython.display import Image,HTML,clear_output,display as d
from wand.image import Image as WImage
from nilearn.plotting import plot_roi,plot_anat

import seaborn as sns



# Neuroimaging analysis stuff

import nibabel as nib

from nipype.interfaces.fsl import BET
from nipype.interfaces.fsl import FNIRT

# Workdocs-cloudfiles stuff

sys.path.append(le['ipynb_workdocs_dir'])
from new_utils import nb_fig,cloudfiles_nb,jg_df

Initialize aws api and workdocs-cloudfiles folder

cnb = cloudfiles_nb('aws', [aws_key,aws_secret])
cnb.initialize_folder(nb_name)

Calico document tools

%%javascript
IPython.load_extensions('calico-spell-check',
                        'calico-document-tools', 'calico-cell-tools');

Go to output folder

os.chdir(outdir)

Ok, let's get cracking...

Making diagnostic registration images

Example 1: Zheng data

roi_file = '/phocaea/mcintosh_lab/jwang/ADNI_tmp/warped_rmask_2_t1.nii'
t1_file = '/phocaea/mcintosh_lab/jwang/ADNI_tmp/T1.nii.gz'
roi_img = nib.load(roi_file)
t1_img = nib.load(t1_file)

We get a nice plot of the roi image by just calling plot_roi off the shelf:

(0_0)
broken link .

First things first, for present purposes we want to

  • drop the cross-bar
  • not dim the background image
  • binarize the overlay, and plot as a single bright colour with medium transparency

So:

roibin_img = nib.Nifti1Image((roi_img.get_data()!=0).astype(int),
                             roi_img.affine)
(0_0)
broken link .

For diagnostic purposes it's actually better to plot a series of slices in each plane

(0_0)
broken link .
(0_0)
broken link .
(0_0)
broken link .

The coronal and axial slices are most useful for looking at registration accuracy here.

Example 2: registration from scratch

# to do...

Making report documents

Making reports with matplotlib

Matplotlib is the python scientific visualization library of choice. There are a number of...

  • pyreport - library by Gael Varoquaux for making reports from python scripts. As the big red boxes indicated, this is not currently being maintained; but might nevertheless have some useful stuff

    https://github.com/joblib/pyreport

A4 sized documents

files = [[roi_file,t1_file] for _ in range(12)]

pdf=PdfPages('test_report2.pdf')

nfigspp = 4
fignum = 0

subnum = 0
  
for sub_it,(roi_file,t1_file) in enumerate(files):


  fig, ax = plt.subplots(nrows=4,ncols=1, figsize=(12,12))

    
  # Add subjects to page
    
  for r in range(nfigspp):

    roi_img = nib.load(roi_file)
    t1_img = nib.load(t1_file)
    roibin_img = nib.Nifti1Image((roi_img.get_data()!=0).astype(int),
                                  roi_img.affine)    
        
    a = ax.ravel()[r]
    
    title = 'sub %s'  %(subnum+1)
    plot_roi(roibin_img,bg_img=t1_img,alpha=0.3,
             draw_cross=False,dim=None,cmap='hot',vmin=0,vmax=1.2,
             display_mode='y',title=title,axes=a)

    subnum+=1


  # Add page to pdf
  pdf.savefig(fig)
    
  plt.close()
  clear_output()
    
  
    
pdf.close()    
#!evince test_report2.pdf

Check out the pdf pages with

WImage(filename='test_report.pdf[1]')

WImage(filename='test_report.pdf[2]')

etc...

Big images

# to do...