-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxnet.py
432 lines (340 loc) · 12.3 KB
/
xnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/python
# -*- coding: <utf-8> -*-
from igraph import Graph;
import io;
import numpy as np;
import re;
import sys
def __textSplit2(text):
entries = text.split()
if(len(entries)<2):
raise ValueError();
return (float(entries[0]),float(entries[1]));
def __textSplit3(text):
entries = text.split()
if(len(entries)<3):
raise ValueError();
return (float(entries[0]),float(entries[1]),float(entries[2]));
def __readNumberIgnoringNone(value):
if(value.lower()=="none"):
return 0;
else:
return float(value);
__propertyHeaderRegular = re.compile("#([ve]) \"(.+)\" ([sn]|v2|v3)");
__propertyFunctions = {
"s": str,
"n": __readNumberIgnoringNone,
"v2": __textSplit2,
"v3": __textSplit3
}
def __readXnetVerticesHeader(fp,currentLineIndex,lastLine=""):
headerLine = lastLine;
if(len(headerLine)==0):
for lineData in fp:
headerLine = lineData.decode("utf-8").rstrip();
currentLineIndex+=1;
if(len(headerLine)>0):
break;
headerEntries = headerLine.split();
nodeCount = 0;
if(len(headerEntries)==0 or headerEntries[0].lower() != "#vertices"):
raise ValueError("Malformed xnet file [%s:%d]\n\t>%s"%(fp.name,currentLineIndex,headerLine));
try:
nodeCount = int(headerEntries[1]);
except ValueError:
raise ValueError("Malformed xnet file [%s:%d]\n\t>%s"%(fp.name,currentLineIndex,headerLine));
return (nodeCount,currentLineIndex,"");
def __readXnetNames(fp,currentLineIndex,lastLine=""):
names = [];
fileEnded = True;
for lineData in fp:
lineString = lineData.decode("utf-8").rstrip();
currentLineIndex+=1;
if(len(lineString)==0):
continue;
lastLine = lineString;
if(len(lineString)>0 and lineString[0]=="#"):
fileEnded = False;
break;
if(len(lineString)>1 and lineString[0]=="\"" and lineString[-1]=="\""):
lineString = lineString[1:-1];
names.append(lineString);
if(fileEnded):
lastLine = "";
return (names,currentLineIndex,lastLine);
def __readXnetEdgesHeader(fp,currentLineIndex,lastLine=""):
headerLine = lastLine;
if(len(headerLine)==0):
for lineData in fp:
headerLine = lineData.decode("utf-8").rstrip();
currentLineIndex+=1;
if(len(headerLine)>0):
break;
headerEntries = headerLine.split();
weighted = False;
directed = False;
if(len(headerEntries)==0 or headerEntries[0].lower() != "#edges"):
raise ValueError("Malformed xnet file [%s:%d]\n\t>%s"%(fp.name,currentLineIndex,headerLine));
for headerEntry in headerEntries:
if(headerEntry == "weighted"):
weighted = True;
if(headerEntry == "nonweighted"):
weighted = False;
if(headerEntry == "directed"):
directed = True;
if(headerEntry == "undirected"):
directed = False;
return ((weighted,directed),currentLineIndex,"");
def __readXnetEdges(fp,currentLineIndex,lastLine=""):
edges = [];
weights = [];
fileEnded = True;
for lineData in fp:
lineString = lineData.decode("utf-8").rstrip();
currentLineIndex+=1;
if(len(lineString)==0):
continue;
lastLine = lineString;
if(len(lineString)>0 and lineString[0]=="#"):
fileEnded=False;
break;
entries = lineString.split();
if(len(entries)<2):
raise ValueError("Malformed xnet file [%s:%d]\n\t>%s"%(fp.name,currentLineIndex,headerLine));
try:
edge = (int(entries[0]),int(entries[1]));
weight = 1.0;
if(len(entries)>2):
weight = float(entries[2]);
edges.append(edge);
weights.append(weight);
except ValueError:
raise ValueError("Malformed xnet file [%s:%d]\n\t>%s"%(fp.name,currentLineIndex,headerLine));
if(fileEnded):
lastLine = "";
return ((edges,weights),currentLineIndex,lastLine);
def __readXnetPropertyHeader(fp,currentLineIndex,lastLine=""):
global __propertyHeaderRegular;
headerLine = lastLine;
if(len(headerLine)==0):
for lineData in fp:
headerLine = lineData.decode("utf-8").rstrip();
currentLineIndex+=1;
if(len(headerLine)>0):
break;
headerEntries = __propertyHeaderRegular.findall(headerLine);
if(len(headerEntries)==0 or (len(headerEntries)==1 and len(headerEntries[0])!=3)):
raise ValueError("Malformed xnet file [%s:%d]\n\t>%s"%(fp.name,currentLineIndex,headerLine));
(propertyType,propertyName,propertyFormat) = headerEntries[0];
return ((propertyType,propertyName,propertyFormat),currentLineIndex,"");
def __readXnetProperty(fp,propertyFormat,currentLineIndex,lastLine=""):
global __propertyFunctions;
properties = [];
propertyFunction = __propertyFunctions[propertyFormat];
fileEnded = True;
for lineData in fp:
lineString = lineData.decode("utf-8").rstrip();
currentLineIndex+=1;
if(len(lineString)==0):
continue;
lastLine = lineString;
if(len(lineString)>0 and lineString[0]=="#"):
fileEnded = False;
break;
if(len(lineString)>1 and lineString[0]=="\"" and lineString[-1]=="\""):
lineString = lineString[1:-1];
try:
properties.append(propertyFunction(lineString));
except ValueError:
raise ValueError("Malformed xnet file [%s:%d]\n\t>%s"%(fp.name,currentLineIndex,lineString));
if(fileEnded):
lastLine = "";
return (properties,currentLineIndex,lastLine);
def xnet2igraph(fileName='test.xnet'):
"""
Read a Graph from a xnet formatted file.
Parameters
----------
fileName : string
Input file path.
"""
network = None;
with open(fileName, 'rb') as fp:
currentLineIndex = 0;
lastLine = "";
(nodeCount,currentLineIndex,lastLine) = __readXnetVerticesHeader(fp,currentLineIndex,lastLine);
(names,currentLineIndex,lastLine) = __readXnetNames(fp,currentLineIndex,lastLine);
if(len(names)>0 and len(names)<nodeCount):
raise ValueError("Malformed xnet file [%s:%d]\n\t>%s [%d entries expected but only %d found]"%(fp.name,currentLineIndex,headerLine,nodeCount,len(names)));
((weighted,directed),currentLineIndex,lastLine) = __readXnetEdgesHeader(fp,currentLineIndex,lastLine);
((edges,weights),currentLineIndex,lastLine) = __readXnetEdges(fp,currentLineIndex,lastLine);
network = Graph(nodeCount,edges=edges,directed=directed);
if(len(names)>0):
network.vs["name"] = names;
if(weighted):
network.es["weight"] = weights;
while(lastLine!=""):
((propertyType,propertyName,propertyFormat),currentLineIndex,lastLine) = __readXnetPropertyHeader(fp,currentLineIndex,lastLine);
(properties,currentLineIndex,lastLine) = __readXnetProperty(fp,propertyFormat,currentLineIndex,lastLine);
if(propertyType=="e"):
network.es[propertyName] = properties;
elif(propertyType=="v"):
network.vs[propertyName] = properties;
return network;
def igraph2xnet(g,fileName='test.xnet',ignoredNodeAtts=[],ignoredEdgeAtts=[]):
"""
Write igraph object to .xnet format.
Vertex attributes 'name' and 'weight' are treated in a special manner. They
correspond to attributes assigned inside the #vertices tag.
Edge attribute 'weight' is assigned to edges inside the #edges tag.
Parameters
----------
g : igraph.Graph
Input graph.
fileName : string
Output file.
ignoredNodeAtts : list
List of node attributes to ignore when writing graph.
ignoredEdgeAtts : list
List of edge attributes to ignore when writing graph.
"""
N = g.vcount()
E = g.ecount()
nodesAtt = g.vs.attributes()
edgesAtt = g.es.attributes()
if ('weight' in nodesAtt) and ('weight' not in ignoredNodeAtts):
isNodeWeighted = True
isNodeWeightedString = 'weighted'
else:
isNodeWeighted = False
isNodeWeightedString = 'nonweighted'
if ('name' in nodesAtt) and ('name' not in ignoredNodeAtts):
isNodeNamed = True
else:
isNodeNamed = False
fd = open(fileName,'w')
fd.write('#vertices '+str(N)+' '+isNodeWeightedString+'\n')
if isNodeNamed and isNodeWeighted:
for i in range(N):
fd.write('\"'+g.vs[i]['name']+'\" '+str(g.vs[i]['weight'])+'\n')
elif isNodeNamed and (not isNodeWeighted):
for i in range(N):
fd.write('\"'+g.vs[i]['name']+'\"'+'\n')
elif (not isNodeNamed) and isNodeWeighted:
for i in range(N):
fd.write(str(g.vs[i]['weight'])+'\n')
if ('weight' in edgesAtt) and ('weight' not in ignoredEdgeAtts):
isEdgeWeighted = True
isEdgeWeightedString = 'weighted'
else:
isEdgeWeighted = False
isEdgeWeightedString = 'nonweighted'
if g.is_directed()==True:
isEdgeDirected = True
isEdgeDirectedString = 'directed'
else:
isEdgeDirected = False
isEdgeDirectedString = 'undirected'
fd.write('#edges '+isEdgeWeightedString+' '+isEdgeDirectedString+'\n')
for i in range(E):
edge = g.es[i].tuple
if isEdgeWeighted:
fd.write(str(edge[0])+' '+str(edge[1])+' '+str(g.es[i]['weight'])+'\n')
else:
fd.write(str(edge[0])+' '+str(edge[1])+'\n')
if isNodeWeighted:
nodesAtt.remove('weight')
if isNodeNamed:
nodesAtt.remove('name')
if isEdgeWeighted:
edgesAtt.remove('weight')
nodesAtt.sort()
edgesAtt.sort()
isPython2 = sys.version_info[0] == 2
if isPython2:
stringType = basestring
else:
stringType = str
for att in nodesAtt:
if att not in ignoredNodeAtts:
sample = g.vs[0][att]
typeSample = type(sample)
if isinstance(sample, stringType):
typeSampleString = 's'
elif np.isscalar(sample)==True:
typeSampleString = 'n'
elif (typeSample==list) or (typeSample==tuple) or (typeSample==np.ndarray):
if len(sample)==2:
typeSampleString = 'v2'
elif len(sample)==3:
typeSampleString = 'v3'
fd.write('#v \"'+att+'\" '+typeSampleString+'\n')
for i in range(N):
if typeSampleString == 'n':
fd.write(str(g.vs[i][att])+'\n')
elif typeSampleString == 'v2':
fd.write(str(g.vs[i][att][0])+' '+str(g.vs[i][att][1])+'\n')
elif typeSampleString == 'v3':
fd.write(str(g.vs[i][att][0])+' '+str(g.vs[i][att][1])+' '+str(g.vs[i][att][2])+'\n')
elif typeSampleString == 's':
fd.write('\"'+g.vs[i][att]+'\"'+'\n')
for att in edgesAtt:
if att not in ignoredEdgeAtts:
sample = g.es[0][att]
typeSample = type(sample)
if isinstance(sample, stringType):
typeSampleString = 's'
elif np.isscalar(sample)==True:
typeSampleString = 'n'
elif (typeSample==list) or (typeSample==tuple) or (typeSample==np.ndarray):
if len(sample)==2:
typeSampleString = 'v2'
elif len(sample)==3:
typeSampleString = 'v3'
fd.write('#e \"'+att+'\" '+typeSampleString+'\n')
for i in range(E):
if typeSampleString == 'n':
fd.write(str(g.es[i][att])+'\n')
elif typeSampleString == 'v2':
fd.write(str(g.es[i][att][0])+' '+str(g.es[i][att][1])+'\n')
elif typeSampleString == 'v3':
fd.write(str(g.es[i][att][0])+' '+str(g.es[i][att][1])+' '+str(g.es[i][att][2])+'\n')
elif typeSampleString == 's':
fd.write('\"'+g.es[i][att]+'\"'+'\n')
fd.close()
def igraph2xnet_fast(g):
"""
Convert igraph object to a .xnet format string. This string
can then be written to a file or used in a pipe. The purpose
of this function is to have a fast means to convert a graph
without any attributes to the .xnet format.
Parameters
----------
g : igraph.Graph
Input graph.
Returns
-------
strOut : string
String that can be written to a .xnet file.
"""
N = g.vcount()
strOut = "#vertices "+str(N)+" nonweighted\n#edges nonweighted undirected"
edList = np.array(g.get_edgelist())
s = io.BytesIO()
np.savetxt(s, edList, fmt='%d', header=strOut, comments='')
strOut = s.getvalue()
s.close()
return strOut
# Simple test if running as script
if __name__ == '__main__':
g = Graph(edges=[[0,1],[0,2],[1,2],[2,3],[3,4],[1,5],[1,6]],directed=False)
g.vs['name'] = ['Node 0','Node 1','Node 2','Node 3','Node 4','Node 5','Node 6']
g.vs['weight'] = [1.,4.,2.,0.,1.,5.,3.]
g.vs['community'] = [0,0,0,1,1,2,3]
g.vs['pos'] = [[1.2,3.],[0.2,0.1],[0.4,0.6],[2.3,5.5],[2.4,0.4],[10.2,3.],[5.,5.]]
g.vs['type'] = ['Crazy','Crazy','Normal','Sausage','Cat','Batman','Mouse']
g.es['weight'] = [0.,-4.,5.,3.,2.,10.,3.]
g.es['name'] = ['Edge 0','Edge 1','Edge 2','Edge 3','Edge 4','Edge 5','Edge 6']
g.es['color'] = [[0.1,0.1,0.1],[0.,1.,0.],[1.,0.,1.],[1.,1.,0.],[0.2,0.4,0.6],[0.1,0.5,0.9],[0.5,0.5,0.5]]
igraph2xnet(g,'test.xnet',[],[])
stringGraph = igraph2xnet_fast(g)