def decimate(self, dec): '''nim is the mri data in python format dec is the decimation factor''' #nim = self.nifti header = self.nifti.get_header() self.filename = self.nifti.get_filename() xend = self.nifti.get_shape()[0] yend = self.nifti.get_shape()[1] zend = self.nifti.get_shape()[2] if type(dec) != float and type(dec) != int: print 'nonuniform decimation' xstartval=ceil(dec[0]/2); print xstartval, ystartval=ceil(dec[1]/2); zstartval=ceil(dec[2]/2); decimg=array(self.data[xstartval::dec[0],:,:][:,ystartval::dec[1],:][:,:,zstartval::dec[2]]) nonz = where(decimg > 0) x = ((nonz[0])*dec[0])+(dec[0]) y = ((nonz[1])*dec[1])+(dec[1]) z = ((nonz[2])*dec[2])+(dec[2]) else: print 'uniform decimation' startval=ceil(dec/2); decimg=array(self.data[startval::dec,:,:][:,startval::dec,:][:,:,startval::dec]) nonz = where(decimg > 0) x = ((nonz[0]+1)*dec)#-(dec) y = ((nonz[1]+1)*dec)#-(dec) z = ((nonz[2]+1)*dec)#-(dec) #--20090702--danc--not sure about reordering #mrixyz = array([x,y,z]) #--20090702--danc--adding voxel scaling. this had to be a huge bug, so why i haven't noticed it so far??? # The effect would be non existant on isotropic volumes, so thats probably why. voxdim = header['pixdim'][1:4]#.voxdim[::-1] #flipped voxel dims print voxdim #print 'WARNING: assuming voxdim reverse of img.voxdim. Could be wrong' mrixyz = (array([x,y,z]).T*voxdim).T self.mrixyz = mrixyz self.img = decimg self.factor = dec self.ind = array(nonz) try: [t,r] = transform.meg2mri(self.lpa,self.rpa,self.nas) self.megxyz = transform.mri2meg(t,r,self.mrixyz) except AttributeError: print 'Transform Error, skipping'
def handler(points, mr, gofscale, gof, sigma): from pdf2py import readwrite from meg import density from mri import transform from scipy import ndimage from nifti import NiftiImage from numpy import float32, int16, array report = {} fids = eval(mr.description) lpa = fids[0] rpa = fids[1] nas = fids[2] # self.points = array([[0,0,0],[10,0,0],[0,20,0]])#DEBUG----------------- xyz = transform.meg2mri(lpa, rpa, nas, dipole=points) # readwrite.writedata(xyz, os.path.dirname(mripath)+'/'+'xyz') print "lpa, rpa, nas", lpa, rpa, nas print mr.pixdim # do some scaling of the dips using the GOF as a weight. VoxDim = mr.voxdim[::-1] xyzscaled = (xyz / VoxDim).T print xyzscaled d = density.calc(xyz) gofscale = float32(gofscale) print "gofscale", gofscale s = gof - gofscale sf = (1 / (1 - gofscale)) * s ds = d * sf # apply a 1D gaussian filter z = density.val2img(mr.data, ds, xyzscaled) # sigma = float32(self.sigmaval.GetValue()) print "sigma", sigma # sigma = 3 print "filtering 1st dimension" f = ndimage.gaussian_filter1d(z, sigma * 1 / VoxDim[0], axis=0) print "filtering 2nd dimension" f = ndimage.gaussian_filter1d(f, sigma * 1 / VoxDim[1], axis=1) print "filtering 3rd dimension" f = ndimage.gaussian_filter1d(f, sigma * 1 / VoxDim[2], axis=2) scaledf = int16((z.max() / f.max()) * f * 1000) print "writing nifti output image" overlay = NiftiImage(int16(scaledf)) overlay.setDescription(mr.description) overlay.setFilename(mr.filename + "dd") overlay.setQForm(mr.getQForm()) return overlay
def handler(points,mr,gofscale,gof,sigma): from pdf2py import readwrite from meg import density from mri import transform from scipy import ndimage #from nifti import NiftiImage from mri import img_nibabel as img from numpy import float32, int16, array from pdf2py import readwrite import nibabel,os report = {} filename = mr.nifti.get_filename() #try: xfm = readwrite.readdata(os.path.splitext(filename)[0]+'.pym') #except: print 'Error reading coregistration info' lpa = mr.lpa rpa = mr.rpa nas = mr.nas #fids = eval(mr.description) #lpa = fids[0] #rpa = fids[1] #nas = fids[2] #self.points = array([[0,0,0],[10,0,0],[0,20,0]])#DEBUG----------------- xyz = transform.meg2mri(lpa,rpa,nas, dipole=points) #readwrite.writedata(xyz, os.path.dirname(mripath)+'/'+'xyz') print 'lpa, rpa, nas', lpa, rpa, nas print 'xyz in mri space', xyz print 'pixdim', mr.pixdim #do some scaling of the dips using the GOF as a weight. #VoxDim = mr.voxdim[::-1] VoxDim = mr.pixdim xyzscaled = (xyz/VoxDim).T print xyzscaled d = density.calc(xyz) gofscale = float32(gofscale) print 'gofscale',gofscale s= gof-gofscale sf=(1/(1-gofscale))*s ds = d*sf #apply a 1D gaussian filter z = density.val2img(mr.data, ds, xyzscaled) #sigma = float32(self.sigmaval.GetValue()) print 'sigma',sigma #sigma = 3 print 'filtering 1st dimension' f = ndimage.gaussian_filter1d(z, sigma*1/VoxDim[0], axis=0) print 'filtering 2nd dimension' f = ndimage.gaussian_filter1d(f, sigma*1/VoxDim[1], axis=1) print 'filtering 3rd dimension' f = ndimage.gaussian_filter1d(f, sigma*1/VoxDim[2], axis=2) scaledf = int16((z.max()/f.max())*f*1000) print 'writing nifti output image' #overlay = NiftiImage(int16(scaledf)) overlay = nibabel.Nifti1Image(scaledf,mr.nifti.get_affine(),mr.nifti.get_header()) #overlay = NiftiImage(int16(scaledf)) #overlay.setDescription(mr.description) filename = os.path.splitext(mr.nifti.get_filename())[0] overlay.to_filename(filename+'dd.nii.gz') print 'Density Image Saved', filename+'dd.nii.gz' #overlay.setFilename(mr.filename+'dd') #overlay.setQForm(mr.getQForm()) return overlay