Ejemplo n.º 1
0
def getSingleFeatureLineFromFile(file, decisions, shot, leave_out_class=None):
    """
    This is a less troublesome but slow method to get a featureLine.
    """
    beatList, context = getContextAndBeatListFromFile(file)
    blockList = coalesceBeats(beatList)
    Features.initializeContextVars(context)
    lastShotId, context, blockList = applyDecisionsToBeatscript(context, blockList,
        decisions)
    featureLine = getFeatureLine(context, blockList[len(decisions)], shot, lastShotId,
        leave_out_class)
    return featureLine
Ejemplo n.º 2
0
def createFeatureLines(context, beatList, shot, leave_out_class=None):
    """
    Returns the list of featureLines converted from the Beats in beatList
    """
    featureLines = []
    blockList = coalesceBeats(beatList)
    Features.initializeContextVars(context)
    lastShotId = -1
    for block in blockList:
        featureLines.append(
            getFeatureLine(context, block, shot, lastShotId, leave_out_class))
        context["BygoneBlocks"].append(block)
        lastShotId = block[-1].shotId
    return featureLines
Ejemplo n.º 3
0
def getFeatureNames(leave_out_class=None):
    """
    Returns an array of feature names corresponding to the featureLine.
    """
    names = []
    featureClassList = Features.getAllFeatureClasses()
    context = createContext()
    dummy_beat = Beat("0_1\tfull_shot\tfalse\tintroduce\tperson§Nobody", context)
    context = Features.createBeatList(context, [dummy_beat])
    Features.initializeContextVars(context)
    for featureClass in [x for x in featureClassList if x != leave_out_class]:
        feature = featureClass(context, [dummy_beat])
        names += feature.getNames()
    return names
Ejemplo n.º 4
0
def getSingleFeatureLine(context, blockList, decisions, shot, leave_out_class=None):
    """
    Returns a featureLine based on the context and the decisions.
    """
    Features.initializeContextVars(context)
    lastShotId, context, blockList = applyDecisionsToBeatscript(context, blockList,
        decisions)
    # This is for preventing the classifier to cheat by using the correct class.
    # Activate if you're suspicious.
    #for beat in blockList[len(decisions)]:
    #    beat.shot = 0
    featureLine = getFeatureLine(context, blockList[len(decisions)], shot, lastShotId,
        leave_out_class)
    return featureLine
Ejemplo n.º 5
0
def main():
    beatscriptFile = open(sys.argv[1], "r")
    lines = beatscriptFile.readlines()
    context = readContext(lines)
    beatList = readBeatscript(lines, context)
    blockList = coalesceBeats(beatList)
    Features.initializeContextVars(context)
    dataLines = []
    for block in blockList:
        dataLines.append(createDataLine(context, block))
        context["BygoneBlocks"].append(block)

    outputFile = open(sys.argv[2], "w")
    for dataLine in dataLines:
        outputFile.write(DELIMITER.join([str(x) for x in dataLine]) + "\n")
        #outputFile.write(DELIMITER.join(dataLine) + "\n")
    outputFile.close()
Ejemplo n.º 6
0
def onlineFeatureLineCreator(filename, use_classified_shot = False, use_history = True):
    # load context and complete beatlist from file
    context, beatList = getContextAndBeatListFromFile(filename)
    Features.initializeContextVars(context)
    blockList = coalesceBeats(beatList)
    context["BygoneBlocks"] = []
    for block in blockList:
        shot_true = block[-1].shot
        # get current feature line and true shot class
        featureLine = getFeatureLine(context, block, True, -1)
        features = np.array(featureLine[:-1], dtype=np.float64)
        shot_classified = yield features, shot_true
        # update block and lastShotId
        if use_classified_shot:
            for beat in block:
                beat.shot = shot_classified
        if use_history:
            context["BygoneBlocks"].append(block)
Ejemplo n.º 7
0
def main():
    if len(sys.argv) < 2:
        print("Usage: python ClassificationProcess.py <Beatscript-Filename>")
        return 1
    # Initialization and Training
    classifiers, scaler = trainWithAllExamples(True)
    cutClassifiers, cutScaler = trainWithAllExamples(False)
    try:
        beatscript_file = open(sys.argv[1], "r")
    except IOError:
        print("Error: The Beatscipt could not be opened.")
        print("Usage: python ClassificationProcess.py <Beatscript-Filename>")
        return 1
    lines = beatscript_file.readlines()
    context = readContext(lines)#
    beatscript = readBeatscript(lines, context)
    Features.initializeContextVars(context)
    beatList = getBeatsBetweenFrames(beatscript, -1, 0)
    current_frame = 0
    lastBlock = beatList
    context["BygoneBlocks"] = []
    sys.stdout.write("Training finished." + "\n")
    sys.stdout.flush()
    # Get Distribution
    dist = classifyForShot(lastBlock, context, classifiers, scaler)
    cutBeforeThisClassification = classifyForCut(lastBlock, context, cutClassifiers,
        cutScaler)
    while True:
        choice = raw_input("")
        if choice == "e":
            printListOfEntities(context)
        elif choice == "t": # get the names of the target and the linetarget
            determine_targets(context, lastBlock)
        elif choice == "p": # print out the classification propabilities
            pickle.dump(dist, sys.stdout)

        elif choice == "c": # Print out, if we should cut at this point
            blockList = []
            decisions = []
            for block in context["BygoneBlocks"]:
                blockList.append(block)
                decisions.append(block[0].shot)
            blockList.append(lastBlock)
            decisions.append(dist.index(max(dist)))
            if cutBeforeThisClassification[0] < cutBeforeThisClassification[1]:
                sys.stdout.write("yes\n")
            else:
                sys.stdout.write("no\n")

        elif choice == "f": # check framenumber for a new block
            new_frame = int(raw_input(""))
            beatList = getBeatsBetweenFrames(beatscript, current_frame, new_frame)
            current_frame = new_frame
            if beatList :
                context["BygoneBlocks"].append(lastBlock)
                lastBlock = beatList
                dist = classifyForShot(lastBlock, context, classifiers, scaler)
                cutBeforeThisClassification = classifyForCut(lastBlock, context, cutClassifiers, cutScaler)
                sys.stdout.write("yes\n")
            else:
                sys.stdout.write("no\n")

        elif choice == "d": # recieve the decision for the lastBlock
            decision = int(raw_input(""))
            for beat in lastBlock:
                beat.shot = decision
            sys.stdout.write("decision recieved\n")

        elif choice == "q": # quit...
            sys.stdout.write("exiting...\n")
            sys.stdout.flush()
            break
        else:
            sys.stdout.write("You didn't enter something useful.\n")
        sys.stdout.flush()