Esempio n. 1
0
def genMusic(genScore, args):
   samples = featureManager.loadJson(config.getGenOutFeatFilename(args))
   sample = samples[0] #only one score, to match the format of trainFeat
   perfFeats = sample['perfFeats']
   outScore = featureManager.applyFeats(genScore, perfFeats)
   musicOutputFunc = getMusicOutputFunc()
   musicOutputFunc(outScore, args)
Esempio n. 2
0
   def gen(self, args):
      genFeats = featureManager.loadJson(config.getGenInFeatFilename(args))
      genFeat = genFeats[0]
      scoreName = genFeat['name']
      scoreFeats = genFeat['scoreFeats']
      #wrap genFeats in [] to match data structure in train
      lines = self.formatLineDirect([genFeat]) 
      #logging.printDebug(lines)
      allLines = map(lambda l: "0 " + l, lines)
      #logging.printDebug(allLines)

      #TODO: collect filenames

      SVMGenInputFilename = self.getGenInputFilename(args)

      with open(SVMGenInputFilename, 'w') as f:
         f.writelines(map(lambda x:x+"\n", allLines))

      for featName in config.perfFeatsList:
         cmd = [self.genBinPath]
         cmd.append(SVMGenInputFilename)
         singleModelFilename= self.getSingleModelFilename(args, featName)
         cmd.append(singleModelFilename)
         perfFeatFilename= self.getGenOutputFilename(args, featName)
         cmd.append(perfFeatFilename)

         #logging.printDebug(" ".join(cmd))
         subprocess.call(" ".join(cmd), shell=True)

         #TODO: read all perfFeatFilename, transform to dict and return 


      perfFeats = {}
      for featName in config.perfFeatsList:
         #TODO: put all filename definition in one place
         #perfFeatFilename= self.getGenOutputFilename(args, featName)
         with open(perfFeatFilename, 'r') as f:
             lines = f.readlines()
         #dequantize
         q = quantizer.getQuantizerObj(self.getQuantizeFilename(args, featName))
         realVals= q.dequantize(lines)
         #feat = map(float, lines)
         perfFeats[featName]=realVals

      #logging.printDebug(perfFeats)
      return featureManager.formatFeatFile(scoreName, {}, perfFeats)
Esempio n. 3
0
    def gen(self, args):
        genFeats = featureManager.loadJson(config.getGenInFeatFilename(args))
        genFeat = genFeats[0]
        scoreName = genFeat['name']
        scoreFeats = genFeat['scoreFeats']
        #wrap genFeats in [] to match data structure in train
        lines = self.formatLineDirect([genFeat])
        #logging.printDebug(lines)
        allLines = map(lambda l: "0 " + l, lines)
        #logging.printDebug(allLines)

        #TODO: collect filenames

        SVMGenInputFilename = self.getGenInputFilename(args)

        with open(SVMGenInputFilename, 'w') as f:
            f.writelines(map(lambda x: x + "\n", allLines))

        for featName in config.perfFeatsList:
            cmd = [self.genBinPath]
            cmd.append(SVMGenInputFilename)
            singleModelFilename = self.getSingleModelFilename(args, featName)
            cmd.append(singleModelFilename)
            perfFeatFilename = self.getGenOutputFilename(args, featName)
            cmd.append(perfFeatFilename)

            #logging.printDebug(" ".join(cmd))
            subprocess.call(" ".join(cmd), shell=True)

            #TODO: read all perfFeatFilename, transform to dict and return

        perfFeats = {}
        for featName in config.perfFeatsList:
            #TODO: put all filename definition in one place
            #perfFeatFilename= self.getGenOutputFilename(args, featName)
            with open(perfFeatFilename, 'r') as f:
                lines = f.readlines()
            #dequantize
            q = quantizer.getQuantizerObj(
                self.getQuantizeFilename(args, featName))
            realVals = q.dequantize(lines)
            #feat = map(float, lines)
            perfFeats[featName] = realVals

        #logging.printDebug(perfFeats)
        return featureManager.formatFeatFile(scoreName, {}, perfFeats)
Esempio n. 4
0
    def train(self, args):
        #ex filenames:
        # trainFeat.json -> [quantizer] -> <perfFeatName>.train.dat
        #                       |                                 |
        #                       +->  <perfFeatName>.quant         |
        # +-------------------------------------------------------+
        # +-> [svm^hmm] -> <perfFeatName>.model.bin

        trainFeats = featureManager.loadJson(
            config.getTrainInFeatFilename(args))
        perfFeats = collectPerfFeats(trainFeats)
        #logging.printDebug(perfFeats)
        #for each perfFeature, a svmFeatFilename (quantized feat for svm^hmm)
        #and singleModelFilename (model file for svm^hmm) will be saved
        #scoreFeatLines = self.formatLine(scoreFeats)
        scoreFeatLines = self.formatLineDirect(trainFeats)
        for pkey, pval in perfFeats.items():
            allLines = [("# " + pkey)]
            #TODO:quantize pval
            q = quantizer.getQuantizerObj(self.getQuantizeFilename(args, pkey))
            quantizedVal = q.quantize(pval)
            lines = zip(map(str, quantizedVal), scoreFeatLines)
            #logging.printDebug(lines)
            allLines = map(lambda l: " ".join(l), lines)
            #logging.printDebug(allLines)

            svmFeatFilename = self.getTrainInputFilename(args, pkey)
            with open(svmFeatFilename, 'w') as f:
                f.writelines(map(lambda x: x + "\n", allLines))

            cmd = [self.trainBinPath]
            if (config.svmhmm_c != None):
                cmd.append("-c " + str(config.svmhmm_c))
            else:
                cmd.append("-c 0.01")
            cmd.append(svmFeatFilename)
            singleModelFilename = self.getSingleModelFilename(args, pkey)
            cmd.append(singleModelFilename)

            #logging.printDebug(" ".join(cmd))
            subprocess.call(" ".join(cmd), shell=True)
Esempio n. 5
0
   def train(self, args):
      #ex filenames:
      # trainFeat.json -> [quantizer] -> <perfFeatName>.train.dat 
      #                       |                                 |
      #                       +->  <perfFeatName>.quant         |
      # +-------------------------------------------------------+
      # +-> [svm^hmm] -> <perfFeatName>.model.bin

      trainFeats = featureManager.loadJson(config.getTrainInFeatFilename(args))
      perfFeats = collectPerfFeats(trainFeats)
      #logging.printDebug(perfFeats)
      #for each perfFeature, a svmFeatFilename (quantized feat for svm^hmm)
      #and singleModelFilename (model file for svm^hmm) will be saved
      #scoreFeatLines = self.formatLine(scoreFeats) 
      scoreFeatLines = self.formatLineDirect(trainFeats) 
      for pkey, pval in perfFeats.items():
         allLines = [("# " + pkey)]
         #TODO:quantize pval
         q = quantizer.getQuantizerObj(self.getQuantizeFilename(args, pkey))
         quantizedVal = q.quantize(pval)
         lines = zip(map(str, quantizedVal), scoreFeatLines) 
         #logging.printDebug(lines)
         allLines = map(lambda l:" ".join(l), lines)
         #logging.printDebug(allLines)

         svmFeatFilename = self.getTrainInputFilename(args, pkey)
         with open(svmFeatFilename, 'w') as f:
            f.writelines(map(lambda x:x+"\n", allLines))

         cmd = [self.trainBinPath]
         if (config.svmhmm_c != None):
             cmd.append("-c " + str(config.svmhmm_c))
         else:
             cmd.append("-c 0.01")
         cmd.append(svmFeatFilename)
         singleModelFilename = self.getSingleModelFilename(args,pkey)
         cmd.append(singleModelFilename)

         #logging.printDebug(" ".join(cmd))
         subprocess.call(" ".join(cmd), shell=True)
Esempio n. 6
0
   def train(self, trainFeatFilename, modelFilename):
      trainFeats = featureManager.loadJson(trainFeatFilename)
      scoreFeats = collectScoreFeats(trainFeats)
      perfFeats = collectPerfFeats(trainFeats)
      #data = r('data.frame(scoreFeats, perfFeats)')
      formula = 'cbind(' + ','.join(config.perfFeatsList) +')'
      formula += '~'
      formula += '*'.join(config.scoreFeatsList)
      #logging.printDebug(formula)
      r.assign('fstr', formula)
      r.assign('scoreFeats',scoreFeats)
      r.assign('perfFeats',perfFeats)
      r('f <- formula(fstr)')
      r('scoreFeats <- data.frame(scoreFeats)')
      r('perfFeats <- data.frame(perfFeats)')
      if config.DEBUG: 
         r('str(perfFeats)')
         r('str(scoreFeats)')
         r('str(f)')
      r.assign('modelFilename',modelFilename)

      r.source('modelMultiLinearRegress.R')
      r('trainMultiLinearRegress(f, perfFeats, scoreFeats, modelFilename)')
Esempio n. 7
0
 def gen(self, genFeatFilename, modelFilename):
    genFeats = featureManager.loadJson(genFeatFilename)
    scoreFeats = genFeats['scoreFeats']
    #logging.printDebug(scoreFeats)
    r.assign('scoreFeats',scoreFeats)
    r.assign('modelFilename',modelFilename)
    r('scoreFeats <- data.frame(scoreFeats)')
    if config.DEBUG: 
       r('str(scoreFeats)')
       r('save(scoreFeats, file="../output/debug_genFeat.rda")')
    r.source('modelMultiLinearRegress.R')
    perfFeats = r('perfFeats<-genMultiLinearRegress(scoreFeats, modelFilename)')
    if config.DEBUG: r('str(perfFeats)')
    #workaround: if only one perfFeat is used. predict will return a bad format data.frame without proper perfFeatName
    #logging.printDebug('PerfFeatures from genModel')
    #logging.printDebug(perfFeats)
    #logging.printDebug(perfFeats.items())
    if len(perfFeats) == 1:
       newPerfFeats = {} 
       for key, val in perfFeats.items(): 
          newPerfFeats[config.perfFeatsList[0]] = val
       return newPerfFeats
    return perfFeats
Esempio n. 8
0
    def train(self, trainFeatFilename, modelFilename):
        trainFeats = featureManager.loadJson(trainFeatFilename)
        scoreFeats = collectScoreFeats(trainFeats)
        perfFeats = collectPerfFeats(trainFeats)
        #data = r('data.frame(scoreFeats, perfFeats)')
        formula = 'cbind(' + ','.join(config.perfFeatsList) + ')'
        formula += '~'
        formula += '*'.join(config.scoreFeatsList)
        #logging.printDebug(formula)
        r.assign('fstr', formula)
        r.assign('scoreFeats', scoreFeats)
        r.assign('perfFeats', perfFeats)
        r('f <- formula(fstr)')
        r('scoreFeats <- data.frame(scoreFeats)')
        r('perfFeats <- data.frame(perfFeats)')
        if config.DEBUG:
            r('str(perfFeats)')
            r('str(scoreFeats)')
            r('str(f)')
        r.assign('modelFilename', modelFilename)

        r.source('modelMultiLinearRegress.R')
        r('trainMultiLinearRegress(f, perfFeats, scoreFeats, modelFilename)')
Esempio n. 9
0
 def gen(self, genFeatFilename, modelFilename):
     genFeats = featureManager.loadJson(genFeatFilename)
     scoreFeats = genFeats['scoreFeats']
     #logging.printDebug(scoreFeats)
     r.assign('scoreFeats', scoreFeats)
     r.assign('modelFilename', modelFilename)
     r('scoreFeats <- data.frame(scoreFeats)')
     if config.DEBUG:
         r('str(scoreFeats)')
         r('save(scoreFeats, file="../output/debug_genFeat.rda")')
     r.source('modelMultiLinearRegress.R')
     perfFeats = r(
         'perfFeats<-genMultiLinearRegress(scoreFeats, modelFilename)')
     if config.DEBUG: r('str(perfFeats)')
     #workaround: if only one perfFeat is used. predict will return a bad format data.frame without proper perfFeatName
     #logging.printDebug('PerfFeatures from genModel')
     #logging.printDebug(perfFeats)
     #logging.printDebug(perfFeats.items())
     if len(perfFeats) == 1:
         newPerfFeats = {}
         for key, val in perfFeats.items():
             newPerfFeats[config.perfFeatsList[0]] = val
         return newPerfFeats
     return perfFeats
Esempio n. 10
0
#main
parser = argparse.ArgumentParser()
parser.add_argument(
    "dir",  #nargs="1" , 
    help="Path to all feature files.",
    default=defaultDir)
args = parser.parse_args()

filenames = os.listdir(args.dir)
realPerfFilnames = fnmatch.filter(filenames, realPerfExt)
genPerfFilnames = fnmatch.filter(filenames, genPerfExt)

lines = []

#prepare col names
feat = featureManager.loadJson(args.dir + realPerfFilnames[0])
lines += parseColNames(feat)

for realPerfFilname in realPerfFilnames:
    if (DEBUG):
        lines += ["# " + realPerfFilname]
    realFeats = featureManager.loadJson(args.dir + realPerfFilname)
    lines += parseLines(realFeats, "real")

for genPerfFilname in genPerfFilnames:
    if (DEBUG):
        lines += ["# " + genPerfFilname]
    genFeats = featureManager.loadJson(args.dir + genPerfFilname)
    lines += parseLines(genFeats, "gen")

for line in lines:
Esempio n. 11
0
import model
import config 
import featureManager 

trainFeats = featureManager.loadJson(config.defaultTrainFeatsFilename)
print(model.collectScoreFeats(trainFeats))
print(model.collectPerfFeats(trainFeats))

model = model.trainMultiLinearRegress(trainFeats, config.defaultModelFilename)
print(model)

def dummyFunc():
   print("this is a function for type(dummyFunc) comparison")

#for mode in ['train', 'gen']:
#   print(model.getModelFunc(mode))
#   assert type(model.getModelFunc(mode)) == type(dummyFunc), 'getModelFunc() returned a non-function object'
#
Esempio n. 12
0
import model
import config
import featureManager

trainFeats = featureManager.loadJson(config.defaultTrainFeatsFilename)
print(model.collectScoreFeats(trainFeats))
print(model.collectPerfFeats(trainFeats))

model = model.trainMultiLinearRegress(trainFeats, config.defaultModelFilename)
print(model)


def dummyFunc():
    print("this is a function for type(dummyFunc) comparison")


#for mode in ['train', 'gen']:
#   print(model.getModelFunc(mode))
#   assert type(model.getModelFunc(mode)) == type(dummyFunc), 'getModelFunc() returned a non-function object'
#
Esempio n. 13
0
    return lines


# main
parser = argparse.ArgumentParser()
parser.add_argument("dir", help="Path to all feature files.", default=defaultDir)  # nargs="1" ,
args = parser.parse_args()

filenames = os.listdir(args.dir)
realPerfFilnames = fnmatch.filter(filenames, realPerfExt)
genPerfFilnames = fnmatch.filter(filenames, genPerfExt)

lines = []

# prepare col names
feat = featureManager.loadJson(args.dir + realPerfFilnames[0])
lines += parseColNames(feat)

for realPerfFilname in realPerfFilnames:
    if DEBUG:
        lines += ["# " + realPerfFilname]
    realFeats = featureManager.loadJson(args.dir + realPerfFilname)
    lines += parseLines(realFeats, "real")


for genPerfFilname in genPerfFilnames:
    if DEBUG:
        lines += ["# " + genPerfFilname]
    genFeats = featureManager.loadJson(args.dir + genPerfFilname)
    lines += parseLines(genFeats, "gen")