Skip to content

Commit 7d4bb96

Browse files
committed
remove np.matrix unit test infrastructure
1 parent af4fa21 commit 7d4bb96

File tree

10 files changed

+164
-258
lines changed

10 files changed

+164
-258
lines changed

.github/workflows/doctest.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ jobs:
3333
- name: Run doctest
3434
shell: bash -l {0}
3535
env:
36-
PYTHON_CONTROL_ARRAY_AND_MATRIX: ${{ matrix.array-and-matrix }}
3736
MPLBACKEND: ${{ matrix.mplbackend }}
3837
working-directory: doc
3938
run: |

.github/workflows/python-package-conda.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ jobs:
99
${{ matrix.slycot || 'no' }} Slycot;
1010
${{ matrix.pandas || 'no' }} Pandas;
1111
${{ matrix.cvxopt || 'no' }} CVXOPT
12-
${{ matrix.array-and-matrix == 1 && '; array and matrix' || '' }}
1312
${{ matrix.mplbackend && format('; {0}', matrix.mplbackend) }}
1413
runs-on: ubuntu-latest
1514

@@ -22,14 +21,12 @@ jobs:
2221
pandas: [""]
2322
cvxopt: ["", "conda"]
2423
mplbackend: [""]
25-
array-and-matrix: [0]
2624
include:
2725
- python-version: '3.11'
2826
slycot: conda
2927
pandas: conda
3028
cvxopt: conda
3129
mplbackend: QtAgg
32-
array-and-matrix: 1
3330

3431
steps:
3532
- uses: actions/checkout@v3
@@ -63,7 +60,6 @@ jobs:
6360
- name: Test with pytest
6461
shell: bash -l {0}
6562
env:
66-
PYTHON_CONTROL_ARRAY_AND_MATRIX: ${{ matrix.array-and-matrix }}
6763
MPLBACKEND: ${{ matrix.mplbackend }}
6864
run: pytest -v --cov=control --cov-config=.coveragerc control/tests
6965

control/tests/conftest.py

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,21 @@
1313

1414
# some common pytest marks. These can be used as test decorators or in
1515
# pytest.param(marks=)
16-
slycotonly = pytest.mark.skipif(not control.exception.slycot_check(),
17-
reason="slycot not installed")
18-
cvxoptonly = pytest.mark.skipif(not control.exception.cvxopt_check(),
19-
reason="cvxopt not installed")
20-
matrixfilter = pytest.mark.filterwarnings("ignore:.*matrix subclass:"
21-
"PendingDeprecationWarning")
22-
matrixerrorfilter = pytest.mark.filterwarnings("error:.*matrix subclass:"
23-
"PendingDeprecationWarning")
16+
slycotonly = pytest.mark.skipif(
17+
not control.exception.slycot_check(), reason="slycot not installed")
18+
cvxoptonly = pytest.mark.skipif(
19+
not control.exception.cvxopt_check(), reason="cvxopt not installed")
2420

2521

2622
@pytest.fixture(scope="session", autouse=True)
2723
def control_defaults():
2824
"""Make sure the testing session always starts with the defaults.
2925
30-
This should be the first fixture initialized,
31-
so that all other fixtures see the general defaults (unless they set them
32-
themselves) even before importing control/__init__. Enforce this by adding
33-
it as an argument to all other session scoped fixtures.
26+
This should be the first fixture initialized, so that all other
27+
fixtures see the general defaults (unless they set them themselves)
28+
even before importing control/__init__. Enforce this by adding it as an
29+
argument to all other session scoped fixtures.
30+
3431
"""
3532
control.reset_defaults()
3633
the_defaults = control.config.defaults.copy()
@@ -39,63 +36,6 @@ def control_defaults():
3936
assert control.config.defaults == the_defaults
4037

4138

42-
@pytest.fixture(scope="function", autouse=TEST_MATRIX_AND_ARRAY,
43-
params=[pytest.param("arrayout", marks=matrixerrorfilter),
44-
pytest.param("matrixout", marks=matrixfilter)])
45-
def matarrayout(request):
46-
"""Switch the config to use np.ndarray and np.matrix as returns."""
47-
restore = control.config.defaults['statesp.use_numpy_matrix']
48-
control.use_numpy_matrix(request.param == "matrixout", warn=False)
49-
yield
50-
control.use_numpy_matrix(restore, warn=False)
51-
52-
53-
def ismatarrayout(obj):
54-
"""Test if the returned object has the correct type as configured.
55-
56-
note that isinstance(np.matrix(obj), np.ndarray) is True
57-
"""
58-
use_matrix = control.config.defaults['statesp.use_numpy_matrix']
59-
return (isinstance(obj, np.ndarray)
60-
and isinstance(obj, np.matrix) == use_matrix)
61-
62-
63-
def asmatarrayout(obj):
64-
"""Return a object according to the configured default."""
65-
use_matrix = control.config.defaults['statesp.use_numpy_matrix']
66-
matarray = np.asmatrix if use_matrix else np.asarray
67-
return matarray(obj)
68-
69-
70-
@contextmanager
71-
def check_deprecated_matrix():
72-
"""Check that a call produces a deprecation warning because of np.matrix."""
73-
use_matrix = control.config.defaults['statesp.use_numpy_matrix']
74-
if use_matrix:
75-
with pytest.deprecated_call():
76-
try:
77-
yield
78-
finally:
79-
pass
80-
else:
81-
yield
82-
83-
84-
@pytest.fixture(scope="function",
85-
params=[p for p, usebydefault in
86-
[(pytest.param(np.array,
87-
id="arrayin"),
88-
True),
89-
(pytest.param(np.matrix,
90-
id="matrixin",
91-
marks=matrixfilter),
92-
False)]
93-
if usebydefault or TEST_MATRIX_AND_ARRAY])
94-
def matarrayin(request):
95-
"""Use array and matrix to construct input data in tests."""
96-
return request.param
97-
98-
9939
@pytest.fixture(scope="function")
10040
def editsdefaults():
10141
"""Make sure any changes to the defaults only last during a test."""

control/tests/iosys_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import control as ct
1919
from control import iosys as ios
20-
from control.tests.conftest import matrixfilter
2120

2221

2322
class TestIOSys:

control/tests/modelsimp_test.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99

1010
from control import StateSpace, forced_response, tf, rss, c2d
1111
from control.exception import ControlMIMONotImplemented
12-
from control.tests.conftest import slycotonly, matarrayin
12+
from control.tests.conftest import slycotonly
1313
from control.modelsimp import balred, hsvd, markov, modred
1414

1515

1616
class TestModelsimp:
1717
"""Test model reduction functions"""
1818

1919
@slycotonly
20-
def testHSVD(self, matarrayout, matarrayin):
21-
A = matarrayin([[1., -2.], [3., -4.]])
22-
B = matarrayin([[5.], [7.]])
23-
C = matarrayin([[6., 8.]])
24-
D = matarrayin([[9.]])
20+
def testHSVD(self):
21+
A = np.array([[1., -2.], [3., -4.]])
22+
B = np.array([[5.], [7.]])
23+
C = np.array([[6., 8.]])
24+
D = np.array([[9.]])
2525
sys = StateSpace(A, B, C, D)
2626
hsv = hsvd(sys)
2727
hsvtrue = np.array([24.42686, 0.5731395]) # from MATLAB
@@ -32,8 +32,8 @@ def testHSVD(self, matarrayout, matarrayin):
3232
assert isinstance(hsv, np.ndarray)
3333
assert not isinstance(hsv, np.matrix)
3434

35-
def testMarkovSignature(self, matarrayout, matarrayin):
36-
U = matarrayin([[1., 1., 1., 1., 1.]])
35+
def testMarkovSignature(self):
36+
U = np.array([[1., 1., 1., 1., 1.]])
3737
Y = U
3838
m = 3
3939
H = markov(Y, U, m, transpose=False)
@@ -111,17 +111,17 @@ def testMarkovResults(self, k, m, n):
111111
# for k=5, m=n=10: 0.015 %
112112
np.testing.assert_allclose(Mtrue, Mcomp, rtol=1e-6, atol=1e-8)
113113

114-
def testModredMatchDC(self, matarrayin):
114+
def testModredMatchDC(self):
115115
#balanced realization computed in matlab for the transfer function:
116116
# num = [1 11 45 32], den = [1 15 60 200 60]
117-
A = matarrayin(
117+
A = np.array(
118118
[[-1.958, -1.194, 1.824, -1.464],
119119
[-1.194, -0.8344, 2.563, -1.351],
120120
[-1.824, -2.563, -1.124, 2.704],
121121
[-1.464, -1.351, -2.704, -11.08]])
122-
B = matarrayin([[-0.9057], [-0.4068], [-0.3263], [-0.3474]])
123-
C = matarrayin([[-0.9057, -0.4068, 0.3263, -0.3474]])
124-
D = matarrayin([[0.]])
122+
B = np.array([[-0.9057], [-0.4068], [-0.3263], [-0.3474]])
123+
C = np.array([[-0.9057, -0.4068, 0.3263, -0.3474]])
124+
D = np.array([[0.]])
125125
sys = StateSpace(A, B, C, D)
126126
rsys = modred(sys,[2, 3],'matchdc')
127127
Artrue = np.array([[-4.431, -4.552], [-4.552, -5.361]])
@@ -133,30 +133,30 @@ def testModredMatchDC(self, matarrayin):
133133
np.testing.assert_array_almost_equal(rsys.C, Crtrue, decimal=3)
134134
np.testing.assert_array_almost_equal(rsys.D, Drtrue, decimal=2)
135135

136-
def testModredUnstable(self, matarrayin):
136+
def testModredUnstable(self):
137137
"""Check if an error is thrown when an unstable system is given"""
138-
A = matarrayin(
138+
A = np.array(
139139
[[4.5418, 3.3999, 5.0342, 4.3808],
140140
[0.3890, 0.3599, 0.4195, 0.1760],
141141
[-4.2117, -3.2395, -4.6760, -4.2180],
142142
[0.0052, 0.0429, 0.0155, 0.2743]])
143-
B = matarrayin([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]])
144-
C = matarrayin([[1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0]])
145-
D = matarrayin([[0.0, 0.0], [0.0, 0.0]])
143+
B = np.array([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]])
144+
C = np.array([[1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0]])
145+
D = np.array([[0.0, 0.0], [0.0, 0.0]])
146146
sys = StateSpace(A, B, C, D)
147147
np.testing.assert_raises(ValueError, modred, sys, [2, 3])
148148

149-
def testModredTruncate(self, matarrayin):
149+
def testModredTruncate(self):
150150
#balanced realization computed in matlab for the transfer function:
151151
# num = [1 11 45 32], den = [1 15 60 200 60]
152-
A = matarrayin(
152+
A = np.array(
153153
[[-1.958, -1.194, 1.824, -1.464],
154154
[-1.194, -0.8344, 2.563, -1.351],
155155
[-1.824, -2.563, -1.124, 2.704],
156156
[-1.464, -1.351, -2.704, -11.08]])
157-
B = matarrayin([[-0.9057], [-0.4068], [-0.3263], [-0.3474]])
158-
C = matarrayin([[-0.9057, -0.4068, 0.3263, -0.3474]])
159-
D = matarrayin([[0.]])
157+
B = np.array([[-0.9057], [-0.4068], [-0.3263], [-0.3474]])
158+
C = np.array([[-0.9057, -0.4068, 0.3263, -0.3474]])
159+
D = np.array([[0.]])
160160
sys = StateSpace(A, B, C, D)
161161
rsys = modred(sys,[2, 3],'truncate')
162162
Artrue = np.array([[-1.958, -1.194], [-1.194, -0.8344]])
@@ -170,18 +170,18 @@ def testModredTruncate(self, matarrayin):
170170

171171

172172
@slycotonly
173-
def testBalredTruncate(self, matarrayin):
173+
def testBalredTruncate(self):
174174
# controlable canonical realization computed in matlab for the transfer
175175
# function:
176176
# num = [1 11 45 32], den = [1 15 60 200 60]
177-
A = matarrayin(
177+
A = np.array(
178178
[[-15., -7.5, -6.25, -1.875],
179179
[8., 0., 0., 0.],
180180
[0., 4., 0., 0.],
181181
[0., 0., 1., 0.]])
182-
B = matarrayin([[2.], [0.], [0.], [0.]])
183-
C = matarrayin([[0.5, 0.6875, 0.7031, 0.5]])
184-
D = matarrayin([[0.]])
182+
B = np.array([[2.], [0.], [0.], [0.]])
183+
C = np.array([[0.5, 0.6875, 0.7031, 0.5]])
184+
D = np.array([[0.]])
185185

186186
sys = StateSpace(A, B, C, D)
187187
orders = 2
@@ -211,18 +211,18 @@ def testBalredTruncate(self, matarrayin):
211211
np.testing.assert_array_almost_equal(Dr, Drtrue, decimal=4)
212212

213213
@slycotonly
214-
def testBalredMatchDC(self, matarrayin):
214+
def testBalredMatchDC(self):
215215
# controlable canonical realization computed in matlab for the transfer
216216
# function:
217217
# num = [1 11 45 32], den = [1 15 60 200 60]
218-
A = matarrayin(
218+
A = np.array(
219219
[[-15., -7.5, -6.25, -1.875],
220220
[8., 0., 0., 0.],
221221
[0., 4., 0., 0.],
222222
[0., 0., 1., 0.]])
223-
B = matarrayin([[2.], [0.], [0.], [0.]])
224-
C = matarrayin([[0.5, 0.6875, 0.7031, 0.5]])
225-
D = matarrayin([[0.]])
223+
B = np.array([[2.], [0.], [0.], [0.]])
224+
C = np.array([[0.5, 0.6875, 0.7031, 0.5]])
225+
D = np.array([[0.]])
226226

227227
sys = StateSpace(A, B, C, D)
228228
orders = 2

0 commit comments

Comments
 (0)