Skip to content

Commit 5f36a55

Browse files
committed
Fix some MATLAB tests
Fixes #30, suppressing some known warnings for systems with direct feedthrough terms. Also removes a duplicate test in `test_control_matlab`, and fixes some previously broken, skipped tests: test_convert_MIMO_to_SISO test_impulse This update also removes a previous test to make sure warning messages are issued: this turns out to be unreliable, because the default behavior is for warnings to be issued only once, so depending on the order in which tests are executed, the warning may or may not be raised.
1 parent e54a81b commit 5f36a55

2 files changed

Lines changed: 17 additions & 29 deletions

File tree

control/tests/matlab_test.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,22 @@ def testImpulse(self):
178178
np.testing.assert_array_almost_equal(tout, t)
179179

180180
# produce a warning for a system with direct feedthrough
181-
with warnings.catch_warnings(record=True) as warn:
181+
with warnings.catch_warnings():
182+
warnings.simplefilter("ignore")
182183
#Test SISO system
183184
sys = self.siso_ss1
184185
youttrue = np.array([86., 70.1808, 57.3753, 46.9975, 38.5766, 31.7344,
185186
26.1668, 21.6292, 17.9245, 14.8945])
186187
yout, tout = impulse(sys, T=t)
187-
self.assertEqual(len(warn), 1)
188-
self.assertIn("direct feedthrough", str(warn[-1].message))
189188
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
190189
np.testing.assert_array_almost_equal(tout, t)
191190

192-
#Test MIMO system, which contains ``siso_ss1`` twice
193-
sys = self.mimo_ss1
194-
y_00, _t = impulse(sys, T=t, input=0, output=0)
195-
y_11, _t = impulse(sys, T=t, input=1, output=1)
196-
np.testing.assert_array_almost_equal(y_00, youttrue, decimal=4)
197-
np.testing.assert_array_almost_equal(y_11, youttrue, decimal=4)
191+
#Test MIMO system, which contains ``siso_ss1`` twice
192+
sys = self.mimo_ss1
193+
y_00, _t = impulse(sys, T=t, input=0, output=0)
194+
y_11, _t = impulse(sys, T=t, input=1, output=1)
195+
np.testing.assert_array_almost_equal(y_00, youttrue, decimal=4)
196+
np.testing.assert_array_almost_equal(y_11, youttrue, decimal=4)
198197

199198
def testInitial(self):
200199
#Test SISO system

control/tests/test_control_matlab.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ss2tf
2020
from control.statesp import _mimo2siso
2121
from control.timeresp import _check_convert_array
22+
import warnings
2223

2324
class TestControlMatlab(unittest.TestCase):
2425

@@ -66,7 +67,6 @@ def make_MIMO_mats(self):
6667
D = zeros((2, 2))
6768
return A, B, C, D
6869

69-
7070
def test_dcgain(self):
7171
"""Test function dcgain with different systems"""
7272
#Test MIMO systems
@@ -95,13 +95,13 @@ def test_dcgain(self):
9595
array([[0.0269]]),
9696
decimal=4)
9797

98-
9998
def test_dcgain_2(self):
10099
"""Test function dcgain with different systems"""
101100
#Create different forms of a SISO system
102101
A, B, C, D = self.make_SISO_mats()
103-
Z, P, k = scipy.signal.ss2zpk(A, B, C, D)
104102
num, den = scipy.signal.ss2tf(A, B, C, D)
103+
# numerator is only a constant here; pick it out to avoid numpy warning
104+
Z, P, k = scipy.signal.tf2zpk(num[0][-1], den)
105105
sys_ss = ss(A, B, C, D)
106106

107107
#Compute the gain with ``dcgain``
@@ -125,13 +125,6 @@ def test_dcgain_2(self):
125125
0.026948],
126126
decimal=6)
127127

128-
#Test with MIMO system
129-
A, B, C, D = self.make_MIMO_mats()
130-
gain_mimo = dcgain(A, B, C, D)
131-
# print('gain_mimo: \n', gain_mimo)
132-
assert_array_almost_equal(gain_mimo, [[0.026948, 0 ],
133-
[0, 0.026948]], decimal=6)
134-
135128
def test_step(self):
136129
"""Test function ``step``."""
137130
figure(); plot_shape = (1, 3)
@@ -160,9 +153,6 @@ def test_step(self):
160153
t, y = step(sys)
161154
plot(t, y)
162155

163-
#show()
164-
165-
@unittest.skip("skipping test_impulse, need to update test")
166156
def test_impulse(self):
167157
A, B, C, D = self.make_SISO_mats()
168158
sys = ss(A, B, C, D)
@@ -182,8 +172,10 @@ def test_impulse(self):
182172
#Test system with direct feed-though, the function should print a warning.
183173
D = [[0.5]]
184174
sys_ft = ss(A, B, C, D)
185-
t, y = impulse(sys_ft)
186-
plot(t, y, label='Direct feedthrough D=[[0.5]]')
175+
with warnings.catch_warnings():
176+
warnings.simplefilter("ignore")
177+
t, y = impulse(sys_ft)
178+
plot(t, y, label='Direct feedthrough D=[[0.5]]')
187179

188180
#Test MIMO system
189181
A, B, C, D = self.make_MIMO_mats()
@@ -378,12 +370,10 @@ def assert_systems_behave_equal(self, sys1, sys2):
378370
assert_array_almost_equal(dcgain(sys1), dcgain(sys2))
379371

380372
#Results of ``step`` simulation must be the same too
381-
t, y1 = step(sys1)
382-
_t, y2 = step(sys2, t)
373+
y1, t1 = step(sys1)
374+
y2, t2 = step(sys2, t1)
383375
assert_array_almost_equal(y1, y2)
384376

385-
#! Old test; no longer functional?? (RMM, 3 Nov 2012)
386-
@unittest.skip("skipping test_convert_MIMOto_SISO: need to update test")
387377
def test_convert_MIMO_to_SISO(self):
388378
'''Convert mimo to siso systems'''
389379
#Test with our usual systems --------------------------------------------
@@ -474,5 +464,4 @@ def debug_nasty_import_problem():
474464

475465
if __name__ == '__main__':
476466
unittest.main()
477-
show()
478467
# vi:ts=4:sw=4:expandtab

0 commit comments

Comments
 (0)