Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make systems/inputs/outputs/connections editable #52

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions pyxdsm/XDSM.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import json
import subprocess
from collections import namedtuple
from recordclass import recordclass

from pyxdsm import __version__ as pyxdsm_version

Expand Down Expand Up @@ -110,10 +110,10 @@ def _label_to_spec(label, spec):
spec.add(var)


System = namedtuple("System", "node_name style label stack faded label_width spec_name")
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")
System = recordclass("System", "node_name style label stack faded label_width spec_name")
Input = recordclass("Input", "node_name label label_width style stack faded")
Output = recordclass("Output", "node_name label label_width style stack faded side")
Connection = recordclass("Connection", "src target label label_width style stack faded")


class XDSM(object):
Expand Down Expand Up @@ -196,6 +196,7 @@ def add_system(

sys = System(node_name, style, label, stack, faded, label_width, spec_name)
self.systems.append(sys)
return sys

def add_input(self, name, label, label_width=None, style="DataIO", stack=False, faded=False):
"""
Expand Down Expand Up @@ -229,7 +230,9 @@ def add_input(self, name, label, label_width=None, style="DataIO", stack=False,
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, faded)
inp = Input("output_" + name, label, label_width, style, stack, faded)
self.ins[name] = inp
return inp

def add_output(self, name, label, label_width=None, style="DataIO", stack=False, faded=False, side="left"):
"""
Expand Down Expand Up @@ -268,11 +271,14 @@ def add_output(self, name, label, label_width=None, style="DataIO", stack=False,
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, faded, side)
outp = Output("left_output_" + name, label, label_width, style, stack, faded, side)
self.left_outs[name] = outp
elif side == "right":
self.right_outs[name] = Output("right_output_" + name, label, label_width, style, stack, faded, side)
outp = Output("right_output_" + name, label, label_width, style, stack, faded, side)
self.right_outs[name] = outp
else:
raise ValueError("The option 'side' must be given as either 'left' or 'right'!")
return outp

def connect(
self,
Expand Down Expand Up @@ -325,7 +331,9 @@ def connect(
if (not isinstance(label_width, int)) and (label_width is not None):
raise ValueError("label_width argument must be an integer")

self.connections.append(Connection(src, target, label, label_width, style, stack, faded))
connection = Connection(src, target, label, label_width, style, stack, faded)
self.connections.append(connection)
return connection

def add_process(self, systems, arrow=True):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"pyxdsm",
],
package_data={"pyxdsm": ["*.tex"]},
install_requires=["numpy>=1.16"],
install_requires=["numpy>=1.16", "recordclass>=0.16"],
python_requires=">=3",
classifiers=[
"Operating System :: OS Independent",
Expand Down
11 changes: 10 additions & 1 deletion tests/test_xdsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shutil
import tempfile
import subprocess
from pyxdsm.XDSM import XDSM, OPT, FUNC, SOLVER, LEFT, RIGHT
from pyxdsm.XDSM import XDSM, OPT, FUNC, SOLVER, LEFT, RIGHT, IFUNC
from numpy.distutils.exec_command import find_executable

basedir = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -323,6 +323,15 @@ def test_write_outdir(self):
# no files outside the subdirs
self.assertFalse(any(os.path.isfile(fp) for fp in os.listdir(self.tempdir)))

def test_mutability_of_systems(self):
x = XDSM()
f = x.add_system("func", FUNC, "func")
g = x.add_system("cons", IFUNC, "cons")
f.stack = True
g.label = "cons_new"
self.assertEqual(x.systems[0].stack, True)
self.assertEqual(x.systems[1].label, "cons_new")


if __name__ == "__main__":
unittest.main()