-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkflow_visualization.py
More file actions
364 lines (297 loc) · 11.5 KB
/
workflow_visualization.py
File metadata and controls
364 lines (297 loc) · 11.5 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python3
"""
Workflow Visualization Example
==============================
This example demonstrates how to visualize Graflow workflows using the built-in
visualization utilities. Learn how to:
- Extract NetworkX graphs from workflows
- Generate ASCII representations
- Create Mermaid diagrams
- Generate PNG visualizations
- Analyze workflow structure
Concepts Covered:
-----------------
1. Graph extraction from TaskGraph
2. ASCII visualization (requires grandalf)
3. Mermaid diagram generation
4. PNG generation (requires pygraphviz)
5. Workflow analysis and debugging
Dependencies:
-------------
Optional (for full functionality):
- pip install grandalf # ASCII visualization
- pip install pygraphviz # PNG generation
- pip install requests # Mermaid PNG via API
The example works without these packages but with reduced functionality.
"""
from graflow.core.context import TaskExecutionContext
from graflow.core.decorators import task
from graflow.core.workflow import workflow
from graflow.utils.graph import (
draw_ascii,
draw_mermaid,
draw_png,
show_graph_info,
visualize_dependencies,
)
def example_1_simple_pipeline():
"""Example 1: Visualize a simple sequential pipeline."""
print("\n" + "=" * 70)
print("EXAMPLE 1: Simple Sequential Pipeline")
print("=" * 70)
with workflow("simple_pipeline") as ctx:
@task
def load_data():
"""Load data from source."""
print(" 📥 Loading data...")
return {"rows": 1000, "columns": 10}
@task
def validate_data():
"""Validate the loaded data."""
print(" ✓ Validating data...")
return {"valid": True}
@task
def transform_data():
"""Transform the data."""
print(" 🔄 Transforming data...")
return {"transformed": True}
@task
def save_results():
"""Save the results."""
print(" 💾 Saving results...")
return {"saved": True}
# Build pipeline
load_data >> validate_data >> transform_data >> save_results
# Show workflow structure
print("\n📊 Workflow Structure:")
ctx.show_info()
# Extract NetworkX graph
nx_graph = ctx.graph.nx_graph()
# Visualize with different methods
print("\n🔍 Graph Analysis:")
print("-" * 70)
show_graph_info(nx_graph)
print("\n📝 Task Dependencies:")
print("-" * 70)
visualize_dependencies(nx_graph)
print("\n🎨 ASCII Visualization:")
print("-" * 70)
try:
ascii_repr = draw_ascii(nx_graph)
print(ascii_repr)
except Exception as e:
print(f"⚠️ ASCII drawing unavailable: {e}")
print(" Install with: pip install grandalf")
print("\n🌊 Mermaid Diagram:")
print("-" * 70)
mermaid = draw_mermaid(nx_graph, title="Simple Pipeline")
print(mermaid)
print("\n📸 PNG Generation:")
print("-" * 70)
try:
png_bytes = draw_png(nx_graph, output_path="/tmp/simple_pipeline.png")
if png_bytes:
print(f"✅ PNG saved: /tmp/simple_pipeline.png ({len(png_bytes)} bytes)")
except Exception as e:
print(f"⚠️ PNG generation unavailable: {e}")
print(" Install with: pip install pygraphviz")
def example_2_parallel_workflow():
"""Example 2: Visualize a workflow with parallel execution."""
print("\n" + "=" * 70)
print("EXAMPLE 2: Parallel Processing Workflow")
print("=" * 70)
with workflow("parallel_workflow") as ctx:
@task(inject_context=True)
def load_data(context: TaskExecutionContext):
"""Load data from source."""
print(" 📥 Loading data...")
context.get_channel().set("data", {"items": [1, 2, 3, 4, 5]})
@task
def process_batch_1():
"""Process first batch."""
print(" ⚙️ Processing batch 1...")
return {"batch": 1, "processed": True}
@task
def process_batch_2():
"""Process second batch."""
print(" ⚙️ Processing batch 2...")
return {"batch": 2, "processed": True}
@task
def process_batch_3():
"""Process third batch."""
print(" ⚙️ Processing batch 3...")
return {"batch": 3, "processed": True}
@task
def aggregate_results():
"""Aggregate all batch results."""
print(" 📊 Aggregating results...")
return {"aggregated": True, "total": 3}
@task
def generate_report():
"""Generate final report."""
print(" 📄 Generating report...")
return {"report": "complete"}
# Build parallel workflow
load_data >> (process_batch_1 | process_batch_2 | process_batch_3)
(process_batch_1 | process_batch_2 | process_batch_3) >> aggregate_results
aggregate_results >> generate_report
# Visualization
print("\n📊 Workflow Structure:")
ctx.show_info()
nx_graph = ctx.graph.nx_graph()
print("\n📝 Task Dependencies:")
print("-" * 70)
visualize_dependencies(nx_graph)
print("\n🌊 Mermaid Diagram:")
print("-" * 70)
mermaid = draw_mermaid(
nx_graph,
title="Parallel Processing",
node_colors={"load_data": "#90EE90", "aggregate_results": "#FFB6C1", "generate_report": "#87CEEB"},
)
print(mermaid)
print("\n📸 PNG with Custom Colors:")
print("-" * 70)
try:
node_colors = {
"load_data": "lightgreen",
"process_batch_1": "lightyellow",
"process_batch_2": "lightyellow",
"process_batch_3": "lightyellow",
"aggregate_results": "lightcoral",
"generate_report": "lightblue",
}
png_bytes = draw_png(nx_graph, node_colors=node_colors, output_path="/tmp/parallel_workflow.png")
if png_bytes:
print(f"✅ PNG saved: /tmp/parallel_workflow.png ({len(png_bytes)} bytes)")
except Exception as e:
print(f"⚠️ PNG generation unavailable: {e}")
def example_3_complex_workflow():
"""Example 3: Visualize a complex workflow with branching."""
print("\n" + "=" * 70)
print("EXAMPLE 3: Complex ML Training Workflow")
print("=" * 70)
with workflow("ml_training") as ctx:
@task
def load_training_data():
"""Load training dataset."""
return {"samples": 10000}
@task
def preprocess_data():
"""Preprocess the data."""
return {"preprocessed": True}
@task
def feature_engineering():
"""Engineer features."""
return {"features": 50}
@task
def train_model():
"""Train the model."""
return {"model": "trained"}
@task
def validate_model():
"""Validate model performance."""
return {"accuracy": 0.95}
@task
def hyperparameter_tuning():
"""Tune hyperparameters."""
return {"optimized": True}
@task
def final_training():
"""Final model training."""
return {"final_model": True}
@task
def model_evaluation():
"""Evaluate final model."""
return {"eval_metrics": "complete"}
@task
def model_deployment():
"""Deploy the model."""
return {"deployed": True}
@task
def monitoring_setup():
"""Setup monitoring."""
return {"monitoring": "active"}
# Build complex workflow
load_training_data >> preprocess_data >> feature_engineering
feature_engineering >> train_model >> validate_model
validate_model >> hyperparameter_tuning >> final_training
final_training >> model_evaluation >> model_deployment
model_deployment >> monitoring_setup
# Visualization
print("\n📊 Workflow Structure:")
ctx.show_info()
nx_graph = ctx.graph.nx_graph()
print("\n🔍 Graph Analysis:")
print("-" * 70)
show_graph_info(nx_graph)
print("\n🌊 Mermaid Diagram:")
print("-" * 70)
# Color code by stage
stage_colors = {
"load_training_data": "#E6F3FF", # Data loading - light blue
"preprocess_data": "#E6F3FF",
"feature_engineering": "#FFF4E6", # Feature eng - light orange
"train_model": "#FFEBE6", # Training - light red
"validate_model": "#FFEBE6",
"hyperparameter_tuning": "#FFEBE6",
"final_training": "#FFEBE6",
"model_evaluation": "#E6FFE6", # Evaluation - light green
"model_deployment": "#F3E6FF", # Deployment - light purple
"monitoring_setup": "#F3E6FF",
}
mermaid = draw_mermaid(nx_graph, title="ML Training Pipeline", node_colors=stage_colors)
print(mermaid)
print("\n📸 PNG Generation:")
print("-" * 70)
try:
png_colors = {
"load_training_data": "lightblue",
"preprocess_data": "lightblue",
"feature_engineering": "lightyellow",
"train_model": "lightcoral",
"validate_model": "lightcoral",
"hyperparameter_tuning": "lightcoral",
"final_training": "lightcoral",
"model_evaluation": "lightgreen",
"model_deployment": "plum",
"monitoring_setup": "plum",
}
png_bytes = draw_png(nx_graph, node_colors=png_colors, output_path="/tmp/ml_workflow.png")
if png_bytes:
print(f"✅ PNG saved: /tmp/ml_workflow.png ({len(png_bytes)} bytes)")
except Exception as e:
print(f"⚠️ PNG generation unavailable: {e}")
def main():
"""Run all workflow visualization examples."""
print("\n" + "=" * 70)
print("GRAFLOW WORKFLOW VISUALIZATION EXAMPLES")
print("=" * 70)
print("\nThis example demonstrates various methods to visualize Graflow workflows.")
print("Visualization helps you understand, debug, and document your workflows.")
# Run examples
example_1_simple_pipeline()
example_2_parallel_workflow()
example_3_complex_workflow()
# Summary
print("\n" + "=" * 70)
print("VISUALIZATION COMPLETE")
print("=" * 70)
print("\n📁 Generated Files:")
print(" • /tmp/simple_pipeline.png")
print(" • /tmp/parallel_workflow.png")
print(" • /tmp/ml_workflow.png")
print("\n📦 Optional Dependencies:")
print(" • pip install grandalf # ASCII visualization")
print(" • pip install pygraphviz # PNG generation")
print("\n💡 Tips:")
print(" • Use show_info() for quick workflow structure overview")
print(" • Use Mermaid diagrams for documentation (copy to markdown)")
print(" • Use PNG for presentations and reports")
print(" • Use ASCII for quick terminal debugging")
print("\n✨ Next Steps:")
print(" • Try visualizing your own workflows")
print(" • Customize colors to indicate task types")
print(" • Use visualizations to identify optimization opportunities")
if __name__ == "__main__":
main()