Alexsavio scripts

  1. !/usr/bin/python
  1. -------------------------------------------------------------------------------
  2. License GPL v3.0
  3. Author: Alexandre Manhaes Savio <alexsavio@gmail.com>
  4. Grupo de Inteligencia Computational <www.ehu.es/ccwintco>
  5. Universidad del Pais Vasco UPV/EHU
  6. Use this at your own risk!
  7. -------------------------------------------------------------------------------

import sys import argparse import numpy as np import nibabel as nib import scipy.ndimage as scn

  1. -------------------------------------------------------------------------------

def set_parser():

   parser = argparse.ArgumentParser(description='Empties each ROI in the 3D input volume and saves the result in the output volume.')
   parser.add_argument('-i', '--in', dest='input', required=True, help='input file')
   parser.add_argument('-o', '--out', dest='output', required=True, help='output file')
   return parser
  1. -------------------------------------------------------------------------------

def add_extension_if_needed (fpath, ext, check_if_exists=False):

  if fpath.find(ext) < 0:
    fpath += ext
  if check_if_exists:
     if not os.path.exists (fpath):
        err = 'File not found: ' + fpath
        raise IOError(err)
  return fpath
  1. -------------------------------------------------------------------------------

def main(argv=None):

   parser  = set_parser()
   try:
       args = parser.parse_args ()
   except argparse.ArgumentError, exc:
       print (exc.message + '\n' + exc.argument)
       parser.error(str(msg))
       return 0
   ifname  = args.input.strip()
   ofname   = args.output.strip()
   ofname = add_extension_if_needed(ofname, '.nii.gz')
   aff = nib.load(ifname).get_affine()
   vol = nib.load(ifname).get_data()
   out = np.zeros(vol.shape, dtype=vol.dtype)
   if vol.ndim == 2:
       kernel = np.ones([3,3], dtype=int)
   elif vol.ndim == 3:
       kernel = np.ones([3,3,3], dtype=int)
   elif vol.ndim == 4:
       kernel = np.ones([3,3,3,3], dtype=int)
   vals = np.unique(vol)
   vals = vals[vals != 0]
   for i in vals:
       roi  = vol == i
       hits = scn.binary_hit_or_miss (roi, kernel)
       roi[hits] = 0
       out[roi > 0] = i

FSL Tools

create_mean_template.sh

  #!/bin/bash
  #This script reads all images from the $subjs_root directory and creates a new smoothed image with the mean intensity value of all the images.
  subjs_root=../Female/original
  target_dir=$PWD
  cd $subjs_root
  
  cont=0
  subj_num=`ls *t88_gfc*hdr | wc -l`
  
  template=template.nii.gz
  temp=temp.nii.gz
  
  
  if [ -f $template ]
  then
     rm -f $template
  fi
  
  if [ -f $temp ]
  then
     rm -f $temp
  fi
  
  ls *hdr | while read vol;
  do
     cont=$(($cont + 1))
  
     echo $cont - $vol
  
     if [ -f $template ]
     then
        tmp=`fslstats $vol -r` 
        max_intensity=${tmp:`expr index "$tmp" ' '`:${#tmp}}
        fslmaths -dt double $vol  -div $subj_num $temp
        fslmaths -dt double $temp -add $template $template -odt double
     else
        tmp=`fslstats $vol -r` 
        max_intensity=${tmp:`expr index "$tmp" ' '`:${#tmp}}
        fslmaths -dt double $vol -div $subj_num $template
     fi
  done
  
  cont=`ls *hdr | wc -l`
  
  #smooth
  fslmaths $template -kernel gauss 2 -fmean $template
  
  mv $template $target_dir
  
  rm -f $temp
  
  echo $cont files processed.


create_tissue_prob_maps.sh

  #!/bin/bash
  
  subjs_root=../Female/segmented
  target_dir=$PWD
  cd $subjs_root
  
  cont=0
  
  white_map=white.nii.gz
  grey_map=grey.nii.gz
  csf_map=csf.nii.gz
  
  temp_white=temp_white.nii.gz
  temp_grey=temp_grey.nii.gz
  temp_csf=temp_csf.nii.gz
  
  if [ -f $white_map ]
  then
     rm $white_map
  fi
  
  if [ -f $grey_map ]
  then
     rm $grey_map
  fi
  
  if [ -f $csf_map ]
  then
     rm $csf_map
  fi
  
  ls *hdr | while read vol;
  do
     cont=$(($cont + 1))
  
     echo $cont - $vol
  
     #obtain white matter
     fslmaths $vol -thr  3 -bin $temp_white
  
     #obtain csf
     fslmaths $vol -uthr 1 -bin $temp_csf
  
     #obtain grey
     fslmaths $vol -thr 2 $temp_grey
     fslmaths $temp_grey -uthr 2 -bin $temp_grey
  
     #sum white
     if [ -f $white_map ]
     then
        fslmaths $white_map -add $temp_white $white_map
     else
        cp $temp_white $white_map
     fi
  
     if [ -f $grey_map ]
     then
        fslmaths $grey_map -add $temp_grey $grey_map
     else
        cp $temp_grey $grey_map
     fi
  
     if [ -f $csf_map ]
     then
        fslmaths $csf_map -add $temp_csf $csf_map
     else
        cp $temp_csf $csf_map
     fi
  done
  
  cont=`ls *hdr | wc -l`
  
  #normalize
  fslmaths -dt float $white_map -div $cont $white_map -odt float
  fslmaths -dt float $grey_map  -div $cont $grey_map  -odt float
  fslmaths -dt float $csf_map   -div $cont $csf_map   -odt float
  
  #smooth
  fslmaths $white_map -kernel gauss 2 -fmean $white_map
  fslmaths $grey_map  -kernel gauss 2 -fmean $grey_map
  fslmaths $csf_map   -kernel gauss 2 -fmean $csf_map
  
  #move to target directory
  mv $white_map $target_dir
  mv $grey_map  $target_dir
  mv $csf_map   $target_dir
  
  rm $temp_white
  rm $temp_csf
  rm $temp_grey
  
  echo $cont files processed.