Skip to content

Commit 37243f6

Browse files
committed
change system name when converting tf to ss
1 parent 0b5e3fa commit 37243f6

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

control/iosys.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2395,7 +2395,9 @@ def ss(*args, **kwargs):
23952395
"non-unique state space realization")
23962396

23972397
# Create a state space system from an LTI system
2398-
sys = LinearIOSystem(_convert_to_statespace(sys), **kwargs)
2398+
sys = LinearIOSystem(
2399+
_convert_to_statespace(sys, use_prefix_suffix=True), **kwargs)
2400+
23992401
else:
24002402
raise TypeError("ss(sys): sys must be a StateSpace or "
24012403
"TransferFunction object. It is %s." % type(sys))

control/statesp.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ def output(self, t, x, u=None, params=None):
15191519

15201520

15211521
# TODO: add discrete time check
1522-
def _convert_to_statespace(sys):
1522+
def _convert_to_statespace(sys, use_prefix_suffix=False):
15231523
"""Convert a system to state space form (if needed).
15241524
15251525
If sys is already a state space, then it is returned. If sys is a
@@ -1556,11 +1556,10 @@ def _convert_to_statespace(sys):
15561556
denorder, den, num, tol=0)
15571557

15581558
states = ssout[0]
1559-
return StateSpace(
1559+
newsys = StateSpace(
15601560
ssout[1][:states, :states], ssout[2][:states, :sys.ninputs],
1561-
ssout[3][:sys.noutputs, :states], ssout[4], sys.dt,
1562-
inputs=sys.input_labels, outputs=sys.output_labels,
1563-
name=sys.name)
1561+
ssout[3][:sys.noutputs, :states], ssout[4], sys.dt)
1562+
15641563
except ImportError:
15651564
# No Slycot. Scipy tf->ss can't handle MIMO, but static
15661565
# MIMO is an easy special case we can check for here
@@ -1585,9 +1584,13 @@ def _convert_to_statespace(sys):
15851584
# the squeeze
15861585
A, B, C, D = \
15871586
sp.signal.tf2ss(squeeze(sys.num), squeeze(sys.den))
1588-
return StateSpace(
1589-
A, B, C, D, sys.dt, inputs=sys.input_labels,
1590-
outputs=sys.output_labels, name=sys.name)
1587+
newsys = StateSpace(A, B, C, D, sys.dt)
1588+
1589+
# Copy over the signal (and system) names
1590+
newsys._copy_names(
1591+
sys,
1592+
prefix_suffix_name='converted' if use_prefix_suffix else None)
1593+
return newsys
15911594

15921595
elif isinstance(sys, FrequencyResponseData):
15931596
raise TypeError("Can't convert FRD to StateSpace system.")
@@ -1600,7 +1603,6 @@ def _convert_to_statespace(sys):
16001603
except Exception:
16011604
raise TypeError("Can't convert given type to StateSpace system.")
16021605

1603-
16041606
# TODO: add discrete time option
16051607
def _rss_generate(
16061608
states, inputs, outputs, cdtype, strictly_proper=False, name=None):
@@ -1914,7 +1916,8 @@ def tf2ss(*args, **kwargs):
19141916
if not isinstance(sys, TransferFunction):
19151917
raise TypeError("tf2ss(sys): sys must be a TransferFunction "
19161918
"object.")
1917-
return StateSpace(_convert_to_statespace(sys), **kwargs)
1919+
return StateSpace(_convert_to_statespace(sys, use_prefix_suffix=True),
1920+
**kwargs)
19181921
else:
19191922
raise ValueError("Needs 1 or 2 arguments; received %i." % len(args))
19201923

control/tests/namedio_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def test_io_naming(fun, args, kwargs):
169169
assert sys_ss != sys_r
170170
assert sys_ss.input_labels == input_labels
171171
assert sys_ss.output_labels == output_labels
172+
if not isinstance(sys_r, ct.StateSpace):
173+
# System should get unique name
174+
assert sys_ss.name != sys_r.name
172175

173176
# Reassign system and signal names
174177
sys_ss = ct.ss(
@@ -247,11 +250,11 @@ def test_convert_to_statespace():
247250

248251
# check that name, inputs, and outputs passed through
249252
sys_new = ct.ss(sys)
250-
assert sys_new.name == 'sys'
253+
assert sys_new.name == 'sys$converted'
251254
assert sys_new.input_labels == ['u']
252255
assert sys_new.output_labels == ['y']
253256
sys_new = ct.ss(sys_static)
254-
assert sys_new.name == 'sys_static'
257+
assert sys_new.name == 'sys_static$converted'
255258
assert sys_new.input_labels == ['u']
256259
assert sys_new.output_labels == ['y']
257260

0 commit comments

Comments
 (0)