8000 add pzmap_test · basicmachines/python-control@2ca5220 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ca5220

Browse files
committed
add pzmap_test
1 parent bdbd198 commit 2ca5220

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
[pytest.param(dict(), id="default"),
18+
pytest.param(dict(plot=False), id="plot=False"),
19+
pytest.param(dict(plot=True), id="plot=True"),
20+
pytest.param(dict(grid=True), id="grid=True"),
21+
pytest.param(dict(title="My Title"), id="title")])
22+
@pytest.mark.parametrize("setdefaults", [False, True], ids=["kw", "config"])
23+
@pytest.mark.parametrize("dt", [0, 1], ids=["s", "z"])
24+
def test_pzmap(kwargs, setdefaults, dt, editsdefaults, mplcleanup):
25+
"""Test pzmap"""
26+
# T from from pvtol-nested example
27+
T = TransferFunction([-9.0250000e-01, -4.7200750e+01, -8.6812900e+02,
28+
+5.6261850e+03, +2.1258472e+05, +8.4724600e+05,
29+
+1.0192000e+06, +2.3520000e+05],
30+
[9.02500000e-03, 9.92862812e-01, 4.96974094e+01,
31+
1.35705659e+03, 2.09294163e+04, 1.64898435e+05,
32+
6.54572220e+05, 1.25274600e+06, 1.02420000e+06,
33+
2.35200000e+05],
34+
dt)
35+
36+
Pref = [-23.8877+19.3837j, -23.8877-19.3837j, -23.8349+15.7846j,
37+
-23.8349-15.7846j, -5.2320 +0.4117j, -5.2320 -0.4117j,
38+
-2.2246 +0.0000j, -1.5160 +0.0000j, -0.3627 +0.0000j]
39+
Zref = [-23.8877+19.3837j, -23.8877-19.3837j, +14.3637 +0.0000j,
40+
-14.3637 +0.0000j, -2.2246 +0.0000j, -2.0000 +0.0000j,
41+
-0.3000 +0.0000j]
42+
43+
pzkwargs = kwargs.copy()
44+
if setdefaults:
45+
for k in ['plot', 'grid']:
46+
if k in pzkwargs:
47+
v = pzkwargs.pop(k)
48+
config.set_defaults('pzmap', **{k: v})
49+
50+
P, Z = pzmap(T, **pzkwargs)
51+
52+
np.testing.assert_allclose(P, Pref, rtol=1e-3)
53+
np.testing.assert_allclose(Z, Zref, rtol=1e-3)
54+
55+
if kwargs.get('plot', True):
56+
ax = plt.gca()
57+
assert ax.get_title() == kwargs.get('title', 'Pole Zero Map')
58+
if kwargs.get('grid', False):
59+
# TODO: check for correct grid
60+
pass
61+
62+
63+
def test_pzmap_warns():
64+
with pytest.warns(FutureWarning):
65+
pzmap(TransferFunction([1], [1, 2]), Plot=True)
66+
67+
68+
def test_pzmap_raises():
69+
with pytest.raises(TypeError):
70+
# not an LTI system
71+
pzmap(([1], [1,2]))

0 commit comments

Comments
 (0)
0