Skip to content

Commit e646370

Browse files
committed
add pzmap_test
1 parent bdbd198 commit e646370

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

control/tests/conftest.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
# contest.py - pytest local plugins and fixtures
22

3-
import control
43
import os
54

5+
import matplotlib as mpl
66
import pytest
77

8+
import control
9+
810

911
@pytest.fixture(scope="session", autouse=True)
1012
def use_numpy_ndarray():
1113
"""Switch the config to use ndarray instead of matrix"""
1214
if os.getenv("PYTHON_CONTROL_STATESPACE_ARRAY") == "1":
1315
control.config.defaults['statesp.use_numpy_matrix'] = False
16+
17+
18+
@pytest.fixture(scope="function")
19+
def editsdefaults():
20+
"""Make sure any changes to the defaults only last during a test"""
21+
restore = control.config.defaults.copy()
22+
yield
23+
control.config.defaults.update(restore)
24+
25+
26+
@pytest.fixture(scope="function")
27+
def mplcleanup():
28+
"""Workaround for python2
29+
30+
python 2 does not like to mix the original mpl decorator with pytest
31+
fixtures. So we roll our own.
32+
"""
33+
save = mpl.units.registry.copy()
34+
try:
35+
yield
36+
finally:
37+
mpl.units.registry.clear()
38+
mpl.units.registry.update(save)
39+
mpl.pyplot.close("all")

control/tests/pzmap_test.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
""" pzmap_test.py - test pzmap()
3+
4+
Created on Thu Aug 20 20:06:21 2020
5+
6+
@author: bnavigator
7+
"""
8+
9+
import numpy as np
10+
import pytest
11+
from matplotlib import pyplot as plt
12+
13+
from control import TransferFunction, config, pzmap
14+
15+
16+
@pytest.mark.parametrize("kwargs",
17+
[dict(),
18+
dict(plot=False),
19+
dict(plot=True),
20+
dict(grid=True),
21+
dict(title="Custom Title")],
22+
ids=["default",
23+
"noplot",
24+
"plotTrue",
25+
"grid",
26+
"title"])
27+
@pytest.mark.parametrize("setdefaults", # set by kw or config
28+
[False, True],
29+
ids=["kw", "config"])
30+
@pytest.mark.parametrize("dt", [0, True], ids=["s", "z"])
31+
def test_pzmap(kwargs, setdefaults, dt, editsdefaults, mplcleanup):
32+
"""Test pzmap"""
33+
# T from from pvtol-nested example
34+
T = TransferFunction([-9.0250000e-01, -4.7200750e+01, -8.6812900e+02,
35+
+5.6261850e+03, +2.1258472e+05, +8.4724600e+05,
36+
+1.0192000e+06, +2.3520000e+05],
37+
[9.02500000e-03, 9.92862812e-01, 4.96974094e+01,
38+
1.35705659e+03, 2.09294163e+04, 1.64898435e+05,
39+
6.54572220e+05, 1.25274600e+06, 1.02420000e+06,
40+
2.35200000e+05])
41+
42+
Pref = [-23.8877+19.3837j, -23.8877-19.3837j, -23.8349+15.7846j,
43+
-23.8349-15.7846j, -5.2320 +0.4117j, -5.2320 -0.4117j,
44+
-2.2246 +0.0000j, -1.5160 +0.0000j, -0.3627 +0.0000j]
45+
Zref = [-23.8877+19.3837j, -23.8877-19.3837j, +14.3637 +0.0000j,
46+
-14.3637 +0.0000j, -2.2246 +0.0000j, -2.0000 +0.0000j,
47+
-0.3000 +0.0000j]
48+
49+
pzkwargs = kwargs.copy()
50+
if setdefaults:
51+
for k in ['plot', 'grid']:
52+
if k in pzkwargs:
53+
v = pzkwargs.pop(k)
54+
config.set_defaults('pzmap', **{k: v})
55+
56+
P, Z = pzmap(T, **pzkwargs)
57+
58+
np.testing.assert_allclose(P, Pref, rtol=1e-3)
59+
np.testing.assert_allclose(Z, Zref, rtol=1e-3)
60+
61+
if kwargs.get('plot', True):
62+
ax = plt.gca()
63+
assert ax.get_title() == kwargs.get('title', 'Pole Zero Map')
64+
if kwargs.get('grid', False):
65+
# TODO: check for correct grid
66+
pass
67+
68+
69+
def test_pzmap_warns():
70+
with pytest.warns(FutureWarning):
71+
pzmap(TransferFunction([1], [1, 2]), Plot=True)
72+
73+
74+
def test_pzmap_raises():
75+
with pytest.raises(TypeError):
76+
# not an LTI system
77+
pzmap(([1], [1,2]))

0 commit comments

Comments
 (0)