def transform_vol(invol, xfm, space_defining):
    invol = bg.unzip_file(invol)# in case zipped
    xfm =  bg.unzip_file(xfm)# in case zipped
    space_defining = bg.unzip_file(space_defining)# in case zipped
    pp.apply_transform_onefile(xfm, invol)
    pp.reslice(space_defining, invol)
    rinvol = pp.prefix_filename(invol, prefix='r')
    bg.remove_files([invol])
    bg.zip_files([space_defining])
    return rinvol
def transform_vol(invol, xfm, space_defining):
    invol = bg.unzip_file(invol)  # in case zipped
    xfm = bg.unzip_file(xfm)  # in case zipped
    space_defining = bg.unzip_file(space_defining)  # in case zipped
    pp.apply_transform_onefile(xfm, invol)
    pp.reslice(space_defining, invol)
    rinvol = pp.prefix_filename(invol, prefix='r')
    bg.remove_files([invol])
    bg.zip_files([space_defining])
    return rinvol
Esempio n. 3
0
def binarize_erode_mask(infile, min = 0.05, erode = 1):
    """ use freesurfer to binarize and erode a mask"""
    outfile = pp.prefix_filename(infile,
                                 prefix='ero%d_%2.2fbin_'%(erode, min))
                                 
    bin = freesurfer.Binarize()
    bin.inputs.in_file = infile
    bin.inputs.min = min
    bin.inputs.binary_file = outfile
    bin.inputs.erode = erode
    binout = bin.run()
    return binout
Esempio n. 4
0
def binarize_erode_mask(infile, min=0.05, erode=1):
    """ use freesurfer to binarize and erode a mask"""
    outfile = pp.prefix_filename(infile,
                                 prefix='ero%d_%2.2fbin_' % (erode, min))

    bin = freesurfer.Binarize()
    bin.inputs.in_file = infile
    bin.inputs.min = min
    bin.inputs.binary_file = outfile
    bin.inputs.erode = erode
    binout = bin.run()
    return binout
Esempio n. 5
0
def make_pvc(pet, psf_brainmask, brainmask, prefix = 'pvc_'):
    """ uses the given per psf brain mask to correct the data in pet
    """
    pimg = ni.load(pet)
    psfdat = ni.load(psf_brainmask).get_data()
    bmaskdat = ni.load(brainmask).get_data()
    
    pdat = pimg.get_data().copy()
    pvc_pdat = pdat / psfdat
    pdat[bmaskdat > 0] = pvc_pdat[bmaskdat > 0]
    newimg = ni.Nifti1Image(pdat, pimg.get_affine(), ni.Nifti1Header())
    outfile = pp.prefix_filename(pet, prefix=prefix)
    newimg.to_filename(outfile)
    return outfile
Esempio n. 6
0
def make_pvc(pet, psf_brainmask, brainmask, prefix='pvc_'):
    """ uses the given per psf brain mask to correct the data in pet
    """
    pimg = ni.load(pet)
    psfdat = ni.load(psf_brainmask).get_data()
    bmaskdat = ni.load(brainmask).get_data()

    pdat = pimg.get_data().copy()
    pvc_pdat = pdat / psfdat
    pdat[bmaskdat > 0] = pvc_pdat[bmaskdat > 0]
    newimg = ni.Nifti1Image(pdat, pimg.get_affine(), ni.Nifti1Header())
    outfile = pp.prefix_filename(pet, prefix=prefix)
    newimg.to_filename(outfile)
    return outfile
Esempio n. 7
0
def fsl_erode2d(infile):
    outfile = pp.prefix_filename(infile, prefix='ero-')
    outfile = outfile.replace('.nii', '.nii.gz')
    cmd = ' '.join(['fslmaths', infile, '-kernel 2D', '-ero',outfile])
    os.system(cmd)
    return outfile
            if not len(aparc) > 0:
                logging.error('NO aparcaseg for %s' % globstr)
                continue
            aparc = aparc[0]
            caparc = bg.copy_file(aparc, roidir)
            caparc = bg.unzip_file(caparc)  # in case zipped
            globstr = '%s/coreg_mri2fdg/*.mat*' % pth
            xfm = pp.find_single_file(globstr)
            if xfm is None:
                logging.error('NO transform for %s' % globstr)
                continue
            cxfm = bg.copy_file(xfm, roidir)
            cxfm = bg.unzip_file(cxfm)  # in case zipped
            pp.apply_transform_onefile(cxfm, caparc)
            pp.reslice(dat, caparc)
            raparc = pp.prefix_filename(caparc, prefix='r')

        data = pp.nibabel.load(dat).get_data()
        meand = pp.mean_from_labels(roid, raparc, data)
        alld[subid] = meand

    ###write to file
    _, roifname = os.path.split(roifile)
    outf = os.path.join(userhome,
                        'roivalues_%s_%s_%s' % (tracer, cleantime, roifname))
    fid = open(outf, 'w+')
    fid.write('SUBID,')
    rois = sorted(meand.keys())
    roiss = ','.join(rois)
    fid.write(roiss)
    fid.write(',\n')
Esempio n. 9
0
 if not corg_out.runtime.returncode == 0:
     logging.warning(corg_out.runtime.stderr)
     continue
 apply_out = pp.apply_transform_onefile(xfm_file, cpons)
 if not apply_out.runtime.returncode == 0:
     logging.warning(apply_out.runtime.stderr)
     continue
 apply_out = pp.apply_transform_onefile(xfm_file, caparc)
 if not apply_out.runtime.returncode == 0:
     logging.warning(apply_out.runtime.stderr)
     continue
 rout_mri = pp.reslice(cpet, cmri)
 if not rout_mri.runtime.returncode == 0:
     logging.warning(rout_mri.runtime.stderr)
 else:
     rmri = pp.prefix_filename(cmri, prefix="r")
     _, rmri_nme = os.path.split(rmri)
     new_rmri = rmri_nme.replace("rbr", "rfdg_br")
     newmri = bg.copy_file(rmri, "%s/anatomy/%s" % (sub, new_rmri))
     if newmri:
         bg.remove_files([cmri, rmri])
 rout_pons = pp.reslice(cpet, cpons)
 if not rout_pons.runtime.returncode == 0:
     logging.warning(rout_pons.runtime.stderr)
 else:
     rpons = pp.prefix_filename(cpons, prefix="r")
     newpons = bg.copy_file(rpons, "%s/ref_region" % (tracerdir))
     if newpons:
         bg.remove_files([cpons, rpons])
 rout_aparc = pp.reslice(cpet, caparc)
 if not rout_aparc.runtime.returncode == 0:
            if not len(aparc)>0:
                logging.error('NO aparcaseg for %s'%globstr)
                continue
            aparc = aparc[0]
            caparc = bg.copy_file(aparc, roidir)
            caparc = bg.unzip_file(caparc)# in case zipped
            globstr = '%s/coreg_mri2fdg/*.mat*'%pth
            xfm = pp.find_single_file(globstr)
            if xfm is None:
                logging.error('NO transform for %s'%globstr)
                continue
            cxfm = bg.copy_file(xfm, roidir)
            cxfm = bg.unzip_file(cxfm)# in case zipped
            pp.apply_transform_onefile(cxfm, caparc)
            pp.reslice(dat, caparc)
            raparc = pp.prefix_filename(caparc, prefix='r')
            
                
        data = pp.nibabel.load(dat).get_data()
        meand = pp.mean_from_labels(roid, raparc, data)
        alld[subid] = meand

    ###write to file
    _, roifname = os.path.split(roifile)
    outf = os.path.join(userhome, 'roivalues_%s_%s_%s'%(tracer,
                                                        cleantime,
                                                        roifname))
    fid =open(outf, 'w+')
    fid.write('SUBID,')
    rois = sorted(meand.keys())
    roiss = ','.join(rois)
 if not corg_out.runtime.returncode == 0:
     logging.warning(corg_out.runtime.stderr)
     continue
 apply_out = pp.apply_transform_onefile(xfm_file, cpons)
 if not apply_out.runtime.returncode == 0:
     logging.warning(apply_out.runtime.stderr)
     continue
 apply_out = pp.apply_transform_onefile(xfm_file, caparc)
 if not apply_out.runtime.returncode == 0:
     logging.warning(apply_out.runtime.stderr)
     continue
 rout_mri = pp.reslice(cpet, cmri)
 if not rout_mri.runtime.returncode == 0:
     logging.warning(rout_mri.runtime.stderr)
 else:
     rmri = pp.prefix_filename(cmri, prefix='r')
     _, rmri_nme = os.path.split(rmri)
     new_rmri = rmri_nme.replace('rbr', 'rfdg_br')
     newmri = bg.copy_file(rmri, '%s/anatomy/%s' % (sub, new_rmri))
     if newmri:
         bg.remove_files([cmri, rmri])
 rout_pons = pp.reslice(cpet, cpons)
 if not rout_pons.runtime.returncode == 0:
     logging.warning(rout_pons.runtime.stderr)
 else:
     rpons = pp.prefix_filename(cpons, prefix='r')
     newpons = bg.copy_file(rpons, '%s/ref_region' % (tracerdir))
     if newpons:
         bg.remove_files([cpons, rpons])
 rout_aparc = pp.reslice(cpet, caparc)
 if not rout_aparc.runtime.returncode == 0:
Esempio n. 12
0
def fsl_erode2d(infile):
    outfile = pp.prefix_filename(infile, prefix='ero-')
    outfile = outfile.replace('.nii', '.nii.gz')
    cmd = ' '.join(['fslmaths', infile, '-kernel 2D', '-ero', outfile])
    os.system(cmd)
    return outfile