This directory contains examples demonstrating the scikit-learn style pipeline functionality in py3plex.
The pipeline module provides a composable, scikit-learn inspired API for chaining network analysis operations. This makes it easy to create reproducible workflows and experiment with different analysis strategies.
from py3plex.pipeline import Pipeline, LoadStep, ComputeStats
pipe = Pipeline([
("load", LoadStep(generator='random_er', n=50, l=3, p=0.1)),
("stats", ComputeStats()),
])
result = pipe.run()- LoadStep: Load networks from files or generate random networks
- Supports GraphML, gpickle, multiedgelist formats
- Can generate random Erdos-Renyi multilayer networks
- AggregateLayers: Aggregate edges across multiple layers
- Methods: 'sum', 'mean', 'max'
- FilterNodes: Filter nodes based on criteria
- By degree (min/max)
- By explicit node list
- ComputeStats: Compute basic network statistics
- Node/edge counts, density
- Optional layer-specific statistics
- LouvainCommunity: Louvain community detection
- LeidenMultilayer: Leiden algorithm for multilayer networks (requires leidenalg)
- SaveNetwork: Save networks to files
- Formats: GraphML, gpickle, edgelist
File: example_1_basic_stats.py
Load a random network and compute statistics.
python example_1_basic_stats.pyFile: example_2_aggregation.py
Generate a multilayer network, aggregate layers, and compute statistics.
python example_2_aggregation.pyFile: example_3_community_detection.py
Detect communities using the Louvain algorithm.
python example_3_community_detection.pyFile: example_4_leiden_multilayer.py
Advanced multilayer community detection using Leiden algorithm.
Requirements: pip install leidenalg
python example_4_leiden_multilayer.pyFile: example_5_filtering.py
Filter nodes by degree before analysis.
python example_5_filtering.pyFile: example_6_complex_pipeline.py
Demonstrates a complex pipeline: load -> filter -> aggregate -> community detection.
python example_6_complex_pipeline.pyFile: example_7_save_load.py
Save intermediate results and load them in subsequent pipelines.
python example_7_save_load.pyYou can create custom pipeline steps by inheriting from PipelineStep:
from py3plex.pipeline import PipelineStep
class CustomStep(PipelineStep):
def __init__(self, param1, param2):
self.param1 = param1
self.param2 = param2
def transform(self, data):
# Your custom transformation logic
return transformed_data- Composable: Chain multiple steps together
- Type-safe: Steps validate input types
- Reproducible: Set random seeds for deterministic results
- Flexible: Easy to add custom steps
- Logging: Built-in logging for pipeline execution
- Parameter inspection:
get_params()andset_params()methods
py3plex also supports config-driven workflows (see py3plex.workflows). The key differences:
| Feature | Pipeline | Config-Driven Workflow |
|---|---|---|
| Style | Programmatic, scikit-like | Declarative, YAML/JSON |
| Use case | Prototyping, scripting | Production, automation |
| Extensibility | Python classes | Configuration schemas |
| Type hints | Yes | Limited |
Choose pipelines for interactive development and experimentation. Choose config-driven workflows for deployment and automation.