-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconvert_to_python.py
More file actions
183 lines (139 loc) · 6.18 KB
/
Copy pathconvert_to_python.py
File metadata and controls
183 lines (139 loc) · 6.18 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
from jinja2 import Environment, FileSystemLoader
import os
from inspect import signature
from .pathsim_utils import (
map_str_to_object,
map_str_to_event,
make_blocks,
make_global_variables,
get_input_index,
get_output_index,
find_block_by_id,
find_node_by_id,
make_var_name,
)
def convert_graph_to_python(graph_data: dict) -> str:
"""Convert graph data to a Python script as a string."""
# Get the directory of this file to properly reference templates
current_dir = os.path.dirname(os.path.abspath(__file__))
templates_dir = os.path.join(current_dir, "templates")
environment = Environment(loader=FileSystemLoader(templates_dir))
template = environment.get_template("template_with_macros.py")
# Process the graph data
context = process_graph_data_from_dict(graph_data)
# Render the template
return template.render(context)
def process_node_data(nodes: list[dict]) -> list[dict]:
"""
Given a list of node and edge data as dictionaries, process the nodes to create
variable names, class names, and expected arguments for each node.
Returns:
The processed node data with variable names, class names, and expected arguments.
"""
nodes = nodes.copy()
for node in nodes:
node["var_name"] = make_var_name(node)
# Add pathsim class name
block_class = map_str_to_object.get(node["type"])
node["class_name"] = block_class.__name__
node["module_name"] = block_class.__module__
# Add expected arguments
node["expected_arguments"] = signature(block_class).parameters
return nodes
# TODO: this is effectively a duplicate of pathsim_utils.make_connections
# need to refactor
def make_edge_data(data: dict) -> list[dict]:
"""
Process edges to add source/target variable names and ports.
Does it by creating pathsim.blocks and Connections from the data with
``make_blocks`` and ``make_connections`` functions.
Then, since the data (source/target blocks, ports) is already in the
connections, we can simply read the ports id from the actual pathsim
connections and add them to the edges.
Args:
data: The graph data containing "nodes" and "edges".
Returns:
The processed edges with source/target variable names and ports.
"""
data = data.copy()
# we need the namespace since we call make_blocks
namespace = make_global_variables(data["globalVariables"])
blocks, _ = make_blocks(data["nodes"], eval_namespace=namespace)
# Process each node and its sorted incoming edges to create connections
block_to_input_index = {b: 0 for b in blocks}
for node in data["nodes"]:
outgoing_edges = [
edge for edge in data["edges"] if edge["source"] == node["id"]
]
outgoing_edges.sort(key=lambda x: x["target"])
block = find_block_by_id(node["id"], blocks)
for edge in outgoing_edges:
target_block = find_block_by_id(edge["target"], blocks)
target_node = find_node_by_id(edge["target"], data["nodes"])
output_index = get_output_index(block, edge)
input_index = get_input_index(target_block, edge, block_to_input_index)
# if it's a scope, find labels
if target_node["type"] == "scope":
if target_node["data"]["labels"] == "":
target_node["data"]["labels"] = []
if isinstance(target_node["data"]["labels"], list):
label = node["data"]["label"]
if edge["sourceHandle"]:
label += f" ({edge['sourceHandle']})"
target_node["data"]["labels"].append(label)
edge["source_var_name"] = node["var_name"]
edge["target_var_name"] = target_node["var_name"]
if isinstance(output_index, str):
edge["source_port"] = f"['{output_index}']"
else:
edge["source_port"] = f"[{output_index}]"
if isinstance(input_index, str):
edge["target_port"] = f"['{input_index}']"
else:
edge["target_port"] = f"[{input_index}]"
block_to_input_index[target_block] += 1
return data["edges"]
def make_events_data(data: dict) -> list[dict]:
"""
Process events data from the graph data.
This function extracts event definitions from the graph data and prepares them
for use in the simulation.
Args:
data: The graph data containing "events".
Returns:
A list of processed events.
"""
for event in data["events"]:
# Add pathsim class name
event_class = map_str_to_event.get(event["type"])
event["class_name"] = event_class.__name__
event["module_name"] = event_class.__module__
# Add expected arguments
event["expected_arguments"] = signature(event_class).parameters
if "func_evt" in event:
# replace the name of the function by something unique
func_evt = event["func_evt"]
func_evt = func_evt.replace("def func_evt", f"def {event['name']}_func_evt")
event["func_evt"] = f"{event['name']}_func_evt"
event["func_evt_desc"] = func_evt
if "func_act" in event:
# replace the name of the function by something unique
func_act = event["func_act"]
func_act = func_act.replace("def func_act", f"def {event['name']}_func_act")
event["func_act"] = f"{event['name']}_func_act"
event["func_act_desc"] = func_act
return data["events"]
def process_graph_data_from_dict(data: dict) -> dict:
"""
Process graph data from a dictionary.
Adds variable names, class names, and expected arguments to nodes,
and processes edges to include source/target variable names and ports.
This processed data can then be more easily used to generate Python code.
"""
data = data.copy()
# Process nodes to create variable names and class names
data["nodes"] = process_node_data(data["nodes"])
# Process to add source/target variable names to edges + ports
data["edges"] = make_edge_data(data)
data["events"] = make_events_data(data)
return data