MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / parse_pajek

Function parse_pajek

easygraph/readwrite/pajek.py:217–326  ·  view source on GitHub ↗

Parse Pajek format graph from string or iterable. Parameters ---------- lines : string or iterable Data in Pajek format. Returns ------- G : EasyGraph graph See Also -------- read_pajek

(lines)

Source from the content-addressed store, hash-verified

215
216
217def parse_pajek(lines):
218 """Parse Pajek format graph from string or iterable.
219
220 Parameters
221 ----------
222 lines : string or iterable
223 Data in Pajek format.
224
225 Returns
226 -------
227 G : EasyGraph graph
228
229 See Also
230 --------
231 read_pajek
232
233 """
234 import shlex
235
236 # multigraph=False
237 if isinstance(lines, str):
238 lines = iter(lines.split("\n"))
239 # from itertools import tee
240 # lines, lines2 = tee(lines)
241 # from icecream import ic
242 # ic(next(lines2))
243 lines = iter([line.rstrip("\n") for line in lines])
244 G = eg.MultiDiGraph() # are multiedges allowed in Pajek? assume yes
245 labels = [] # in the order of the file, needed for matrix
246 while lines:
247 try:
248 l = next(lines)
249 except: # EOF
250 break
251 if l.lower().startswith("*network"):
252 try:
253 label, name = l.split(None, 1)
254 except ValueError:
255 # Line was not of the form: *network NAME
256 pass
257 else:
258 G.graph["name"] = name
259 elif l.lower().startswith("*vertices"):
260 nodelabels = {}
261 l, nnodes = l.split()
262 for i in range(int(nnodes)):
263 l = next(lines)
264 try:
265 splitline = [x for x in shlex.split(str(l))]
266 except AttributeError:
267 splitline = shlex.split(str(l))
268 id, label = splitline[0:2]
269 labels.append(label)
270 G.add_node(label)
271 nodelabels[id] = label
272 G.nodes[label]["id"] = id
273 try:
274 x, y, shape = splitline[2:5]

Callers 1

read_pajekFunction · 0.85

Calls 6

add_nodeMethod · 0.95
add_edgeMethod · 0.95
add_edges_fromMethod · 0.95
appendMethod · 0.80
updateMethod · 0.80
DiGraphMethod · 0.80

Tested by

no test coverage detected