Skip to content

Commit 0cafd68

Browse files
committed
fix unit tests that generate multiple warnings
1 parent c8d9c8f commit 0cafd68

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

control/tests/freqresp_test.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
including bode plots.
77
"""
88

9+
import math
10+
import re
11+
912
import matplotlib.pyplot as plt
1013
import numpy as np
11-
from numpy.testing import assert_allclose
12-
import math
1314
import pytest
15+
from numpy.testing import assert_allclose
1416

1517
import control as ctrl
18+
from control.freqplot import (bode_plot, nyquist_plot, nyquist_response,
19+
singular_values_plot, singular_values_response)
20+
from control.matlab import bode, rss, ss, tf
1621
from control.statesp import StateSpace
17-
from control.xferfcn import TransferFunction
18-
from control.matlab import ss, tf, bode, rss
19-
from control.freqplot import bode_plot, nyquist_plot, nyquist_response, \
20-
singular_values_plot, singular_values_response
2122
from control.tests.conftest import slycotonly
23+
from control.xferfcn import TransferFunction
2224

2325
pytestmark = pytest.mark.usefixtures("mplcleanup")
2426

@@ -101,12 +103,17 @@ def test_nyquist_basic(ss_siso):
101103
response = nyquist_response(tf_siso, omega_num=20)
102104
assert len(response.contour) == 20
103105

104-
with pytest.warns(UserWarning, match="encirclements was a non-integer"):
106+
with pytest.warns() as record:
105107
count, contour = nyquist_plot(
106108
tf_siso, plot=False, omega_limits=(1, 100), return_contour=True)
107109
assert_allclose(contour[0], 1j)
108110
assert_allclose(contour[-1], 100j)
109111

112+
# Check known warnings happened as expected
113+
assert len(record) == 2
114+
assert re.search("encirclements was a non-integer", str(record[0].message))
115+
assert re.search("return values .* deprecated", str(record[1].message))
116+
110117
response = nyquist_response(tf_siso, omega=np.logspace(-1, 1, 10))
111118
assert len(response.contour) == 10
112119

@@ -428,14 +435,25 @@ def test_freqresp_warn_infinite():
428435
np.testing.assert_almost_equal(sys_finite(0, warn_infinite=True), 100)
429436

430437
# Transfer function with infinite zero frequency gain
431-
with pytest.warns(RuntimeWarning, match="divide by zero"):
438+
with pytest.warns() as record:
432439
np.testing.assert_almost_equal(
433440
sys_infinite(0), complex(np.inf, np.nan))
434-
with pytest.warns(RuntimeWarning, match="divide by zero"):
441+
assert len(record) == 2 # generates two RuntimeWarnings
442+
assert record[0].category is RuntimeWarning
443+
assert re.search("divide by zero", str(record[0].message))
444+
assert record[1].category is RuntimeWarning
445+
assert re.search("invalid value", str(record[1].message))
446+
447+
with pytest.warns() as record:
435448
np.testing.assert_almost_equal(
436449
sys_infinite(0, warn_infinite=True), complex(np.inf, np.nan))
437450
np.testing.assert_almost_equal(
438451
sys_infinite(0, warn_infinite=False), complex(np.inf, np.nan))
452+
assert len(record) == 2 # generates two RuntimeWarnings
453+
assert record[0].category is RuntimeWarning
454+
assert re.search("divide by zero", str(record[0].message))
455+
assert record[1].category is RuntimeWarning
456+
assert re.search("invalid value", str(record[1].message))
439457

440458
# Switch to state space
441459
sys_finite = ctrl.tf2ss(sys_finite)

control/tests/nyquist_test.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
99
"""
1010

11+
import re
1112
import warnings
1213

13-
import pytest
14-
import numpy as np
1514
import matplotlib.pyplot as plt
15+
import numpy as np
16+
import pytest
17+
1618
import control as ct
1719

1820
pytestmark = pytest.mark.usefixtures("mplcleanup")
@@ -66,9 +68,12 @@ def test_nyquist_basic():
6668
assert _Z(sys) == N_sys + _P(sys)
6769

6870
# With a larger indent_radius, we get a warning message + wrong answer
69-
with pytest.warns(UserWarning, match="contour may miss closed loop pole"):
71+
with pytest.warns() as rec:
7072
N_sys = ct.nyquist_response(sys, indent_radius=0.2)
7173
assert _Z(sys) != N_sys + _P(sys)
74+
assert len(rec) == 2
75+
assert re.search("contour may miss closed loop pole", str(rec[0].message))
76+
assert re.search("encirclements does not match", str(rec[1].message))
7277

7378
# Unstable system
7479
sys = ct.tf([10], [1, 2, 2, 1])
@@ -104,11 +109,15 @@ def test_nyquist_basic():
104109
sys, np.linspace(1e-4, 1e2, 100), indent_radius=1e-2,
105110
return_contour=True)
106111
assert not all(contour_indented.real == 0)
107-
with pytest.warns(UserWarning, match="encirclements does not match"):
112+
113+
with pytest.warns() as record:
108114
count, contour = ct.nyquist_response(
109115
sys, np.linspace(1e-4, 1e2, 100), indent_radius=1e-2,
110116
return_contour=True, indent_direction='none')
111117
np.testing.assert_almost_equal(contour, 1j*np.linspace(1e-4, 1e2, 100))
118+
assert len(record) == 2
119+
assert re.search("encirclements .* non-integer", str(record[0].message))
120+
assert re.search("encirclements does not match", str(record[1].message))
112121

113122
# Nyquist plot with poles at the origin, omega unspecified
114123
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0])
@@ -264,14 +273,19 @@ def test_nyquist_indent_default(indentsys):
264273
def test_nyquist_indent_dont(indentsys):
265274
# first value of default omega vector was 0.1, replaced by 0. for contour
266275
# indent_radius is larger than 0.1 -> no extra quater circle around origin
267-
with pytest.warns(UserWarning, match="encirclements does not match"):
276+
with pytest.warns() as record:
268277
count, contour = ct.nyquist_response(
269278
indentsys, omega=[0, 0.2, 0.3, 0.4], indent_radius=.1007,
270279
plot=False, return_contour=True)
271280
np.testing.assert_allclose(contour[0], .1007+0.j)
272281
# second value of omega_vector is larger than indent_radius: not indented
273282
assert np.all(contour.real[2:] == 0.)
274283

284+
# Make sure warnings are as expected
285+
assert len(record) == 2
286+
assert re.search("encirclements .* non-integer", str(record[0].message))
287+
assert re.search("encirclements does not match", str(record[1].message))
288+
275289

276290
def test_nyquist_indent_do(indentsys):
277291
plt.figure();
@@ -352,9 +366,8 @@ def test_nyquist_exceptions():
352366
ct.nyquist_plot(sys, indent_direction='up')
353367

354368
# Discrete time system sampled above Nyquist frequency
355-
sys = ct.drss(2, 1, 1)
356-
sys.dt = 0.01
357-
with pytest.warns(UserWarning, match="above Nyquist"):
369+
sys = ct.ss([[-0.5, 0], [1, 0.5]], [[0], [1]], [[1, 0]], 0, 0.1)
370+
with pytest.warns(UserWarning, match="evaluation above Nyquist"):
358371
ct.nyquist_plot(sys, np.logspace(-2, 3))
359372

360373

control/tests/xferfcn_test.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
RMM, 30 Mar 2011 (based on TestXferFcn from v0.4a)
44
"""
55

6+
import operator
7+
import re
8+
69
import numpy as np
710
import pytest
8-
import operator
911

1012
import control as ct
11-
from control import StateSpace, TransferFunction, rss, evalfr
12-
from control import ss, ss2tf, tf, tf2ss, zpk
13-
from control import isctime, isdtime, sample_system
14-
from control import defaults, reset_defaults, set_defaults
13+
from control import (StateSpace, TransferFunction, defaults, evalfr, isctime,
14+
isdtime, reset_defaults, rss, sample_system, set_defaults,
15+
ss, ss2tf, tf, tf2ss, zpk)
1516
from control.statesp import _convert_to_statespace
16-
from control.xferfcn import _convert_to_transfer_function
1717
from control.tests.conftest import slycotonly
18+
from control.xferfcn import _convert_to_transfer_function
1819

1920

2021
class TestXferFcn:
@@ -836,9 +837,14 @@ def test_dcgain_discr(self):
836837

837838
# differencer, with warning
838839
sys = TransferFunction(1, [1, -1], True)
839-
with pytest.warns(RuntimeWarning, match="divide by zero"):
840+
with pytest.warns() as record:
840841
np.testing.assert_equal(
841842
sys.dcgain(warn_infinite=True), np.inf)
843+
assert len(record) == 2 # generates two RuntimeWarnings
844+
assert record[0].category is RuntimeWarning
845+
assert re.search("divide by zero", str(record[0].message))
846+
assert record[1].category is RuntimeWarning
847+
assert re.search("invalid value", str(record[1].message))
842848

843849
# summer
844850
sys = TransferFunction([1, -1], [1], True)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy