How to Load and Build Networks
Goal: Create multilayer networks from scratch or load them from files.
Prerequisites: Basic Python knowledge. For conceptual background, see The py3plex Core Model.
Start from the format you already have: add edges directly if you are prototyping, or load from files/dataframes when your data is already tabular. The examples below always build a network object you can reuse in later steps.
Creating Networks from Scratch
Use these options when you want to assemble a small network interactively or generate synthetic structures.
Method 1: Add Edges Directly (Recommended)
The simplest approach—nodes are created automatically when you add edges.
from py3plex.core import multinet
# Create empty network
network = multinet.multi_layer_network()
# Add edges in list format
# Format: [source_node, source_layer, target_node, target_layer, weight]
network.add_edges([
['Alice', 'friends', 'Bob', 'friends', 1.0],
['Bob', 'friends', 'Carol', 'friends', 1.0],
['Alice', 'colleagues', 'Bob', 'colleagues', 1.0],
['Bob', 'colleagues', 'Dave', 'colleagues', 1.0]
], input_type="list")
# Verify
stats = network.basic_stats()
Expected output:
Number of nodes: 6
Number of edges: 4
Number of unique node IDs (across all layers): 4
Nodes per layer:
Layer 'friends': 3 nodes
Layer 'colleagues': 3 nodes
Method 2: Add Nodes First
For more control over node attributes and to preserve isolated node replicas. Use this pattern when you need explicit node-layer registration before edges.
from py3plex.core import multinet
network = multinet.multi_layer_network()
# Add nodes explicitly as dictionaries
network.add_nodes([
{'source': 'Alice', 'type': 'friends'},
{'source': 'Bob', 'type': 'friends'},
{'source': 'Carol', 'type': 'friends'},
{'source': 'Alice', 'type': 'colleagues'},
{'source': 'Bob', 'type': 'colleagues'},
{'source': 'Dave', 'type': 'colleagues'}
])
# Add edges between existing nodes
network.add_edges([
['Alice', 'friends', 'Bob', 'friends', 1.0],
['Bob', 'friends', 'Carol', 'friends', 1.0]
], input_type="list")
# Verify network was built correctly
network.basic_stats()
Expected output:
Number of nodes: 6
Number of edges: 2
Number of unique node IDs (across all layers): 4
Nodes per layer:
Layer 'friends': 3 nodes
Layer 'colleagues': 3 nodes
Method 3: Use Dictionary Format
For networks with edge attributes and mixed field names (timestamps, types, etc.):
from py3plex.core import multinet
network = multinet.multi_layer_network()
# Add edges with custom attributes
network.add_edges([
{
'source': 'Alice',
'source_type': 'friends',
'target': 'Bob',
'target_type': 'friends',
'weight': 1.0,
'timestamp': '2024-01-15',
'interaction_type': 'message'
},
{
'source': 'Bob',
'source_type': 'friends',
'target': 'Carol',
'target_type': 'friends',
'weight': 2.5,
'timestamp': '2024-01-16'
}
], input_type="dict")
# Verify network was built correctly
network.basic_stats()
Expected output:
Number of nodes: 3
Number of edges: 2
Number of unique node IDs (across all layers): 3
Nodes per layer:
Layer 'friends': 3 nodes
Loading Networks from Files
Use file-based loading when your data already lives on disk. Edge lists remain the most compact choice; JSON is convenient for richer metadata. Each snippet builds the same network interface shown above.
Load Edge Lists
Most common format: one edge per line. The multiedgelist loader expects
source source_layer target target_layer weight (weight defaults to 1.0 if omitted).
File format (stored as datasets/simple_multiplex.txt in the repository):
Alice friends Bob friends 1.0
Bob friends Carol friends 1.0
Alice colleagues Bob colleagues 1.0
Load it:
from py3plex.core import multinet
network = multinet.multi_layer_network()
# Option 1: Write the sample above to a file and load it
from pathlib import Path
Path("simple_multiplex.txt").write_text(
"Alice friends Bob friends 1.0\n"
"Bob friends Carol friends 1.0\n"
"Alice colleagues Bob colleagues 1.0\n"
)
network.load_network(
"simple_multiplex.txt",
input_type="multiedgelist"
)
# Option 2: Create from data directly (recommended for examples)
network.add_edges([
['Alice', 'friends', 'Bob', 'friends', 1.0],
['Bob', 'friends', 'Carol', 'friends', 1.0],
['Alice', 'colleagues', 'Bob', 'colleagues', 1.0],
], input_type="list")
network.basic_stats()
Load from Pandas DataFrame
Convert tabular data to networks (each row becomes an edge). Use separate layer columns if endpoints belong to different layers.
import pandas as pd
from py3plex.core import multinet
# Your data
df = pd.DataFrame({
'source': ['Alice', 'Bob', 'Alice'],
'layer': ['friends', 'friends', 'work'],
'target': ['Bob', 'Carol', 'Carol'],
'weight': [1.0, 1.0, 2.0]
})
# Convert to network
network = multinet.multi_layer_network()
for _, row in df.iterrows():
network.add_edges([{'source': row['source'], 'target': row['target'],
'source_type': row['layer'], 'target_type': row['layer'],
'weight': row['weight']}])
# If your dataframe has ``source_layer`` and ``target_layer`` columns,
# pass those instead of a single ``layer`` column:
# Use source_type=row['source_layer'], target_type=row['target_layer']
# in the edge dictionary when the endpoints have separate layer columns.
network.basic_stats()
Load from JSON
For complex networks with metadata (define both nodes and edges explicitly). Keep
edge dictionaries consistent: source, source_layer, target,
target_layer, plus any other attributes you want to preserve.
import json
from py3plex.core import multinet
# Create a minimal sample file (omit this when loading your own file)
sample = {"nodes": [{"id": "Isolated", "layer": "friends"}],
"edges": [{"source": "Alice", "source_layer": "friends",
"target": "Bob", "target_layer": "friends",
"attributes": {"weight": 2.5}}]}
with open('network.json', 'w') as f:
json.dump(sample, f)
# Load JSON
with open('network.json', 'r') as f:
data = json.load(f)
network = multinet.multi_layer_network()
# Preserve isolated nodes and any node attributes before adding edges
network.add_nodes([{'source': node['id'], 'type': node['layer'],
**node.get('attributes', {})} for node in data.get('nodes', [])])
for edge in data['edges']:
network.add_edges([{'source': edge['source'], 'target': edge['target'],
'source_type': edge['source_layer'],
'target_type': edge['target_layer'],
**edge.get('attributes', {})}])
network.basic_stats()
Load from NetworkX
Convert existing single-layer networks by assigning a default layer name. If the
original graph has edge attributes, include them in each dictionary passed to add_edges.
import networkx as nx
from py3plex.core import multinet
# Existing NetworkX graph
G = nx.karate_club_graph()
# Convert to multilayer (single layer)
network = multinet.multi_layer_network()
for u, v in G.edges():
network.add_edges([{'source': u, 'target': v,
'source_type': 'layer1', 'target_type': 'layer1',
**G[u][v]}])
Loading with Inter-layer Edges
To represent the same entity across layers, connect node-layer tuples that share the same node identifier:
network = multinet.multi_layer_network()
# Intra-layer edges
network.add_edges([
['Alice', 'friends', 'Bob', 'friends', 1.0],
['Alice', 'work', 'Carol', 'work', 1.0],
], input_type="list")
# Inter-layer edges (same person across layers)
network.add_edges([{'source': person, 'source_type': 'friends',
'target': person, 'target_type': 'work',
'type': 'interlayer'} for person in ['Alice', 'Bob']])
Common Patterns
Pick a template that matches your data model, then extend it with attributes as needed.
Pattern 2: Heterogeneous Networks
Different entity types:
# Author-Paper bipartite network
network = multinet.multi_layer_network()
network.add_edges([
['Alice', 'authors', 'Paper1', 'papers', 1],
['Bob', 'authors', 'Paper1', 'papers', 1],
['Alice', 'authors', 'Paper2', 'papers', 1],
], input_type="list")
Pattern 3: Temporal Networks
Time-stamped edges:
network = multinet.multi_layer_network()
network.add_edges([
{
'source': 'Alice',
'source_type': 'social',
'target': 'Bob',
'target_type': 'social',
't': '2024-01-15T10:00:00' # Point in time
},
{
'source': 'Bob',
'source_type': 'social',
'target': 'Carol',
'target_type': 'social',
't_start': '2024-01-15', # Time range
't_end': '2024-01-20'
}
], input_type="dict")
See API Documentation for temporal network details.
Verifying Your Network
After loading, verify the structure to catch layer/weight mistakes early:
# Rebuild the small example whose counts we want to inspect
network = multinet.multi_layer_network(directed=False)
network.add_edges([
['Alice', 'friends', 'Bob', 'friends', 1],
['Bob', 'friends', 'Carol', 'friends', 1],
['Alice', 'colleagues', 'Bob', 'colleagues', 1],
['Bob', 'colleagues', 'Dave', 'colleagues', 1],
], input_type="list")
# Basic stats
network.basic_stats()
# Get specific information
layers = network.layers
print(f"Layers: {layers}")
num_nodes = len(list(network.get_nodes()))
print(f"Total nodes: {num_nodes}")
# Check a specific layer
from py3plex.dsl import Q, L
layer_nodes = Q.nodes().from_layers(L["friends"]).execute(network)
print(f"Friends layer: {len(layer_nodes)} nodes")
Expected output:
Layers: ['colleagues', 'friends']
Total nodes: 6
Friends layer: 3 nodes
Next Steps
Compute statistics: How to Compute Network Statistics
Visualize your network: How to Visualize Multilayer Networks
Query the network: How to Query Multilayer Graphs with the SQL-like DSL
Understand the data model: The py3plex Core Model
API reference: API Documentation