Skip to content

Commit d66db80

Browse files
committed
refactor config module to use local dictionaries + better helper functions
1 parent 44de990 commit d66db80

10 files changed

+406
-172
lines changed

control/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,6 @@
8484
from numpy.testing import Tester
8585
test = Tester().test
8686
bench = Tester().bench
87+
88+
# Initialize default parameter values
89+
reset_defaults()

control/config.py

+97-30
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,103 @@
99

1010
import warnings
1111

12-
__all__ = ['reset_defaults', 'use_matlab_defaults', 'use_fbs_defaults',
12+
__all__ = ['defaults', 'set_defaults', 'reset_defaults',
13+
'use_matlab_defaults', 'use_fbs_defaults',
1314
'use_numpy_matrix']
1415

15-
# Dictionary of default values
16-
defaults = {
17-
'bode.dB':False, 'bode.deg':True, 'bode.Hz':False, 'bode.grid':True,
18-
'freqplot.feature_periphery_decades':1, 'freqplot.number_of_samples':None,
19-
'statesp.use_numpy_matrix':True,
16+
# Package level default values
17+
_control_defaults = {
18+
# No package level defaults (yet)
2019
}
20+
defaults = dict(_control_defaults)
21+
22+
23+
def set_defaults(module, **keywords):
24+
"""Set default values of parameters for a module.
25+
26+
The set_defaults() function can be used to modify multiple parameter
27+
values for a module at the same time, using keyword arguments:
28+
29+
control.set_defaults('module', param1=val, param2=val)
30+
31+
"""
32+
if not isinstance(module, str):
33+
raise ValueError("module must be a string")
34+
for key, val in keywords.items():
35+
defaults[module + '.' + key] = val
2136

2237

2338
def reset_defaults():
24-
"""Reset configuration values to their default values."""
25-
defaults['bode.dB'] = False
26-
defaults['bode.deg'] = True
27-
defaults['bode.Hz'] = False
28-
defaults['bode.grid'] = True
29-
defaults['freqplot.number_of_samples'] = None
30-
defaults['freqplot.feature_periphery_decades'] = 1.0
31-
defaults['statesp.use_numpy_matrix'] = True
39+
"""Reset configuration values to their default (initial) values."""
40+
# System level defaults
41+
defaults.update(_control_defaults)
42+
43+
from .freqplot import _bode_defaults, _freqplot_defaults
44+
defaults.update(_bode_defaults)
45+
defaults.update(_freqplot_defaults)
46+
47+
from .nichols import _nichols_defaults
48+
defaults.update(_nichols_defaults)
49+
50+
from .pzmap import _pzmap_defaults
51+
defaults.update(_pzmap_defaults)
52+
53+
from .rlocus import _rlocus_defaults
54+
defaults.update(_rlocus_defaults)
55+
56+
from .statesp import _statesp_defaults
57+
defaults.update(_statesp_defaults)
58+
59+
60+
def _get_param(module, param, argval=None, defval=None, pop=False):
61+
"""Return the default value for a configuration option.
62+
63+
The _get_param() function is a utility function used to get the value of a
64+
parameter for a module based on the default parameter settings and any
65+
arguments passed to the function. The precedence order for parameters is
66+
the value passed to the function (as a keyword), the value from the
67+
config.defaults dictionary, and the default value `defval`.
68+
69+
Parameters
70+
----------
71+
module : str
72+
Name of the module whose parameters are being requested.
73+
param : str
74+
Name of the parameter value to be determeind.
75+
argval : object or dict
76+
Value of the parameter as passed to the function. This can either be
77+
an object or a dictionary (i.e. the keyword list from the function
78+
call). Defaults to None.
79+
defval : object
80+
Default value of the parameter to use, if it is not located in the
81+
`config.defaults` dictionary. If a dictionary is provided, then
82+
`module.param` is used to determine the default value. Defaults to
83+
None.
84+
pop : bool
85+
If True and if argval is a dict, then pop the remove the parameter
86+
entry from the argval dict after retreiving it. This allows the use
87+
of a keyword argument list to be passed through to other functions
88+
internal to the function being called.
89+
90+
"""
91+
92+
# Make sure that we were passed sensible arguments
93+
if not isinstance(module, str) or not isinstance(param, str):
94+
raise ValueError("module and param must be strings")
95+
96+
# Construction the name of the key, for later use
97+
key = module + '.' + param
98+
99+
# If we were passed a dict for the argval, get the param value from there
100+
if isinstance(argval, dict):
101+
argval = argval.pop(param, None) if pop else argval.get(param, None)
102+
103+
# If we were passed a dict for the defval, get the param value from there
104+
if isinstance(defval, dict):
105+
defval = defval.get(key, None)
106+
107+
# Return the parameter value to use (argval > defaults > defval)
108+
return argval if argval is not None else defaults.get(key, defval)
32109

33110

34111
# Set defaults to match MATLAB
@@ -38,33 +115,23 @@ def use_matlab_defaults():
38115
The following conventions are used:
39116
* Bode plots plot gain in dB, phase in degrees, frequency in
40117
Hertz, with grids
118+
* State space class and functions use Numpy matrix objects
41119
42120
"""
43-
# Bode plot defaults
44-
from .freqplot import bode_plot
45-
defaults['bode.dB'] = True
46-
defaults['bode.deg'] = True
47-
defaults['bode.Hz'] = True
48-
defaults['bode.grid'] = True
49-
defaults['statesp.use_numpy_matrix'] = True
121+
set_defaults('bode', dB=True, deg=True, Hz=True, grid=True)
122+
set_defaults('statesp', use_numpy_matrix=True)
50123

51124

52125
# Set defaults to match FBS (Astrom and Murray)
53126
def use_fbs_defaults():
54127
"""Use `Feedback Systems <http://fbsbook.org>`_ (FBS) compatible settings.
55128
56129
The following conventions are used:
57-
58130
* Bode plots plot gain in powers of ten, phase in degrees,
59131
frequency in Hertz, no grid
60132
61133
"""
62-
# Bode plot defaults
63-
from .freqplot import bode_plot
64-
defaults['bode.dB'] = False
65-
defaults['bode.deg'] = True
66-
defaults['bode.Hz'] = False
67-
defaults['bode.grid'] = False
134+
set_defaults('bode', dB=False, deg=True, Hz=False, grid=False)
68135

69136

70137
# Decide whether to use numpy.matrix for state space operations
@@ -87,5 +154,5 @@ class and functions. If flat is `False`, then matrices are
87154
"""
88155
if flag and warn:
89156
warnings.warn("Return type numpy.matrix is soon to be deprecated.",
90-
stacklevel=2)
91-
defaults['statesp.use_numpy_matrix'] = flag
157+
stacklevel=2)
158+
set_defaults('statesp', use_numpy_matrix=flag)

0 commit comments

Comments
 (0)