예제 #1
0
    def solvePageRank(self, eps, sortRes=False, saveVals=True):
        #DO NOT MODIFY THIS FUNCTION -add any extra functions you want to use
        #in solveRankToEps

        self.rankVec = self.solveRankToEps(eps)

        if (len(self.rankVec) > 0):
            if (sortRes):
                #when converges, to sort self.rankVec also need to sort self.nodeIDs
                #so that rankvec is descending and node ids still aligns with it
                sortedIDXs, self.rankVec = util.getSortResIDXs(self.rankVec)
                newIDVec = [self.nodeIDs[i] for i in sortedIDXs]
                self.nodeIDs = newIDVec
            else:
                #don't save node id list if rank vec not sorted - node ids are
                #just idxs in order
                newIDVec = None
        else:
            print('Zero-size PageRank vector Error.')
            newIDVec = None

        if (saveVals):
            print('Saving results')
            util.saveRankData(self.outFileName, newIDVec, self.rankVec)
            print('Results saved')

        return self.rankVec, self.nodeIDs
예제 #2
0
def plotRes(prObj):
    #for graphs you may wish to code
    #import matplotlib as plt
    import matplotlib.pyplot as plt
    #for plotting
    import numpy as np

    stSiteLoc = 0
    endSiteLoc = 20
    # use results for .75, .85 and .95 alphas to build plots - vNodeIDxx and
    # vRankVecXX results come back sorted in descending rank order
    vNodeID75, vRankVec75, dictRV75 = util.getResForPlots(prObj, .75)
    vNodeID85, vRankVec85, dictRV85 = util.getResForPlots(prObj, .85)
    vNodeID95, vRankVec95, dictRV95 = util.getResForPlots(prObj, .95)

    #find union of all top x sites' site ids
    allNodesSet = set()
    allNodesSet.update(vNodeID75[stSiteLoc:endSiteLoc])
    allNodesSet.update(vNodeID85[stSiteLoc:endSiteLoc])
    allNodesSet.update(vNodeID95[stSiteLoc:endSiteLoc])
    #all nodes in top 20 for any of the 3 alpha settings - unsorted
    allTopNodes = list(allNodesSet)

    #list of values in allTopNodes order
    pltPRVals85tmp = [dictRV85[x] for x in allTopNodes]
    #find appropriate order  - use idxs to find actual node IDS in allTopNodes
    srtdIDXsInATNlst, pltPRVals85 = util.getSortResIDXs(pltPRVals85tmp)
    allTopNodes2 = [allTopNodes[x] for x in srtdIDXsInATNlst]
    #comparing alpha of .75 and .95 to alpha of .85

    pltPRVals75 = [dictRV75[x] for x in allTopNodes2]
    pltPRVals95 = [dictRV95[x] for x in allTopNodes2]

    # data to plot
    n_groups = len(allTopNodes2)

    # create plot
    fig, ax = plt.subplots()
    #index = np.asarray(allTopNodes)#np.arange(n_groups)
    index = np.arange(n_groups)
    bar_width = 0.30
    opacity = 0.8

    r1 = plt.bar(index,
                 pltPRVals75,
                 bar_width,
                 alpha=opacity,
                 color='b',
                 label='alpha=.75')

    r2 = plt.bar(index + bar_width,
                 pltPRVals85,
                 bar_width,
                 alpha=opacity,
                 color='r',
                 label='alpha=.85')
    r3 = plt.bar(index + 2 * bar_width,
                 pltPRVals95,
                 bar_width,
                 alpha=opacity,
                 color='g',
                 label='alpha=.95')

    plt.xlabel('Site Rank')
    plt.ylabel('Score')
    ttlStr = 'Scores for dataset {} for sites ranked {} through {}\n'.format(prObj.inFileName,(stSiteLoc+1),endSiteLoc) \
                  + 'for 3 alpha values using {} sink handling'.format('self-loop' if prObj.selfLoops else 'Type 3')
    plt.title(ttlStr)
    plt.legend()

    plt.tight_layout()
    plt.show()