0
# File I/O and Formats
1
2
Comprehensive support for reading and writing graphs in various formats including GraphML, GML, Pajek, DIMACS, and more. igraph provides seamless data interchange with other network analysis tools and databases.
3
4
## Capabilities
5
6
### Reading Graphs
7
8
Load graphs from files in multiple formats with automatic format detection and manual specification options.
9
10
```python { .api }
11
class Graph:
12
@classmethod
13
def Read(cls, filename, format=None, **kwargs):
14
"""
15
Read graph from file with automatic format detection.
16
17
Parameters:
18
- filename: str, path to input file
19
- format: str, file format (None for auto-detection)
20
Supported: "edgelist", "ncol", "lgl", "graphml", "gml",
21
"net" (pajek), "dimacs", "graphdb", "pickle", "svg"
22
- **kwargs: format-specific parameters
23
24
Returns:
25
Graph object loaded from file
26
"""
27
28
@classmethod
29
def Read_Edgelist(cls, filename, directed=True):
30
"""
31
Read simple edge list format.
32
33
Parameters:
34
- filename: str, input file path
35
- directed: bool, whether edges are directed
36
37
Returns:
38
Graph from edge list
39
"""
40
41
@classmethod
42
def Read_Ncol(cls, filename, names=True, weights="auto", directed="auto"):
43
"""
44
Read NCOL format (name-based edge list).
45
46
Parameters:
47
- filename: str, input file
48
- names: bool, whether file contains vertex names
49
- weights: str/bool, weight handling ("auto", True, False, or attribute name)
50
- directed: str/bool, direction handling ("auto", True, False)
51
52
Returns:
53
Graph with vertex names and optional weights
54
"""
55
56
@classmethod
57
def Read_GraphML(cls, filename, index=0):
58
"""
59
Read GraphML format.
60
61
Parameters:
62
- filename: str, GraphML file path
63
- index: int, graph index (for files with multiple graphs)
64
65
Returns:
66
Graph with full attribute preservation
67
"""
68
```
69
70
**Usage Examples:**
71
72
```python
73
import igraph as ig
74
75
# Automatic format detection
76
g1 = ig.Graph.Read("network.graphml")
77
g2 = ig.Graph.Read("edges.txt") # Auto-detects as edgelist
78
79
# Explicit format specification
80
g3 = ig.Graph.Read("data.net", format="pajek")
81
82
# Simple edge list
83
g4 = ig.Graph.Read_Edgelist("simple_edges.txt", directed=False)
84
85
# Named vertices with weights
86
g5 = ig.Graph.Read_Ncol("weighted_network.ncol", weights=True)
87
88
# GraphML with attributes
89
g6 = ig.Graph.Read_GraphML("attributed_network.graphml")
90
91
# Handle multiple graphs in one file
92
for i in range(3): # If file contains 3 graphs
93
g = ig.Graph.Read_GraphML("multi_graph.graphml", index=i)
94
print(f"Graph {i}: {g.vcount()} vertices, {g.ecount()} edges")
95
```
96
97
### Writing Graphs
98
99
Save graphs to files in various formats for interoperability with other tools.
100
101
```python { .api }
102
class Graph:
103
def write(self, filename, format=None, **kwargs):
104
"""
105
Write graph to file.
106
107
Parameters:
108
- filename: str, output file path
109
- format: str, output format (None for auto-detection from extension)
110
- **kwargs: format-specific options
111
112
Returns:
113
None
114
"""
115
116
def write_edgelist(self, filename):
117
"""
118
Write simple edge list.
119
120
Parameters:
121
- filename: str, output path
122
123
Returns:
124
None
125
"""
126
127
def write_ncol(self, filename, names=None, weights=None):
128
"""
129
Write NCOL format.
130
131
Parameters:
132
- filename: str, output path
133
- names: str/bool, vertex name attribute or True for indices
134
- weights: str/bool, edge weight attribute or True for equal weights
135
136
Returns:
137
None
138
"""
139
140
def write_graphml(self, filename):
141
"""
142
Write GraphML format with full attribute preservation.
143
144
Parameters:
145
- filename: str, output path
146
147
Returns:
148
None
149
"""
150
151
def write_gml(self, filename):
152
"""
153
Write GML format.
154
155
Parameters:
156
- filename: str, output path
157
158
Returns:
159
None
160
"""
161
162
def write_pajek(self, filename):
163
"""
164
Write Pajek NET format.
165
166
Parameters:
167
- filename: str, output path
168
169
Returns:
170
None
171
"""
172
```
173
174
**Usage Examples:**
175
176
```python
177
# Create sample graph with attributes
178
g = ig.Graph([(0,1), (1,2), (2,0), (1,3)], directed=False)
179
g.vs["name"] = ["Alice", "Bob", "Carol", "Dave"]
180
g.vs["age"] = [25, 30, 28, 35]
181
g.es["weight"] = [1.0, 2.5, 1.8, 0.9]
182
g.es["type"] = ["friend", "work", "family", "friend"]
183
184
# Save in different formats
185
g.write("network.graphml") # GraphML with all attributes
186
g.write("network.gml") # GML format
187
g.write("network.net") # Pajek format
188
g.write("edges.txt", format="edgelist") # Simple edge list
189
190
# Named edge list
191
g.write_ncol("named_edges.ncol", names="name", weights="weight")
192
193
# Simple formats
194
g.write_edgelist("simple.edges")
195
196
# Format-specific options
197
g.write("network.dot", format="dot") # Graphviz DOT format
198
```
199
200
### Specialized Formats
201
202
Support for domain-specific and research-oriented file formats.
203
204
```python { .api }
205
class Graph:
206
@classmethod
207
def Read_Pajek(cls, filename):
208
"""
209
Read Pajek NET format.
210
211
Parameters:
212
- filename: str, Pajek file path
213
214
Returns:
215
Graph with Pajek-specific attributes
216
"""
217
218
@classmethod
219
def Read_GML(cls, filename):
220
"""
221
Read GML (Graph Modeling Language) format.
222
223
Parameters:
224
- filename: str, GML file path
225
226
Returns:
227
Graph with GML attributes
228
"""
229
230
@classmethod
231
def Read_Lgl(cls, filename, names=True, weights="auto"):
232
"""
233
Read LGL (Large Graph Layout) format.
234
235
Parameters:
236
- filename: str, LGL file path
237
- names: bool, vertex names present
238
- weights: str/bool, weight handling
239
240
Returns:
241
Graph from LGL format
242
"""
243
244
@classmethod
245
def Read_Dimacs(cls, filename, directed=False):
246
"""
247
Read DIMACS challenge format.
248
249
Parameters:
250
- filename: str, DIMACS file path
251
- directed: bool, directed graph
252
253
Returns:
254
Graph in DIMACS format
255
"""
256
257
def write_dimacs(self, filename, source=None, target=None, capacity=None):
258
"""
259
Write DIMACS flow format.
260
261
Parameters:
262
- filename: str, output path
263
- source: int, source vertex for flow problems
264
- target: int, sink vertex for flow problems
265
- capacity: str, edge capacity attribute
266
267
Returns:
268
None
269
"""
270
```
271
272
**Usage Examples:**
273
274
```python
275
# Domain-specific formats
276
pajek_g = ig.Graph.Read_Pajek("social_network.net")
277
gml_g = ig.Graph.Read_GML("biological_network.gml")
278
lgl_g = ig.Graph.Read_Lgl("large_network.lgl")
279
dimacs_g = ig.Graph.Read_Dimacs("flow_network.dimacs")
280
281
# Write DIMACS for flow problems
282
flow_graph = ig.Graph([(0,1), (0,2), (1,2), (1,3), (2,3)], directed=True)
283
flow_graph.es["capacity"] = [10, 10, 1, 10, 10]
284
flow_graph.write_dimacs("flow_problem.dimacs", source=0, target=3, capacity="capacity")
285
286
# Check format-specific attributes
287
if "coordinate" in pajek_g.vs.attributes():
288
print("Pajek file contained vertex coordinates")
289
if "weight" in gml_g.es.attributes():
290
print("GML file contained edge weights")
291
```
292
293
### NetworkX Integration
294
295
Seamless conversion between igraph and NetworkX for accessing different algorithm implementations.
296
297
```python { .api }
298
class Graph:
299
def to_networkx(self):
300
"""
301
Convert igraph Graph to NetworkX graph.
302
303
Returns:
304
NetworkX graph with preserved attributes
305
"""
306
307
@classmethod
308
def from_networkx(cls, graph):
309
"""
310
Create igraph Graph from NetworkX graph.
311
312
Parameters:
313
- graph: NetworkX graph object
314
315
Returns:
316
igraph Graph with converted attributes
317
"""
318
```
319
320
**Usage Examples:**
321
322
```python
323
try:
324
import networkx as nx
325
326
# Create igraph graph
327
ig_graph = ig.Graph.Barabasi(100, m=3, directed=False)
328
ig_graph.vs["betweenness"] = ig_graph.betweenness()
329
330
# Convert to NetworkX
331
nx_graph = ig_graph.to_networkx()
332
333
# Use NetworkX algorithms
334
nx_pagerank = nx.pagerank(nx_graph)
335
nx_clustering = nx.clustering(nx_graph)
336
337
# Convert back to igraph
338
nx_graph.graph["description"] = "Processed with NetworkX"
339
for node in nx_graph.nodes():
340
nx_graph.nodes[node]["nx_pagerank"] = nx_pagerank[node]
341
nx_graph.nodes[node]["nx_clustering"] = nx_clustering[node]
342
343
converted_back = ig.Graph.from_networkx(nx_graph)
344
345
# Compare results
346
print("NetworkX PageRank:", list(nx_pagerank.values())[:5])
347
print("igraph PageRank:", converted_back.pagerank()[:5])
348
349
except ImportError:
350
print("NetworkX not available")
351
```
352
353
### Database and Web Formats
354
355
Support for graph databases and web-based data sources.
356
357
```python { .api }
358
class Graph:
359
@classmethod
360
def Read_GraphDB(cls, filename, directed=True):
361
"""
362
Read GraphDB format.
363
364
Parameters:
365
- filename: str, GraphDB file path
366
- directed: bool, graph direction
367
368
Returns:
369
Graph from database format
370
"""
371
372
def write_svg(self, filename, layout=None, **kwargs):
373
"""
374
Write SVG vector graphics format.
375
376
Parameters:
377
- filename: str, SVG output path
378
- layout: Layout, vertex positions
379
- **kwargs: styling parameters
380
381
Returns:
382
None
383
"""
384
385
def write_dot(self, filename):
386
"""
387
Write Graphviz DOT format.
388
389
Parameters:
390
- filename: str, DOT output path
391
392
Returns:
393
None
394
"""
395
```
396
397
**Usage Examples:**
398
399
```python
400
# Database format
401
db_graph = ig.Graph.Read_GraphDB("graph_database.db")
402
403
# Vector graphics output
404
layout = g.layout_fruchterman_reingold()
405
g.write_svg("network.svg",
406
layout=layout,
407
vertex_size=20,
408
vertex_color="lightblue",
409
edge_color="gray")
410
411
# Graphviz format for external processing
412
g.write_dot("network.dot")
413
414
# Can then use: dot -Tpng network.dot -o network.png
415
```
416
417
### Binary and Compressed Formats
418
419
Efficient storage formats for large graphs and fast I/O operations.
420
421
```python { .api }
422
class Graph:
423
@classmethod
424
def Read_Pickle(cls, filename):
425
"""
426
Read Python pickle format (binary).
427
428
Parameters:
429
- filename: str, pickle file path
430
431
Returns:
432
Graph from pickle
433
"""
434
435
def write_pickle(self, filename):
436
"""
437
Write Python pickle format.
438
439
Parameters:
440
- filename: str, output path
441
442
Returns:
443
None
444
"""
445
446
def write_picklez(self, filename):
447
"""
448
Write compressed pickle format.
449
450
Parameters:
451
- filename: str, output path (will be compressed)
452
453
Returns:
454
None
455
"""
456
```
457
458
**Usage Examples:**
459
460
```python
461
import time
462
463
# Create large graph for performance testing
464
large_g = ig.Graph.Barabasi(10000, m=5)
465
large_g.vs["random_attr"] = [random.random() for _ in range(large_g.vcount())]
466
large_g.es["weights"] = [random.uniform(0.1, 2.0) for _ in range(large_g.ecount())]
467
468
# Compare I/O performance
469
formats_to_test = [
470
("pickle", large_g.write_pickle, ig.Graph.Read_Pickle),
471
("graphml", large_g.write_graphml, ig.Graph.Read_GraphML),
472
("gml", large_g.write_gml, ig.Graph.Read_GML)
473
]
474
475
for format_name, write_func, read_func in formats_to_test:
476
filename = f"large_graph.{format_name}"
477
478
# Write timing
479
start_time = time.time()
480
write_func(filename)
481
write_time = time.time() - start_time
482
483
# Read timing
484
start_time = time.time()
485
loaded_g = read_func(filename)
486
read_time = time.time() - start_time
487
488
# File size
489
import os
490
file_size = os.path.getsize(filename) / (1024*1024) # MB
491
492
print(f"{format_name}: Write {write_time:.2f}s, Read {read_time:.2f}s, Size {file_size:.1f}MB")
493
494
# Compressed pickle for space efficiency
495
large_g.write_picklez("large_graph.pkl.gz")
496
```
497
498
### Custom Format Handling
499
500
Handle custom file formats and edge cases in data loading.
501
502
**Usage Examples:**
503
504
```python
505
import csv
506
from io import StringIO
507
508
def read_custom_csv(filename, source_col=0, target_col=1, weight_col=None,
509
directed=True, skip_header=True):
510
"""Read graph from custom CSV format."""
511
edges = []
512
weights = []
513
514
with open(filename, 'r') as f:
515
reader = csv.reader(f)
516
if skip_header:
517
next(reader) # Skip header row
518
519
for row in reader:
520
source = row[source_col].strip()
521
target = row[target_col].strip()
522
edges.append((source, target))
523
524
if weight_col is not None:
525
weights.append(float(row[weight_col]))
526
527
# Create graph from edge list
528
g = ig.Graph.TupleList(edges, directed=directed)
529
530
if weight_col is not None:
531
g.es["weight"] = weights
532
533
return g
534
535
def write_custom_format(graph, filename, include_attributes=True):
536
"""Write graph in custom text format."""
537
with open(filename, 'w') as f:
538
# Write header
539
f.write(f"# Graph with {graph.vcount()} vertices and {graph.ecount()} edges\\n")
540
541
# Write vertices with attributes
542
if include_attributes and graph.vs.attributes():
543
f.write("# Vertices:\\n")
544
for v in graph.vs:
545
attrs = {attr: v[attr] for attr in v.attributes()}
546
f.write(f"v {v.index} {attrs}\\n")
547
548
# Write edges with attributes
549
f.write("# Edges:\\n")
550
for e in graph.es:
551
if include_attributes and e.attributes():
552
attrs = {attr: e[attr] for attr in e.attributes()}
553
f.write(f"e {e.source} {e.target} {attrs}\\n")
554
else:
555
f.write(f"e {e.source} {e.target}\\n")
556
557
# Usage
558
custom_g = read_custom_csv("network_data.csv", weight_col=2)
559
write_custom_format(custom_g, "custom_format.txt")
560
561
# Batch processing multiple files
562
import glob
563
564
def process_graph_files(pattern, output_dir):
565
"""Process multiple graph files and convert formats."""
566
files = glob.glob(pattern)
567
568
for file_path in files:
569
try:
570
# Try to read with auto-detection
571
g = ig.Graph.Read(file_path)
572
573
# Generate output filename
574
base_name = os.path.splitext(os.path.basename(file_path))[0]
575
output_path = os.path.join(output_dir, f"{base_name}.graphml")
576
577
# Save in standardized format
578
g.write_graphml(output_path)
579
print(f"Converted {file_path} -> {output_path}")
580
581
except Exception as e:
582
print(f"Failed to process {file_path}: {e}")
583
584
# Convert all network files to GraphML
585
process_graph_files("data/*.net", "converted/")
586
process_graph_files("data/*.gml", "converted/")
587
```