Exemple #1
0
def pearson_correlation(datadir,
                        outdir,
                        usemask=True,
                        excludef='',
                        exclude_idx=-1):

    slidir = datadir + os.path.sep + au.slices_str()

    subjsfile = datadir + os.path.sep + au.subjects_str()
    labelsfile = datadir + os.path.sep + au.labels_str()

    lst = os.listdir(slidir)
    n = au.count_match(lst, au.data_str() + '_' + au.slice_regex())

    exclude_log = ''
    if exclude_idx > -1:
        exclude_log = ' excluding subject ' + str(exclude_idx)

    au.log.info('Calculating correlation of ' + slidir + os.path.sep +
                au.data_str() + '_' + au.slice_regex() + exclude_log)

    for i in range(n):
        slino = au.zeropad(i)

        dataf = slidir + os.path.sep + au.data_str() + '_' + au.slice_str(
        ) + '_' + slino + au.ext_str()
        maskf = slidir + os.path.sep + au.mask_str() + '_' + au.slice_str(
        ) + '_' + slino + au.ext_str()
        outf = outdir + os.path.sep + au.pearson_str() + '_' + au.slice_str(
        ) + '_' + slino

        if exclude_idx > -1:
            outf += '_' + au.excluded_str() + str(exclude_idx) + au.ext_str()
        else:
            outf += au.ext_str()

        if not os.path.isfile(dataf):
            au.log.error('Could not find ' + dataf)
            continue

        if not usemask:
            maskf = ''

        try:
            measure_pearson(dataf, labelsfile, outf, maskf, excludef,
                            exclude_idx)
        except:
            au.log.error(
                'pearson_correlation: Error measuring correlation on ' + dataf)
            au.log.error("Unexpected error: ", sys.exc_info()[0])
            exit(1)
def pearson_correlation (datadir, outdir, usemask=True, excludef='', exclude_idx=-1):

   slidir = datadir + os.path.sep + au.slices_str()

   subjsfile  = datadir + os.path.sep + au.subjects_str()
   labelsfile = datadir + os.path.sep + au.labels_str()

   lst = os.listdir(slidir)
   n = au.count_match(lst, au.data_str() + '_' + au.slice_regex())

   exclude_log = ''
   if exclude_idx > -1:
      exclude_log = ' excluding subject ' + str(exclude_idx)
   
   au.log.info ('Calculating correlation of ' + slidir + os.path.sep + au.data_str() + '_' + au.slice_regex() + exclude_log)

   for i in range(n):
      slino = au.zeropad(i)

      dataf = slidir + os.path.sep + au.data_str()    + '_' + au.slice_str() + '_' + slino + au.ext_str()
      maskf = slidir + os.path.sep + au.mask_str()    + '_' + au.slice_str() + '_' + slino + au.ext_str()
      outf  = outdir + os.path.sep + au.pearson_str() + '_' + au.slice_str() + '_' + slino

      if exclude_idx > -1:
         outf += '_' + au.excluded_str() + str(exclude_idx) + au.ext_str()
      else:
         outf += au.ext_str()

      if not os.path.isfile(dataf): 
         au.log.error('Could not find ' + dataf)
         continue

      if not usemask:
         maskf = ''

      try:
         measure_pearson(dataf, labelsfile, outf, maskf, excludef, exclude_idx)
      except:
         au.log.error('pearson_correlation: Error measuring correlation on ' + dataf)
         au.log.error("Unexpected error: ", sys.exc_info()[0] )
         exit(1)
            np.savetxt(outdir + os.path.sep + au.excluded_subjlabels_str(),
                       exclulabels,
                       fmt='%i')
            exclfilename = get_filepath(outdir, excl_ofname, otype)

        #saving binary file depending on output type
        if otype == 'numpybin':
            np.save(filename, feats)

            if excluf:
                np.save(exclfilename, exclfeats)

        elif otype == 'octave':
            sio.savemat(filename, {
                au.feats_str(): feats,
                au.labels_str(): labels
            })
            if excluf:
                exclulabels[exclulabels == 0] = -1
                sio.savemat(exclfilename, {
                    au.feats_str(): exclfeats,
                    au.labels_str(): exclulabels
                })

        elif otype == 'svmperf':
            labels[labels == 0] = -1
            ae.write_svmperf_dat(filename, dataname, feats, labels)

            if excluf:
                exclulabels[exclulabels == 0] = -1
                ae.write_svmperf_dat(exclfilename, dataname, exclfeats,
Exemple #4
0
def save_data (outdir, prefix, dataname, otype, excluding, leave, feats, labels, exclfeats, exclulabels, dmin, dmax, scale, scale_min, scale_max, lthr, uthr, thrp, absolute):

    #setting output file name
    ofname = au.feats_str()

    if leave > -1:
        ofname += '.' + au.excluded_str() + str(leave)

    if absolute:  ofname += '.' + au.abs_str()
    if lthr:      ofname += '.lthr_' + str(lthr)
    if uthr:      ofname += '.uthr_' + str(uthr)
    if thrp:      ofname += '.thrP_' + str(thrp)
    if scale:     ofname += '.' + au.scaled_str()

    if excluding:
        excl_ofname  = au.excluded_str() + '_' + ofname
        exclfilename = get_filepath (outdir, excl_ofname , otype)

    if prefix:
        ofname = prefix + '_' + ofname
        excl_ofname = prefix + '_' + excl_ofname

    filename = get_filepath (outdir, ofname, otype)

    #writing in a text file the scaling values of this training set
    if scale:
        write_scalingrange_file (outdir + os.path.sep + ofname + '.scaling_range', dmin, dmax, scale_min, scale_max)

    #saving binary file depending on output type
    if otype == 'numpybin':
        np.save (filename, feats)

        if excluding:
            np.save (exclfilename, exclfeats)

    elif otype == 'octave':
        sio.savemat (filename, {au.feats_str(): feats, au.labels_str(): labels})
        if excluding:
            exclulabels[exclulabels == 0] = -1
            sio.savemat (exclfilename, {au.feats_str(): exclfeats, au.labels_str(): exclulabels})

    elif otype == 'svmperf':
        labels[labels == 0] = -1
        ae.write_svmperf_dat(filename, dataname, feats, labels)

        if excluding:
            exclulabels[exclulabels == 0] = -1
            ae.write_svmperf_dat(exclfilename, dataname, exclfeats, exclulabels)

    elif otype == 'arff':
        featnames = np.arange(nfeats) + 1
        ae.write_arff (filename, dataname, featnames, feats, labels)

        if excluding:
            ae.write_arff (exclfilename, dataname, featnames, exclfeats, exclulabels)

    else:
        err = 'Output method not recognised!'
        au.log.error(err)
        sys.exit(-1)

    return [filename, exclfilename]
Exemple #5
0
            au.log.info ("Saving data files.")
            [filename, exclfilename] = save_data (outdir, prefix, dataname, otype, excluding, leave, feats, labels, exclfeats, exclulabels, dmin, dmax, scale, scale_min, scale_max, lthr, uthr, t, absolute)

    else:
        au.log.info ("Extracting features.")
        [feats, exclfeats, dmin, dmax] = extract_features (subjs, exclusubjs, mask, maskf, scale, scale_min, scale_max)

        au.log.info ("Saving data files.")
        [filename, exclfilename] = save_data (outdir, prefix, dataname, otype, excluding, leave, feats, labels, exclfeats, exclulabels, dmin, dmax, scale, scale_min, scale_max, lthr, uthr, thrps, absolute)

    au.log.info ("Saved " + filename)
    if excluding:
        au.log.info ("Saved " + exclfilename)

    #saving description files
    np.savetxt(filename + '.' + au.subjectfiles_str(), subjs,  fmt='%s')
    np.savetxt(filename + '.' + au.labels_str(),       labels, fmt='%i')

    if excluding:
        np.savetxt(exclfilename + '.' + au.subjectfiles_str(), exclusubjs,  fmt='%s')
        np.savetxt(exclfilename + '.' + au.labels_str(),       exclulabels, fmt='%i')

    return 1

#-------------------------------------------------------------------------------
## END EXTRACT FEATSET
#-------------------------------------------------------------------------------

if __name__ == "__main__":
    sys.exit(main())
    #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()
        if not (os.path.exists(chkf)):
            au.touch(chkf)
    else:
        chkf = ''

    #saving data in files where further processes can find them
    outf_subjs = outdir + os.path.sep + au.subjects_str()
    outf_labels = outdir + os.path.sep + au.labels_str()
    np.savetxt(outf_subjs, subjs, fmt='%s')
    np.savetxt(outf_labels, subjlabels, fmt='%i')

    #creating folder for slices
    slidir = outdir + os.path.sep + au.slices_str()
    if not (os.path.exists(slidir)):
        os.mkdir(slidir)
        #slice the volumes

    #creating group and mask slices
    pre.slice_and_merge(outf_subjs, outf_labels, chkf, outdir, maskf)

    #creating measure output folder
    if measure == 'pea':
        measure_fname = au.pearson_str()
        np.savetxt(outdir + os.path.sep + au.included_subjlabels_str(), labels, fmt="%i")

        if excluf:
            np.savetxt(outdir + os.path.sep + au.excluded_subjects_str(), exclusubjs, fmt="%s")
            np.savetxt(outdir + os.path.sep + au.excluded_subjlabels_str(), exclulabels, fmt="%i")
            exclfilename = get_filepath(outdir, excl_ofname, otype)

        # saving binary file depending on output type
        if otype == "numpybin":
            np.save(filename, feats)

            if excluf:
                np.save(exclfilename, exclfeats)

        elif otype == "octave":
            sio.savemat(filename, {au.feats_str(): feats, au.labels_str(): labels})
            if excluf:
                exclulabels[exclulabels == 0] = -1
                sio.savemat(exclfilename, {au.feats_str(): exclfeats, au.labels_str(): exclulabels})

        elif otype == "svmperf":
            labels[labels == 0] = -1
            ae.write_svmperf_dat(filename, dataname, feats, labels)

            if excluf:
                exclulabels[exclulabels == 0] = -1
                ae.write_svmperf_dat(exclfilename, dataname, exclfeats, exclulabels)

        elif otype == "arff":
            featnames = np.arange(nfeats) + 1
            ae.write_arff(filename, dataname, featnames, feats, labels)
   #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()
      if not(os.path.exists(chkf)):
         au.touch(chkf)
   else:
      chkf = ''

   #saving data in files where further processes can find them
   outf_subjs  = outdir + os.path.sep + au.subjects_str()
   outf_labels = outdir + os.path.sep + au.labels_str()
   np.savetxt(outf_subjs,  subjs,      fmt='%s')
   np.savetxt(outf_labels, subjlabels, fmt='%i')

   #creating folder for slices
   slidir = outdir + os.path.sep + au.slices_str()
   if not(os.path.exists(slidir)):
      os.mkdir(slidir)
      #slice the volumes

   #creating group and mask slices
   pre.slice_and_merge(outf_subjs, outf_labels, chkf, outdir, maskf)

   #creating measure output folder
   if measure == 'pea':
      measure_fname = au.pearson_str()
                   fmt='%s')
        np.savetxt(outdir + os.path.sep + au.excluded_subjlabels_str(),
                   exclulabels,
                   fmt='%i')

    #saving the feature matrix and labels in a binary file

    filename = set_filename(outdir, prefix + '_' + au.features_str(), otype)

    print('Creating ' + filename)

    if otype == 'numpybin':
        np.save(filename, feats)

    elif otype == 'octave':
        sio.savemat(filename, {au.feats_str(): feats, au.labels_str(): labels})

    elif otype == 'svmperf':
        labels[labels == 0] = -1
        ae.write_svmperf_dat(filename, dataname, feats, labels)
        if excluf:
            exclulabels[exclulabels == 0] = -1
            exclfilename = set_filename(
                outdir, prefix + '_' + au.excluded_str() + au.feats_str(),
                otype)
            ae.write_svmperf_dat(exclfilename, dataname, exclfeats,
                                 exclulabels)

    elif otype == 'arff':
        featnames = np.arange(nfeats) + 1
        ae.write_arff(filename, dataname, featnames, feats, labels)
   if excluf:
      np.savetxt(outdir + os.path.sep + au.excluded_subjects_str(),   exclusubjs,  fmt='%s')
      np.savetxt(outdir + os.path.sep + au.excluded_subjlabels_str(), exclulabels, fmt='%i')

   #saving the feature matrix and labels in a binary file

   filename = set_filename (outdir, prefix + '_' + au.features_str(), otype)

   print ('Creating ' + filename)

   if otype == 'numpybin':
      np.save (filename, feats)

   elif otype == 'octave':
      sio.savemat (filename, {au.feats_str(): feats, au.labels_str(): labels})

   elif otype == 'svmperf':
      labels[labels == 0] = -1
      ae.write_svmperf_dat(filename, dataname, feats, labels)
      if excluf:
         exclulabels[exclulabels == 0] = -1
         exclfilename = set_filename(outdir, prefix + '_' + au.excluded_str() + au.feats_str(), otype)
         ae.write_svmperf_dat(exclfilename, dataname, exclfeats, exclulabels)

   elif otype == 'arff':
      featnames = np.arange(nfeats) + 1
      ae.write_arff (filename, dataname, featnames, feats, labels)

   else:
      err = 'Output method not recognised!'