Ejemplo n.º 1
0
def saveResults(meta, resultPath, results, extras, bestIndex, analyze, hiddenResults=None, hiddenDetails=None, databaseCGI=None, reclassify=False, details=True):
    if extras == None:
        print "No detailed information for cross-validation"
        return
    if not os.path.exists(os.path.dirname(resultPath)):
        os.makedirs(os.path.dirname(resultPath))
    meta = result.getMeta(meta)
    # Add general results
    meta["results"] = {"best":results[bestIndex], "all":results}
    if hiddenResults != None:
        meta["results"]["hidden"] = hiddenResults
    # Insert detailed results
    if details:
        featureByIndex = result.getFeaturesByIndex(meta)
        if hiddenDetails != None:
            saveDetails(meta, hiddenDetails.get("predictions", None), hiddenDetails.get("importances", None), "hidden", featureByIndex, reclassify=reclassify)
        fold = 0
        for extra in extras:
            saveDetails(meta, extra.get("predictions", None), extra.get("importances", None), fold, featureByIndex, reclassify=reclassify)
            fold += 1
    else:
        if "examples" in meta:
            del meta["examples"]
        if "features" in meta:
            del meta["features"]
    
    # Analyze results
    if analyze:
        print "Analyzing results"
        meta = gene.analyze.analyze(meta, databaseCGI)              
    # Save results
    if resultPath != None:
        result.saveMeta(meta, resultPath)
Ejemplo n.º 2
0
def saveDetails(meta, predictions, importances, fold, featureByIndex=None, reclassify=False):
    if featureByIndex == None:
        featureByIndex = result.getFeaturesByIndex(meta)
    if predictions != None:
        for index in predictions:
            if fold == "hidden":
                example = result.getExampleFromSet(meta, index, "hidden")
            else:
                example = result.getExampleFromSet(meta, index, "train")
            if reclassify and ("classification" in example):
                del example["classification"]
            if "classification" in example:
                raise Exception("Example " + str(index) + " has already been classified " + str([fold, str(example)]))
            result.setValue(example, "prediction", predictions[index], "classification")
            result.setValue(example, "fold", fold, "classification")
    if importances != None:
        for i in range(len(importances)):
            if importances[i] != 0:
                feature = result.getFeature(meta, i, featureByIndex)
                if fold != "hidden":
                    result.setValue(feature, fold, importances[i], "importances")
                    #if "sort" not in feature:
                    #    result.setValue(feature, "sort", 0)
                else:
                    result.setValue(feature, "hidden-importance", importances[i])
                    result.setValue(feature, "sort", importances[i])
Ejemplo n.º 3
0
def saveResults(meta, resultPath, perfs, selected):
    if not os.path.exists(os.path.dirname(resultPath)):
        os.makedirs(os.path.dirname(resultPath))
    meta = result.getMeta(meta)
    featureByIndex = result.getFeaturesByIndex(meta)
    for foldIndex in range(len(selected)):
        ranks = selected[foldIndex]
        for featureRank in range(len(ranks)):
            featureIndex = ranks[featureRank]
            feature = result.getFeature(meta, featureIndex, featureByIndex)
            result.setValue(feature, foldIndex, featureRank, "ranks")
            result.setValue(feature, "sort", -sum(feature["ranks"].values()) / len(feature["ranks"]))
    result.saveMeta(meta, resultPath)