def run(self): self.graph, authorMap, conference, citationsPublications = SampleGraphUtility.constructSkewedCitationPublicationExample( introduceRandomness=False ) # Get the nodes we care about authors = [ authorMap["Alice"], authorMap["Bob"], authorMap["Carol"], authorMap["Dave"], authorMap["Ed"], authorMap["Frank"], ] metaPathUtility = EdgeBasedMetaPathUtility() # Output adjacency matrices self.output("\nCPA Adjacency Matrix:") cpaadjMatrix, nodesIndex = metaPathUtility.getAdjacencyMatrixFromGraph( self.graph, [Conference, Paper, Author], project=True ) adjMatrixTable = texttable.Texttable() rows = [["Conference"] + [author.name for author in authors]] rows += [[conference.name] + [cpaadjMatrix[nodesIndex[conference]][nodesIndex[author]] for author in authors]] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) self.output("\nCPPA Adjacency Matrix:") cpaadjMatrix, nodesIndex = metaPathUtility.getAdjacencyMatrixFromGraph( self.graph, [Conference, Paper, Paper, Author], project=True ) adjMatrixTable = texttable.Texttable() rows = [["Conference"] + [author.name for author in authors]] rows += [[conference.name] + [cpaadjMatrix[nodesIndex[conference]][nodesIndex[author]] for author in authors]] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) # Total citation & publication counts self.output("\nCitation & Publication Counts") adjMatrixTable = texttable.Texttable() rows = [["Measure"] + [author.name for author in authors]] rows += [["Citations"] + [citationsPublications[author][0] for author in authors]] rows += [["Publications"] + [citationsPublications[author][1] for author in authors]] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) # Output NeighborSim & PathSim similarity scores neighborSimStrategy = NeighborSimStrategy(self.graph, [Conference, Paper, Author], symmetric=True) self.outputSimilarityScores(authorMap, authors, neighborSimStrategy, "APCPA PathSim") neighborSimStrategy = NeighborSimStrategy(self.graph, [Conference, Paper, Paper, Author]) self.outputSimilarityScores(authorMap, authors, neighborSimStrategy, "APPCPPA PathSim") # Omit extra duplicate entry in path, and weight at different levels of 'relative' for strategy, generalStrategyTitle in [ (FlattenedMatrixStrategy, "FlatMat"), (VectorProductStrategy, "VectorProduct"), ]: for w in [1.0, 0.5, 0]: neighborPathShapeStrategy = strategy( self.graph, weight=w, omit=[], metaPath=[Conference, Paper, Paper, Author], symmetric=True ) strategyTitle = "APPCPPA %s ShapeSim (%1.2f weight)" % (generalStrategyTitle, w) self.outputSimilarityScores(authorMap, authors, neighborPathShapeStrategy, strategyTitle) w = 1.0 neighborPathShapeStrategy = VectorProductStrategy( self.graph, weight=w, omit=[0], metaPath=[Conference, Paper, Paper, Author], symmetric=True ) strategyTitle = "APPCPPA VectorProduct ShapeSim omitting CPC (%1.2f weight)" % w self.outputSimilarityScores(authorMap, authors, neighborPathShapeStrategy, strategyTitle) # Output recursive pathsim strategy score(s) recursivePathSimStrategy = RecursivePathSimStrategy(self.graph, [Conference, Paper, Paper, Author]) self.outputSimilarityScores(authorMap, authors, recursivePathSimStrategy, "APPCPPA Recursive PathSim")
def run(self): self.graph, authorMap, conference, citationsPublications = \ SampleGraphUtility.constructSkewedCitationPublicationExample(introduceRandomness=False) # Get the nodes we care about authors = [ authorMap['Alice'], authorMap['Bob'], authorMap['Carol'], authorMap['Dave'], authorMap['Ed'], authorMap['Frank'] ] metaPathUtility = EdgeBasedMetaPathUtility() # Output adjacency matrices self.output('\nCPA Adjacency Matrix:') cpaadjMatrix, nodesIndex = metaPathUtility.getAdjacencyMatrixFromGraph( self.graph, [Conference, Paper, Author], project=True ) adjMatrixTable = texttable.Texttable() rows = [['Conference'] + [author.name for author in authors]] rows += [[conference.name] + [cpaadjMatrix[nodesIndex[conference]][nodesIndex[author]] for author in authors]] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) self.output('\nCPPA Adjacency Matrix:') cpaadjMatrix, nodesIndex = metaPathUtility.getAdjacencyMatrixFromGraph( self.graph, [Conference, Paper, Paper, Author], project=True ) adjMatrixTable = texttable.Texttable() rows = [['Conference'] + [author.name for author in authors]] rows += [[conference.name] + [cpaadjMatrix[nodesIndex[conference]][nodesIndex[author]] for author in authors]] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) # Total citation & publication counts self.output('\nCitation & Publication Counts') adjMatrixTable = texttable.Texttable() rows = [['Measure'] + [author.name for author in authors]] rows += [['Citations'] + [citationsPublications[author][0] for author in authors]] rows += [['Publications'] + [citationsPublications[author][1] for author in authors]] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) # Output NeighborSim & PathSim similarity scores neighborSimStrategy = NeighborSimStrategy(self.graph, [Conference, Paper, Author], symmetric=True) self.outputSimilarityScores(authorMap, authors, neighborSimStrategy, 'APCPA PathSim') neighborSimStrategy = NeighborSimStrategy(self.graph, [Conference, Paper, Paper, Author]) self.outputSimilarityScores(authorMap, authors, neighborSimStrategy, 'APPCPPA PathSim') # Omit extra duplicate entry in path, and weight at different levels of 'relative' for strategy, generalStrategyTitle in [(FlattenedMatrixStrategy, 'FlatMat'), (VectorProductStrategy, 'VectorProduct')]: for w in [1.0, 0.5, 0]: neighborPathShapeStrategy = strategy( self.graph, weight=w, omit=[], metaPath=[Conference, Paper, Paper, Author], symmetric=True ) strategyTitle = 'APPCPPA %s ShapeSim (%1.2f weight)' % (generalStrategyTitle, w) self.outputSimilarityScores(authorMap, authors, neighborPathShapeStrategy, strategyTitle) w = 1.0 neighborPathShapeStrategy = VectorProductStrategy( self.graph, weight=w, omit=[0], metaPath=[Conference, Paper, Paper, Author], symmetric=True ) strategyTitle = 'APPCPPA VectorProduct ShapeSim omitting CPC (%1.2f weight)' % w self.outputSimilarityScores(authorMap, authors, neighborPathShapeStrategy, strategyTitle) # Output recursive pathsim strategy score(s) recursivePathSimStrategy = RecursivePathSimStrategy(self.graph, [Conference, Paper, Paper, Author]) self.outputSimilarityScores(authorMap, authors, recursivePathSimStrategy, 'APPCPPA Recursive PathSim')
def run(self): self.graph, authorMap, conferenceMap = SampleGraphUtility.constructPathSimExampleThree() # Get the nodes we care about conferences = [ conferenceMap['SIGMOD'], conferenceMap['VLDB'], conferenceMap['ICDE'], conferenceMap['KDD'] ] authors = [ authorMap['Mike'], authorMap['Jim'], authorMap['Mary'], authorMap['Bob'], authorMap['Ann'], ] metaPathUtility = EdgeBasedMetaPathUtility() self.output('\nAPC Adjacency Matrix:') apcadjMatrix, nodesIndex = metaPathUtility.getAdjacencyMatrixFromGraph(self.graph, [Author, Paper, Conference], project=True) adjMatrixTable = texttable.Texttable() rows = [['Author'] + [conference.name for conference in conferences]] rows += [[author.name] + [apcadjMatrix[nodesIndex[author]][nodesIndex[conference]] for conference in conferences] for author in authors] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) self.output('\nCPA Adjacency Matrix:') cpaadjMatrix, dsad = metaPathUtility.getAdjacencyMatrixFromGraph(self.graph, [Conference, Paper, Author], project=True) adjMatrixTable = texttable.Texttable() rows = [['Conference'] + [author.name for author in authors]] rows += [[conference.name] + [cpaadjMatrix[nodesIndex[conference]][nodesIndex[author]] for author in authors] for conference in conferences] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) self.output('\nAPCPA Adjacency Matrix (Computed):') adjMatrix = numpy.dot(apcadjMatrix, cpaadjMatrix) adjMatrixTable = texttable.Texttable() rows = [['Author'] + [author.name for author in authors]] rows += [[author.name] + [adjMatrix[nodesIndex[author]][nodesIndex[otherAuthor]] for otherAuthor in authors] for author in authors] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) # Output homogeneous simrank comparison homogeneousSimRankStrategy = SimRankStrategy(self.graph) self.outputSimilarityScores(authorMap, authors, homogeneousSimRankStrategy, 'Homogeneous SimRank') projectedGraph = metaPathUtility.createHeterogeneousProjection(self.graph, [Author, Paper, Conference], symmetric = True) # Output heterogeneous simrank comparison heterogeneousSimRankStrategy = SimRankStrategy(projectedGraph) self.outputSimilarityScores(authorMap, authors, heterogeneousSimRankStrategy, 'APC Heterogeneous SimRank') # Output heterogeneous simrank w/ squared neighbors comparison def sqNeighborsNorm(graph, a, b, sim): aNeighbors, bNeighbors = graph.getPredecessors(a), graph.getPredecessors(b) return float(len(aNeighbors)**2 * len(bNeighbors)**2) heterogeneousSquaredSimRankStrategy = SimRankStrategy(projectedGraph, normalization=sqNeighborsNorm) self.outputSimilarityScores(authorMap, authors, heterogeneousSquaredSimRankStrategy, 'Squared Heterogeneous SimRank') # Output NeighborSim similarity scores neighborSimStrategy = NeighborSimStrategy(self.graph, [Author, Paper, Conference], symmetric=True) self.outputSimilarityScores(authorMap, authors, neighborSimStrategy, 'APC NeighborSim') # Output the PathSim similarity scores pathsimStrategy = PathSimStrategy(self.graph, [Author, Paper, Conference, Paper, Author], symmetric=True) self.outputSimilarityScores(authorMap, authors, pathsimStrategy, 'APCPA PathSim')
def run(self): self.graph, authorMap, conferenceMap = SampleGraphUtility.constructPathSimExampleThree( ) # Get the nodes we care about conferences = [ conferenceMap['SIGMOD'], conferenceMap['VLDB'], conferenceMap['ICDE'], conferenceMap['KDD'] ] authors = [ authorMap['Mike'], authorMap['Jim'], authorMap['Mary'], authorMap['Bob'], authorMap['Ann'], ] metaPathUtility = EdgeBasedMetaPathUtility() self.output('\nAPC Adjacency Matrix:') apcadjMatrix, nodesIndex = metaPathUtility.getAdjacencyMatrixFromGraph( self.graph, [Author, Paper, Conference], project=True) adjMatrixTable = texttable.Texttable() rows = [['Author'] + [conference.name for conference in conferences]] rows += [[author.name] + [ apcadjMatrix[nodesIndex[author]][nodesIndex[conference]] for conference in conferences ] for author in authors] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) self.output('\nCPA Adjacency Matrix:') cpaadjMatrix, dsad = metaPathUtility.getAdjacencyMatrixFromGraph( self.graph, [Conference, Paper, Author], project=True) adjMatrixTable = texttable.Texttable() rows = [['Conference'] + [author.name for author in authors]] rows += [[conference.name] + [ cpaadjMatrix[nodesIndex[conference]][nodesIndex[author]] for author in authors ] for conference in conferences] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) self.output('\nAPCPA Adjacency Matrix (Computed):') adjMatrix = numpy.dot(apcadjMatrix, cpaadjMatrix) adjMatrixTable = texttable.Texttable() rows = [['Author'] + [author.name for author in authors]] rows += [[author.name] + [ adjMatrix[nodesIndex[author]][nodesIndex[otherAuthor]] for otherAuthor in authors ] for author in authors] adjMatrixTable.add_rows(rows) self.output(adjMatrixTable.draw()) # Output homogeneous simrank comparison homogeneousSimRankStrategy = SimRankStrategy(self.graph) self.outputSimilarityScores(authorMap, authors, homogeneousSimRankStrategy, 'Homogeneous SimRank') projectedGraph = metaPathUtility.createHeterogeneousProjection( self.graph, [Author, Paper, Conference], symmetric=True) # Output heterogeneous simrank comparison heterogeneousSimRankStrategy = SimRankStrategy(projectedGraph) self.outputSimilarityScores(authorMap, authors, heterogeneousSimRankStrategy, 'APC Heterogeneous SimRank') # Output heterogeneous simrank w/ squared neighbors comparison def sqNeighborsNorm(graph, a, b, sim): aNeighbors, bNeighbors = graph.getPredecessors( a), graph.getPredecessors(b) return float(len(aNeighbors)**2 * len(bNeighbors)**2) heterogeneousSquaredSimRankStrategy = SimRankStrategy( projectedGraph, normalization=sqNeighborsNorm) self.outputSimilarityScores(authorMap, authors, heterogeneousSquaredSimRankStrategy, 'Squared Heterogeneous SimRank') # Output NeighborSim similarity scores neighborSimStrategy = NeighborSimStrategy(self.graph, [Author, Paper, Conference], symmetric=True) self.outputSimilarityScores(authorMap, authors, neighborSimStrategy, 'APC NeighborSim') # Output the PathSim similarity scores pathsimStrategy = PathSimStrategy( self.graph, [Author, Paper, Conference, Paper, Author], symmetric=True) self.outputSimilarityScores(authorMap, authors, pathsimStrategy, 'APCPA PathSim')