9
9
10
10
import warnings
11
11
12
- __all__ = ['reset_defaults' , 'use_matlab_defaults' , 'use_fbs_defaults' ,
12
+ __all__ = ['defaults' , 'set_defaults' , 'reset_defaults' ,
13
+ 'use_matlab_defaults' , 'use_fbs_defaults' ,
13
14
'use_numpy_matrix' ]
14
15
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)
20
19
}
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
21
36
22
37
23
38
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 )
32
109
33
110
34
111
# Set defaults to match MATLAB
@@ -38,33 +115,23 @@ def use_matlab_defaults():
38
115
The following conventions are used:
39
116
* Bode plots plot gain in dB, phase in degrees, frequency in
40
117
Hertz, with grids
118
+ * State space class and functions use Numpy matrix objects
41
119
42
120
"""
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 )
50
123
51
124
52
125
# Set defaults to match FBS (Astrom and Murray)
53
126
def use_fbs_defaults ():
54
127
"""Use `Feedback Systems <http://fbsbook.org>`_ (FBS) compatible settings.
55
128
56
129
The following conventions are used:
57
-
58
130
* Bode plots plot gain in powers of ten, phase in degrees,
59
131
frequency in Hertz, no grid
60
132
61
133
"""
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 )
68
135
69
136
70
137
# Decide whether to use numpy.matrix for state space operations
@@ -87,5 +154,5 @@ class and functions. If flat is `False`, then matrices are
87
154
"""
88
155
if flag and warn :
89
156
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