-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathtype_conversion_test.py
More file actions
193 lines (173 loc) · 8.78 KB
/
Copy pathtype_conversion_test.py
File metadata and controls
193 lines (173 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# type_conversion_test.py - test type conversions
# RMM, 3 Jan 2021
#
# This set of tests looks at how various classes are converted when using
# algebraic operations. See GitHub issue #459 for some discussion on what the
# desired combinations should be.
import control as ct
import numpy as np
import operator
import pytest
@pytest.fixture()
def sys_dict():
sdict = {}
sdict['ss'] = ct.StateSpace([[-1]], [[1]], [[1]], [[0]])
sdict['tf'] = ct.TransferFunction([1],[0.5, 1])
sdict['tfx'] = ct.TransferFunction([1, 1], [1]) # non-proper TF
sdict['frd'] = ct.frd([10+0j, 9 + 1j, 8 + 2j, 7 + 3j], [1, 2, 3, 4])
sdict['lio'] = ct.LinearIOSystem(ct.ss([[-1]], [[5]], [[5]], [[0]]))
sdict['ios'] = ct.NonlinearIOSystem(
sdict['lio']._rhs, sdict['lio']._out, inputs=1, outputs=1, states=1)
sdict['arr'] = np.array([[2.0]])
sdict['flt'] = 3.
return sdict
type_dict = {
'ss': ct.StateSpace, 'tf': ct.TransferFunction,
'frd': ct.FrequencyResponseData, 'lio': ct.LinearICSystem,
'ios': ct.InterconnectedSystem, 'arr': np.ndarray, 'flt': float}
#
# Current table of expected conversions
#
# This table describes all of the conversions that are supposed to
# happen for various system combinations. This is written out this way
# to make it easy to read, but this is converted below into a list of
# specific tests that can be iterated over.
#
# Items marked as 'E' should generate an exception.
#
# Items starting with 'x' currently generate an expected exception but
# should eventually generate a useful result (when everything is
# implemented properly).
#
# Note 1: some of the entries below are currently converted to to lower level
# types than needed. In particular, LinearIOSystems should combine with
# StateSpace and TransferFunctions in a way that preserves I/O system
# structure when possible.
#
# Note 2: eventually the operator entry for this table can be pulled out and
# tested as a separate parameterized variable (since all operators should
# return consistent values).
#
# Note 3: this table documents the current state, but not actually the desired
# state. See bottom of the file for the (eventual) desired behavior.
#
rtype_list = ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'flt']
conversion_table = [
# op left ss tf frd lio ios arr flt
('add', 'ss', ['ss', 'ss', 'frd', 'ss', 'ios', 'ss', 'ss' ]),
('add', 'tf', ['tf', 'tf', 'frd', 'lio', 'ios', 'tf', 'tf' ]),
('add', 'frd', ['frd', 'frd', 'frd', 'frd', 'E', 'frd', 'frd']),
('add', 'lio', ['lio', 'lio', 'xrd', 'lio', 'ios', 'lio', 'lio']),
('add', 'ios', ['ios', 'ios', 'E', 'ios', 'ios', 'ios', 'ios']),
('add', 'arr', ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'arr']),
('add', 'flt', ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'flt']),
# op left ss tf frd lio ios arr flt
('sub', 'ss', ['ss', 'ss', 'frd', 'ss', 'ios', 'ss', 'ss' ]),
('sub', 'tf', ['tf', 'tf', 'frd', 'lio', 'ios', 'tf', 'tf' ]),
('sub', 'frd', ['frd', 'frd', 'frd', 'frd', 'E', 'frd', 'frd']),
('sub', 'lio', ['lio', 'lio', 'xrd', 'lio', 'ios', 'lio', 'lio']),
('sub', 'ios', ['ios', 'ios', 'E', 'ios', 'ios', 'ios', 'ios']),
('sub', 'arr', ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'arr']),
('sub', 'flt', ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'flt']),
# op left ss tf frd lio ios arr flt
('mul', 'ss', ['ss', 'ss', 'frd', 'ss', 'ios', 'ss', 'ss' ]),
('mul', 'tf', ['tf', 'tf', 'frd', 'lio', 'ios', 'tf', 'tf' ]),
('mul', 'frd', ['frd', 'frd', 'frd', 'frd', 'E', 'frd', 'frd']),
('mul', 'lio', ['lio', 'lio', 'xrd', 'lio', 'ios', 'lio', 'lio']),
('mul', 'ios', ['ios', 'ios', 'E', 'ios', 'ios', 'ios', 'ios']),
('mul', 'arr', ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'arr']),
('mul', 'flt', ['ss', 'tf', 'frd', 'lio', 'ios', 'arr', 'flt']),
# op left ss tf frd lio ios arr flt
('truediv', 'ss', ['xs', 'tf', 'frd', 'xio', 'xos', 'xs', 'xs' ]),
('truediv', 'tf', ['tf', 'tf', 'xrd', 'tf', 'xos', 'tf', 'tf' ]),
('truediv', 'frd', ['frd', 'frd', 'frd', 'frd', 'E', 'frd', 'frd']),
('truediv', 'lio', ['xio', 'tf', 'frd', 'xio', 'xio', 'xio', 'xio']),
('truediv', 'ios', ['xos', 'xos', 'E', 'xos', 'xos' 'xos', 'xos']),
('truediv', 'arr', ['xs', 'tf', 'frd', 'xio', 'xos', 'arr', 'arr']),
('truediv', 'flt', ['xs', 'tf', 'frd', 'xio', 'xos', 'arr', 'flt'])]
# Now create list of the tests we actually want to run
test_matrix = []
for i, (opname, ltype, expected_list) in enumerate(conversion_table):
for rtype, expected in zip(rtype_list, expected_list):
# Add this to the list of tests to run
test_matrix.append([opname, ltype, rtype, expected])
@pytest.mark.parametrize("opname, ltype, rtype, expected", test_matrix)
def test_operator_type_conversion(opname, ltype, rtype, expected, sys_dict):
op = getattr(operator, opname)
leftsys = sys_dict[ltype]
rightsys = sys_dict[rtype]
# Get rid of warnings for InputOutputSystem objects by making a copy
if isinstance(leftsys, ct.InputOutputSystem) and leftsys == rightsys:
rightsys = leftsys.copy()
# Make sure we get the right result
if expected == 'E' or expected[0] == 'x':
# Exception expected
with pytest.raises(TypeError):
op(leftsys, rightsys)
else:
# Operation should work and return the given type
result = op(leftsys, rightsys)
# Print out what we are testing in case something goes wrong
assert isinstance(result, type_dict[expected])
#
# Updated table that describes desired outputs for all operators
#
# General rules (subject to change)
#
# * For LTI/LTI, keep the type of the left operand whenever possible. This
# prioritizes the first operand, but we need to watch out for non-proper
# transfer functions (in which case TransferFunction should be returned)
#
# * For FRD/LTI, convert LTI to FRD by evaluating the LTI transfer function
# at the FRD frequencies (can't got the other way since we can't convert
# an FRD object to state space/transfer function).
#
# * For IOS/LTI, convert to IOS. In the case of a linear I/O system (LIO),
# this will preserve the linear structure since the LTI system will
# be converted to state space.
#
# * When combining state space or transfer with linear I/O systems, the
# * output should be of type Linear IO system, since that maintains the
# * underlying state space attributes.
#
# Note: tfx = non-proper transfer function, order(num) > order(den)
#
type_list = ['ss', 'tf', 'tfx', 'frd', 'lio', 'ios', 'arr', 'flt']
conversion_table = [
('ss', ['ss', 'ss', 'tf' 'frd', 'lio', 'ios', 'ss', 'ss' ]),
('tf', ['tf', 'tf', 'tf' 'frd', 'lio', 'ios', 'tf', 'tf' ]),
('tfx', ['tf', 'tf', 'tf', 'frd', 'E', 'E', 'tf', 'tf' ]),
('frd', ['frd', 'frd', 'frd', 'frd', 'E', 'E', 'frd', 'frd']),
('lio', ['lio', 'lio', 'E', 'E', 'lio', 'ios', 'lio', 'lio']),
('ios', ['ios', 'ios', 'E', 'E', 'ios', 'ios', 'ios', 'ios']),
('arr', ['ss', 'tf', 'tf' 'frd', 'lio', 'ios', 'arr', 'arr']),
('flt', ['ss', 'tf', 'tf' 'frd', 'lio', 'ios', 'arr', 'flt'])]
@pytest.mark.skip(reason="future test; conversions not yet fully implemented")
# @pytest.mark.parametrize("opname", ['add', 'sub', 'mul', 'truediv'])
# @pytest.mark.parametrize("opname", ['add', 'sub', 'mul'])
# @pytest.mark.parametrize("ltype", type_list)
# @pytest.mark.parametrize("rtype", type_list)
def test_binary_op_type_conversions(opname, ltype, rtype, sys_dict):
op = getattr(operator, opname)
leftsys = sys_dict[ltype]
rightsys = sys_dict[rtype]
expected = \
conversion_table[type_list.index(ltype)][1][type_list.index(rtype)]
# Get rid of warnings for InputOutputSystem objects by making a copy
if isinstance(leftsys, ct.InputOutputSystem) and leftsys == rightsys:
rightsys = leftsys.copy()
# Make sure we get the right result
if expected == 'E' or expected[0] == 'x':
# Exception expected
with pytest.raises(TypeError):
op(leftsys, rightsys)
else:
# Operation should work and return the given type
result = op(leftsys, rightsys)
# Print out what we are testing in case something goes wrong
assert isinstance(result, type_dict[expected])
# Make sure that input, output, and state names make sense
assert len(result.input_labels) == result.ninputs
assert len(result.output_labels) == result.noutputs
if result.nstates is not None:
assert len(result.state_labels) == result.nstates