~ubuntu-branches/ubuntu/trusty/python-networkx/trusty

« back to all changes in this revision

Viewing changes to networkx/tests/convert_scipy.txt

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2009-11-23 15:44:34 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20091123154434-ellm2ut3a4edf9wh
Tags: 1.0~rc1~svn1492-1
* New upstream snapshot, past 1.0~rc1, as requested by Yaroslav
  Halchenko (Closes: #549996).
* Refresh patch accordingly:
   + debian/patches/10_doc_relocation.
* Get rid of extra LICENSE.txt file in /usr/share/doc.
* Use dh_compress -Xexamples/ to avoid compressing examples, thanks to
  Sandro Tosi (Closes: #539942).
* Bump Standards-Version from 3.8.0 to 3.8.3 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Convert
2
 
=======
3
 
 
4
 
>>> import os,tempfile
5
 
>>> from networkx import *
6
 
>>> from networkx.convert import *
7
 
>>> from networkx.operators import *
8
 
>>> from networkx.generators.classic import barbell_graph,cycle_graph
9
 
>>> import scipy
10
 
>>> import scipy.sparse
11
 
 
12
 
Bad shape
13
 
---------
14
 
 
15
 
>>> A=scipy.sparse.lil_matrix([[1,2,3],[4,5,6]])
16
 
>>> from_scipy_sparse_matrix(A)
17
 
Traceback (most recent call last):
18
 
...
19
 
NetworkXError: Adjacency matrix is not square. nx,ny=(2, 3)
20
 
 
21
 
Simple Graphs
22
 
--------------
23
 
 
24
 
>>> G=barbell_graph(10,3)
25
 
 
26
 
Scipy sparse matrix
27
 
~~~~~~~~~~~~~~~~~~~
28
 
 
29
 
>>> A=to_scipy_sparse_matrix(G)
30
 
>>> GG=from_scipy_sparse_matrix(A)
31
 
>>> sorted(G.nodes())==sorted(GG.nodes())
32
 
True
33
 
>>> sorted(G.edges())==sorted(GG.edges())
34
 
True
35
 
>>> GW=from_whatever(A)
36
 
>>> sorted(G.nodes())==sorted(GW.nodes())
37
 
True
38
 
>>> sorted(G.edges())==sorted(GW.edges())
39
 
True
40
 
>>> GI=Graph(A)
41
 
>>> sorted(G.nodes())==sorted(GI.nodes())
42
 
True
43
 
>>> sorted(G.edges())==sorted(GI.edges())
44
 
True
45
 
>>> ACSR=A.tocsr()
46
 
>>> GI=Graph(ACSR)
47
 
>>> sorted(G.nodes())==sorted(GI.nodes())
48
 
True
49
 
>>> sorted(G.edges())==sorted(GI.edges())
50
 
True
51
 
>>> ACOO=A.tocoo()
52
 
>>> GI=Graph(ACOO)
53
 
>>> sorted(G.nodes())==sorted(GI.nodes())
54
 
True
55
 
>>> sorted(G.edges())==sorted(GI.edges())
56
 
True
57
 
>>> ACSC=A.tocsc()
58
 
>>> GI=Graph(ACSC)
59
 
>>> sorted(G.nodes())==sorted(GI.nodes())
60
 
True
61
 
>>> sorted(G.edges())==sorted(GI.edges())
62
 
True
63
 
>>> AD=A.todense()
64
 
>>> GI=Graph(AD)
65
 
>>> sorted(G.nodes())==sorted(GI.nodes())
66
 
True
67
 
>>> sorted(G.edges())==sorted(GI.edges())
68
 
True
69
 
>>> AA=A.toarray()
70
 
>>> GI=Graph(AA)
71
 
>>> sorted(G.nodes())==sorted(GI.nodes())
72
 
True
73
 
>>> sorted(G.edges())==sorted(GI.edges())
74
 
True
75
 
 
76
 
DiGraphs
77
 
--------
78
 
 
79
 
>>> G=cycle_graph(10,create_using=DiGraph())
80
 
 
81
 
Scipy sparse matrix
82
 
~~~~~~~~~~~~~~~~~~~
83
 
 
84
 
>>> A=to_scipy_sparse_matrix(G)
85
 
>>> GG=from_scipy_sparse_matrix(A,create_using=DiGraph())
86
 
>>> sorted(G.nodes())==sorted(GG.nodes())
87
 
True
88
 
>>> sorted(G.edges())==sorted(GG.edges())
89
 
True
90
 
>>> GW=from_whatever(A,create_using=DiGraph())
91
 
>>> sorted(G.nodes())==sorted(GW.nodes())
92
 
True
93
 
>>> sorted(G.edges())==sorted(GW.edges())
94
 
True
95
 
>>> GI=DiGraph(A)
96
 
>>> sorted(G.nodes())==sorted(GI.nodes())
97
 
True
98
 
>>> sorted(G.edges())==sorted(GI.edges())
99
 
True
100
 
 
101
 
 
102
 
Graph
103
 
------
104
 
 
105
 
>>> G=cycle_graph(4)
106
 
>>> e=G.edges()
107
 
>>> source=[u for u,v in e]
108
 
>>> dest=[v for u,v in e]
109
 
>>> weight=[s+10 for s in source]
110
 
>>> ex=zip(source,dest,weight)
111
 
>>> XG=Graph()
112
 
>>> XG.add_edges_from(ex)
113
 
 
114
 
 
115
 
Numpy matrix
116
 
~~~~~~~~~~~~
117
 
 
118
 
>>> A=to_scipy_sparse_matrix(XG)
119
 
>>> GG=from_scipy_sparse_matrix(A,create_using=Graph())
120
 
>>> sorted(XG.nodes())==sorted(GG.nodes())
121
 
True
122
 
>>> sorted(XG.edges())==sorted(GG.edges())
123
 
True
124
 
>>> GW=from_whatever(A,create_using=Graph())
125
 
>>> sorted(XG.nodes())==sorted(GW.nodes())
126
 
True
127
 
>>> sorted(XG.edges())==sorted(GW.edges())
128
 
True
129
 
>>> GI=Graph(A)
130
 
>>> sorted(XG.nodes())==sorted(GI.nodes())
131
 
True
132
 
>>> sorted(XG.edges())==sorted(GI.edges())
133
 
True
134
 
 
135
 
 
136
 
DiGraph
137
 
------
138
 
 
139
 
>>> G=cycle_graph(4)
140
 
>>> e=G.edges()
141
 
>>> source=[u for u,v in e]
142
 
>>> dest=[v for u,v in e]
143
 
>>> weight=[s+10 for s in source]
144
 
>>> ex=zip(source,dest,weight)
145
 
>>> XG=DiGraph()
146
 
>>> XG.add_edges_from(ex)
147
 
 
148
 
 
149
 
Scipy sparse matrix
150
 
~~~~~~~~~~~~~~~~~~~
151
 
 
152
 
>>> A=to_scipy_sparse_matrix(XG)
153
 
>>> GG=from_scipy_sparse_matrix(A,create_using=DiGraph())
154
 
>>> sorted(XG.nodes())==sorted(GG.nodes())
155
 
True
156
 
>>> sorted(XG.edges())==sorted(GG.edges())
157
 
True
158
 
>>> GW=from_whatever(A,create_using=DiGraph())
159
 
>>> sorted(XG.nodes())==sorted(GW.nodes())
160
 
True
161
 
>>> sorted(XG.edges())==sorted(GW.edges())
162
 
True
163
 
>>> GI=DiGraph(A)
164
 
>>> sorted(XG.nodes())==sorted(GI.nodes())
165
 
True
166
 
>>> sorted(XG.edges())==sorted(GI.edges())
167
 
True
168
 
 
169
 
 
170
 
With nodelist keyword
171
 
---------------------
172
 
 
173
 
>>> P4=path_graph(4)
174
 
>>> P3=path_graph(3)
175
 
>>> A=to_scipy_sparse_matrix(P4,nodelist=[0,1,2])
176
 
>>> GA=Graph(A)
177
 
>>> sorted(GA.nodes())==sorted(P3.nodes())
178
 
True
179
 
>>> sorted(GA.edges())==sorted(P3.edges())
180
 
True