예제 #1
0
def merge_slices(datadir, fileregex, outfname, outdir='', cleanup=True):

    if not outdir:
        outdir = datadir

    au.log.info('Merging the ' + fileregex + ' files in ' + outdir)

    fregex = datadir + os.path.sep + fileregex
    imglob = ''
    imglob = au.exec_comm(['imglob', fregex])
    imglob = imglob.strip()

    outdata = ''
    if imglob:
        if os.path.isabs(outfname): outdata = outfname
        else: outdata = outdir + os.path.sep + outfname

        os.system('fslmerge -z ' + outdata + ' ' + imglob)
        if cleanup:
            au.imrm(fregex)
    else:
        au.log.error('aizkolari_postproc: Error: could not find ' + fregex +
                     ' in ' + datadir)

    return outdata
예제 #2
0
def merge_slices (datadir, fileregex, outfname, outdir='', cleanup=True):

    if not outdir:
        outdir = datadir

    au.log.info ('Merging the ' + fileregex + ' files in ' + outdir)

    fregex = datadir + os.path.sep + fileregex
    imglob = ''
    imglob = au.exec_comm(['imglob', fregex])
    imglob = imglob.strip()

    outdata = ''
    if imglob:
        if os.path.isabs (outfname): outdata = outfname
        else:                        outdata = outdir + os.path.sep + outfname

        os.system('fslmerge -z ' + outdata + ' ' + imglob)
        if cleanup:
            au.imrm(fregex)
    else:
        au.log.error ('aizkolari_postproc: Error: could not find ' + fregex + ' in ' + datadir)

    return outdata
예제 #3
0
def merge_stats_slices (datadir, group):
    slice_str = au.slice_str()

    groupfregex = group + 's_' + slice_str + '_????' + '.'

    #This is a 4D volume with all subjects, it can be a big file, so I'm not creating it
    #merge_slices (datadir, groupfregex, group + 's')
    au.imrm(datadir + os.path.sep + groupfregex)

    [statfnames, mnames] = get_stats_fnames (group, outdir='')

    statfnames = statfnames[0]

    out = []
    for i in np.arange(len(statfnames)):
        fname  = statfnames[i]
        m      = mnames[i]

        regex  = group + 's_' + slice_str + '_????' + '_' + m
        o = merge_slices (datadir, regex , fname, datadir, cleanup=False)
        au.imrm(datadir + os.path.sep + regex)
        out.append(o)

    return out
예제 #4
0
def merge_stats_slices(datadir, group):
    slice_str = au.slice_str()

    groupfregex = group + 's_' + slice_str + '_????' + '.'

    #This is a 4D volume with all subjects, it can be a big file, so I'm not creating it
    #merge_slices (datadir, groupfregex, group + 's')
    au.imrm(datadir + os.path.sep + groupfregex)

    [statfnames, mnames] = get_stats_fnames(group, outdir='')

    statfnames = statfnames[0]

    out = []
    for i in np.arange(len(statfnames)):
        fname = statfnames[i]
        m = mnames[i]

        regex = group + 's_' + slice_str + '_????' + '_' + m
        o = merge_slices(datadir, regex, fname, datadir, cleanup=False)
        au.imrm(datadir + os.path.sep + regex)
        out.append(o)

    return out
예제 #5
0
def slice_and_merge (subsfname, labelsfname, checkfname, outdir='', maskfname='', outbase=''):

   check_data (subsfname, labelsfname, outdir, maskfname, outbase)

   nsubs = au.file_len(subsfname)
   nlabs = au.file_len(labelsfname)

   if not outdir:
      outdir = os.getcwd()

   slicesdir = outdir + os.path.sep + au.slices_str()
   tempdir = outdir + os.path.sep + au.temp_str()
   tmpdirlst = os.listdir(tempdir)

   fpath = ''
   olddir = os.getcwd()

   if not au.is_done (checkfname, au.preslicingdata_str()):
      #slicing subjects
      au.log.info ('Slicing all subjects: takes a while')
      subsfile = open(subsfname, 'r')
      for line in subsfile:
         fpath  = line.strip()
         isfile = os.path.basename(fpath)
         osfile = tempdir + os.path.sep + isfile

         #test if file has been sliced
         fdim3 = au.fslval(fpath, 'dim3')
         regex = au.remove_ext(isfile) + '*'
         nslices = au.count_match (tmpdirlst, regex)
         if fdim3 != nslices:
            #if not, then slice it in tempdir
            au.log.debug('Slicing ' + isfile)
            os.chdir (tempdir)

            shutil.copy (fpath, tempdir)
            au.fslslice (osfile)

            os.remove(osfile)
            os.chdir (olddir)
         else:
            au.log.debug(isfile + ' previously sliced')

      subsfile.close()
      au.checklist_add (checkfname, au.preslicingdata_str())

   if not au.is_done (checkfname, au.premergingdata_str()):
      #merging each slice
      if not outbase:
         outbase = au.data_str()

      if not fpath:
         subsfile = open(subsfname, 'r')
         for line in subsfile:
            fpath = line.strip()
            break

      au.log.info ('Merging all subject slices: takes a while')

      nslices  = int(au.fslval(fpath,'dim3'))
      for slice in range(nslices):
         slicezp = au.zeropad(slice)

         mergeout  = outbase + '_slice_' + slicezp
         outdata   = slicesdir + os.path.sep + mergeout
         #check if data file exists
         if not au.imtest(outdata):
            #if not, then create it, merging all the corresponding slices
            au.log.debug('Merging slice ' + slicezp)

            imglob   = ''
            subsfile = open(subsfname, 'r')
            for line in subsfile:
               fpath   = line.strip()
               isfile  = au.remove_ext(os.path.basename(fpath)).strip()
               isfile  = tempdir + os.path.sep + isfile + '_slice_' + slicezp
               imglob += isfile + ' '

            os.system('fslmerge -t ' + outdata + ' ' + imglob)

            for f in imglob.split():
               au.imrm(f)
         else:
            au.log.debug('Slice ' + slicezp + ' previously done')

         au.checklist_add (checkfname, au.premergingdata_str())

   #slicing mask
   if maskfname:
      if os.path.exists(maskfname) and not au.is_done (checkfname, au.preslicingmask_str()):
         au.log.info('Slicing mask ' + maskfname)

         au.imcp (maskfname, slicesdir + os.path.sep + au.mask_str())
         os.chdir (slicesdir)
         au.fslslice (au.mask_str())
         au.imrm     (au.mask_str())

         au.checklist_add (checkfname, au.preslicingmask_str())
         os.chdir (olddir)

   au.log.debug('Done preprocessing')