Quick Start Tutorial

Get started with py3plex in just a few minutes. This tutorial shows you the essential concepts through three focused examples.

Executable Examples

Clone the repository first (the example scripts are not installed by pip). All examples are in the examples/getting_started/ directory and can be run directly:

python examples/getting_started/01_basic_query.py
python examples/getting_started/tutorial_10min.py
python examples/getting_started/02_community_detection.py

You will learn:

  • Load and query multilayer networks

  • Create networks from scratch

  • Detect communities across layers

Installation

python -m pip install py3plex

For Docker setup or optional dependencies, see Installation and Setup. Run inside a virtual environment to keep dependencies isolated.


Example 1: Load and Query (1 min)

Load a built-in dataset and run a simple query:

from py3plex.datasets import load_aarhus_cs
from py3plex.dsl import Q

# 1. Load network
network = load_aarhus_cs()

# 2. Run query - find nodes with degree > 5
result = (
    Q.nodes()
    .where(degree__gt=5)
    .compute("degree_centrality")
    .execute(network)
)

# 3. Inspect result
print(f"Found {len(result.nodes)} high-degree nodes")
df = result.to_pandas()
print("\nTop high-degree nodes:")
print(df[['id', 'degree_centrality']].head())

Expected output (illustrative):

Found 8 high-degree nodes

Top high-degree nodes:
             id  degree_centrality
0  (u12, work)           0.241379
1  (u05, work)           0.206897
2  (u41, social)         0.189655
3  (u33, social)         0.172414
4  (u20, leisure)        0.172414

What this does:

  • Loads the Aarhus CS multilayer social network (61 nodes, 5 layers)

  • Filters nodes with more than 5 connections

  • Computes degree centrality

  • Displays results as a pandas DataFrame

Next: See examples/network_analysis/ for advanced DSL queries.


Example 2: Create Network (2 min)

Create a simple multilayer network from scratch:

from py3plex.core.multinet import multi_layer_network

# 1. Create network
network = multi_layer_network(directed=False)

# 2. Add edges (nodes created automatically)
network.add_edges([
    {'source': 'Alice', 'target': 'Bob',
     'source_type': 'social', 'target_type': 'social'},
    {'source': 'Bob', 'target': 'Charlie',
     'source_type': 'social', 'target_type': 'social'},
    {'source': 'Alice', 'target': 'Charlie',
     'source_type': 'social', 'target_type': 'social'},
    {'source': 'Alice', 'target': 'Bob',
     'source_type': 'work', 'target_type': 'work'},
    {'source': 'Bob', 'target': 'David',
     'source_type': 'work', 'target_type': 'work'},
])

# 3. Print statistics
print(f"Nodes: {len(list(network.get_nodes()))}")
print(f"Edges: {len(list(network.get_edges()))}")
print(f"Layers: {len(network.layers)}")

What this does:

  • Creates an undirected multilayer network

  • Adds edges in two layers: ‘social’ and ‘work’

  • Nodes are created automatically from edge definitions

  • Displays basic network statistics

Next: See examples/getting_started/ for more construction patterns.


Example 3: Community Detection API (2 min)

Learn the API for community detection in multilayer networks:

from py3plex.datasets import load_aarhus_cs

# 1. Load network
network = load_aarhus_cs()

# 2. Network info
nodes = list(network.get_nodes())
print(f"Network loaded: {len(nodes)} nodes")
print(f"Layers: {len(network.layers)}")

# 3. Community detection API pattern
print("\nCommunity detection API:")
print("  from py3plex.algorithms.community_detection.multilayer_modularity import louvain_multilayer")
print("  partition = louvain_multilayer(network, random_state=42)")

What this does:

  • Loads the Aarhus CS network

  • Shows network structure (nodes and layers)

  • Demonstrates the API pattern for multilayer community detection

  • Points to detailed examples in the repository

Note: Node IDs may be strings or numbers. Each partition key identifies a node replica as (node_id, layer).

Next: See examples/network_analysis/ for detailed community detection patterns.


Next Steps

Continue learning:

  1. More Examples: Explore examples/ directory

  2. DSL Guide: SQL-like DSL for Multilayer Networks - SQL-like queries

  3. Visualization: Visualization - Create plots

  4. Advanced Topics: See the book (PDF) for case studies

Example categories:

  • getting_started/ - Introductory scripts and datasets

  • network_analysis/ - DSL queries and community detection

  • io_and_data/ - Loading and exporting data

  • visualization/ - Network plots

  • advanced/ - Dynamics, uncertainty, and specialized analyses

  • pipelines/ - Reusable analysis workflows

See Examples & Recipes for the complete examples catalog.

Getting help: