예제 #1
0
def LoadPathways(pathwayPath, listFile):
    pathways = []
    with open(listFile) as inFile:
        for pathwayLine in inFile:
            pathwayLine = pathwayLine.strip()
            # Each line is a relative path to a pathway file
            pathway = NetworkUtil.LoadGraphiteNetwork(
                os.path.join(pathwayPath, pathwayLine))

            pathway.graph["filename"] = os.path.join(pathwayPath, pathwayLine)
            if pathwayLine.endswith(".txt"):
                pathwayLine = pathwayLine[0:-4]  # Remove ".txt"
            pathway.graph["name"] = pathwayLine

            # Debugging
            print "Loaded %s with %d nodes and %d edges" % (
                pathway.graph["name"], pathway.order(), pathway.size())
            # Add the pathway to the list
            pathways.append(pathway)
    return pathways
def Evaluate(network2Pathway, outFileName, fraction, noise, weightedNetworks=False):
    with open(outFileName, "w") as outFile:
        npSum = 0
        nrSum = 0
        epSum = 0
        erSum = 0
        outFile.write("Steiner forest\tPathway\tTrue prizes\tNoisy prizes\tForest nodes\tPathway nodes\tIntersection nodes\tNode precision\tNode recall\tForest edges\tPathway edges\tIntersection edges\tEdge precision\tEdge recall\n")
        # The name forestFile assumes the networks to evaluate are Steiner forests, but they can
        # be any network        
        for forestFile, pathwayFile in network2Pathway:
            # For each Steiner forest, compute the precision and recall with respect to the original pathway
            forest = NetworkUtil.LoadNetwork(forestFile, weight=weightedNetworks)
            # Remove the artificial node if the forest is not empty
            if "DUMMY" in forest:
                forest.remove_node("DUMMY")
            # NetworkUtil.LoadNetwork only works for the simple format used when writing synthetic
            # pathways.  LoadGraphiteNetwork works for the simple format and the graphite edge list.
            pathway = NetworkUtil.LoadGraphiteNetwork(pathwayFile)
            intersection = NetworkUtil.Intersection(forest, pathway)
            if forest.order() == 0:
                nPrecision = 0
            else:
                nPrecision = float(intersection.order())/forest.order()
            npSum += nPrecision
            nRecall = float(intersection.order())/pathway.order()
            nrSum += nRecall
            if forest.size() == 0:
                ePrecision = 0
            else:
                ePrecision = float(intersection.size())/forest.size()
            epSum += ePrecision
            eRecall = float(intersection.size())/pathway.size()
            erSum += eRecall
            truePrizes = int(math.ceil(fraction*pathway.order()))
            noisyPrizes = int(math.ceil(noise*truePrizes))
            outFile.write("%s\t%s\t%d\t%d\t%d\t%d\t%d\t%f\t%f\t%d\t%d\t%d\t%f\t%f\n" % (os.path.basename(forestFile), os.path.basename(pathwayFile), truePrizes, noisyPrizes, forest.order(), pathway.order(), intersection.order(), nPrecision, nRecall, forest.size(), pathway.size(), intersection.size(), ePrecision, eRecall))
        # Write the average node/edge precision/recall
        outFile.write("Average\t\t\t\t\t\t\t%f\t%f\t\t\t\t%f\t%f\n" % (npSum/len(network2Pathway), nrSum/len(network2Pathway), epSum/len(network2Pathway), erSum/len(network2Pathway)))