if bound is not None: if bound[1] <= bound[0]: print 'ERROR: Lower bound of %f is not lower than upper bound of %f'%(bound[0],bound[1]) sys.exit() #Load needed libraries import numpy as np, nibabel as nib, nagini, sys, scipy.optimize as opt, scipy.interpolate as interp from tqdm import tqdm ######################### ###Data Pre-Processing### ######################### print ('Loading images...') #Load image headers pet = nagini.loadHeader(args.pet[0]) brain = nagini.loadHeader(args.brain[0]) #Load in the idaif. idaif = nagini.loadIdaif(args.idaif[0]) #Load in the info file info = nagini.loadInfo(args.info[0]) #Check to make sure dimensions match if pet.shape[0:3] != brain.shape[0:3] or pet.shape[3] != idaif.shape[0] or pet.shape[3] != info.shape[0]: print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Get the image data petData = pet.get_data()
maxs = np.fmin(np.max(coords,axis=0) + 1, img.shape) #Return cropped image return img[mins[0]:maxs[0],mins[1]:maxs[1]] #Change default math font mpl.rcParams['mathtext.default'] = 'regular' #Setup colors if args.white is False: faceColor = 'black'; textColor = 'white' else: faceColor = 'white'; textColor = 'black' #Load in image header img = nagini.loadHeader(args.img[0]) #Check to make sure image is three or four dimensions nImgDim = len(img.shape) if nImgDim !=3 and nImgDim !=4: print 'Error: Plot image is not three or four dimensional' sys.exit() #Get the actual image data imgData = img.get_data() #If we have a four dimensional image, extract the slice we want. if nImgDim == 4: #Check to see if we can actually plot that time point if args.f < 0 or args.f >= imgData.shape[3]:
argParse = argparse.ArgumentParser( description='Performes PCA on two 3D-images') argParse.add_argument('one', help='First Nifti Image', nargs=1, type=str) argParse.add_argument('two', help='Second Nifti Image', nargs=1, type=str) argParse.add_argument('out', help='Root for output files', nargs=1, type=str) argParse.add_argument('-brain', help='Brain mask for images', nargs=1, type=str) args = argParse.parse_args() #Load libraries import numpy as np, nibabel as nib, nagini, sys #Load image headers one = nagini.loadHeader(args.one[0]) two = nagini.loadHeader(args.two[0]) #Make sure dimensions match if one.shape != two.shape: print 'ERROR: Image dimensions do not match. Exiting...' sys.exit() #Get image data and flatten oneData = one.get_data().flatten() twoData = two.get_data().flatten() #Mask a mask of shared non-zero voxels maskData = np.logical_and(oneData != 0, twoData != 0) #Load mask if needed
#Get time variable drawTime = np.copy(midTime) else: #Load in simple aif text file aifData = np.loadtxt(args.dta[0]) #Rename so variables have same names drawTime = aifData[:,0]; corrCounts = aifData[:,1] #Apply time offset that was applied to images corrCounts *= np.exp(np.log(2)/6586.26*info[0,0]) #Load image headers pet = nagini.loadHeader(args.pet[0]) roi = nagini.loadHeader(args.roi[0]) cbv = nagini.loadHeader(args.cbv[0]) #Check to make sure dimensions match if pet.shape[3] != info.shape[0] or pet.shape[0:3] != cbv.shape[0:3] or pet.shape[0:3] != roi.shape[0:3]: print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Brain mask logic if args.brain is not None: #Load brain mask header brain = nagini.loadHeader(args.brain[0]) #Make sure its dimensions match
######################### ###Data Pre-Processing### ######################### print('Loading images...') #Load in the input function if args.dcv != 1: aif = nagini.loadAif(args.aif[0]) else: aif = nagini.loadAif(args.aif[0], dcv=True) #Load in the info file info = nagini.loadInfo(args.info[0]) #Load image headers pet = nagini.loadHeader(args.pet[0]) if args.ps is not None: cbf = nagini.loadHeader(args.ps[0]) if np.prod(pet.shape[0:3]) != np.prod(cbf.shape[0:3]): print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Check to make sure dimensions match if pet.shape[3] != info.shape[0]: print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Load in the kernel if necessary if args.kernel is not None: try: kernel = np.loadtxt(args.kernel[0])
###Parse Arguments### ##################### import argparse, sys argParse = argparse.ArgumentParser(description='Performes PCA on two 3D-images') argParse.add_argument('one',help='First Nifti Image',nargs=1,type=str) argParse.add_argument('two',help='Second Nifti Image',nargs=1,type=str) argParse.add_argument('out',help='Root for output files',nargs=1,type=str) argParse.add_argument('-brain',help='Brain mask for images',nargs=1,type=str) args = argParse.parse_args() #Load libraries import numpy as np, nibabel as nib, nagini, sys #Load image headers one = nagini.loadHeader(args.one[0]) two = nagini.loadHeader(args.two[0]) #Make sure dimensions match if one.shape != two.shape: print 'ERROR: Image dimensions do not match. Exiting...' sys.exit() #Get image data and flatten oneData = one.get_data().flatten() twoData = two.get_data().flatten() #Mask a mask of shared non-zero voxels maskData = np.logical_and(oneData!=0,twoData!=0) #Load mask if needed
return img[mins[0]:maxs[0], mins[1]:maxs[1]] #Change default math font mpl.rcParams['mathtext.default'] = 'regular' #Setup colors if args.white is False: faceColor = 'black' textColor = 'white' else: faceColor = 'white' textColor = 'black' #Load in image header img = nagini.loadHeader(args.img[0]) #Check to make sure image is three or four dimensions nImgDim = len(img.shape) if nImgDim != 3 and nImgDim != 4: print 'Error: Plot image is not three or four dimensional' sys.exit() #Get the actual image data imgData = img.get_data() #If we have a four dimensional image, extract the slice we want. if nImgDim == 4: #Check to see if we can actually plot that time point if args.f < 0 or args.f >= imgData.shape[3]:
######################### ###Data Pre-Processing### ######################### print ('Loading images...') #Load in the input function if args.dcv != 1: aif = nagini.loadAif(args.aif[0]) else: aif = nagini.loadAif(args.aif[0],dcv=True) #Load in the info file info = nagini.loadInfo(args.info[0]) #Load image headers pet = nagini.loadHeader(args.pet[0]) brain = nagini.loadHeader(args.brain[0]) roi = nagini.loadHeader(args.roi[0]) #Check to make sure dimensions match if pet.shape[0:3] != brain.shape[0:3] or pet.shape[3] != info.shape[0] or brain.shape[0:3] != roi.shape[0:3]: print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Get the image data petData = pet.get_data() brainData = brain.get_data() roiData = roi.get_data() #Flatten the PET images and then mask petFlat = nagini.reshape4d(petData)
action='store_true') argParse.set_defaults(showMin=False, showMax=True) args = argParse.parse_args() #Make sure user min is above maximum if args.thr is not None: if args.thr[0] >= args.thr[1]: print 'Error: Minimum value of %f is not above maximum value of %f' % ( args.thr[0], args.thr[1]) sys.exit() #Load in the libraries we will need import numpy as np, matplotlib as mpl, matplotlib.pyplot as plt, nibabel as nib, nagini, sys #Load in image headers pImg = nagini.loadHeader(args.pImg[0]) nImg = nagini.loadHeader(args.nImg[0]) #Make sure images have same dimensions if pImg.shape != nImg.shape: print 'Error: P-value images do not have the same dimensions.' sys.exit() #Check to make sure image is three or four dimensions nImgDim = len(pImg.shape) if nImgDim != 3 and nImgDim != 4: print 'Error: P-value images are three or four dimensional' sys.exit() #Get the actual image data pData = pImg.get_data()
##################### ###Parse Arguments### ##################### import argparse argParse = argparse.ArgumentParser(description='Project ROI values back') argParse.add_argument('val', help='ROI values to project', nargs=1) argParse.add_argument('roi', help='Image containing ROIs', nargs=1) argParse.add_argument('out', help='Root for output file', nargs=1) args = argParse.parse_args() #Load needed libraries import numpy as np, nibabel as nib, nagini, sys #Load images headers val = nagini.loadHeader(args.val[0]) roi = nagini.loadHeader(args.roi[0]) #Load image data valData = val.get_data() roiData = roi.get_data() #Reshape image data as necessary if len(valData.shape) == 4: valData = nagini.reshape4d(valData) else: valData = valData.reshape((val.shape[0], 1)) roiData = roiData.flatten() #Sample into ROIs projData = nagini.roiBack(valData, roiData)
argParse.add_argument( '-nii', help='Output ROI as a ROIx1x1xTime Nifti file. Default is text file', action='store_const', const=1) argParse.add_argument('-stat', help='Statistic to compute.', choices=['mean', 'min', 'max', 'median'], default='mean') args = argParse.parse_args() #Load needed libraries import numpy as np, nibabel as nib, nagini, sys #Load image headers img = nagini.loadHeader(args.img[0]) roi = nagini.loadHeader(args.roi[0]) #Check to make sure images have same dimensions if img.shape[0:3] != roi.shape[0:3]: print 'ERROR: Images do not have same dimensions. Exiting...' sys.exit() #Load image data imgData = img.get_data() roiData = roi.get_data() #Reshape image data as necessary if len(imgData.shape) == 4: imgData = nagini.reshape4d(imgData) else:
arg_parse.add_argument('seg',help='3d segmentation image',nargs=1) arg_parse.add_argument('out',help='Root for output image',nargs=1) arg_parse.add_argument('-fwhm',help='FWHM for PET PSF in mm. Default is 8.0',nargs=1,type=float,default=[8.0]) arg_parse.add_argument('-mask',help='Mask for RBV image',nargs=1) arg_parse.add_argument('-noZero',help='Do not count 0 as a seperate ROI',action='store_const',const=1) arg_parse.add_argument('-nii',help='Output RSF ROI averages as a ROIx1x1xTime Nifti file. Default is text file',action='store_const',const=1) arg_parse.add_argument('-weight',help='Input previously calculated w weight matrix',nargs=1) args = arg_parse.parse_args() #Import libraries import numpy as np, nibabel as nib, scipy.ndimage.filters as filt import scipy.optimize as opt, nagini, sys from tqdm import tqdm #Load headers pet = nagini.loadHeader(args.pet[0]) seg = nagini.loadHeader(args.seg[0]) #Check that images have same dimensions if pet.shape[0:3] != seg.shape[0:3]: print 'ERROR: Images do have not same dimensions...' sys.exit() #Setup mask if args.mask is not None: #Get mask header mask = nagini.loadHeader(args.mask[0]) #Check to make sure dimensions mask if mask.shape[0:3] != pet.shape[0:3]:
help='Binary mask over which to compute correlation') argParse.add_argument('-rank', help='Compute Spearman correlation instead of Pearson', action='store_const', const=1) args = argParse.parse_args() #Load needed libraries import numpy as np, nibabel as nib, nagini, sys, scipy.stats as stats ######################### ###Data Pre-Processing### ######################### #Load image headers imgOne = nagini.loadHeader(args.imgOne[0]) imgTwo = nagini.loadHeader(args.imgTwo[0]) #Check to make sure dimensions match if imgOne.shape != imgOne.shape: print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Load in image data oneData = imgOne.get_data().flatten() twoData = imgTwo.get_data().flatten() #Process mask if necessary if args.mask is not None: #Load in mask header
help= 'Output RSF ROI averages as a ROIx1x1xTime Nifti file. Default is text file', action='store_const', const=1) arg_parse.add_argument('-weight', help='Input previously calculated w weight matrix', nargs=1) args = arg_parse.parse_args() #Import libraries import numpy as np, nibabel as nib, scipy.ndimage.filters as filt import scipy.optimize as opt, nagini, sys from tqdm import tqdm #Load headers pet = nagini.loadHeader(args.pet[0]) seg = nagini.loadHeader(args.seg[0]) #Check that images have same dimensions if pet.shape[0:3] != seg.shape[0:3]: print 'ERROR: Images do have not same dimensions...' sys.exit() #Setup mask if args.mask is not None: #Get mask header mask = nagini.loadHeader(args.mask[0]) #Check to make sure dimensions mask if mask.shape[0:3] != pet.shape[0:3]:
##################### import argparse argParse = argparse.ArgumentParser(description='Samples a image at several ROIs:') argParse.add_argument('img',help='Image to put into ROIs',nargs=1) argParse.add_argument('roi',help='Image containing ROIs',nargs=1) argParse.add_argument('out',help='Root for output file',nargs=1) argParse.add_argument('-nii',help='Output ROI as a ROIx1x1xTime Nifti file. Default is text file',action='store_const',const=1) argParse.add_argument('-stat',help='Statistic to compute.',choices=['mean','min','max','median'],default='mean') args = argParse.parse_args() #Load needed libraries import numpy as np, nibabel as nib, nagini, sys #Load image headers img = nagini.loadHeader(args.img[0]) roi = nagini.loadHeader(args.roi[0]) #Check to make sure images have same dimensions if img.shape[0:3] != roi.shape[0:3]: print 'ERROR: Images do not have same dimensions. Exiting...' sys.exit() #Load image data imgData = img.get_data() roiData = roi.get_data() #Reshape image data as necessary if len(imgData.shape) == 4: imgData = nagini.reshape4d(imgData) else:
if bound[1] <= bound[0]: print 'ERROR: Lower bound of %f is not lower than upper bound of %f' % ( bound[0], bound[1]) sys.exit() #Load needed libraries import numpy as np, nibabel as nib, nagini, sys, scipy.optimize as opt, scipy.interpolate as interp from tqdm import tqdm ######################### ###Data Pre-Processing### ######################### print('Loading images...') #Load image headers pet = nagini.loadHeader(args.pet[0]) brain = nagini.loadHeader(args.brain[0]) #Load in the idaif. idaif = nagini.loadIdaif(args.idaif[0]) #Load in the info file info = nagini.loadInfo(args.info[0]) #Check to make sure dimensions match if pet.shape[0:3] != brain.shape[0:3] or pet.shape[3] != idaif.shape[ 0] or pet.shape[3] != info.shape[0]: print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Get the image data
drawTime = np.copy(midTime) else: #Load in simple aif text file aifData = np.loadtxt(args.dta[0]) #Rename so variables have same names drawTime = aifData[:, 0] corrCounts = aifData[:, 1] #Apply time offset that was applied to images corrCounts *= np.exp(np.log(2) / 6586.26 * info[0, 0]) #Load image headers pet = nagini.loadHeader(args.pet[0]) roi = nagini.loadHeader(args.roi[0]) cbv = nagini.loadHeader(args.cbv[0]) #Check to make sure dimensions match if pet.shape[3] != info.shape[0] or pet.shape[0:3] != cbv.shape[ 0:3] or pet.shape[0:3] != roi.shape[0:3]: print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Brain mask logic if args.brain is not None: #Load brain mask header brain = nagini.loadHeader(args.brain[0])
#Ignore invalid and overflow warnings np.seterr(invalid='ignore',over='ignore') ######################### ###Data Pre-Processing### ######################### print ('Loading images...') #Load in the dta file dta = nagini.loadDta(args.dta[0]) #Load in the info file info = nagini.loadInfo(args.info[0]) #Load image headers pet = nagini.loadHeader(args.pet[0]) cbf = nagini.loadHeader(args.cbf[0]) cbv = nagini.loadHeader(args.cbv[0]) #Check to make sure dimensions match if pet.shape[3] != info.shape[0] or pet.shape[0:3] != cbf.shape[0:3] or pet.shape[0:3] != cbv.shape[0:3] : print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Brain mask logic if args.brain is not None: #Load brain mask header brain = nagini.loadHeader(args.brain[0]) #Make sure its dimensions match
##################### ###Parse Arguments### ##################### import argparse argParse = argparse.ArgumentParser(description='Project ROI values back') argParse.add_argument('val',help='ROI values to project',nargs=1) argParse.add_argument('roi',help='Image containing ROIs',nargs=1) argParse.add_argument('out',help='Root for output file',nargs=1) args = argParse.parse_args() #Load needed libraries import numpy as np, nibabel as nib, nagini, sys #Load images headers val = nagini.loadHeader(args.val[0]) roi = nagini.loadHeader(args.roi[0]) #Load image data valData = val.get_data() roiData = roi.get_data() #Reshape image data as necessary if len(valData.shape) == 4: valData = nagini.reshape4d(valData) else: valData = valData.reshape((val.shape[0],1)) roiData = roiData.flatten() #Sample into ROIs projData = nagini.roiBack(valData,roiData)
#Ignore invalid and overflow warnings np.seterr(invalid='ignore', over='ignore') ######################### ###Data Pre-Processing### ######################### print('Loading images...') #Load in the dta file dta = nagini.loadDta(args.dta[0]) #Load in the info file info = nagini.loadInfo(args.info[0]) #Load image headers pet = nagini.loadHeader(args.pet[0]) roi = nagini.loadHeader(args.roi[0]) cbf = nagini.loadHeader(args.cbf[0]) cbv = nagini.loadHeader(args.cbv[0]) #Check to make sure dimensions match if pet.shape[3] != info.shape[0] or pet.shape[0:3] != roi.shape[ 0:3] or pet.shape[0:3] != cbf.shape[0:3] or pet.shape[ 0:3] != cbv.shape[0:3]: print 'ERROR: Data dimensions do not match. Please check...' sys.exit() #Brain mask logic if args.brain is not None: #Load brain mask header
arg_parse.add_argument('-p_size', help='Size for local patch. Default is 3.', nargs=1, type=int, default=[3], metavar='int') arg_parse.add_argument('-w_size',help='Size for non-local window. Default is 11.', nargs=1, type=int, default=[11], metavar='int') arg_parse.add_argument('-t_start', help='First timepoint to consider when computing weights.', nargs=1, type=int, metavar='int') arg_parse.add_argument('-dim3', action='store_true' ,help='Use a 3D patch and window. Default is 2d (slice by slice).') args = arg_parse.parse_args() #Load libraries import numpy as np import nibabel as nib import nagini import itertools import sys from tqdm import tqdm #Load pet and mask headers pet = nagini.loadHeader(args.pet[0]); mask = nagini.loadHeader(args.mask[0]) #Make sure image dimensions match if pet.shape[0:3] != mask.shape[0:3]: print 'Error: Image dimensions do not match. Please check...' sys.exit() #Load image data pet_data = pet.get_data(); mask_data = mask.get_data() #Check for singleton dimensions if pet_data.shape[-1] == 1: pet_data = np.squeeze(pet_data) if mask_data.shape[-1] == 1: mask_data = np.squeeze(mask_data)