def testConcat(self):
        numVertices = 5
        graph = SparseGraph(GeneralVertexList(numVertices))
        graph.addEdge(1, 1, 0.1)
        graph.addEdge(1, 3, 0.5)
        graph.addEdge(2, 4, 1)
        graph.addEdge(2, 3, 2)
        graph.setVertex(0, "abc")

        graph2 = SparseGraph(GeneralVertexList(numVertices))
        graph2.addEdge(1, 1)
        graph2.addEdge(1, 4)
        graph2.setVertex(1, "def")

        graph3 = graph.concat(graph2)

        self.assertTrue(graph3.getNumVertices, 10)
        self.assertEquals(graph3.getVertex(0), "abc")
        self.assertEquals(graph3.getVertex(6), "def")
        self.assertEquals(graph3.getEdge(1, 1), 0.1)
        self.assertEquals(graph3.getEdge(1, 3), 0.5)
        self.assertEquals(graph3.getEdge(2, 4), 1)
        self.assertEquals(graph3.getEdge(2, 3), 2)

        self.assertEquals(graph3.getEdge(6, 6), 1)
        self.assertEquals(graph3.getEdge(6, 9), 1)
    def testConcat(self):
        numVertices = 5
        graph = SparseGraph(GeneralVertexList(numVertices))
        graph.addEdge(1, 1, 0.1)
        graph.addEdge(1, 3, 0.5)
        graph.addEdge(2, 4, 1)
        graph.addEdge(2, 3, 2)
        graph.setVertex(0, "abc")

        graph2 = SparseGraph(GeneralVertexList(numVertices))
        graph2.addEdge(1, 1)
        graph2.addEdge(1, 4)
        graph2.setVertex(1, "def")

        graph3 = graph.concat(graph2)

        self.assertTrue(graph3.getNumVertices, 10)
        self.assertEquals(graph3.getVertex(0), "abc")
        self.assertEquals(graph3.getVertex(6), "def")
        self.assertEquals(graph3.getEdge(1, 1), 0.1)
        self.assertEquals(graph3.getEdge(1, 3), 0.5)
        self.assertEquals(graph3.getEdge(2, 4), 1)
        self.assertEquals(graph3.getEdge(2, 3), 2)

        self.assertEquals(graph3.getEdge(6, 6), 1)
        self.assertEquals(graph3.getEdge(6, 9), 1)
Beispiel #3
0
 def graphFromMatFile(matFileName):
     """
     Generate a sparse graph from a Matlab file of ego and alters and their transmissions. This is a mostly 
     disconnected graph made up of pairs of connected vertices, i.e each vertex has degree 1.  
     """
     examplesList = ExamplesList.readFromMatFile(matFileName)
     numExamples = examplesList.getNumExamples()
     numFeatures = examplesList.getDataFieldSize("X", 1)
     numVertexFeatures = numFeatures/2+1
     vList = VertexList(numExamples*2, int(numVertexFeatures))
     sGraph = SparseGraph(vList)
     
     for i in range(0, examplesList.getNumExamples()): 
         v1Index = i*2 
         v2Index = i*2+1
         example = examplesList.getSubDataField("X", numpy.array([i])).ravel()
         vertex1 = numpy.r_[example[0:numFeatures/2], numpy.array([1])]
         vertex2 = numpy.r_[example[numFeatures/2:numFeatures], numpy.array([0])]
         
         sGraph.setVertex(v1Index, vertex1)
         sGraph.setVertex(v2Index, vertex2)
         sGraph.addEdge(v1Index, v2Index)
     
     return sGraph 
Beispiel #4
0
# -*- coding: utf-8 -*-
import scipy.sparse as sps
from apgl.graph.GeneralVertexList import GeneralVertexList
from apgl.graph.SparseGraph import SparseGraph

numVertices = 10
vList = GeneralVertexList(numVertices)

Wght = sps.lil_matrix((numVertices, numVertices))
graph = SparseGraph(vList, W=Wght, undirected=False)

# Add some edges to the graph.
# Vertices are indexed starting from 0.
graph[0, 1] = 1
graph[0, 2] = 1

# Set the label of the 0th vertex to [2, 3].
graph.setVertex(0, "abc")
graph.setVertex(1, 123)

print(graph.inDegreeDistribution())
Data: December, 2016
function:
"""

import numpy

from apgl.graph.VertexList import VertexList
from apgl.graph.SparseGraph import SparseGraph

numVertex = 5
numFeature = 2  # vector labels of size 2
graph = SparseGraph(VertexList(numVertex, numFeature))

# Add some edges to the graph(method 1)
# Vertices are indexed starting from 0
# graph[0, 1] = 0.1
# graph[1, 2] = 1

# Add some edges to the graph(method 2 is identity to method 1)
edges = numpy.array([[0, 1], [1, 2]], numpy.int)
edgeValues = numpy.array([0.1, 1])
graph.addEdges(edges, edgeValues)

# Set the label of 0th vertex to [2, 3]
graph.setVertex(0, numpy.array([2, 3]))

# Displays edge weights
print(graph[1, 2])
print(graph[1, 3])

"""
Name: Generate Graph:
Author: Jia_qiu Wang(王佳秋)
Data: December, 2016
function:
"""

import numpy
import scipy.sparse as sps
from apgl.graph.GeneralVertexList import GeneralVertexList
from apgl.graph.SparseGraph import SparseGraph

numVertices = 10
vList = GeneralVertexList(numVertices)
wght = sps.csc_matrix(numVertices, numVertices)
graph = SparseGraph(vList, W=wght, undirected=False)

graph[0, 1] = 1
graph[0, 2] = 1
graph.setVertex(0, "abc")
graph.setVertex(1, 123)

print(graph)
Beispiel #7
0
#!/usr/bin/env python
import numpy as np
from apgl.graph.VertexList import VertexList
from apgl.graph.SparseGraph import SparseGraph
numVertices = 5
numFeatures = 2
graph = SparseGraph(VertexList(numVertices, numFeatures))

# Add some edges to the graph.
# Vertices are indexed starting from 0.
graph[0, 1] = 0.1
graph[1, 2] = 1.0

# Set the label of the 0th vertex to [2, 3].
graph.setVertex(0, np.array([2, 3]))

# Display edge weights.
print(graph[1, 2]) # 1.0
print(graph[1, 3]) # 0.0 (default)

# Add some edges to the graph.
edges = np.array([[0, 1], [1, 2]], np.int)
edgeValues = np.array([0.1, 1.0])
graph.addEdges(edges, edgeValues)

# Display the edge weight between v1 and v2.
print(graph[1, 2])