Example #1
0
def extract_features (subjs, exclusubjs, mask, maskf, scale, scale_min, scale_max):

    #population features
    nsubjs  = len(subjs)
    s       = nib.load(subjs[0])
    subjsiz = np.prod (s.shape)
    stype   = s.get_data_dtype()

    #loading subject data
    data = np.empty([nsubjs, subjsiz], dtype=stype)

    #number of voxels > 0 in mask
    mask   = mask.flatten()
    nfeats = np.sum(mask > 0)

    #reading each subject and saving the features in a vector
    feats = np.empty([nsubjs, nfeats], dtype=stype)

    #extracting features from non-excluded subjects
    c = 0
    for s in subjs:
        au.log.debug("Reading " + s)
        #check geometries
        au.check_has_same_geometry (s, maskf)
        #load subject
        subj = nib.load(s).get_data().flatten()
        #mask data and save it
        feats[c,:] = subj[mask > 0]
        c += 1

    #scaling if asked
    dmin = scale_min
    dmax = scale_max
    if scale:
        au.log.info("Scaling data.")
        [feats, dmin, dmax] = rescale(feats, scale_min, scale_max)

    #extracting features from excluded subjects
    exclfeats = []
    if exclusubjs:
        au.log.info("Processing excluded subjects.")
        nexcl     = len(exclusubjs)
        exclfeats = np.empty([nexcl, nfeats], dtype=stype)
        c = 0
        for s in exclusubjs:
            au.log.debug("Reading " + s)
            #check geometries
            au.check_has_same_geometry (s, maskf)
            #load subject
            subj = nib.load(s).get_data().flatten()
            #mask data and save it
            exclfeats[c,:] = subj[mask > 0]
            c += 1

        if scale:
            [exclfeats, emin, emax] = rescale(exclfeats, scale_min, scale_max, dmin, dmax)

    return [feats, exclfeats, dmin, dmax]
Example #2
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()
        mask = nib.load(maskf).get_data()
        if globalmask:
            mask = mask[gm > 0]

        #number of voxels > 0 in mask
        mask = mask.flatten()
        nfeats = np.sum(mask > 0)

        #reading each subject and saving the features in a vector
        feats = np.empty([nsubjs, nfeats], dtype=float)

        #extracting features from non-excluded subjects
        c = 0
        for s in subjs:
            #check geometries
            au.check_has_same_geometry(s, maskf)
            #mask data and save it
            feats[c, :] = data[c, mask > 0]
            c += 1

        #scaling if asked
        if scale:
            [feats, dmin, dmax] = rescale(feats, scale_min, scale_max)
            #writing in a text file the scaling values of this training set
            f = open(outdir + os.path.sep + ofname + '.scaling_range', 'w')
            f.write('#data_min, data_max, range_min, range_max')
            f.write('\n')
            f.write(
                str(dmin) + ',' + str(dmax) + ',' + str(scale_min) + ',' +
                str(scale_max))
            f.close()
        mask = nib.load(maskf).get_data()
        if globalmask:
            mask = mask[gm > 0]

        # number of voxels > 0 in mask
        mask = mask.flatten()
        nfeats = np.sum(mask > 0)

        # reading each subject and saving the features in a vector
        feats = np.empty([nsubjs, nfeats], dtype=float)

        # extracting features from non-excluded subjects
        c = 0
        for s in subjs:
            # check geometries
            au.check_has_same_geometry(s, maskf)
            # mask data and save it
            feats[c, :] = data[c, mask > 0]
            c += 1

        # scaling if asked
        if scale:
            [feats, dmin, dmax] = rescale(feats, scale_min, scale_max)
            # writing in a text file the scaling values of this training set
            f = open(outdir + os.path.sep + ofname + ".scaling_range", "w")
            f.write("#data_min, data_max, range_min, range_max")
            f.write("\n")
            f.write(str(dmin) + "," + str(dmax) + "," + str(scale_min) + "," + str(scale_max))
            f.close()

        # extracting features from excluded subjects