Skip to content

Commit 3b4e3a4

Browse files
committed
address @slivingston comments + pycodestyle updates
1 parent fc9fadb commit 3b4e3a4

4 files changed

Lines changed: 59 additions & 50 deletions

File tree

control/config.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# config.py - package defaults
22
# RMM, 4 Nov 2012
33
#
4-
# TODO: add ability to read/write configuration files (ala matplotlib)
4+
# TODO: add ability to read/write configuration files (a la matplotlib)
55

66
"""Functions to access default parameter values.
77
@@ -390,7 +390,7 @@ def _process_legacy_keyword(kwargs, oldkey, newkey, newval, warn_oldkey=True):
390390
Use this function to handle a legacy keyword that has been renamed.
391391
This function pops the old keyword off of the kwargs dictionary and
392392
issues a warning. If both the old and new keyword are present, a
393-
ControlArgument exception is raised.
393+
`ControlArgument` exception is raised.
394394
395395
Parameters
396396
----------
@@ -442,7 +442,7 @@ def _process_param(name, defval, kwargs, alias_mapping, sigval=None):
442442
aliases and legacy aliases::
443443
444444
alias_mapping = {
445-
'argument_name_1', (['alias', ...], ['legacy', ...]),
445+
'argument_name_1': (['alias', ...], ['legacy', ...]),
446446
...}
447447
448448
If `param` is a named keyword in the function signature with default
@@ -452,7 +452,7 @@ def _process_param(name, defval, kwargs, alias_mapping, sigval=None):
452452
param = _process_param('param', defval, kwargs, function_aliases)
453453
454454
If `param` is a variable keyword argument (in `kwargs`), `defval` can
455-
be pssed as either None or the default value to use if `param` is not
455+
be passed as either None or the default value to use if `param` is not
456456
present in `kwargs`.
457457
458458
Parameters
@@ -462,7 +462,7 @@ def _process_param(name, defval, kwargs, alias_mapping, sigval=None):
462462
defval : object or dict
463463
Default value for the parameter.
464464
kwargs : dict
465-
Dictionary of varaible keyword arguments.
465+
Dictionary of variable keyword arguments.
466466
alias_mapping : dict
467467
Dictionary providing aliases and legacy names.
468468
sigval : object, optional
@@ -478,7 +478,7 @@ def _process_param(name, defval, kwargs, alias_mapping, sigval=None):
478478
Raises
479479
------
480480
TypeError
481-
If multiple keyword aliased are used for the same parameter.
481+
If multiple keyword aliases are used for the same parameter.
482482
483483
Warns
484484
-----
@@ -527,7 +527,7 @@ def _process_kwargs(kwargs, alias_mapping):
527527
a tuple consisting of valid aliases and legacy aliases::
528528
529529
alias_mapping = {
530-
'argument_name_1', (['alias', ...], ['legacy', ...]),
530+
'argument_name_1': (['alias', ...], ['legacy', ...]),
531531
...}
532532
533533
If an alias is present in the dictionary of keywords, it will be used

control/optimal.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@
4949
'optimal.solve_ivp_options': {},
5050
}
5151

52+
# Parameter and keyword aliases
53+
_optimal_aliases = {
54+
# param: ([alias, ...], [legacy, ...])
55+
'integral_cost': (['trajectory_cost', 'cost'], []),
56+
'initial_state': (['x0', 'X0'], []),
57+
'initial_input': (['u0', 'U0'], []),
58+
'final_state': (['xf'], []),
59+
'final_input': (['uf'], []),
60+
'initial_time': (['T0'], []),
61+
'trajectory_constraints': (['constraints'], []),
62+
'return_states': (['return_x'], []),
63+
}
64+
5265

5366
class OptimalControlProblem():
5467
"""Description of a finite horizon, optimal control problem.
@@ -334,7 +347,8 @@ def _cost_function(self, coeffs):
334347

335348
# Integrate the cost
336349
costs = np.array(costs)
337-
# Approximate the integral using trapezoidal rule
350+
351+
# Approximate the integral using trapezoidal rule
338352
cost = np.sum(0.5 * (costs[:-1] + costs[1:]) * dt)
339353

340354
else:
@@ -1018,20 +1032,6 @@ def __init__(
10181032
self.states = response.states
10191033

10201034

1021-
# Parameter and keyword aliases
1022-
_optimal_aliases = {
1023-
# param: ([alias, ...], [legacy, ...])
1024-
'integral_cost': (['trajectory_cost', 'cost'], []),
1025-
'initial_state': (['x0', 'X0'], []),
1026-
'initial_input': (['u0', 'U0'], []),
1027-
'final_state': (['xf'], []),
1028-
'final_input': (['uf'], []),
1029-
'initial_time': (['T0'], []),
1030-
'trajectory_constraints': (['constraints'], []),
1031-
'return_states': (['return_x'], []),
1032-
}
1033-
1034-
10351035
# Compute the input for a nonlinear, (constrained) optimal control problem
10361036
def solve_optimal_trajectory(
10371037
sys, timepts, initial_state=None, integral_cost=None,
@@ -1183,7 +1183,8 @@ def solve_optimal_trajectory(
11831183
kwargs['minimize_method'] = method
11841184
else:
11851185
if kwargs.get('trajectory_method'):
1186-
raise ValueError("'trajectory_method' specified more than once")
1186+
raise ValueError(
1187+
"'trajectory_method' specified more than once")
11871188
warnings.warn(
11881189
"'method' parameter is deprecated; assuming trajectory_method",
11891190
FutureWarning)
@@ -1819,14 +1820,14 @@ def compute_estimate(
18191820
return OptimalEstimationResult(
18201821
self, res, squeeze=squeeze, print_summary=print_summary)
18211822

1822-
18231823
#
18241824
# Create an input/output system implementing an moving horizon estimator
18251825
#
18261826
# This function creates an input/output system that has internal state
18271827
# xhat, u, v, y for all previous time points. When the system update
18281828
# function is called,
18291829
#
1830+
18301831
def create_mhe_iosystem(
18311832
self, estimate_labels=None, measurement_labels=None,
18321833
control_labels=None, inputs=None, outputs=None, **kwargs):
@@ -2490,6 +2491,7 @@ def _evaluate_output_range_constraint(x, u):
24902491
# Return a nonlinear constraint object based on the polynomial
24912492
return (opt.NonlinearConstraint, _evaluate_output_range_constraint, lb, ub)
24922493

2494+
24932495
#
24942496
# Create a constraint on the disturbance input
24952497
#
@@ -2534,6 +2536,7 @@ def disturbance_range_constraint(sys, lb, ub):
25342536
# Utility functions
25352537
#
25362538

2539+
25372540
#
25382541
# Process trajectory constraints
25392542
#
@@ -2545,6 +2548,7 @@ def disturbance_range_constraint(sys, lb, ub):
25452548
# internal representation (currently a tuple with the constraint type as the
25462549
# first element.
25472550
#
2551+
25482552
def _process_constraints(clist, name):
25492553
if clist is None:
25502554
clist = []
@@ -2560,7 +2564,7 @@ def _process_constraints(clist, name):
25602564
if isinstance(constraint, tuple):
25612565
# Original style of constraint
25622566
ctype, fun, lb, ub = constraint
2563-
if not ctype in [opt.LinearConstraint, opt.NonlinearConstraint]:
2567+
if ctype not in [opt.LinearConstraint, opt.NonlinearConstraint]:
25642568
raise TypeError(f"unknown {name} constraint type {ctype}")
25652569
constraint_list.append(constraint)
25662570
elif isinstance(constraint, opt.LinearConstraint):

control/timeresp.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@
5454
'initial_response', 'impulse_response', 'TimeResponseData',
5555
'TimeResponseList']
5656

57+
# Dictionary of aliases for time response commands
58+
_timeresp_aliases = {
59+
# param: ([alias, ...], [legacy, ...])
60+
'timepts': (['T'], []),
61+
'inputs': (['U'], ['u']),
62+
'outputs': (['Y'], ['y']),
63+
'initial_state': (['X0'], ['x0']),
64+
'final_output': (['yfinal'], []),
65+
'return_states': (['return_x'], []),
66+
'evaluation_times': (['t_eval'], []),
67+
'timepts_num': (['T_num'], []),
68+
'input_indices': (['input'], []),
69+
'output_indices': (['output'], []),
70+
}
71+
5772

5873
class TimeResponseData:
5974
"""Input/output system time response data.
@@ -344,7 +359,7 @@ def __init__(
344359
# Make sure the shape is OK
345360
if multi_trace and \
346361
(self.x.ndim != 3 or self.x.shape[1] != self.ntraces) or \
347-
not multi_trace and self.x.ndim != 2 :
362+
not multi_trace and self.x.ndim != 2:
348363
raise ValueError("State vector is the wrong shape")
349364

350365
# Make sure time dimension of state is the right length
@@ -701,6 +716,7 @@ def plot(self, *args, **kwargs):
701716
"""
702717
return time_response_plot(self, *args, **kwargs)
703718

719+
704720
#
705721
# Time response data list class
706722
#
@@ -739,20 +755,6 @@ def plot(self, *args, **kwargs):
739755
lines[row, col] += cplt.lines[row, col]
740756
return ControlPlot(lines, cplt.axes, cplt.figure)
741757

742-
# Dictionary of aliases for time response commands
743-
_timeresp_aliases = {
744-
# param: ([alias, ...], [legacy, ...])
745-
'timepts': (['T'], []),
746-
'inputs': (['U'], ['u']),
747-
'outputs': (['Y'], ['y']),
748-
'initial_state': (['X0'], ['x0']),
749-
'final_output': (['yfinal'], []),
750-
'return_states': (['return_x'], []),
751-
'evaluation_times': (['t_eval'], []),
752-
'timepts_num': (['T_num'], []),
753-
'input_indices': (['input'], []),
754-
'output_indices': (['output'], []),
755-
}
756758

757759
# Process signal labels
758760
def _process_labels(labels, signal, length):
@@ -991,8 +993,8 @@ def forced_response(
991993
True, the array is 1D (indexed by time). If the system is not SISO or
992994
`squeeze` is False, the array is 2D (indexed by output and time).
993995
resp.states : array
994-
Time evolution of the state vector, represented as a 2D array indexed by
995-
state and time.
996+
Time evolution of the state vector, represented as a 2D array
997+
indexed by state and time.
996998
resp.inputs : array
997999
Input(s) to the system, indexed by input and time.
9981000
@@ -1428,7 +1430,8 @@ def step_response(
14281430
output = _process_param(
14291431
'output_indices', output_indices, kwargs, _timeresp_aliases)
14301432
return_x = _process_param(
1431-
'return_states', return_states, kwargs, _timeresp_aliases, sigval=False)
1433+
'return_states', return_states, kwargs, _timeresp_aliases,
1434+
sigval=False)
14321435
T_num = _process_param(
14331436
'timepts_num', timepts_num, kwargs, _timeresp_aliases)
14341437

@@ -1839,7 +1842,8 @@ def initial_response(
18391842
output = _process_param(
18401843
'output_indices', output_indices, kwargs, _timeresp_aliases)
18411844
return_x = _process_param(
1842-
'return_states', return_states, kwargs, _timeresp_aliases, sigval=False)
1845+
'return_states', return_states, kwargs, _timeresp_aliases,
1846+
sigval=False)
18431847
T_num = _process_param(
18441848
'timepts_num', timepts_num, kwargs, _timeresp_aliases)
18451849

@@ -1972,7 +1976,8 @@ def impulse_response(
19721976
output = _process_param(
19731977
'output_indices', output_indices, kwargs, _timeresp_aliases)
19741978
return_x = _process_param(
1975-
'return_states', return_states, kwargs, _timeresp_aliases, sigval=False)
1979+
'return_states', return_states, kwargs, _timeresp_aliases,
1980+
sigval=False)
19761981
T_num = _process_param(
19771982
'timepts_num', timepts_num, kwargs, _timeresp_aliases)
19781983

doc/develop.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Parameter aliases
305305
-----------------
306306

307307
As described above, parameter names are generally longer strings that
308-
describe the purpose fo the paramater. Similar to `matplotlib` (e.g.,
308+
describe the purpose of the parameter. Similar to `matplotlib` (e.g.,
309309
the use of `lw` as an alias for `linewidth`), some commonly used
310310
parameter names can be specified using an "alias" that allows the use
311311
of a shorter key.
@@ -321,11 +321,11 @@ variable by replacing aliases with the full key::
321321
_process_kwargs(kwargs, aliases)
322322

323323
The values for named parameters can then be assigned to a local
324-
variable using a call to :func:`~config.process_param` of the form::
324+
variable using a call to :func:`~config._process_param` of the form::
325325

326-
var = _process_kwargs('param', param, kwargs, aliases)
326+
var = _process_param('param', param, kwargs, aliases)
327327

328-
where 'param` is the named parameter used in the function signature
328+
where `param` is the named parameter used in the function signature
329329
and var is the local variable in the function (may also be `param`,
330330
but doesn't have to be).
331331

@@ -356,7 +356,7 @@ The alias mapping is a dictionary that returns a tuple consisting of
356356
valid aliases and legacy aliases::
357357

358358
alias_mapping = {
359-
'argument_name_1', (['alias', ...], ['legacy', ...]),
359+
'argument_name_1': (['alias', ...], ['legacy', ...]),
360360
...}
361361

362362
If an alias is present in the dictionary of keywords, it will be used

0 commit comments

Comments
 (0)