Skip to content

Commit

Permalink
Allow fading of connection edges, inputs, and outputs (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
A-CGray authored Aug 25, 2022
1 parent 3f39ffa commit b4e0c7a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
*.lay
*.plt
_build*
*.egg-info
*.vscode
58 changes: 41 additions & 17 deletions pyxdsm/XDSM.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def _label_to_spec(label, spec):


System = namedtuple("System", "node_name style label stack faded label_width spec_name")
Input = namedtuple("Input", "node_name label label_width style stack")
Output = namedtuple("Output", "node_name label label_width style stack side")
Input = namedtuple("Input", "node_name label label_width style stack faded")
Output = namedtuple("Output", "node_name label label_width style stack faded side")
Connection = namedtuple("Connection", "src target label label_width style stack faded")


Expand Down Expand Up @@ -197,7 +197,7 @@ def add_system(
sys = System(node_name, style, label, stack, faded, label_width, spec_name)
self.systems.append(sys)

def add_input(self, name, label, label_width=None, style="DataIO", stack=False):
def add_input(self, name, label, label_width=None, style="DataIO", stack=False, faded=False):
"""
Add an input, which will appear in the top row of the diagram.
Expand Down Expand Up @@ -225,10 +225,13 @@ def add_input(self, name, label, label_width=None, style="DataIO", stack=False):
stack : bool
If true, the system will be displayed as several stacked rectangles,
indicating the component is executed in parallel.
faded : bool
If true, the component will be faded, in order to highlight some other system.
"""
self.ins[name] = Input("output_" + name, label, label_width, style, stack)
self.ins[name] = Input("output_" + name, label, label_width, style, stack, faded)

def add_output(self, name, label, label_width=None, style="DataIO", stack=False, side="left"):
def add_output(self, name, label, label_width=None, style="DataIO", stack=False, faded=False, side="left"):
"""
Add an output, which will appear in the left or right-most column of the diagram.
Expand Down Expand Up @@ -257,14 +260,17 @@ def add_output(self, name, label, label_width=None, style="DataIO", stack=False,
If true, the system will be displayed as several stacked rectangles,
indicating the component is executed in parallel.
faded : bool
If true, the component will be faded, in order to highlight some other system.
side : str
Must be one of ``['left', 'right']``. This parameter controls whether the output
is placed on the left-most column or the right-most column of the diagram.
"""
if side == "left":
self.left_outs[name] = Output("left_output_" + name, label, label_width, style, stack, side)
self.left_outs[name] = Output("left_output_" + name, label, label_width, style, stack, faded, side)
elif side == "right":
self.right_outs[name] = Output("right_output_" + name, label, label_width, style, stack, side)
self.right_outs[name] = Output("right_output_" + name, label, label_width, style, stack, faded, side)
else:
raise ValueError("The option 'side' must be given as either 'left' or 'right'!")

Expand Down Expand Up @@ -370,9 +376,9 @@ def _build_node_grid(self):
# add all the components on the diagonal
for i_row, j_col, comp in zip(comps_rows, comps_cols, self.systems):
style = comp.style
if comp.stack is True: # stacking
if comp.stack:
style += ",stack"
if comp.faded is True: # fading
if comp.faded:
style += ",faded"

label = _parse_label(comp.label, comp.label_width)
Expand All @@ -391,9 +397,9 @@ def _build_node_grid(self):
loc = (src_row, target_col)

style = conn.style
if conn.stack is True: # stacking
if conn.stack:
style += ",stack"
if conn.faded is True: # fading
if conn.faded:
style += ",faded"

label = _parse_label(conn.label, conn.label_width)
Expand All @@ -409,6 +415,8 @@ def _build_node_grid(self):
style = out.style
if out.stack:
style += ",stack"
if out.faded:
style += ",faded"

i_row = row_idx_map[comp_name]
loc = (i_row, 0)
Expand All @@ -423,6 +431,8 @@ def _build_node_grid(self):
style = out.style
if out.stack:
style += ",stack"
if out.faded:
style += ",faded"

i_row = row_idx_map[comp_name]
loc = (i_row, -1)
Expand All @@ -437,6 +447,8 @@ def _build_node_grid(self):
style = inp.style
if inp.stack:
style += ",stack"
if inp.faded:
style += ",faded"

j_col = col_idx_map[comp_name]
loc = (0, j_col)
Expand All @@ -456,23 +468,35 @@ def _build_edges(self):
h_edges = []
v_edges = []

edge_string = "({start}) edge [DataLine] ({end})"
edge_format_string = "({start}) edge [{style}] ({end})"
for conn in self.connections:
style = "DataLine"
if conn.faded:
style += ",faded"
od_node_name = "{}-{}".format(conn.src, conn.target)
h_edges.append(edge_string.format(start=conn.src, end=od_node_name))
v_edges.append(edge_string.format(start=od_node_name, end=conn.target))
h_edges.append(edge_format_string.format(start=conn.src, end=od_node_name, style=style))
v_edges.append(edge_format_string.format(start=od_node_name, end=conn.target, style=style))

for comp_name, out in self.left_outs.items():
style = "DataLine"
if out.faded:
style += ",faded"
node_name = out.node_name
h_edges.append(edge_string.format(start=comp_name, end=node_name))
h_edges.append(edge_format_string.format(start=comp_name, end=node_name, style=style))

for comp_name, out in self.right_outs.items():
style = "DataLine"
if out.faded:
style += ",faded"
node_name = out.node_name
h_edges.append(edge_string.format(start=comp_name, end=node_name))
h_edges.append(edge_format_string.format(start=comp_name, end=node_name, style=style))

for comp_name, inp in self.ins.items():
style = "DataLine"
if inp.faded:
style += ",faded"
node_name = inp.node_name
v_edges.append(edge_string.format(start=comp_name, end=node_name))
v_edges.append(edge_format_string.format(start=comp_name, end=node_name, style=style))

paths_str = "% Horizontal edges\n" + "\n".join(h_edges) + "\n"
paths_str += "% Vertical edges\n" + "\n".join(v_edges) + ";"
Expand Down
2 changes: 1 addition & 1 deletion pyxdsm/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.2.1"
__version__ = "2.2.2"
2 changes: 1 addition & 1 deletion pyxdsm/diagram_styles.tex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
%% A simple command to give the repeated structure look for components and data
\tikzstyle{stack} = [double copy shadow={shadow xshift=.75ex, shadow yshift=-.75ex}]
%% A simple command to fade components and data, e.g. demonstrating a sequence of steps in an animation
\tikzstyle{faded} = [draw=black!50,fill=white,text opacity=0.5]
\tikzstyle{faded} = [draw=black!10,fill=white,text opacity=0.2]

%% Simple fading commands for the lines
\tikzstyle{fadeddata} = [color=black!20]
Expand Down

0 comments on commit b4e0c7a

Please sign in to comment.