Function iaadjmxtcreate

Description

Create a graph representation from an Adjacency matrix. This is an n by n matrix A, where n is the number of vertices in the graph. If there is an edge from a vertex x to a vertex y, then the element A(x,y) is different of 0. An extension to the Adjacency matrix is to vale its element to represent the arc weight (different from 0). There is an option to visualize the arc weight. The nodes are numbered sequentially starting from zero.

Synopse

Create a graph representation from an Adjacency matrix to show using graphviz.

  • g = iaadjmxtcreate(A,label_vector=0,dist=False, label=False, mst_id = False,seeds_id = False)
    • A: Input Adjacency or weight matrix.
    • label_vector: Optional input: label of each node.
    • dist: Optional input: Display the arc weight.
    • label: Optional input: Display the label of each node.
    • mst_id: Optional input: Display MST.
    • seeds_id: Optional input: Display the seeds or prototypes.
    • g: Output graph representation suitable to display using graphviz.

Limitation

The arc weight display works only for symmetric weight matrix.

Function Code

 1 import StringIO
 2 from numpy import *
 3 
 4 def iaadjmxtcreate(A,label_vector=0,dist=False, label=False, mst_id = False,seeds_id = False):
 5 
 6     from iamstseeds import iamstseeds
 7     from iaprim import iaprim
 8     from iaprototype import iaprototype
 9     import gvgen
10     graph = gvgen.GvGen()
11     graph.smart_mode = 1
12 
13     if mst_id:
14         MST = iaprim(A)
15 
16     # node array create
17     node = []
18     for i in range(A.shape[0]):
19         node.append (graph.newItem(str(i)))
20 
21     if label:
22         for i in range(A.shape[0]):
23             if label_vector[i]==0:
24                 graph.styleAppend("Post_0", "color", "blue")
25                 graph.styleAppend("Post_0", "style", "filled")
26                 graph.styleApply("Post_0", node[i])
27             else:
28                 graph.styleAppend("Post_1", "color", "red")
29                 graph.styleAppend("Post_1", "style", "filled")
30                 graph.styleApply("Post_1", node[i])
31 
32 
33     if seeds_id:
34         seeds = array(iamstseeds(MST,label_vector))
35         for i in range(seeds.shape[0]):
36             if label_vector[seeds[i]]==0:
37                 graph.styleAppend("Post_2", "color", "blue")
38                 graph.styleAppend("Post_2", "style", "filled")
39                 graph.styleAppend("Post_2", "shape", "rectangle")
40                 graph.styleApply("Post_2", node[seeds[i]])
41             else:
42                 graph.styleAppend("Post_3", "color", "red")
43                 graph.styleAppend("Post_3", "style", "filled")
44                 graph.styleAppend("Post_3", "shape", "rectangle")
45                 graph.styleApply("Post_3", node[seeds[i]])
46 
47     ## check if is a symmetric matrix
48 
49     symmetric = (A.transpose() == A).all()
50 
51     for i in range(A.shape[0]):
52         if symmetric:
53             indice = i+1
54         else:
55             indice = A.shape[1]
56         for j in range(indice):
57             if A[j,i]!=float('Inf'):
58                 if dist:
59                     link1 = graph.newLink(node[i],node[j], A[j,i])
60                 else:
61                     link1 = graph.newLink(node[i],node[j])
62                 if symmetric:
63                     graph.propertyAppend(link1, "dir", "none")
64                 if mst_id:
65                     if MST[j,i]==float('Inf'):
66                         graph.propertyAppend(link1, "style","dashed")
67 
68 
69     fd = StringIO.StringIO()
70     graph.dot(fd)
71     dottext = fd.getvalue()
72 
73     return dottext

Graphic Representation of a Adjacency Matrix Example

 1 from numpy import *
 2 from iaadjmxtcreate import iaadjmxtcreate
 3 from iaadjmxtcreate2 import iaadjmxtcreate2
 4 from iaOPF import *
 5 from iaprototype import iaprototype
 6 
 7 A = array([[0,1,3,4],
 8            [1,0,2,5],
 9            [3,2,0,6],
10            [4,5,6,0]])
11 
12 label_vector = array([0,0,0,1])
13 
14 mmgraphviz(iaadjmxtcreate(A,0,True), title='Input adjacent matrix')
15 B = iaprim(A)
16 print 'MST \n',B
17 mmgraphviz(iaadjmxtcreate(B,0,True), title='MST')
18 mmgraphviz(iaadjmxtcreate(A,0,True,False,True), title='MST visualization on Complete Graph')
19 
20 C = iaprototype(A,label_vector)
21 print 'list of prototypes \n',C
22 
23 mmgraphviz(iaadjmxtcreate(A,label_vector,False,True), title='Labels Representation')
24 mmgraphviz(iaadjmxtcreate(A,label_vector,False,False,True,True), title='Prototypes Representation')
25 #iaadjmxtcreate(A,label_vector=0,dist=False, label=False, mst_id = False,seeds_id = False)
MST 
[[ Inf   1.  Inf   4.]
 [  1.  Inf   2.  Inf]
 [ Inf   2.  Inf  Inf]
 [  4.  Inf  Inf  Inf]]
list of prototypes 
[1 1 1 1]
/media/_xsb/iaOPF/iaadjmxtcreate/GRVIZ45325_001.png

Input adjacent matrix

/media/_xsb/iaOPF/iaadjmxtcreate/GRVIZ45325_002.png

MST

/media/_xsb/iaOPF/iaadjmxtcreate/GRVIZ45325_003.png

MST visualization on Complete Graph

/media/_xsb/iaOPF/iaadjmxtcreate/GRVIZ45325_004.png

Labels Representation

/media/_xsb/iaOPF/iaadjmxtcreate/GRVIZ45325_005.png

Prototypes Representation