-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_node_constraints.py
More file actions
97 lines (82 loc) · 2.64 KB
/
test_node_constraints.py
File metadata and controls
97 lines (82 loc) · 2.64 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
#!/usr/bin/env python3
"""
Test script demonstrating node-based sequence constraints in draw() function.
"""
import networkx as nx
import sys
sys.path.insert(0, '/Users/tomescu/Documents/GitHub/flowpaths')
from flowpaths.utils import graphutils
# Create a simple DAG
G = nx.DiGraph()
G.add_edges_from([
('s', 'a', {'flow': 5}),
('s', 'b', {'flow': 3}),
('a', 'c', {'flow': 5}),
('b', 'c', {'flow': 3}),
('c', 'd', {'flow': 8}),
('d', 't', {'flow': 8}),
])
G.graph['id'] = 'test_graph'
# Example 1: Node-based constraints (NEW)
print("Test 1: Node-based constraints (list of nodes)")
print("=" * 50)
# Define constraints as sequences of nodes
constraints_by_nodes = [
['s', 'a', 'c', 'd'], # Path through a
['s', 'b', 'c', 'd'], # Path through b
]
# Draw with node-based constraints
graphutils.draw(
G,
filename='/tmp/test_node_constraints.pdf',
flow_attr='flow',
subpath_constraints=constraints_by_nodes,
draw_options={'show_edge_weights': True, 'show_graph_title': True}
)
print("✓ Generated: /tmp/test_node_constraints.pdf")
print(" - Nodes in each constraint are highlighted with distinct colors")
print(" - Edges between consecutive nodes in each constraint are dashed")
# Example 2: Edge-based constraints (EXISTING, still supported)
print("\nTest 2: Edge-based constraints (list of edges) - backwards compatible")
print("=" * 50)
constraints_by_edges = [
[('s', 'a'), ('a', 'c'), ('c', 'd')],
[('s', 'b'), ('b', 'c'), ('c', 'd')],
]
graphutils.draw(
G,
filename='/tmp/test_edge_constraints.pdf',
flow_attr='flow',
subpath_constraints=constraints_by_edges,
draw_options={'show_edge_weights': True, 'show_graph_title': True}
)
print("✓ Generated: /tmp/test_edge_constraints.pdf")
print(" - Only edges (not nodes) are highlighted")
print(" - Backward compatible with existing code")
# Example 3: Mixed constraints
print("\nTest 3: Node-based with paths overlay")
print("=" * 50)
paths = [
['s', 'a', 'c', 'd', 't'],
['s', 'b', 'c', 'd', 't'],
]
weights = [5, 3]
constraints_by_nodes = [
['a', 'c'], # Intermediate segment
['b', 'c'], # Another intermediate segment
]
graphutils.draw(
G,
filename='/tmp/test_mixed_constraints.pdf',
flow_attr='flow',
paths=paths,
weights=weights,
subpath_constraints=constraints_by_nodes,
draw_options={'show_edge_weights': True, 'show_graph_title': True}
)
print("✓ Generated: /tmp/test_mixed_constraints.pdf")
print(" - Paths are shown as thick colored edges")
print(" - Constraint nodes are highlighted with distinct colors")
print("\n" + "=" * 50)
print("All tests completed successfully!")
print("=" * 50)