Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5e16344
reorganize travis matrix, extend conftest.py
bnavigator Dec 29, 2020
c220a98
pytestify bdalg_test
bnavigator Dec 29, 2020
90a0059
pytestify canonical_test
bnavigator Jul 26, 2020
79b5bfe
pytestify config_test
bnavigator Dec 29, 2020
74d563c
pytestify convert_test.py
bnavigator Jul 24, 2020
c7cc828
pytestify ctrlutil_test
bnavigator Jul 24, 2020
d899140
pytestify delay_test.py
bnavigator Jul 24, 2020
53520e9
pytestify discrete_test
bnavigator Aug 19, 2020
659aa2d
pytestify flatsys_test
bnavigator Dec 29, 2020
1da4c5c
pytestify frd_test
bnavigator Jul 24, 2020
96f0911
pytestify freqresp_test.py
bnavigator Dec 29, 2020
caed712
pytestify input_element_int_test
bnavigator Jul 26, 2020
55a6119
pytestify iosys_test
bnavigator Dec 29, 2020
521e927
pytestify lti_test.py
bnavigator Jul 26, 2020
2c31029
pytestify mateqn_test
bnavigator Dec 29, 2020
fc2f770
pytestify matlab tests
bnavigator Jul 26, 2020
5880473
pytestify minreal_test
bnavigator Jul 26, 2020
97eedaf
pytestify modelsimp
bnavigator Dec 29, 2020
3eb0a92
pytestify nichols_test
bnavigator Jul 26, 2020
000998f
pytestify phaseplot_test
bnavigator Jul 28, 2020
3177796
pytestify rlocus_test.py
bnavigator Jul 28, 2020
f745bd2
pytestify robust tests
bnavigator Dec 29, 2020
2ee0773
pytestify sisotool_test.py
bnavigator Dec 29, 2020
e660363
pytestify slycot_convert_test.py
bnavigator Jul 28, 2020
952c511
pytestify statefbk tests
bnavigator Dec 29, 2020
6386832
pytestify statesp tests
bnavigator Dec 29, 2020
1dd2525
pytestify timeresp_test.py
bnavigator Dec 29, 2020
29883e8
pytestify xferfcn_input_test.py
bnavigator Jul 31, 2020
ca9476f
pytestify xferfcn tests
bnavigator Dec 29, 2020
2b98769
make tests work with pre #431 source code state
bnavigator Dec 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pytestify config_test
  • Loading branch information
bnavigator committed Dec 29, 2020
commit 79b5bfe53da7e042f38f7c14a95c588f47618a94
178 changes: 85 additions & 93 deletions control/tests/config_test.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
#!/usr/bin/env python
#
# config_test.py - test config module
# RMM, 25 may 2019
#
# This test suite checks the functionality of the config module

import unittest
"""config_test.py - test config module

RMM, 25 may 2019

This test suite checks the functionality of the config module
"""

from math import pi, log10

import matplotlib.pyplot as plt
from matplotlib.testing.decorators import cleanup as mplcleanup
import numpy as np
import pytest

import control as ct
import matplotlib.pyplot as plt
from math import pi, log10


class TestConfig(unittest.TestCase):
def setUp(self):
# Create a simple second order system to use for testing
self.sys = ct.tf([10], [1, 2, 1])
@pytest.mark.usefixtures("editsdefaults") # makes sure to reset the defaults
# to the test configuration
class TestConfig:

# Create a simple second order system to use for testing
sys = ct.tf([10], [1, 2, 1])

def test_set_defaults(self):
ct.config.set_defaults('config', test1=1, test2=2, test3=None)
self.assertEqual(ct.config.defaults['config.test1'], 1)
self.assertEqual(ct.config.defaults['config.test2'], 2)
self.assertEqual(ct.config.defaults['config.test3'], None)
assert ct.config.defaults['config.test1'] == 1
assert ct.config.defaults['config.test2'] == 2
assert ct.config.defaults['config.test3'] is None

@mplcleanup
def test_get_param(self):
self.assertEqual(
ct.config._get_param('bode', 'dB'),
ct.config.defaults['bode.dB'])
self.assertEqual(ct.config._get_param('bode', 'dB', 1), 1)
assert ct.config._get_param('bode', 'dB')\
== ct.config.defaults['bode.dB']
assert ct.config._get_param('bode', 'dB', 1) == 1
ct.config.defaults['config.test1'] = 1
self.assertEqual(ct.config._get_param('config', 'test1', None), 1)
self.assertEqual(ct.config._get_param('config', 'test1', None, 1), 1)

ct.config.defaults['config.test3'] = None
self.assertEqual(ct.config._get_param('config', 'test3'), None)
self.assertEqual(ct.config._get_param('config', 'test3', 1), 1)
self.assertEqual(
ct.config._get_param('config', 'test3', None, 1), None)

self.assertEqual(ct.config._get_param('config', 'test4'), None)
self.assertEqual(ct.config._get_param('config', 'test4', 1), 1)
self.assertEqual(ct.config._get_param('config', 'test4', 2, 1), 2)
self.assertEqual(ct.config._get_param('config', 'test4', None, 3), 3)

self.assertEqual(
ct.config._get_param('config', 'test4', {'test4':1}, None), 1)
assert ct.config._get_param('config', 'test1', None) == 1
assert ct.config._get_param('config', 'test1', None, 1) == 1

ct.config.defaults['config.test3'] is None
assert ct.config._get_param('config', 'test3') is None
assert ct.config._get_param('config', 'test3', 1) == 1
assert ct.config._get_param('config', 'test3', None, 1) is None

assert ct.config._get_param('config', 'test4') is None
assert ct.config._get_param('config', 'test4', 1) == 1
assert ct.config._get_param('config', 'test4', 2, 1) == 2
assert ct.config._get_param('config', 'test4', None, 3) == 3

assert ct.config._get_param('config', 'test4', {'test4': 1}, None) == 1

@mplcleanup
def test_fbs_bode(self):
ct.use_fbs_defaults()

Expand Down Expand Up @@ -91,8 +94,7 @@ def test_fbs_bode(self):
phase_x, phase_y = (((plt.gcf().axes[1]).get_lines())[0]).get_data()
np.testing.assert_almost_equal(phase_y[-1], -pi, decimal=2)

ct.reset_defaults()

@mplcleanup
def test_matlab_bode(self):
ct.use_matlab_defaults()

Expand Down Expand Up @@ -120,7 +122,7 @@ def test_matlab_bode(self):
# Make sure the x-axis is in rad/sec and y-axis is in degrees
np.testing.assert_almost_equal(phase_x[-1], 1000, decimal=1)
np.testing.assert_almost_equal(phase_y[-1], -180, decimal=0)

# Override the defaults and make sure that works as well
plt.figure()
ct.bode_plot(self.sys, omega, dB=True)
Expand All @@ -137,8 +139,7 @@ def test_matlab_bode(self):
phase_x, phase_y = (((plt.gcf().axes[1]).get_lines())[0]).get_data()
np.testing.assert_almost_equal(phase_y[-1], -pi, decimal=2)

ct.reset_defaults()

@mplcleanup
def test_custom_bode_default(self):
ct.config.defaults['bode.dB'] = True
ct.config.defaults['bode.deg'] = True
Expand All @@ -160,24 +161,22 @@ def test_custom_bode_default(self):
np.testing.assert_almost_equal(mag_y[0], 20*log10(10), decimal=3)
np.testing.assert_almost_equal(phase_y[-1], -pi, decimal=2)

ct.reset_defaults()

@mplcleanup
def test_bode_number_of_samples(self):
# Set the number of samples (default is 50, from np.logspace)
mag_ret, phase_ret, omega_ret = ct.bode_plot(self.sys, omega_num=87)
self.assertEqual(len(mag_ret), 87)
assert len(mag_ret) == 87

# Change the default number of samples
ct.config.defaults['freqplot.number_of_samples'] = 76
mag_ret, phase_ret, omega_ret = ct.bode_plot(self.sys)
self.assertEqual(len(mag_ret), 76)
assert len(mag_ret) == 76

# Override the default number of samples
mag_ret, phase_ret, omega_ret = ct.bode_plot(self.sys, omega_num=87)
self.assertEqual(len(mag_ret), 87)

ct.reset_defaults()
assert len(mag_ret) == 87

@mplcleanup
def test_bode_feature_periphery_decade(self):
# Generate a sample Bode plot to figure out the range it uses
ct.reset_defaults() # Make sure starting state is correct
Expand All @@ -198,30 +197,26 @@ def test_bode_feature_periphery_decade(self):
np.testing.assert_almost_equal(omega_ret[0], omega_min*10)
np.testing.assert_almost_equal(omega_ret[-1], omega_max/10)

ct.reset_defaults()

def test_reset_defaults(self):
ct.use_matlab_defaults()
ct.reset_defaults()
self.assertEqual(ct.config.defaults['bode.dB'], False)
self.assertEqual(ct.config.defaults['bode.deg'], True)
self.assertEqual(ct.config.defaults['bode.Hz'], False)
self.assertEqual(
ct.config.defaults['freqplot.number_of_samples'], None)
self.assertEqual(
ct.config.defaults['freqplot.feature_periphery_decades'], 1.0)
assert not ct.config.defaults['bode.dB']
assert ct.config.defaults['bode.deg']
assert not ct.config.defaults['bode.Hz']
assert ct.config.defaults['freqplot.number_of_samples'] is None
assert ct.config.defaults['freqplot.feature_periphery_decades'] == 1.0

def test_legacy_defaults(self):
ct.use_legacy_defaults('0.8.3')
assert(isinstance(ct.ss(0,0,0,1).D, np.matrix))

with pytest.deprecated_call():
ct.use_legacy_defaults('0.8.3')
assert(isinstance(ct.ss(0, 0, 0, 1).D, np.matrix))
ct.reset_defaults()
assert(isinstance(ct.ss(0,0,0,1).D, np.ndarray))
assert(not isinstance(ct.ss(0,0,0,1).D, np.matrix))
assert(isinstance(ct.ss(0, 0, 0, 1).D, np.ndarray))
assert(not isinstance(ct.ss(0, 0, 0, 1).D, np.matrix))

ct.use_legacy_defaults('0.9.0')
assert(isinstance(ct.ss(0,0,0,1).D, np.ndarray))
assert(not isinstance(ct.ss(0,0,0,1).D, np.matrix))
assert(isinstance(ct.ss(0, 0, 0, 1).D, np.ndarray))
assert(not isinstance(ct.ss(0, 0, 0, 1).D, np.matrix))

# test that old versions don't raise a problem
ct.use_legacy_defaults('REL-0.1')
Expand All @@ -231,30 +226,27 @@ def test_legacy_defaults(self):
ct.use_legacy_defaults('0.1')

# Make sure that nonsense versions generate an error
self.assertRaises(ValueError, ct.use_legacy_defaults, "a.b.c")
self.assertRaises(ValueError, ct.use_legacy_defaults, "1.x.3")

# Leave everything like we found it
ct.config.reset_defaults()

def test_change_default_dt(self):
ct.set_defaults('statesp', default_dt=0)
self.assertEqual(ct.ss(0,0,0,1).dt, 0)
ct.set_defaults('statesp', default_dt=None)
self.assertEqual(ct.ss(0,0,0,1).dt, None)
ct.set_defaults('xferfcn', default_dt=0)
self.assertEqual(ct.tf(1, 1).dt, 0)
ct.set_defaults('xferfcn', default_dt=None)
self.assertEqual(ct.tf(1, 1).dt, None)


def tearDown(self):
# Get rid of any figures that we created
plt.close('all')

# Reset the configuration defaults
ct.config.reset_defaults()


if __name__ == '__main__':
unittest.main()
with pytest.raises(ValueError):
ct.use_legacy_defaults("a.b.c")
with pytest.raises(ValueError):
ct.use_legacy_defaults("1.x.3")

@pytest.mark.parametrize("dt", [0, None])
def test_change_default_dt(self, dt):
"""Test that system with dynamics uses correct default dt"""
ct.set_defaults('statesp', default_dt=dt)
assert ct.ss(1, 0, 0, 1).dt == dt
ct.set_defaults('xferfcn', default_dt=dt)
assert ct.tf(1, [1, 1]).dt == dt

# nlsys = ct.iosys.NonlinearIOSystem(
# lambda t, x, u: u * x * x,
# lambda t, x, u: x, inputs=1, outputs=1)
# assert nlsys.dt == dt

def test_change_default_dt_static(self):
"""Test that static gain systems always have dt=None"""
ct.set_defaults('control', default_dt=0)
Comment thread
bnavigator marked this conversation as resolved.
assert ct.tf(1, 1).dt is None
assert ct.ss(0, 0, 0, 1).dt is None
# TODO: add in test for static gain iosys