-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexample_graph_program.py
More file actions
316 lines (240 loc) · 9.24 KB
/
Copy pathexample_graph_program.py
File metadata and controls
316 lines (240 loc) · 9.24 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""Example usage of GraphProgram class.
This script demonstrates the key features of GraphProgram:
1. Creating programs from AST
2. Stable hashing
3. Program composition
4. Execution on networks
5. Explanation generation
6. Program comparison (diff)
7. Serialization
Run with:
python examples/advanced/example_graph_program.py
"""
import json
from py3plex.dsl import Q, L
from py3plex.dsl.program import GraphProgram
from py3plex.core import multinet
def create_sample_network():
"""Create a sample multilayer network for testing."""
net = multinet.multi_layer_network()
# Add nodes
nodes = [
{"source": "Alice", "type": "social"},
{"source": "Bob", "type": "social"},
{"source": "Charlie", "type": "social"},
{"source": "David", "type": "social"},
{"source": "Alice", "type": "work"},
{"source": "Bob", "type": "work"},
{"source": "Charlie", "type": "work"},
]
net.add_nodes(nodes)
# Add edges
edges = [
# Social layer
{"source": "Alice", "target": "Bob", "source_type": "social", "target_type": "social"},
{"source": "Alice", "target": "Charlie", "source_type": "social", "target_type": "social"},
{"source": "Bob", "target": "Charlie", "source_type": "social", "target_type": "social"},
{"source": "Charlie", "target": "David", "source_type": "social", "target_type": "social"},
# Work layer
{"source": "Alice", "target": "Bob", "source_type": "work", "target_type": "work"},
{"source": "Bob", "target": "Charlie", "source_type": "work", "target_type": "work"},
]
net.add_edges(edges)
return net
def example_1_basic_program():
"""Example 1: Create and execute a basic program."""
print("=" * 70)
print("Example 1: Basic Program Creation and Execution")
print("=" * 70)
# Create network
net = create_sample_network()
# Create program from DSL query
ast = Q.nodes().compute("degree").order_by("degree", desc=True).limit(5).to_ast()
program = GraphProgram.from_ast(ast)
print(f"\nProgram hash: {program.hash()}")
print(f"Type signature: {program.type_signature}")
print(f"\nProgram explanation:")
print(program.explain())
# Execute program
print("\nExecuting program...")
result = program.execute(net, progress=False)
print("\nResults:")
df = result.to_pandas()
print(df.head())
print()
def example_2_hashing():
"""Example 2: Stable hashing for reproducibility."""
print("=" * 70)
print("Example 2: Stable Hashing")
print("=" * 70)
# Create two identical programs
ast1 = Q.nodes().compute("degree").to_ast()
ast2 = Q.nodes().compute("degree").to_ast()
program1 = GraphProgram.from_ast(ast1)
program2 = GraphProgram.from_ast(ast2)
print(f"\nProgram 1 hash: {program1.hash()}")
print(f"Program 2 hash: {program2.hash()}")
print(f"Hashes match: {program1.hash() == program2.hash()}")
# Different program
ast3 = Q.nodes().compute("betweenness").to_ast()
program3 = GraphProgram.from_ast(ast3)
print(f"\nProgram 3 hash: {program3.hash()}")
print(f"Program 3 differs: {program1.hash() != program3.hash()}")
print()
def example_3_composition():
"""Example 3: Program composition."""
print("=" * 70)
print("Example 3: Program Composition")
print("=" * 70)
# Create network
net = create_sample_network()
# Create two programs
ast1 = Q.nodes().compute("degree").to_ast()
program1 = GraphProgram.from_ast(ast1)
print("\nProgram 1:")
print(program1.explain())
ast2 = Q.nodes().compute("clustering").to_ast()
program2 = GraphProgram.from_ast(ast2)
print("\nProgram 2:")
print(program2.explain())
# Compose programs
composed = program1.compose(program2)
print("\nComposed program:")
print(composed.explain())
# Execute composed program
print("\nExecuting composed program...")
result = composed.execute(net, progress=False)
print("\nResults (both metrics computed):")
df = result.to_pandas()
print(df.head())
# Check provenance
print(f"\nProvenance chain: {composed.metadata.provenance_chain}")
print()
def example_4_layer_filtering():
"""Example 4: Programs with layer filtering."""
print("=" * 70)
print("Example 4: Layer Filtering")
print("=" * 70)
# Create network
net = create_sample_network()
# Program for social layer
ast_social = Q.nodes().from_layers(L["social"]).compute("degree").to_ast()
program_social = GraphProgram.from_ast(ast_social)
print("\nSocial layer program:")
print(program_social.explain())
result_social = program_social.execute(net, progress=False)
print("\nSocial layer results:")
print(result_social.to_pandas().head())
# Program for work layer
ast_work = Q.nodes().from_layers(L["work"]).compute("degree").to_ast()
program_work = GraphProgram.from_ast(ast_work)
print("\nWork layer program:")
print(program_work.explain())
result_work = program_work.execute(net, progress=False)
print("\nWork layer results:")
print(result_work.to_pandas().head())
# Hashes should differ
print(f"\nPrograms have different hashes: {program_social.hash() != program_work.hash()}")
print()
def example_5_program_diff():
"""Example 5: Program comparison with diff."""
print("=" * 70)
print("Example 5: Program Diff")
print("=" * 70)
# Create three programs
ast1 = Q.nodes().compute("degree").to_ast()
program1 = GraphProgram.from_ast(ast1)
ast2 = Q.nodes().compute("degree").to_ast()
program2 = GraphProgram.from_ast(ast2)
ast3 = Q.nodes().compute("betweenness").to_ast()
program3 = GraphProgram.from_ast(ast3)
# Diff identical programs
print("\nDiff of identical programs:")
diff_identical = program1.diff(program2)
print(json.dumps(diff_identical, indent=2))
# Diff different programs
print("\nDiff of different programs:")
diff_different = program1.diff(program3)
print(json.dumps(diff_different, indent=2, default=str))
print()
def example_6_serialization():
"""Example 6: Program serialization."""
print("=" * 70)
print("Example 6: Program Serialization")
print("=" * 70)
# Create program
ast = Q.nodes().compute("degree").compute("betweenness").order_by("degree").limit(10).to_ast()
program = GraphProgram.from_ast(ast)
print("\nOriginal program:")
print(program.explain())
# Serialize to dict
program_dict = program.to_dict()
print("\nSerialized program (metadata only):")
print(json.dumps(
{"hash": program_dict["program_hash"], "metadata": program_dict["metadata"]},
indent=2
))
print("\nNote: Full deserialization (from_dict) not yet implemented")
print(" as AST deserialization is complex.")
print()
def example_7_optimize_placeholder():
"""Example 7: Optimization (placeholder)."""
print("=" * 70)
print("Example 7: Program Optimization (Placeholder)")
print("=" * 70)
# Create program
ast = Q.nodes().compute("degree").compute("betweenness").to_ast()
program = GraphProgram.from_ast(ast)
print("\nOriginal program:")
print(f"Hash: {program.hash()}")
# Optimize (currently a no-op placeholder)
optimized = program.optimize(level=2)
print("\nOptimized program:")
print(f"Hash: {optimized.hash()}")
print(f"Same as original: {program.hash() == optimized.hash()}")
print("\nNote: Optimization is a placeholder - will be implemented")
print(" with rewrite engine in future versions.")
print()
def example_8_provenance():
"""Example 8: Provenance tracking."""
print("=" * 70)
print("Example 8: Provenance Tracking")
print("=" * 70)
# Create programs with custom provenance
ast1 = Q.nodes().compute("degree").to_ast()
program1 = GraphProgram.from_ast(ast1, provenance=["user_query", "step1"])
print("\nProgram 1 provenance:")
print(f" {program1.metadata.provenance_chain}")
ast2 = Q.nodes().compute("betweenness").to_ast()
program2 = GraphProgram.from_ast(ast2, provenance=["user_query", "step2"])
print("\nProgram 2 provenance:")
print(f" {program2.metadata.provenance_chain}")
# Compose - provenance is merged
composed = program1.compose(program2)
print("\nComposed program provenance:")
print(f" {composed.metadata.provenance_chain}")
print("\nNote: Provenance chain includes both original chains plus 'compose' step")
print()
def main():
"""Run all examples."""
print("\n")
print("*" * 70)
print("*" + " " * 68 + "*")
print("*" + "GraphProgram Examples".center(68) + "*")
print("*" + " " * 68 + "*")
print("*" * 70)
print("\n")
example_1_basic_program()
example_2_hashing()
example_3_composition()
example_4_layer_filtering()
example_5_program_diff()
example_6_serialization()
example_7_optimize_placeholder()
example_8_provenance()
print("=" * 70)
print("All examples completed!")
print("=" * 70)
print()
if __name__ == "__main__":
main()