-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy path01_basic_query.py
More file actions
99 lines (83 loc) · 2.76 KB
/
Copy path01_basic_query.py
File metadata and controls
99 lines (83 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""
Basic Network Creation Example
This example demonstrates:
- Creating a simple multilayer network
- Adding nodes and edges
- Querying network structure
Output: Deterministic network statistics
"""
from py3plex.core.multinet import multi_layer_network
from py3plex.dsl import Q
def main():
# Create a simple network
print("Creating a multilayer network...")
network = multi_layer_network(directed=False)
# Add nodes to two layers
nodes = [
{'source': 'Alice', 'type': 'social'},
{'source': 'Bob', 'type': 'social'},
{'source': 'Charlie', 'type': 'social'},
{'source': 'Alice', 'type': 'work'},
{'source': 'Bob', 'type': 'work'},
{'source': 'Diana', 'type': 'work'},
]
network.add_nodes(nodes)
print(f"Added {len(nodes)} nodes")
# Add edges
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': 'Diana', 'source_type': 'work', 'target_type': 'work'},
]
network.add_edges(edges)
print(f"Added {len(edges)} edges")
print()
# Display network statistics
print("Network Statistics:")
print("=" * 40)
# Count nodes per layer
result = (
Q.nodes()
.execute(network)
)
print(f"Total node instances: {len(result.items)}")
# Count edges
edge_result = (
Q.edges()
.execute(network)
)
print(f"Total edges: {len(edge_result.items)}")
print()
# Show nodes with their degrees
print("Node Degrees:")
print("=" * 40)
degree_result = (
Q.nodes()
.compute("degree")
.order_by("degree", desc=True)
.execute(network)
)
df = degree_result.to_pandas()
# Reset index to make it easier to access
df = df.reset_index()
for _, row in df.head(10).iterrows():
# Handle different possible column names
if 'node' in df.columns:
node = row['node']
elif 'level_0' in df.columns:
node = row['level_0']
else:
node = row.iloc[0]
if 'layer' in df.columns:
layer = row['layer']
elif 'level_1' in df.columns:
layer = row['level_1']
else:
layer = row.iloc[1] if len(row) > 1 else 'N/A'
degree = row['degree']
print(f" {str(node):10s} ({str(layer):8s}): {degree}")
if __name__ == "__main__":
main()