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
Beispiel #3
0
        else:
            aparc = bg.copy_file(aparc, outdirs['anatomydir'][0])
            aparcnii = bg.convert(aparc, aparcnii)
            bg.remove_files([aparc])
        # make pons
        refdir, _ = outdirs['refdir']
        brainstem = os.path.join(refdir, 'brainstem.nii.gz')
        if os.path.isfile(brainstem):
            logging.warning('brainstem %s exists, skipping' % (brainstem))

        else:
            # copy aseg+aparc to refdir
            try:
                caparcnii = bg.copy_file(aparcnii, refdir)
                bg.make_brainstem(caparcnii)
                brainstem = bg.unzip_file(brainstem)
                bg.remove_files([caparcnii.replace('.gz', '')])
            except:
                logging.warning('Check  %s' % (brainstem))

        rawtracer, exists = outdirs['rawtracer']
        rawtracer_base, _ = os.path.split(rawtracer)
        os.system('rm -rf %s' % rawtracer_base)
        tracerdir, _ = outdirs['tracerdir']

        newname = '%s_%s' % (subid, tracer)
        copied_ecats = bg.copy_files(ecats, tracerdir)
        bg.convertallecat(copied_ecats, newname)

        logging.info('ecats converted for %s ' % (subid))
Beispiel #4
0
                logging.error('cant find ID in %s' % sub)
                continue

        logging.info('%s' % subid)
        pth = os.path.join(sub, tracer.lower())
        if not os.path.isdir(pth):
            logging.error('%s does not exist, skipping' % pth)
            continue

        # get dvr
        globstr = '%s/dvr/DVR*nii*' % (pth)
        dat = pp.find_single_file(globstr)
        if dat is None:
            logging.error('%s missing, skipping' % (globstr))
            continue
        dat = bg.unzip_file(dat)  # in case zipped
        # get raparc
        globstr = '%s/coreg/rB*aparc_aseg.nii*' % (pth)
        raparc = pp.find_single_file(globstr)
        if raparc is None:
            logging.error('Missing %s, SKIPPING' % globstr)
            continue
        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+')
Beispiel #5
0
 logging.info('Coreg %s' % (subid))
 coregdir, exists = bg.make_dir(pth, 'coreg')
 if exists:
     logging.warning('%s exists, remove to rerun' % (coregdir))
     continue
 # copy brainmask, aparc_aseg, cerebellum to coreg dir
 basedir, _ = os.path.split(pth)
 # brainmask
 globstr = os.path.join(basedir, 'anatomy', 'brainmask.nii*')
 brainmask = pp.find_single_file(globstr)
 if brainmask is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(coregdir)
     continue
 cbrainmask = bg.copy_file(brainmask, coregdir)
 cbrainmask = bg.unzip_file(cbrainmask)
 # aparc aseg
 globstr = os.path.join(basedir, 'anatomy', '*aparc_aseg.nii*')
 aparc = pp.find_single_file(globstr)
 if aparc is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(coregdir)
     continue
 caparc = bg.copy_file(aparc, coregdir)
 caparc = bg.unzip_file(caparc)
 # cerebellum
 globstr = os.path.join(pth, 'ref_region', 'grey_cerebellum.nii*')
 cere = pp.find_single_file(globstr)
 if cere is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(coregdir)
 if not os.path.isdir(dvrdir):
     logging.error('%s doesnt exist,skipping'%(dvrdir))
     continue
 # make/check warp dir
 warpdir, exists = bg.make_dir(dvrdir, 'warp_%s'%tname)
 if exists:
     logging.warning('%s exists, remove to rerun'%(warpdir))
     continue
 #get brainmask
 globstr = os.path.join(dvrdir, 'DVR-%s*.nii*'%subid)
 dvr = pp.find_single_file(globstr)
 if dvr is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(warpdir)
     continue
 dvr = bg.unzip_file(dvr)
 # get mean 20 minute pib
 globstr = os.path.join(sub,'pib','realign_QA', 'mean20min*.nii*')
 mean20 = pp.find_single_file(globstr)
 if mean20 is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(warpdir)
     continue
 mean20 = bg.unzip_file(mean20)        
 # brainmask
 globstr = os.path.join(anatdir, 'brainmask.nii*')
 brainmask = pp.find_single_file(globstr)
 if brainmask is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(warpdir)
     continue
 logging.info('Coreg %s'%(subid))
 coregdir, exists = bg.make_dir(pth, 'coreg')
 if exists:
     logging.warning('%s exists, remove to rerun'%(coregdir))
     continue
 # copy brainmask, aparc_aseg, cerebellum to coreg dir
 basedir, _ = os.path.split(pth)
 # brainmask
 globstr = os.path.join(basedir, 'anatomy', 'brainmask.nii*')
 brainmask = pp.find_single_file(globstr)
 if brainmask is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(coregdir)
     continue
 cbrainmask = bg.copy_file(brainmask, coregdir)
 cbrainmask = bg.unzip_file(cbrainmask)
 # aparc aseg
 globstr = os.path.join(basedir, 'anatomy', '*aparc_aseg.nii*')
 aparc = pp.find_single_file(globstr)
 if aparc is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(coregdir)
     continue
 caparc = bg.copy_file(aparc, coregdir)
 caparc = bg.unzip_file(caparc)
 # cerebellum
 globstr = os.path.join(pth, 'ref_region', 'grey_cerebellum.nii*')
 cere = pp.find_single_file(globstr)
 if cere is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(coregdir)
                logging.error('cant find ID in %s'%sub)
                continue        
            
        logging.info('%s'%subid)
        pth = os.path.join(sub, tracer.lower())
        if not os.path.isdir(pth):
            logging.error('%s does not exist, skipping'%pth)
            continue
                                
        # get dvr
        globstr = '%s/dvr/DVR*nii*'%(pth)
        dat = pp.find_single_file(globstr)
        if dat is None:
            logging.error('%s missing, skipping'%(globstr))
            continue
        dat = bg.unzip_file(dat)# in case zipped
        # get raparc
        globstr = '%s/coreg/rB*aparc_aseg.nii*'%(pth)
        raparc = pp.find_single_file(globstr)
        if raparc is None:
            logging.error('Missing %s, SKIPPING'%globstr)
            continue
        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))
 if not os.path.isdir(dvrdir):
     logging.error('%s doesnt exist,skipping' % (dvrdir))
     continue
 # make/check warp dir
 warpdir, exists = bg.make_dir(dvrdir, 'warp_%s' % tname)
 if exists:
     logging.warning('%s exists, remove to rerun' % (warpdir))
     continue
 #get brainmask
 globstr = os.path.join(dvrdir, 'DVR-%s*.nii*' % subid)
 dvr = pp.find_single_file(globstr)
 if dvr is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(warpdir)
     continue
 dvr = bg.unzip_file(dvr)
 # get mean 20 minute pib
 globstr = os.path.join(sub, 'pib', 'realign_QA', 'mean20min*.nii*')
 mean20 = pp.find_single_file(globstr)
 if mean20 is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(warpdir)
     continue
 mean20 = bg.unzip_file(mean20)
 # brainmask
 globstr = os.path.join(anatdir, 'brainmask.nii*')
 brainmask = pp.find_single_file(globstr)
 if brainmask is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(warpdir)
     continue
Beispiel #10
0
 if not os.path.isdir(tracerdir):
     logging.error('%s doesnt exist,skipping' % (tracerdir))
     continue
 # make/check warp dir
 warpdir, exists = bg.make_dir(tracerdir, 'warp_%s' % tname)
 if exists:
     logging.warning('%s exists, remove to rerun' % (warpdir))
     continue
 #get ponsnormed
 globstr = os.path.join(tracerdir, 'ponsnormed_%s*.nii*' % subid)
 pnfdg = pp.find_single_file(globstr)
 if pnfdg is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(warpdir)
     continue
 pnfdg = bg.unzip_file(pnfdg)
 # get summed fdg
 globstr = os.path.join(tracerdir, 'sum_rB*.nii*')
 sumfdg = pp.find_single_file(globstr)
 if sumfdg is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(warpdir)
     continue
 sumfdg = bg.unzip_file(sumfdg)
 # brainmask
 globstr = os.path.join(anatdir, 'brainmask.nii*')
 brainmask = pp.find_single_file(globstr)
 if brainmask is None:
     logging.error('%s not found. skipping' % globstr)
     shutil.rmtree(warpdir)
     continue
Beispiel #11
0
            logging.warning("remove %s to rerun?" % coregdir)
            continue
        ## find brainmask
        globstr = os.path.join(s, "anatomy", "brainmask.nii")
        brainmask = pp.find_single_file(globstr)
        ## find sum
        globstr = os.path.join(s, "pib", "realign_QA", "mean20min*.nii")
        sum = pp.find_single_file(globstr)

        if brainmask is None or sum is None:
            logging.error("pet2mri sum: %s brainmask: %s" % (sum, brainmask))
            continue
        # move dvr and sum to coregdir, unzip if necessary
        cdvr = bg.copy_file(dvr, coregdir)
        csum = bg.copy_file(sum, coregdir)
        cdvr = bg.unzip_file(cdvr)
        csum = bg.unzip_file(csum)
        ## coreg pet 2 brainmask
        corg_out = pp.simple_coregister(str(brainmask), str(csum), other=str(cdvr))
        if not corg_out.runtime.returncode == 0:
            logging.error(corg_out.runtime.traceback)
            continue
        rdvr = corg_out.outputs.coregistered_files
        # copy dvr to freesurfer subjects petdir
        cdvr = bg.copy_file(rdvr, petdir)
        globstr = os.path.join(fsdir, "mri", "T1.mgz")
        t1 = pp.find_single_file(globstr)
        if t1 is None:
            logging.error("%s not found" % globstr)
            continue
        xfm = fst.fs_generate_dat(cdvr, t1, subid)
 if not os.path.isdir(tracerdir):
     logging.error('%s doesnt exist,skipping'%(tracerdir))
     continue
 # make/check warp dir
 warpdir, exists = bg.make_dir(tracerdir, 'warp_%s'%tname)
 if exists:
     logging.warning('%s exists, remove to rerun'%(warpdir))
     continue
 #get ponsnormed
 globstr = os.path.join(tracerdir, 'ponsnormed_%s*.nii*'%subid)
 pnfdg = pp.find_single_file(globstr)
 if pnfdg is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(warpdir)
     continue
 pnfdg = bg.unzip_file(pnfdg)
 # get summed fdg
 globstr = os.path.join(tracerdir,  'sum_rB*.nii*')
 sumfdg = pp.find_single_file(globstr)
 if sumfdg is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(warpdir)
     continue
 sumfdg = bg.unzip_file(sumfdg)        
 # brainmask
 globstr = os.path.join(anatdir, 'brainmask.nii*')
 brainmask = pp.find_single_file(globstr)
 if brainmask is None:
     logging.error('%s not found. skipping'%globstr)
     shutil.rmtree(warpdir)
     continue
 logging.info('%s'%subid)
 pth = os.path.join(sub, tracer.lower())
 if not os.path.isdir(pth):
     logging.error('%s does not exist, skipping'%pth)
     continue
 
 # find roi directory
 roidir, exists = bg.make_dir(pth, dirname='roi_data')
         
 # get  pons normed
 globstr = '%s/nonan-ponsnormed*fdg**nii*'%(pth)
 dat = pp.find_single_file(globstr)
 if dat is None:
     logging.error('%s missing, skipping'%(globstr))
     continue
 dat = bg.unzip_file(dat)# in case zipped
 # get raparc
 #if exists: #roidir exists so raparc_aseg should also
 #    globstr = '%s/rB*aparc_aseg.nii*'%(roidir)
 #    raparc = pp.find_single_file(globstr)
 #    if raparc is None:
 #        exists = False
 #if not exists: # check coreg directory
 globstr = '%s/coreg_mri2fdg/rB*aparc_aseg.nii*'%(pth)
 raparc = pp.find_single_file(globstr)
 if raparc is None:
     exists = False
 else:
     exists = True
 if not exists: #last chance
     # find aparcaseg
        if len(nifti) < 5:
            logging.warning(
                'fdg frames not found or too few for %s  skipping' % (subid))
            continue
        nifti.sort()
        nifti = bg.unzip_files(nifti)
        hasqa = False
        rlgnout, newnifti = pp.realigntoframe1(nifti)
        if rlgnout is None and newnifti is None:
            logging.warning('%s :existing realign_QA ' % (subid))
            tmprealigned, tmpparameterfile = find_realigned(tracerdir)
            if tmprealigned is None or tmpparameterfile is None:
                logging.warning('%s :missing realigned, skipping ' % (subid))
                continue
            logging.info('found %s %s' % (tmprealigned, tmpparameterfile))
            tmpparameterfile = bg.unzip_file(tmpparameterfile)
            tmprealigned = [bg.unzip_file(x) for x in tmprealigned]
            hasqa = True

        elif rlgnout.runtime.returncode is not 0:
            logging.warning('Failed to realign %s' % subid)
            continue
        else:
            tmprealigned = rlgnout.outputs.realigned_files
            tmpmean = rlgnout.outputs.mean_image
            tmpparameterfile = rlgnout.outputs.realignment_parameters

            logging.info('realigned %s' % subid)

        # make final mean image
        meanimg = pp.make_summed_image(tmprealigned)
        logging.info('%s' % subid)
        pth = os.path.join(sub, tracer.lower())
        if not os.path.isdir(pth):
            logging.error('%s does not exist, skipping' % pth)
            continue

        # find roi directory
        roidir, exists = bg.make_dir(pth, dirname='roi_data')

        # get  pons normed
        globstr = '%s/nonan-ponsnormed*fdg**nii*' % (pth)
        dat = pp.find_single_file(globstr)
        if dat is None:
            logging.error('%s missing, skipping' % (globstr))
            continue
        dat = bg.unzip_file(dat)  # in case zipped
        # get raparc
        #if exists: #roidir exists so raparc_aseg should also
        #    globstr = '%s/rB*aparc_aseg.nii*'%(roidir)
        #    raparc = pp.find_single_file(globstr)
        #    if raparc is None:
        #        exists = False
        #if not exists: # check coreg directory
        globstr = '%s/coreg_mri2fdg/rB*aparc_aseg.nii*' % (pth)
        raparc = pp.find_single_file(globstr)
        if raparc is None:
            exists = False
        else:
            exists = True
        if not exists:  #last chance
            # find aparcaseg
Beispiel #16
0
        if len(nifti) < 5:
            logging.warning("fdg frames not found or too few for %s  skipping" % (subid))
            continue
        nifti.sort()
        nifti = bg.unzip_files(nifti)
        hasqa = False
        rlgnout, newnifti = pp.realigntoframe1(nifti)
        if rlgnout is None and newnifti is None:
            logging.warning("%s :existing realign_QA " % (subid))
            tmprealigned, tmpparameterfile = find_realigned(tracerdir)
            if tmprealigned is None or tmpparameterfile is None:
                logging.warning("%s :missing realigned, skipping " % (subid))
                continue
            logging.info("found %s %s" % (tmprealigned, tmpparameterfile))
            tmpparameterfile = bg.unzip_file(tmpparameterfile)
            tmprealigned = [bg.unzip_file(x) for x in tmprealigned]
            hasqa = True

        elif rlgnout.runtime.returncode is not 0:
            logging.warning("Failed to realign %s" % subid)
            continue
        else:
            tmprealigned = rlgnout.outputs.realigned_files
            tmpmean = rlgnout.outputs.mean_image
            tmpparameterfile = rlgnout.outputs.realignment_parameters

            logging.info("realigned %s" % subid)

        # make final mean image
        meanimg = pp.make_summed_image(tmprealigned)
        else:
            aparc = bg.copy_file(aparc, outdirs["anatomydir"][0])
            aparcnii = bg.convert(aparc, aparcnii)
            bg.remove_files([aparc])
        # make pons
        refdir, _ = outdirs["refdir"]
        brainstem = os.path.join(refdir, "brainstem.nii.gz")
        if os.path.isfile(brainstem):
            logging.warning("brainstem %s exists, skipping" % (brainstem))

        else:
            # copy aseg+aparc to refdir
            try:
                caparcnii = bg.copy_file(aparcnii, refdir)
                bg.make_brainstem(caparcnii)
                brainstem = bg.unzip_file(brainstem)
                bg.remove_files([caparcnii.replace(".gz", "")])
            except:
                logging.warning("Check  %s" % (brainstem))

        rawtracer, exists = outdirs["rawtracer"]
        rawtracer_base, _ = os.path.split(rawtracer)
        os.system("rm -rf %s" % rawtracer_base)
        tracerdir, _ = outdirs["tracerdir"]

        newname = "%s_%s" % (subid, tracer)
        copied_ecats = bg.copy_files(ecats, tracerdir)
        bg.convertallecat(copied_ecats, newname)

        logging.info("ecats converted for %s " % (subid))
            logging.warning('remove %s to rerun?' % coregdir)
            continue
        ## find brainmask
        globstr = os.path.join(s, 'anatomy', 'brainmask.nii')
        brainmask = pp.find_single_file(globstr)
        ## find sum
        globstr = os.path.join(s, 'pib', 'realign_QA', 'mean20min*.nii')
        sum = pp.find_single_file(globstr)

        if brainmask is None or sum is None:
            logging.error('pet2mri sum: %s brainmask: %s' % (sum, brainmask))
            continue
        # move dvr and sum to coregdir, unzip if necessary
        cdvr = bg.copy_file(dvr, coregdir)
        csum = bg.copy_file(sum, coregdir)
        cdvr = bg.unzip_file(cdvr)
        csum = bg.unzip_file(csum)
        ## coreg pet 2 brainmask
        corg_out = pp.simple_coregister(str(brainmask),
                                        str(csum),
                                        other=str(cdvr))
        if not corg_out.runtime.returncode == 0:
            logging.error(corg_out.runtime.traceback)
            continue
        rdvr = corg_out.outputs.coregistered_files
        # copy dvr to freesurfer subjects petdir
        cdvr = bg.copy_file(rdvr, petdir)
        globstr = os.path.join(fsdir, 'mri', 'T1.mgz')
        t1 = pp.find_single_file(globstr)
        if t1 is None:
            logging.error('%s not found' % globstr)