<s> import scipy . io <EOL> import numpy <EOL> import sppy <EOL> from apgl . graph . AbstractMatrixGraph import AbstractMatrixGraph <EOL> from apgl . graph . AbstractVertexList import AbstractVertexList <EOL> from apgl . graph import GeneralVertexList <EOL> from apgl . util . Parameter import Parameter <EOL> from apgl . util . SparseUtils import SparseUtils <EOL> class CsArrayGraph ( AbstractMatrixGraph ) : <EOL> def __init__ ( self , vertices , undirected = True , dtype = numpy . float ) : <EOL> """<STR_LIT>""" <EOL> Parameter . checkBoolean ( undirected ) <EOL> if isinstance ( vertices , AbstractVertexList ) : <EOL> self . vList = vertices <EOL> elif isinstance ( vertices , int ) : <EOL> self . vList = GeneralVertexList ( vertices ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + str ( vertices ) ) <EOL> self . W = sppy . csarray ( ( self . vList . getNumVertices ( ) , self . vList . getNumVertices ( ) ) , dtype ) <EOL> self . undirected = undirected <EOL> def getNumEdges ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . undirected : <EOL> return ( self . W . getnnz ( ) + numpy . flatnonzero ( self . W . diag ( ) ) . shape [ <NUM_LIT:0> ] ) / <NUM_LIT:2> <EOL> else : <EOL> return self . W . getnnz ( ) <EOL> def getNumDirEdges ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . W . getnnz ( ) <EOL> def getWeightMatrix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . W . toarray ( ) <EOL> def neighbours ( self , vertexIndex ) : <EOL> """<STR_LIT>""" <EOL> Parameter . checkIndex ( vertexIndex , <NUM_LIT:0> , self . vList . getNumVertices ( ) ) <EOL> neighbourIndices = self . W . rowInds ( vertexIndex ) <EOL> return neighbourIndices <EOL> def neighbourOf ( self , vertexIndex ) : <EOL> """<STR_LIT>""" <EOL> Parameter . checkIndex ( vertexIndex , <NUM_LIT:0> , self . vList . getNumVertices ( ) ) <EOL> nonZeroIndices = numpy . nonzero ( self . W [ : , vertexIndex ] ) <EOL> neighbourIndices = nonZeroIndices [ <NUM_LIT:0> ] <EOL> return neighbourIndices <EOL> def complement ( self ) : <EOL> """<STR_LIT>""" <EOL> newGraph = CsArrayGraph ( self . vList , self . undirected ) <EOL> newGraph . W = sppy . ones ( ( newGraph . W . shape ) ) <EOL> newGraph . W [ self . W . nonzero ( ) ] = <NUM_LIT:0> <EOL> newGraph . W . prune ( ) <EOL> newGraph . W . compress ( ) <EOL> return newGraph <EOL> def outDegreeSequence ( self ) : <EOL> """<STR_LIT>""" <EOL> degrees = numpy . zeros ( self . W . shape [ <NUM_LIT:0> ] , dtype = numpy . int32 ) <EOL> for i in range ( <NUM_LIT:0> , self . W . shape [ <NUM_LIT:0> ] ) : <EOL> degrees [ i ] = self . W [ i , : ] . getnnz ( ) <EOL> return degrees <EOL> def inDegreeSequence ( self ) : <EOL> """<STR_LIT>""" <EOL> degrees = numpy . zeros ( self . W . shape [ <NUM_LIT:0> ] , dtype = numpy . int32 ) <EOL> for i in range ( <NUM_LIT:0> , self . W . shape [ <NUM_LIT:0> ] ) : <EOL> degrees [ i ] = self . W [ : , i ] . getnnz ( ) <EOL> return degrees <EOL> def subgraph ( self , vertexIndices ) : <EOL> """<STR_LIT>""" <EOL> Parameter . checkList ( vertexIndices , Parameter . checkIndex , ( <NUM_LIT:0> , self . getNumVertices ( ) ) ) <EOL> vertexIndices = numpy . unique ( numpy . array ( vertexIndices ) ) . tolist ( ) <EOL> vList = self . vList . subList ( vertexIndices ) <EOL> subGraph = CsArrayGraph ( vList , self . undirected , self . W . dtype ) <EOL> subGraph . W = self . W [ vertexIndices , : ] [ : , vertexIndices ] <EOL> return subGraph <EOL> def add ( self , graph ) : <EOL> """<STR_LIT>""" <EOL> Parameter . checkClass ( graph , CsArrayGraph ) <EOL> if graph . getNumVertices ( ) != self . getNumVertices ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> newGraph = CsArrayGraph ( self . vList , self . undirected ) <EOL> newGraph . W = self . W + graph . W <EOL> return newGraph <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> graph = CsArrayGraph ( self . vList . copy ( ) , self . undirected , self . W . dtype ) <EOL> graph . W = self . W . copy ( ) <EOL> return graph <EOL> def multiply ( self , graph ) : <EOL> """<STR_LIT>""" <EOL> Parameter . checkClass ( graph , CsArrayGraph ) <EOL> if graph . getNumVertices ( ) != self . getNumVertices ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> newGraph = CsArrayGraph ( self . vList , self . undirected ) <EOL> newGraph . W = self . W . hadamard ( graph . W ) <EOL> return newGraph <EOL> def intersect ( self , graph ) : <EOL> """<STR_LIT>""" <EOL> newGraph = self . multiply ( graph ) <EOL> newGraph . W [ newGraph . W . nonzero ( ) ] = <NUM_LIT:1> <EOL> return newGraph <EOL> def union ( self , graph ) : <EOL> """<STR_LIT>""" <EOL> newGraph = self . add ( graph ) <EOL> newGraph . W [ newGraph . W . nonzero ( ) ] = <NUM_LIT:1> <EOL> return newGraph <EOL> def weightMatrixDType ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . W . dtype <EOL> def setDiff ( self , graph ) : <EOL> """<STR_LIT>""" <EOL> Parameter . checkClass ( graph , CsArrayGraph ) <EOL> if graph . getNumVertices ( ) != self . getNumVertices ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . undirected != graph . undirected : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> A1 = self . adjacencyMatrix ( ) <EOL> A2 = graph . adjacencyMatrix ( ) <EOL> A1 = A1 - A2 <EOL> A1 = ( A1 + numpy . abs ( A1 ** <NUM_LIT:2> ) ) / <NUM_LIT:2> <EOL> newGraph = CsArrayGraph ( self . vList , self . undirected ) <EOL> newGraph . W = sppy . csarray ( A1 ) <EOL> return newGraph <EOL> def getAllDirEdges ( self ) : <EOL> """<STR_LIT>""" <EOL> ( rows , cols ) = numpy . nonzero ( self . W ) <EOL> edges = numpy . c_ [ rows , cols ] <EOL> return edges <EOL> @ staticmethod <EOL> def loadMatrix ( filename ) : <EOL> M = scipy . io . mmread ( filename ) <EOL> if type ( M ) == numpy . ndarray : <EOL> M2 = sppy . csarray ( M ) <EOL> elif scipy . sparse . issparse ( M ) : <EOL> M2 = sppy . csarray ( M . shape , dtype = M . dtype ) <EOL> M2 [ M . nonzero ( ) ] = M . data <EOL> return M2 <EOL> def saveMatrix ( self , W , filename ) : <EOL> W = W . toScipyCsc ( ) <EOL> scipy . io . mmwrite ( filename , W ) <EOL> def setWeightMatrix ( self , W ) : <EOL> """<STR_LIT>""" <EOL> if W . shape != ( self . vList . getNumVertices ( ) , self . vList . getNumVertices ( ) ) : <EOL> raise ValueError ( "<STR_LIT>" + str ( W . shape ) ) <EOL> if self . undirected and type ( W ) == numpy . ndarray and ( W != W . T ) . any ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . undirected and scipy . sparse . issparse ( W ) and not SparseUtils . equals ( W , W . T ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if scipy . sparse . issparse ( W ) : <EOL> W = W . todense ( ) <EOL> self . W = numpy . array ( W ) <EOL> def removeAllEdges ( self ) : <EOL> """<STR_LIT>""" <EOL> self . W . setZero ( ) <EOL> def setWeightMatrixSparse ( self , W ) : <EOL> """<STR_LIT>""" <EOL> self . W [ W . nonzero ( ) ] = W . data <EOL> def addVertices ( self , n ) : <EOL> """<STR_LIT>""" <EOL> W2 = sppy . csarray ( ( self . W . shape [ <NUM_LIT:0> ] + n , self . W . shape [ <NUM_LIT:0> ] + n ) , self . W . dtype ) <EOL> W2 [ self . W . nonzero ( ) ] = self . W . values ( ) <EOL> self . W = W2 <EOL> self . vList . addVertices ( n ) <EOL> undirected = None <EOL> vList = None <EOL> W = None </s>
