示例#1
0
def check_data (subsfname, labelsfname, outdir, maskfname, outbase):

   au.log.debug('Checking data')

   nsubs = au.file_len(subsfname)
   nlabs = au.file_len(labelsfname)
   if (nsubs != nlabs):
      err  = 'Not same number of lines in input files\n'
      err += labelsfname + ': ' + str(nlabs) + '\n'
      err += (subsfname   + ': ' + str(nsubs)) + '\n'
      raise IOError(err)

   if not outdir:
      outdir = os.getcwd()

   slicesdir = outdir + os.path.sep + au.slices_str()
   if not os.path.exists (slicesdir):
      os.mkdir (slicesdir)

   tempdir = outdir + os.path.sep + au.temp_str()
   if not os.path.exists (tempdir):
      os.mkdir (tempdir)

   au.log.info (slicesdir)

   #check all files
   subsfile = open(subsfname, 'r')
   if maskfname:
      for line in subsfile:
         fpath = line.strip()
         au.check_has_same_geometry (maskfname, fpath)
   else:
      path1 = ''
      path2 = ''
      for line in subsfile:
         path1 = line.strip()
         if not path2:
            path2 = line.strip()
            continue
         else:
            au.check_has_same_geometry (path1, path2)
   subsfile.close()
        if prefixes[m]:
            ofname = prefixes[m] + '_' + ofname

        oc = len(au.find(os.listdir(outdir), ofname))
        nouts += oc
        m += 1

    if nouts >= nmasks:
        au.log.debug('Nothing to do in ' + outdir + '. All files processed.')
        return -1
    else:
        au.log.debug('Processing to output in: ' + outdir)

    #number of subjects
    subjsnum = au.file_len(subjsf)

    #reading subjects list
    subjlabels = np.zeros(subjsnum, dtype=int)
    subjslist = {}
    subjfile = open(subjsf, 'r')
    c = 0
    for s in subjfile:
        line = s.strip().split(',')
        subjlabels[c] = int(line[0])
        subjfname = line[1].strip()
        if not os.path.isabs(subjfname) and datadir:
            subjslist[c] = datadir + os.path.sep + subjfname
        else:
            subjslist[c] = subjfname
        c += 1
    thrps      = args.thresholdP.strip().split()
    lthr       = args.lthreshold.strip()
    uthr       = args.uthreshold.strip()
    absolute   = args.absolute

    au.setup_logger(args.verbosity)

    #checking number of files processed
    if not os.path.exists(maskf): 
        err = 'Mask file not found: ' + maskf
        au.log.error(err)
        sys.exit(-1)

    #number of subjects
    subjsnum = au.file_len(subjsf)

    #reading subjects list
    subjlabels = np.zeros(subjsnum, dtype=int)
    subjslist  = {}
    subjfile   = open(subjsf, 'r')
    c = 0
    for s in subjfile:
        line = s.strip().split(',')
        subjlabels[c] = int(line[0])
        subjfname = line[1].strip()

        if not os.path.isabs(subjfname) and datadir:
            subjslist[c] = datadir + os.path.sep + subjfname
        else:
            subjslist[c] = subjfname
示例#4
0
    foldno = args.foldno
    cleanup = args.cleanup
    leave = args.leave
    absval = args.absolute
    verbose = args.verbosity
    chklst = args.checklist

    au.setup_logger(verbose)

    usemask = decide_whether_usemask(maskf)

    foldno = get_fold_numberstr(foldno)

    measure = get_measure_shortname(measure)

    classnum = au.file_len(classf)
    subjsnum = au.file_len(subjsf)

    #reading label file
    [labels, classnames] = parse_labels_file(classf)

    #reading subjects list
    [subjlabels, subjs] = parse_subjects_list(subjsf, datadir)

    #if output dir does not exist, create
    if not (os.path.exists(outdir)):
        os.mkdir(outdir)

    #checklist_fname
    if chklst:
        chkf = outdir + os.path.sep + au.checklist_str()
示例#5
0
   foldno   = args.foldno
   cleanup  = args.cleanup
   leave    = args.leave
   absval   = args.absolute
   verbose  = args.verbosity
   chklst   = args.checklist

   au.setup_logger(verbose)

   usemask = decide_whether_usemask(maskf)

   foldno = get_fold_numberstr (foldno)

   measure = get_measure_shortname (measure)

   classnum = au.file_len(classf)
   subjsnum = au.file_len(subjsf)

   #reading label file
   [labels, classnames] = parse_labels_file (classf)

   #reading subjects list
   [subjlabels, subjs] = parse_subjects_list (subjsf, datadir)

   #if output dir does not exist, create
   if not(os.path.exists(outdir)):
      os.mkdir(outdir)

   #checklist_fname
   if chklst:
      chkf = outdir + os.path.sep + au.checklist_str()
示例#6
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')