Skip to content

Commit ce3a231

Browse files
add capability to switch to legacy defaults (#424)
* add capability to switch to legacy defaults with config.use_legacy_defaults(version) * reverted to using matrix as default in statesp to pass tests * added documentation to statesp, xferfcn, and conventions.rst describing new config parameters default_dt and remove_useless_states
1 parent d5666d5 commit ce3a231

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

control/config.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
__all__ = ['defaults', 'set_defaults', 'reset_defaults',
1313
'use_matlab_defaults', 'use_fbs_defaults',
14-
'use_numpy_matrix']
14+
'use_legacy_defaults', 'use_numpy_matrix']
1515

1616
# Package level default values
1717
_control_defaults = {
@@ -53,6 +53,9 @@ def reset_defaults():
5353
from .rlocus import _rlocus_defaults
5454
defaults.update(_rlocus_defaults)
5555

56+
from .xferfcn import _xferfcn_defaults
57+
defaults.update(_xferfcn_defaults)
58+
5659
from .statesp import _statesp_defaults
5760
defaults.update(_statesp_defaults)
5861

@@ -156,3 +159,16 @@ class and functions. If flat is `False`, then matrices are
156159
warnings.warn("Return type numpy.matrix is soon to be deprecated.",
157160
stacklevel=2)
158161
set_defaults('statesp', use_numpy_matrix=flag)
162+
163+
def use_legacy_defaults(version):
164+
""" Sets the defaults to whatever they were in a given release.
165+
166+
Parameters
167+
----------
168+
version : string
169+
version number of the defaults desired. Currently only supports `0.8.3`.
170+
"""
171+
if version == '0.8.3':
172+
use_numpy_matrix(True) # alternatively: set_defaults('statesp', use_numpy_matrix=True)
173+
else:
174+
raise ValueError('''version number not recognized. Possible values are: ['0.8.3']''')

control/statesp.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
# Define module default parameter values
7272
_statesp_defaults = {
7373
'statesp.use_numpy_matrix': True,
74-
}
74+
'statesp.default_dt': None,
75+
'statesp.remove_useless_states': True,
76+
}
7577

7678

7779
def _ssmatrix(data, axis=1):
@@ -147,7 +149,8 @@ class StateSpace(LTI):
147149
Setting dt = 0 specifies a continuous system, while leaving dt = None
148150
means the system timebase is not specified. If 'dt' is set to True, the
149151
system will be treated as a discrete time system with unspecified sampling
150-
time.
152+
time. The default value of 'dt' is None and can be changed by changing the
153+
value of ``control.config.defaults['statesp.default_dt']``.
151154
152155
"""
153156

@@ -171,7 +174,7 @@ def __init__(self, *args, **kw):
171174
if len(args) == 4:
172175
# The user provided A, B, C, and D matrices.
173176
(A, B, C, D) = args
174-
dt = None
177+
dt = config.defaults['statesp.default_dt']
175178
elif len(args) == 5:
176179
# Discrete time system
177180
(A, B, C, D, dt) = args
@@ -187,12 +190,12 @@ def __init__(self, *args, **kw):
187190
try:
188191
dt = args[0].dt
189192
except NameError:
190-
dt = None
193+
dt = config.defaults['statesp.default_dt']
191194
else:
192195
raise ValueError("Needs 1 or 4 arguments; received %i." % len(args))
193196

194197
# Process keyword arguments
195-
remove_useless = kw.get('remove_useless', True)
198+
remove_useless = kw.get('remove_useless', config.defaults['statesp.remove_useless_states'])
196199

197200
# Convert all matrices to standard form
198201
A = _ssmatrix(A)

control/tests/config_test.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_get_param(self):
4848

4949

5050
def test_fbs_bode(self):
51-
ct.use_fbs_defaults();
51+
ct.use_fbs_defaults()
5252

5353
# Generate a Bode plot
5454
plt.figure()
@@ -94,7 +94,7 @@ def test_fbs_bode(self):
9494
ct.reset_defaults()
9595

9696
def test_matlab_bode(self):
97-
ct.use_matlab_defaults();
97+
ct.use_matlab_defaults()
9898

9999
# Generate a Bode plot
100100
plt.figure()
@@ -211,6 +211,23 @@ def test_reset_defaults(self):
211211
self.assertEqual(
212212
ct.config.defaults['freqplot.feature_periphery_decades'], 1.0)
213213

214+
def test_legacy_defaults(self):
215+
ct.use_legacy_defaults('0.8.3')
216+
assert(isinstance(ct.ss(0,0,0,1).D, np.matrix))
217+
ct.reset_defaults()
218+
assert(isinstance(ct.ss(0,0,0,1).D, np.ndarray))
219+
220+
def test_change_default_dt(self):
221+
ct.set_defaults('statesp', default_dt=0)
222+
self.assertEqual(ct.ss(0,0,0,1).dt, 0)
223+
ct.set_defaults('statesp', default_dt=None)
224+
self.assertEqual(ct.ss(0,0,0,1).dt, None)
225+
ct.set_defaults('xferfcn', default_dt=0)
226+
self.assertEqual(ct.tf(1, 1).dt, 0)
227+
ct.set_defaults('xferfcn', default_dt=None)
228+
self.assertEqual(ct.tf(1, 1).dt, None)
229+
230+
214231
def tearDown(self):
215232
# Get rid of any figures that we created
216233
plt.close('all')

control/xferfcn.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,15 @@
6363
from itertools import chain
6464
from re import sub
6565
from .lti import LTI, timebaseEqual, timebase, isdtime
66+
from . import config
6667

6768
__all__ = ['TransferFunction', 'tf', 'ss2tf', 'tfdata']
6869

6970

71+
# Define module default parameter values
72+
_xferfcn_defaults = {
73+
'xferfcn.default_dt': None}
74+
7075
class TransferFunction(LTI):
7176

7277
"""TransferFunction(num, den[, dt])
@@ -88,7 +93,9 @@ class TransferFunction(LTI):
8893
instance variable and setting it to something other than 'None'. If 'dt'
8994
has a non-zero value, then it must match whenever two transfer functions
9095
are combined. If 'dt' is set to True, the system will be treated as a
91-
discrete time system with unspecified sampling time.
96+
discrete time system with unspecified sampling time. The default value of
97+
'dt' is None and can be changed by changing the value of
98+
``control.config.defaults['xferfcn.default_dt']``.
9299
93100
The TransferFunction class defines two constants ``s`` and ``z`` that
94101
represent the differentiation and delay operators in continuous and
@@ -117,7 +124,7 @@ def __init__(self, *args):
117124
if len(args) == 2:
118125
# The user provided a numerator and a denominator.
119126
(num, den) = args
120-
dt = None
127+
dt = config.defaults['xferfcn.default_dt']
121128
elif len(args) == 3:
122129
# Discrete time transfer function
123130
(num, den, dt) = args
@@ -133,7 +140,7 @@ def __init__(self, *args):
133140
try:
134141
dt = args[0].dt
135142
except NameError: # pragma: no coverage
136-
dt = None
143+
dt = config.defaults['xferfcn.default_dt']
137144
else:
138145
raise ValueError("Needs 1, 2 or 3 arguments; received %i."
139146
% len(args))

doc/conventions.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ the result will be a discrete time system with the sample time of the latter
9898
system. For continuous time systems, the :func:`sample_system` function or
9999
the :meth:`StateSpace.sample` and :meth:`TransferFunction.sample` methods
100100
can be used to create a discrete time system from a continuous time system.
101-
See :ref:`utility-and-conversions`.
101+
See :ref:`utility-and-conversions`. The default value of 'dt' can be changed by
102+
changing the values of ``control.config.defaults['statesp.default_dt']`` and
103+
``control.config.defaults['xferfcn.default_dt']``.
102104

103105
Conversion between representations
104106
----------------------------------
@@ -220,9 +222,15 @@ Selected variables that can be configured, along with their default values:
220222
* freqplot.feature_periphery_decade (1.0): How many decades to include in the
221223
frequency range on both sides of features (poles, zeros).
222224
223-
* statesp.use_numpy_matrix: set the return type for state space matrices to
225+
* statesp.use_numpy_matrix (True): set the return type for state space matrices to
224226
`numpy.matrix` (verus numpy.ndarray)
225227
228+
* statesp.default_dt and xferfcn.default_dt (None): set the default value of dt when
229+
constructing new LTI systems
230+
231+
* statesp.remove_useless_states (True): remove states that have no effect on the
232+
input-output dynamics of the system
233+
226234
Additional parameter variables are documented in individual functions
227235
228236
Functions that can be used to set standard configurations:
@@ -234,3 +242,4 @@ Functions that can be used to set standard configurations:
234242
use_fbs_defaults
235243
use_matlab_defaults
236244
use_numpy_matrix
245+
use_legacy_defaults

0 commit comments

Comments
 (0)