Exemplo n.º 1
0
def main(tgz, newid, outdir=None):
    """ untars tgz, replaces PatientID PatientName
    with new id, and save to outdir (default origdir/newtgz"""
    # mv dicom to tmp directory
    tgz = os.path.abspath(tgz)
    origdir, orignme = os.path.split(tgz)
    tmptgz = bg.copy_tmpdir(tgz)
    # un-archive
    pth = bg.tar_cmd(tmptgz)
    newdir, exists = bg.make_dir(pth, dirname='dicomfiles')
    startdir = os.getcwd()
    os.chdir(pth)
    dcms = bg.find_dicoms(pth)
    keys = dcms.keys()
    keys = [x for x in keys if not newdir in x]
    for k in keys:
        for dcm in dcms[k]:
            plan = dicom.read_file(dcm)
            plan.PatientID = newid
            plan.PatientName = newid
            _, dcm_name = os.path.split(dcm)
            newdcm = os.path.join(newdir, dcm_name)
            dicom.write_file(newdcm, plan)
    # create tar archive of updated dicoms
    if outdir is None:
        outdir, _ = bg.make_dir(origdir, dirname="newtgz")
    newtgz = os.path.join(outdir, orignme)
    cmd = 'tar cfvz %s  dicomfiles' % (newtgz)
    os.system(cmd)
    os.chdir(startdir)
    print 'removing %s' % pth
    os.system('rm -rf %s' % (pth))
    print 'wrote ', newtgz
    return newtgz
Exemplo n.º 2
0
def main(tgz, newid, outdir=None):
    """ untars tgz, replaces PatientID PatientName
    with new id, and save to outdir (default origdir/newtgz"""
    # mv dicom to tmp directory
    tgz = os.path.abspath(tgz)
    origdir, orignme = os.path.split(tgz)
    tmptgz = bg.copy_tmpdir(tgz)
    # un-archive
    pth = bg.tar_cmd(tmptgz)
    newdir, exists = bg.make_dir(pth, dirname='dicomfiles')
    startdir = os.getcwd()
    os.chdir(pth)
    dcms = bg.find_dicoms(pth)
    keys = dcms.keys()
    keys = [x for x in keys if not newdir in x]
    for k in keys:
        for dcm in dcms[k]:
            plan = dicom.read_file(dcm)
            plan.PatientID = newid
            plan.PatientName = newid
            _, dcm_name = os.path.split(dcm)
            newdcm = os.path.join(newdir, dcm_name)
            dicom.write_file(newdcm, plan)
    # create tar archive of updated dicoms
    if outdir is None:
        outdir, _ = bg.make_dir(origdir, dirname="newtgz")
    newtgz = os.path.join(outdir, orignme)
    cmd = 'tar cfvz %s  dicomfiles'%(newtgz)
    os.system(cmd)
    os.chdir(startdir)
    print 'removing %s'%pth
    os.system('rm -rf %s'%(pth))
    print 'wrote ', newtgz
    return newtgz
Exemplo n.º 3
0
def setup_dir(root, subid, tracer):
    """ check for and Make Subjects Target Directories
    Returns Dict of final directories and if they exist"""
    outdirs = {}
        
    subdir, exists = bg.make_dir(root, dirname=subid)
    outdirs.update(dict(subdir=[subdir, exists]))
    tracerdir,exists = bg.make_dir(subdir,
                                dirname = '%s' % (tracer.lower()) )
    outdirs.update(dict(tracerdir=[tracerdir, exists]))
    
    rawdatadir, exists  = bg.make_dir(subdir, dirname = 'raw')
    outdirs.update(dict(rawdatadir=[rawdatadir, exists]))
    
    rawtracer, exists  = bg.make_dir(rawdatadir, dirname = tracer)
    outdirs.update(dict(rawtracer=[rawtracer, exists]))
    
    anatomydir, exists  = bg.make_dir(subdir,dirname='anatomy')
    outdirs.update(dict(anatomydir=[anatomydir, exists]))
    
    refdir, exists = bg.make_rec_dir(tracerdir,dirname='ref_region')
    outdirs.update(dict(refdir=[refdir, exists]))
    
    #print 'directories created for %s' % subid
    logging.info('directories created for %s'%(subid))
    for k, v in sorted(outdirs.values()):
        logging.info('%s : previously existed = %s'%(k,v))
    return outdirs
Exemplo n.º 4
0
def setup_dir(root, subid, tracer):
    """ check for and Make Subjects Target Directories
    Returns Dict of final directories and if they exist"""
    outdirs = {}

    subdir, exists = bg.make_dir(root, dirname=subid)
    outdirs.update(dict(subdir=[subdir, exists]))
    tracerdir, exists = bg.make_dir(subdir, dirname='%s' % (tracer.lower()))
    outdirs.update(dict(tracerdir=[tracerdir, exists]))

    rawdatadir, exists = bg.make_dir(subdir, dirname='raw')
    outdirs.update(dict(rawdatadir=[rawdatadir, exists]))

    rawtracer, exists = bg.make_dir(rawdatadir, dirname=tracer)
    outdirs.update(dict(rawtracer=[rawtracer, exists]))

    anatomydir, exists = bg.make_dir(subdir, dirname='anatomy')
    outdirs.update(dict(anatomydir=[anatomydir, exists]))

    refdir, exists = bg.make_rec_dir(tracerdir, dirname='ref_region')
    outdirs.update(dict(refdir=[refdir, exists]))

    #print 'directories created for %s' % subid
    logging.info('directories created for %s' % (subid))
    for k, v in sorted(outdirs.values()):
        logging.info('%s : previously existed = %s' % (k, v))
    return outdirs
Exemplo n.º 5
0
def realigntoframe17(niftilist):
    """given list of nifti files
    copies/creates realign_QA
    removes first 5 frames
    realignes rest to the 17th frame
    """
    startdir = os.getcwd()
    niftilist.sort()
    basepth, _ = os.path.split(niftilist[0])
    tmpdir, exists = bg.make_dir(basepth, 'realign_QA')
    if exists:
        return None, None
    # copy files to tmp dir
    copiednifti = []
    for f in niftilist:
        newf = bg.copy_file(f, tmpdir)
        copiednifti.append(str(newf))
    # put files in correct order
    os.chdir(tmpdir)
    alteredlist = [x for x in copiednifti]
    frame17 = alteredlist[16]
    alteredlist.remove(frame17)
    alteredlist = alteredlist[5:]
    alteredlist.insert(0, frame17)
    #print 'alteredlist', alteredlist
    # realign
    rlgn = spm.Realign(matlab_cmd='matlab-spm8')
    rlgn.inputs.in_files = alteredlist
    rlgn.inputs.ignore_exception = True
    #rlgn.inputs.write_which = [2,0]
    #rlgn.register_to_mean = True
    rlgnout = rlgn.run()
    os.chdir(startdir)
    return rlgnout, copiednifti
Exemplo n.º 6
0
def realigntoframe1(niftilist):
    """given list of nifti files
    copies relevent files to realign_QA
    realigns to the 1st frame
    """
    startdir = os.getcwd()
    niftilist.sort()
    basepth, _ = os.path.split(niftilist[0])
    tmpdir, exists = bg.make_dir(basepth, 'realign_QA')
    if exists:
        return None, None
    # copy files to tmp dir
    copiednifti = []
    for f in niftilist:
        newf = bg.copy_file(f, tmpdir)
        copiednifti.append(str(newf))
    print 'copied nifti', copiednifti
    # realign to frame1
    os.chdir(tmpdir)
    rlgn = spm.Realign()
    rlgn.inputs.matlab_cmd = 'matlab-spm8'
    rlgn.inputs.in_files = copiednifti
    rlgn.inputs.ignore_exception = True
    #print rlgn.mlab.cmdline
    rlgnout = rlgn.run()
    os.chdir(startdir)
    return rlgnout, copiednifti
Exemplo n.º 7
0
def realigntoframe1(niftilist):
    """given list of nifti files
    copies relevent files to realign_QA
    realigns to the 1st frame
    """
    startdir = os.getcwd()
    niftilist.sort()
    basepth, _ = os.path.split(niftilist[0])
    tmpdir, exists = bg.make_dir(basepth, 'realign_QA')
    if exists:
        return None, None
    # copy files to tmp dir
    copiednifti = []
    for f in niftilist:
        newf = bg.copy_file(f, tmpdir)
        copiednifti.append(str(newf))
    print 'copied nifti', copiednifti
    # realign to frame1
    os.chdir(tmpdir)
    rlgn = spm.Realign()
    rlgn.inputs.matlab_cmd = 'matlab-spm8'
    rlgn.inputs.in_files = copiednifti
    rlgn.inputs.ignore_exception = True
    #print rlgn.mlab.cmdline
    rlgnout = rlgn.run()
    os.chdir(startdir)
    return rlgnout, copiednifti    
Exemplo n.º 8
0
def realigntoframe17(niftilist):
    """given list of nifti files
    copies/creates realign_QA
    removes first 5 frames
    realignes rest to the 17th frame
    """
    startdir = os.getcwd()
    niftilist.sort()
    basepth, _ = os.path.split(niftilist[0])
    tmpdir, exists = bg.make_dir(basepth, 'realign_QA')
    if exists:
        return None, None    
    # copy files to tmp dir
    copiednifti = []
    for f in niftilist:
        newf = bg.copy_file(f, tmpdir)
        copiednifti.append(str(newf))
    # put files in correct order
    os.chdir(tmpdir)
    alteredlist =[x for x in  copiednifti]
    frame17 = alteredlist[16]
    alteredlist.remove(frame17)
    alteredlist = alteredlist[5:]
    alteredlist.insert(0, frame17)
    #print 'alteredlist', alteredlist
    # realign
    rlgn = spm.Realign(matlab_cmd='matlab-spm8')
    rlgn.inputs.in_files = alteredlist
    rlgn.inputs.ignore_exception = True
    #rlgn.inputs.write_which = [2,0]
    #rlgn.register_to_mean = True
    rlgnout = rlgn.run()
    os.chdir(startdir)
    return rlgnout, copiednifti
Exemplo n.º 9
0
def rousset_labellist(labeldir, hemi, outdir='rousset_labels'):
    rousset_labels = {'frontal':['frontalpole','lateralorbitofrontal'],
                      'temporal' : ['temporalpole'],
                      'parietal' : ['inferiorparietal'],
                      'precuneus' : ['precuneus'],
                      'cingulate' : ['posteriorcingulate', 'isthmuscingulate']
                      }
    pth, _ = os.path.split(labeldir)
    outdir, exists = bg.make_dir(pth, outdir)
    
    # for each rousset label make or copy
    labelfiles = []
    for label, regions in rousset_labels.items():
        outlabel = os.path.join(outdir, '%s.rousset_%s.label'%(hemi, label))
        tmpregions = [os.path.join(labeldir,
                                   '%s.%s.label'%(hemi, x)) for x in regions]
        if len(regions) == 1:
            bg.copy_file(tmpregions[0], outlabel)
        else:
            generate_roilabels(tmpregions, outlabel)
        labelfiles.append(outlabel)
    return labelfiles
Exemplo n.º 10
0
def rousset_labellist(labeldir, hemi, outdir='rousset_labels'):
    rousset_labels = {
        'frontal': ['frontalpole', 'lateralorbitofrontal'],
        'temporal': ['temporalpole'],
        'parietal': ['inferiorparietal'],
        'precuneus': ['precuneus'],
        'cingulate': ['posteriorcingulate', 'isthmuscingulate']
    }
    pth, _ = os.path.split(labeldir)
    outdir, exists = bg.make_dir(pth, outdir)

    # for each rousset label make or copy
    labelfiles = []
    for label, regions in rousset_labels.items():
        outlabel = os.path.join(outdir, '%s.rousset_%s.label' % (hemi, label))
        tmpregions = [
            os.path.join(labeldir, '%s.%s.label' % (hemi, x)) for x in regions
        ]
        if len(regions) == 1:
            bg.copy_file(tmpregions[0], outlabel)
        else:
            generate_roilabels(tmpregions, outlabel)
        labelfiles.append(outlabel)
    return labelfiles
Exemplo n.º 11
0
        # QA
        # make 4d for QA
        qadir, exists = qa.make_qa_dir(realigndir, name='data_QA')
        data4d = qa.make_4d_nibabel(allrealigned, outdir=qadir)
        #snrimg = qa.gen_sig2noise_img(data4d,qadir)
        #artout = qa.run_artdetect(data4d,tmpparameterfile)
        #qa.screen_data_dirnme(data4d, qadir)
        qa.plot_movement(tmpparameterfile, subid)
        qa.calc_robust_median_diff(data4d)
        qa.screen_pet(data4d)

        # Coregister cerebellum, brainmask and aparc_aseg to pet space
        #
        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
Exemplo n.º 12
0
    for sub in subs:

        _, fullsubid = os.path.split(sub)
        try:
            m = pp.re.search('B[0-9]{2}-[0-9]{3}', fullsubid)
            subid = m.group()
        except:
            logging.error('cant find ID in %s' % fullsubid)
            continue
        logging.info('%s' % subid)
        pth = os.path.join(sub, tracer.lower())
        dvrdir = os.path.join(pth, 'dvr')
        if not os.path.isdir(dvrdir):
            logging.error('%s missing. skipping' % (dvrdir))
            continue
        pvcdir, exists = bg.make_dir(dvrdir, 'pvc_metzler')
        if exists:
            logging.error('%s exists, remove to re-run' % (pvcdir))
            continue
        # get dvr
        globstr = '%s/DVR-%s*nii*' % (dvrdir, subid)
        dvr = pp.find_single_file(globstr)
        if dvr is None:
            logging.error('%s missing, skipping' % (globstr))
            continue
        # copy dvr to pvc directory
        cdvr = bg.copy_file(dvr, pvcdir)
        # get raparc
        corgdir = os.path.join(pth, 'coreg')
        globstr = '%s/rB*aparc_aseg.nii' % (corgdir)
        raparc = pp.find_single_file(globstr)
Exemplo n.º 13
0
    for sub in subs:

        _, fullsubid = os.path.split(sub)
        try:
            m = pp.re.search("B[0-9]{2}-[0-9]{3}", fullsubid)
            subid = m.group()
        except:
            logging.error("cant find ID in %s" % fullsubid)
            continue
        logging.info("%s" % subid)
        pth = os.path.join(sub, tracer.lower())
        dvrdir = os.path.join(pth, "dvr")
        if not os.path.isdir(dvrdir):
            logging.error("%s missing. skipping" % (dvrdir))
            continue
        pvcdir, exists = bg.make_dir(dvrdir, "pvc_metzler")
        if exists:
            logging.error("%s exists, remove to re-run" % (pvcdir))
            continue
        # get dvr
        globstr = "%s/DVR-%s*nii*" % (dvrdir, subid)
        dvr = pp.find_single_file(globstr)
        if dvr is None:
            logging.error("%s missing, skipping" % (globstr))
            continue
        # copy dvr to pvc directory
        cdvr = bg.copy_file(dvr, pvcdir)
        # get raparc
        corgdir = os.path.join(pth, "coreg")
        globstr = "%s/rB*aparc_aseg.nii" % (corgdir)
        raparc = pp.find_single_file(globstr)
Exemplo n.º 14
0
        if '_v2' in sub:
            logging.info('skipping visit2 %s' % (sub))
            continue

        _, fullsubid = os.path.split(sub)
        try:
            m = pp.re.search('B[0-9]{2}-[0-9]{3}', fullsubid)
            subid = m.group()
        except:
            logging.error('cant find ID in %s' % fullsubid)
            continue
        logging.info('%s' % subid)
        # subjects tracer specific path
        pth = os.path.join(sub, tracer.lower())

        pvcdir, exists = bg.make_dir(dvrdir, 'pvc_rousset')
        if exists:
            logging.error('%s exists, remove to re-run' % (pvcdir))
            continue
        # get pons normed
        globstr = '%s/nonan-ponsnormed_%s*nii*' % (pth, subid)
        ponsnormd = pp.find_single_file(globstr)
        if ponsnormd is None:
            logging.error('%s missing, skipping' % (globstr))
            continue
        # copy ponsnormd to pvc directory
        cponsnormd = bg.copy_file(ponsnormd, pvcdir)
        # get raparc
        corgdir = os.path.join(pth, 'coreg_mri2fdg')
        globstr = '%s/rB*aparc_aseg.nii' % (corgdir)
        raparc = pp.find_single_file(globstr)
Exemplo n.º 15
0
        if mri is None:
            logging.warning('no brainmask found for %s' % (subid))
            continue
        # find aparc_aseg
        searchstring = '%s/anatomy/B*aparc_aseg.nii*' % sub
        aparc = pp.find_single_file(searchstring)
        if aparc is None:
            logging.warning('%s not found' % (searchstring))
            continue
        aparc = bg.unzip_file(aparc)

        # find PET
        pet = movedmean  # use previously made summed image
        # copy files to tmp dir
        logging.info('coreg ref region to %s' % pet)
        coreg_dir, exists = bg.make_dir(tracerdir, dirname='coreg_mri2fdg')
        if exists:
            logging.warning('existing dir %s remove to re-run' % (coreg_dir))
            continue
        cmri = bg.copy_file(mri, coreg_dir)
        cpons = bg.copy_file(pons, coreg_dir)
        cpet = bg.copy_file(pet, coreg_dir)
        caparc = bg.copy_file(aparc, coreg_dir)
        xfm_file = pp.make_transform_name(cpet, cmri)
        logging.info('coreg %s' % (subid))
        corg_out = pp.invert_coreg(cmri, cpet, xfm_file)
        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:
Exemplo n.º 16
0
            bg.remove_files([aparc])
        # make cerebellum
        refdir, _ = outdirs['refdir']
        cerebellum = os.path.join(refdir, 'grey_cerebellum.nii')
        if os.path.isfile(cerebellum):
            logging.warning('cerebellum %s exists, skipping' % (cerebellum))

        else:
            # copy aseg+aparc to refdir
            try:
                caparcnii = bg.copy_file(aparcnii, refdir)
                bg.make_cerebellum_nibabel(caparcnii)
                bg.remove_files([caparcnii])
            except:
                logging.warning('Fail: unable to make %s' % (cerebellum))

        rawtracer, exists = outdirs['rawtracer']
        os.system('rm %s' % rawtracer)
        tracerdir, _ = outdirs['tracerdir']

        newname = '%s_%s' % (subid, tracer)
        niftis = bg.biograph_dicom_convert(tgz[0], tracerdir, subid, tracer)
        ## center new nifti files
        orig_dir, _ = bg.make_dir(tracerdir, dirname='orig')
        copied_orig = bg.copy_files(niftis, orig_dir)
        bg.remove_files(niftis)
        for f, nf in zip(copied_orig, niftis):
            print f, nf
            nicm.CMTransform(f).fix(new_file=nf)
        logging.info('biograph dicoms converted for %s ' % (subid))
Exemplo n.º 17
0
 for sub in subs:
     
     _, fullsubid = os.path.split(sub)
     try:
         m = pp.re.search('B[0-9]{2}-[0-9]{3}',fullsubid)
         subid = m.group()
     except:
         logging.error('cant find ID in %s'%fullsubid)
         continue
     logging.info('%s'%subid)
     # make filepath to tracer dir
     pth = os.path.join(sub, tracer.lower())
     if not os.path.exists(pth):
         logging.error('SKIP: missing %s'%pth)
         continue
     pvcdir, exists = bg.make_dir(pth, 'pvc_metzler')
     if exists:
         logging.error('%s exists, remove to re-run'%(pvcdir))
         continue
     # get ponsnormd
     globstr = '%s/nonan-ponsnormed_%s*nii*'%(pth,subid)                  
     ponsnormd = pp.find_single_file(globstr)
     if ponsnormd is None:
         logging.error('%s missing, skipping'%(globstr))
         continue
     # copy ponsnormd to pvc directory
     cponsnormd = bg.copy_file(ponsnormd, pvcdir)
     # get raparc
     corgdir = os.path.join(pth, 'coreg_mri2fdg')
     globstr = '%s/rB*aparc_aseg.nii'%(corgdir)
     raparc = pp.find_single_file(globstr)
Exemplo n.º 18
0
    tracer = "PIB"
    user = os.environ["USER"]
    logging.info("###START Setup Directory :::")
    logging.info("###TRACER Setup %s  :::" % (tracer))
    logging.info("###USER : %s" % (user))
    subs = bg.MyDirsDialog(prompt="Choose Subjects ", indir="%s/" % root)

    for s in subs:
        _, subid = os.path.split(s)
        # mk tracer directory  in freesurfer
        fsdir = os.path.join(mridir, subid)
        if not os.path.isdir(fsdir):
            logging.warning("%s missing, skipping" % fsdir)
            continue
        petdir, exists = bg.make_dir(fsdir, dirname="pib")
        if exists:
            logging.warning("%s exists, skipping" % (petdir))
            continue
        globstr = os.path.join(s, "pib", "dvr", "DVR*nii*")
        dvr = pp.find_single_file(globstr)
        if dvr is None:
            logging.warning("%s missing, skipping" % (globstr))
            continue
        coregdir, exists = bg.make_dir("%s/pib" % s, "pet2mri")
        if exists:
            logging.warning("remove %s to rerun?" % coregdir)
            continue
        ## find brainmask
        globstr = os.path.join(s, "anatomy", "brainmask.nii")
        brainmask = pp.find_single_file(globstr)
 for sub in subs:
     if '_v2' in sub:
         logging.info('skipping visit2 %s' % (sub))
         continue
     _, subid = os.path.split(sub)
     logging.info('%s' % subid)
     anatdir = os.path.join(sub, 'anatomy')
     if not os.path.isdir(anatdir):
         logging.error('%s doesnt exist,skipping' % (anatdir))
         continue
     dvrdir = os.path.join(sub, 'pib', 'dvr')
     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:
 if subid is None:
     try:
         m = pp.re.search('B[0-9]{2}-[0-9]{3}',sub)
         subid = m.group()
     except:
         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
 
 # find roi directory
 roidir, exists = bg.make_dir(pth, dirname='stroke_masked_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 strokemask
 globstr = '%s/rfs_cortical_mask_tu.nii*'%roidir
 stroke_mask = pp.find_single_file(globstr)
 if stroke_mask is None:
     globstr = '%s/anatomy/fs_cortical_mask_tu.nii*'%sub
     strokem = pp.find_single_file(globstr)
     if strokem is None:
Exemplo n.º 21
0
    _, tname, _ = pp.split_filename(template)
                 

    for sub in subs:
        _, subid = os.path.split(sub)
        logging.info('%s'%subid)
        anatdir = os.path.join(sub, 'anatomy')
        if not os.path.isdir(anatdir):
            logging.error('%s doesnt exist,skipping'%(anatdir))
            continue
        tracerdir = os.path.join(sub,'fdg')
        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:
 if subid is None:
     try:
         m = pp.re.search('B[0-9]{2}-[0-9]{3}',sub)
         subid = m.group()
     except:
         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
 
 # 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
Exemplo n.º 23
0
            continue

        _, fullsubid = os.path.split(sub)
        try:
            m = pp.re.search("B[0-9]{2}-[0-9]{3}", fullsubid)
            subid = m.group()
        except:
            logging.error("cant find ID in %s" % fullsubid)
            continue
        logging.info("%s" % subid)
        pth = os.path.join(sub, tracer.lower())
        dvrdir = os.path.join(pth, "dvr")
        if not os.path.isdir(dvrdir):
            logging.error("%s missing. skipping" % (dvrdir))
            continue
        pvcdir, exists = bg.make_dir(dvrdir, "pvc_rousset")
        if exists:
            logging.error("%s exists, remove to re-run" % (pvcdir))
            continue
        # get dvr
        globstr = "%s/DVR-%s*nii*" % (dvrdir, subid)
        dvr = pp.find_single_file(globstr)
        if dvr is None:
            logging.error("%s missing, skipping" % (globstr))
            continue
        # copy dvr to pvc directory
        cdvr = bg.copy_file(dvr, pvcdir)
        # get raparc
        corgdir = os.path.join(pth, "coreg")
        globstr = "%s/rB*aparc_aseg.nii" % (corgdir)
        raparc = pp.find_single_file(globstr)
Exemplo n.º 24
0
        # QA
        # make 4d for QA
        qadir, exists = qa.make_qa_dir(realigndir, name='data_QA')
        data4d = qa.make_4d_nibabel(allrealigned, outdir=qadir)
        #snrimg = qa.gen_sig2noise_img(data4d,qadir)
        #artout = qa.run_artdetect(data4d,tmpparameterfile)
        #qa.screen_data_dirnme(data4d, qadir)
        qa.plot_movement(tmpparameterfile, subid)
        qa.calc_robust_median_diff(data4d)
        qa.screen_pet(data4d)

        # Coregister cerebellum, brainmask and aparc_aseg to pet space
        #
        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
Exemplo n.º 25
0
    for sub in subs:

        _, fullsubid = os.path.split(sub)
        try:
            m = pp.re.search('B[0-9]{2}-[0-9]{3}', fullsubid)
            subid = m.group()
        except:
            logging.error('cant find ID in %s' % fullsubid)
            continue
        logging.info('%s' % subid)
        # make filepath to tracer dir
        pth = os.path.join(sub, tracer.lower())
        if not os.path.exists(pth):
            logging.error('SKIP: missing %s' % pth)
            continue
        pvcdir, exists = bg.make_dir(pth, 'pvc_metzler')
        if exists:
            logging.error('%s exists, remove to re-run' % (pvcdir))
            continue
        # get ponsnormd
        globstr = '%s/nonan-ponsnormed_%s*nii*' % (pth, subid)
        ponsnormd = pp.find_single_file(globstr)
        if ponsnormd is None:
            logging.error('%s missing, skipping' % (globstr))
            continue
        # copy ponsnormd to pvc directory
        cponsnormd = bg.copy_file(ponsnormd, pvcdir)
        # get raparc
        corgdir = os.path.join(pth, 'coreg_mri2fdg')
        globstr = '%s/rB*aparc_aseg.nii' % (corgdir)
        raparc = pp.find_single_file(globstr)
        if subid is None:
            try:
                m = pp.re.search('B[0-9]{2}-[0-9]{3}', sub)
                subid = m.group()
            except:
                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

        # 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
Exemplo n.º 27
0
 for sub in subs:
     if '_v2' in sub:
         logging.info('skipping visit2 %s'%(sub))
         continue
     _, subid = os.path.split(sub)
     logging.info('%s'%subid)
     anatdir = os.path.join(sub, 'anatomy')
     if not os.path.isdir(anatdir):
         logging.error('%s doesnt exist,skipping'%(anatdir))
         continue
     dvrdir = os.path.join(sub,'pib','dvr')
     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:
Exemplo n.º 28
0
 tracer = 'FDG'
 user = os.environ['USER']
 logging.info('###START Setup Directory :::')
 logging.info('###TRACER Setup %s  :::'%(tracer))
 logging.info('###USER : %s'%(user))
 subs = bg.MyDirsDialog(prompt='Choose Subjects ',
                        indir='%s/'%root)
 
 for s in subs:
     _, subid = os.path.split(s)
     # mk tracer directory  in freesurfer
     fsdir = os.path.join(mridir, subid)
     if not os.path.isdir(fsdir):
          logging.warning('%s missing, skipping'%fsdir)
          continue
     petdir, exists = bg.make_dir(fsdir, dirname = 'fdg')
     if exists:
         logging.warning('%s exists, skipping'%(petdir))
         continue
     globstr = os.path.join(s, 'fdg','nonan*pons*nii*')
     ponsnormed = pp.find_single_file(globstr)
     if ponsnormed is None:
         logging.warning('%s missing, skipping'%(globstr))
         continue
     coregdir,exists = bg.make_dir('%s/fdg'%s, 'pet2mri')
     if exists:
         continue
     ## find brainmask
     globstr = os.path.join(s, 'anatomy','brainmask.nii')
     brainmask = pp.find_single_file(globstr)
     ## find sum
Exemplo n.º 29
0
     logging.info('Skipping visit 2 %s'%(sub))
     continue
 _, fullsubid = os.path.split(sub)
 try:
     m = pp.re.search('B[0-9]{2}-[0-9]{3}_v[0-9]',fullsubid)
     subid = m.group()
 except:
     logging.error('cant find ID in %s'%fullsubid)
     continue
 logging.info('%s'%subid)
 pth = os.path.join(sub, tracer.lower())
 dvrdir  = os.path.join(pth, 'dvr')
 if not os.path.isdir(dvrdir):
     logging.error('%s missing. skipping'%(dvrdir))
     continue
 pvcdir, exists = bg.make_dir(dvrdir, 'pvc_metzler')
 if not exists:
     logging.error('%s missing, skipping'%(pvcdir))
     continue
 # get pvc dvr
 globstr = '%s/pvcfs_DVR-%s*nii*'%(pvcdir,subid)                  
 dvr = pp.find_single_file(globstr)
 if dvr is None:
     logging.error('%s missing, skipping'%(globstr))
     continue
 # get raparc
 corgdir = os.path.join(pth, 'coreg')
 globstr = '%s/rB*aparc_aseg.nii'%(corgdir)
 raparc = pp.find_single_file(globstr)
 if raparc is None:
     logging.error('%s missing, skipping '%(globstr))
        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

        # find roi directory
        roidir, exists = bg.make_dir(pth, dirname='stroke_masked_roi_data')
        if exists:
            globstr = '%s/rfs_cortical_mask_tu.nii*' % roidir
            stroke_mask = pp.find_single_file(globstr)
            if stroke_mask is None:
                exists = False
        if not exists:
            globstr = '%s/anatomy/fs_cortical_mask_tu.nii*' % sub
            strokem = pp.find_single_file(globstr)
            if strokem is None:
                logging.error('%s missing, skipping' % (globstr))
                continue
            cstrokem = bg.copy_file(strokem, roidir)
            globstr = '%s/coreg/*.mat*' % pth
            xfm = pp.find_single_file(globstr)
            if xfm is None:
        # make cerebellum
        refdir,_ = outdirs['refdir']
        cerebellum = os.path.join(refdir, 'grey_cerebellum.nii')
        if os.path.isfile(cerebellum):
            logging.warning('cerebellum %s exists, skipping'%(cerebellum))
            
        else:
            # copy aseg+aparc to refdir
            try:
                caparcnii = bg.copy_file(aparcnii, refdir)                        
                bg.make_cerebellum_nibabel(caparcnii)
                bg.remove_files([caparcnii])
            except:
                logging.warning('Fail: unable to make %s'%(cerebellum))
        
        rawtracer, exists = outdirs['rawtracer']
        os.system('rm %s'%rawtracer)
        tracerdir, _ = outdirs['tracerdir']
        
        newname = '%s_%s' % (subid, tracer)
        niftis = bg.biograph_dicom_convert(tgz[0], tracerdir, subid, tracer)
        ## center new nifti files
        orig_dir, _ = bg.make_dir(tracerdir, dirname='orig')
        copied_orig = bg.copy_files(niftis, orig_dir)
        bg.remove_files(niftis)
        for f,nf  in zip(copied_orig, niftis):
            print f, nf
            nicm.CMTransform(f).fix(new_file = nf)
        logging.info('biograph dicoms converted for %s ' % (subid))                
            
Exemplo n.º 32
0
    template = str(template[0])
    _, tname, _ = pp.split_filename(template)

    for sub in subs:
        _, subid = os.path.split(sub)
        logging.info('%s' % subid)
        anatdir = os.path.join(sub, 'anatomy')
        if not os.path.isdir(anatdir):
            logging.error('%s doesnt exist,skipping' % (anatdir))
            continue
        tracerdir = os.path.join(sub, 'fdg')
        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:
Exemplo n.º 33
0
 for sub in subs:
     
     _, fullsubid = os.path.split(sub)
     try:
         m = pp.re.search('B[0-9]{2}-[0-9]{3}',fullsubid)
         subid = m.group()
     except:
         logging.error('cant find ID in %s'%fullsubid)
         continue
     logging.info('%s'%subid)
     pth = os.path.join(sub, tracer.lower())
     dvrdir  = os.path.join(pth, 'dvr')
     if not os.path.isdir(dvrdir):
         logging.error('%s missing. skipping'%(dvrdir))
         continue
     pvcdir, exists = bg.make_dir(dvrdir, 'pvc_rousset')
     if not exists:
         logging.error('%s does not exist, skipping'%(pvcdir))
         continue
     globstr = '%s/rB*aparc_aseg.nii'%(pvcdir)
     craparc = pp.find_single_file(globstr)
     if craparc is None:
         logging.error('%s missing, skipping '%(globstr))
         continue
     
     transferf = os.path.join(pvcdir, 'transfer_matrix.npy')
     try:
         transfer_mtx = rousset.np.load(transferf)
     except:
         logging.error('%s missing, skipping'%(transferf))
         continue
Exemplo n.º 34
0
        if mri is None:
            logging.warning("no brainmask found for %s" % (subid))
            continue
        # find aparc_aseg
        searchstring = "%s/anatomy/B*aparc_aseg.nii*" % sub
        aparc = pp.find_single_file(searchstring)
        if aparc is None:
            logging.warning("%s not found" % (searchstring))
            continue
        aparc = bg.unzip_file(aparc)

        # find PET
        pet = movedmean  # use previously made summed image
        # copy files to tmp dir
        logging.info("coreg ref region to %s" % pet)
        coreg_dir, exists = bg.make_dir(tracerdir, dirname="coreg_mri2fdg")
        if exists:
            logging.warning("existing dir %s remove to re-run" % (coreg_dir))
            continue
        cmri = bg.copy_file(mri, coreg_dir)
        cpons = bg.copy_file(pons, coreg_dir)
        cpet = bg.copy_file(pet, coreg_dir)
        caparc = bg.copy_file(aparc, coreg_dir)
        xfm_file = pp.make_transform_name(cpet, cmri)
        logging.info("coreg %s" % (subid))
        corg_out = pp.invert_coreg(cmri, cpet, xfm_file)
        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:
Exemplo n.º 35
0
    tracer = 'FDG'
    user = os.environ['USER']
    logging.info('###START Setup Directory :::')
    logging.info('###TRACER Setup %s  :::' % (tracer))
    logging.info('###USER : %s' % (user))
    subs = bg.MyDirsDialog(prompt='Choose Subjects ', indir='%s/' % root)

    for s in subs:
        _, subid = os.path.split(s)
        # mk tracer directory  in freesurfer
        fsdir = os.path.join(mridir, subid)
        if not os.path.isdir(fsdir):
            logging.warning('%s missing, skipping' % fsdir)
            continue
        petdir, exists = bg.make_dir(fsdir, dirname='fdg')
        if exists:
            logging.warning('%s exists, skipping' % (petdir))
            continue
        globstr = os.path.join(s, 'fdg', 'nonan*pons*nii*')
        ponsnormed = pp.find_single_file(globstr)
        if ponsnormed is None:
            logging.warning('%s missing, skipping' % (globstr))
            continue
        coregdir, exists = bg.make_dir('%s/fdg' % s, 'pet2mri')
        if exists:
            continue
        ## find brainmask
        globstr = os.path.join(s, 'anatomy', 'brainmask.nii')
        brainmask = pp.find_single_file(globstr)
        ## find sum
Exemplo n.º 36
0
    tracer = 'PIB'
    user = os.environ['USER']
    logging.info('###START Setup Directory :::')
    logging.info('###TRACER Setup %s  :::' % (tracer))
    logging.info('###USER : %s' % (user))
    subs = bg.MyDirsDialog(prompt='Choose Subjects ', indir='%s/' % root)

    for s in subs:
        _, subid = os.path.split(s)
        # mk tracer directory  in freesurfer
        fsdir = os.path.join(mridir, subid)
        if not os.path.isdir(fsdir):
            logging.warning('%s missing, skipping' % fsdir)
            continue
        petdir, exists = bg.make_dir(fsdir, dirname='pib')
        if exists:
            logging.warning('%s exists, skipping' % (petdir))
            continue
        globstr = os.path.join(s, 'pib', 'dvr', 'DVR*nii*')
        dvr = pp.find_single_file(globstr)
        if dvr is None:
            logging.warning('%s missing, skipping' % (globstr))
            continue
        coregdir, exists = bg.make_dir('%s/pib' % s, 'pet2mri')
        if exists:
            logging.warning('remove %s to rerun?' % coregdir)
            continue
        ## find brainmask
        globstr = os.path.join(s, 'anatomy', 'brainmask.nii')
        brainmask = pp.find_single_file(globstr)