Skip to content

Commit dfa3de6

Browse files
author
Joeperdefloep
committed
added feedback arrows xarray-contrib#181 1.
1 parent 33a4c83 commit dfa3de6

File tree

3 files changed

+280
-24
lines changed

3 files changed

+280
-24
lines changed

xsimlab/dot.py

+41
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from functools import partial
1414

1515
from .utils import variables_dict, import_required, maybe_to_list
16+
17+
# from .process import filter_variables
1618
from .variable import VarIntent, VarType
1719

1820

@@ -136,6 +138,35 @@ def add_var_and_targets(self, p_name, var_name):
136138
):
137139
self._add_var(var, p_name)
138140

141+
def add_feedback_arrows(self):
142+
"""
143+
adds dotted arrows from the last inout processes to all in processes
144+
before the first inout process
145+
"""
146+
# in->inout1->inout2
147+
# ^ /
148+
# \- - - - - /
149+
in_vars = {}
150+
inout_vars = {}
151+
for p_name, p_obj in self.model._processes.items():
152+
p_cls = type(p_obj)
153+
for var_name, var in variables_dict(p_cls).items():
154+
target_keys = tuple(_get_target_keys(p_obj, var_name))
155+
if (
156+
var.metadata["intent"] == VarIntent.IN
157+
and not target_keys in inout_vars
158+
):
159+
if target_keys in in_vars:
160+
in_vars[target_keys].add(p_name)
161+
else:
162+
in_vars[target_keys] = {p_name}
163+
if var.metadata["intent"] == VarIntent.INOUT:
164+
inout_vars[target_keys] = p_name
165+
166+
for target_keys, io_p in inout_vars.items():
167+
for in_p in in_vars[target_keys]:
168+
self.g.edge(io_p, in_p, weight="200", style="dashed")
169+
139170
def get_graph(self):
140171
return self.g
141172

@@ -146,6 +177,7 @@ def to_graphviz(
146177
show_only_variable=None,
147178
show_inputs=False,
148179
show_variables=False,
180+
show_feedbacks=True,
149181
graph_attr={},
150182
**kwargs,
151183
):
@@ -167,6 +199,9 @@ def to_graphviz(
167199
elif show_inputs:
168200
builder.add_inputs()
169201

202+
elif show_feedbacks:
203+
builder.add_feedback_arrows()
204+
170205
return builder.get_graph()
171206

172207

@@ -211,6 +246,7 @@ def dot_graph(
211246
show_only_variable=None,
212247
show_inputs=False,
213248
show_variables=False,
249+
show_feedbacks=True,
214250
**kwargs,
215251
):
216252
"""
@@ -236,6 +272,10 @@ def dot_graph(
236272
show_variables : bool, optional
237273
If True, show also the other variables (default: False).
238274
Ignored if `show_only_variable` is not None.
275+
show_feedbacks: bool, optional
276+
if True, draws dotted arrows to indicate what processes use updated
277+
variables in the next timestep. (default: True)
278+
Ignored if `show_variables` is not None
239279
**kwargs
240280
Additional keyword arguments to forward to `to_graphviz`.
241281
@@ -262,6 +302,7 @@ def dot_graph(
262302
show_only_variable=show_only_variable,
263303
show_inputs=show_inputs,
264304
show_variables=show_variables,
305+
show_feedbacks=show_feedbacks,
265306
**kwargs,
266307
)
267308

0 commit comments

Comments
 (0)