Skip to content

Commit 614de83

Browse files
committed
add signal_table function, test against show_names
1 parent b5925c3 commit 614de83

3 files changed

Lines changed: 60 additions & 13 deletions

File tree

control/nlsys.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
__all__ = ['NonlinearIOSystem', 'InterconnectedSystem', 'nlsys',
3333
'input_output_response', 'find_eqpt', 'linearize',
34-
'interconnect']
34+
'interconnect', 'signal_table']
3535

3636

3737
class NonlinearIOSystem(InputOutputSystem):
@@ -1020,7 +1020,7 @@ def signal_table(self, show_names=False):
10201020
>>> P = ct.ss(1,1,1,0, inputs='u', outputs='y')
10211021
>>> C = ct.tf(10, [.1, 1], inputs='e', outputs='u')
10221022
>>> L = ct.interconnect([C, P], inputs='e', outputs='y')
1023-
>>> L.signal_table()
1023+
>>> L.signal_table() # doctest: +SKIP
10241024
signal | source | destination
10251025
--------------------------------------------------------------
10261026
e | input | system 0
@@ -2563,3 +2563,36 @@ def _convert_static_iosystem(sys):
25632563
return NonlinearIOSystem(
25642564
None, lambda t, x, u, params: sys @ u,
25652565
outputs=sys.shape[0], inputs=sys.shape[1])
2566+
2567+
def signal_table(sys, **kwargs):
2568+
"""Print table of signal names, sources, and destinations.
2569+
2570+
Intended primarily for systems that have been connected implicitly
2571+
using signal names.
2572+
2573+
Parameters
2574+
----------
2575+
sys : :class:`InterconnectedSystem`
2576+
Interconnected system object
2577+
show_names : bool (optional)
2578+
Instead of printing out the system number, print out the name of
2579+
each system. Default is False because system name is not usually
2580+
specified when performing implicit interconnection using
2581+
:func:`interconnect`.
2582+
2583+
Examples
2584+
--------
2585+
>>> P = ct.ss(1,1,1,0, inputs='u', outputs='y')
2586+
>>> C = ct.tf(10, [.1, 1], inputs='e', outputs='u')
2587+
>>> L = ct.interconnect([C, P], inputs='e', outputs='y')
2588+
>>> ct.signal_table(L) # doctest: +SKIP
2589+
signal | source | destination
2590+
--------------------------------------------------------------
2591+
e | input | system 0
2592+
u | system 0 | system 1
2593+
y | system 1 | output
2594+
"""
2595+
assert isinstance(sys, InterconnectedSystem), "system must be"\
2596+
"an InterconnectedSystem."
2597+
2598+
sys.signal_table(**kwargs)

control/tests/interconnect_test.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,37 @@ def test_interconnect_docstring():
201201
np.testing.assert_almost_equal(T.C @ T. A @ T.B, T_ss.C @ T_ss.A @ T_ss.B)
202202
np.testing.assert_almost_equal(T.D, T_ss.D)
203203

204-
def test_signal_table(capsys):
205-
P = ct.ss(1,1,1,0, inputs='u', outputs='y')
206-
C = ct.tf(10, [.1, 1], inputs='e', outputs='u')
204+
@pytest.mark.parametrize("show_names", (True, False))
205+
def test_signal_table(capsys, show_names):
206+
P = ct.ss(1,1,1,0, inputs='u', outputs='y', name='P')
207+
C = ct.tf(10, [.1, 1], inputs='e', outputs='u', name='C')
207208
L = ct.interconnect([C, P], inputs='e', outputs='y')
208-
L.signal_table()
209-
captured = capsys.readouterr().out
209+
L.signal_table(show_names=show_names)
210+
captured_from_method = capsys.readouterr().out
211+
212+
ct.signal_table(L, show_names=show_names)
213+
captured_from_function = capsys.readouterr().out
210214

211215
# break the following strings separately because the printout order varies
212-
# because signals are stored as a dict
216+
# because signal names are stored as a set
213217
mystrings = \
214218
["signal | source | destination",
215-
"-------------------------------------------------------------",
216-
"e | input | system 0",
217-
"u | system 0 | system 1",
218-
"y | system 1 | output"]
219+
"-------------------------------------------------------------"]
220+
if show_names:
221+
mystrings += \
222+
["e | input | C",
223+
"u | C | P",
224+
"y | P | output"]
225+
else:
226+
mystrings += \
227+
["e | input | system 0",
228+
"u | system 0 | system 1",
229+
"y | system 1 | output"]
219230

220231
for str_ in mystrings:
221-
assert str_ in captured
232+
assert str_ in captured_from_method
233+
assert str_ in captured_from_function
234+
222235

223236
def test_interconnect_exceptions():
224237
# First make sure the docstring example works

doc/control.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ System interconnections
3636
negate
3737
parallel
3838
series
39+
signal_table
3940

4041

4142
Frequency domain plotting

0 commit comments

Comments
 (0)