|
1 | 1 | # ctrlplot_test.py - test out control plotting utilities |
2 | 2 | # RMM, 27 Jun 2024 |
3 | 3 |
|
| 4 | +import inspect |
| 5 | +import warnings |
| 6 | + |
4 | 7 | import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
5 | 9 | import pytest |
6 | 10 |
|
7 | 11 | import control as ct |
8 | 12 |
|
| 13 | +# List of all plotting functions |
| 14 | +resp_plot_fcns = [ |
| 15 | + # response function plotting function |
| 16 | + (ct.frequency_response, ct.bode_plot), |
| 17 | + (ct.frequency_response, ct.nichols_plot), |
| 18 | + (ct.singular_values_response, ct.singular_values_plot), |
| 19 | + (ct.gangof4_response, ct.gangof4_plot), |
| 20 | + (ct.describing_function_response, ct.describing_function_plot), |
| 21 | + (None, ct.phase_plane_plot), |
| 22 | + (ct.pole_zero_map, ct.pole_zero_plot), |
| 23 | + (ct.nyquist_response, ct.nyquist_plot), |
| 24 | + (ct.root_locus_map, ct.root_locus_plot), |
| 25 | + (ct.initial_response, ct.time_response_plot), |
| 26 | + (ct.step_response, ct.time_response_plot), |
| 27 | + (ct.impulse_response, ct.time_response_plot), |
| 28 | + (ct.forced_response, ct.time_response_plot), |
| 29 | + (ct.input_output_response, ct.time_response_plot), |
| 30 | +] |
| 31 | + |
| 32 | +deprecated_fcns = [ct.phase_plot] |
| 33 | + |
| 34 | +# Make sure we didn't miss any plotting functions |
| 35 | +def test_find_respplot_functions(): |
| 36 | + # Get the list of plotting functions |
| 37 | + plot_fcns = {respplot[1] for respplot in resp_plot_fcns} |
| 38 | + |
| 39 | + # Look through every object in the package |
| 40 | + found = 0 |
| 41 | + for name, obj in inspect.getmembers(ct): |
| 42 | + # Skip anything that is outside of this module |
| 43 | + if inspect.getmodule(obj) is not None and \ |
| 44 | + not inspect.getmodule(obj).__name__.startswith('control'): |
| 45 | + # Skip anything that isn't part of the control package |
| 46 | + continue |
| 47 | + |
| 48 | + # Only look for non-deprecated functions ending in 'plot' |
| 49 | + if not inspect.isfunction(obj) or name[-4:] != 'plot' or \ |
| 50 | + obj in deprecated_fcns: |
| 51 | + continue |
| 52 | + |
| 53 | + # Make sure that we have this on our list of functions |
| 54 | + assert obj in plot_fcns |
| 55 | + found += 1 |
| 56 | + |
| 57 | + assert found == len(plot_fcns) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize("resp_fcn, plot_fcn", resp_plot_fcns) |
| 61 | +@pytest.mark.usefixtures('mplcleanup') |
| 62 | +def test_plot_functions(resp_fcn, plot_fcn): |
| 63 | + # Create some systems to use |
| 64 | + sys1 = ct.rss(2, 1, 1, strictly_proper=True) |
| 65 | + sys2 = ct.rss(4, 1, 1, strictly_proper=True) |
| 66 | + |
| 67 | + # Set up arguments |
| 68 | + kwargs = meth_kwargs = plot_fcn_kwargs = {} |
| 69 | + match resp_fcn, plot_fcn: |
| 70 | + case ct.describing_function_response, _: |
| 71 | + F = ct.descfcn.saturation_nonlinearity(1) |
| 72 | + amp = np.linspace(1, 4, 10) |
| 73 | + args = (sys1, F, amp) |
| 74 | + |
| 75 | + case ct.gangof4_response, _: |
| 76 | + args = (sys1, sys2) |
| 77 | + |
| 78 | + case ct.frequency_response, ct.nichols_plot: |
| 79 | + args = (sys1, ) |
| 80 | + meth_kwargs = {'plot_type': 'nichols'} |
| 81 | + |
| 82 | + case ct.root_locus_map, ct.root_locus_plot: |
| 83 | + args = (sys1, ) |
| 84 | + meth_kwargs = plot_fcn_kwargs = {'interactive': False} |
| 85 | + |
| 86 | + case (ct.forced_response | ct.input_output_response, _): |
| 87 | + timepts = np.linspace(1, 10) |
| 88 | + U = np.sin(timepts) |
| 89 | + args = (sys1, timepts, U) |
| 90 | + |
| 91 | + case _, _: |
| 92 | + args = (sys1, ) |
| 93 | + |
| 94 | + # Call the plot through the response function |
| 95 | + if resp_fcn is not None: |
| 96 | + resp = resp_fcn(*args, **kwargs) |
| 97 | + cplt1 = resp.plot(**kwargs, **meth_kwargs) |
| 98 | + assert isinstance(cplt1, ct.ControlPlot) |
| 99 | + |
| 100 | + # Call the plot directly, plotting on top of previous plot |
| 101 | + if plot_fcn not in [None, ct.time_response_plot]: |
| 102 | + cplt2 = plot_fcn(*args, **kwargs, **plot_fcn_kwargs) |
| 103 | + assert isinstance(cplt2, ct.ControlPlot) |
| 104 | + |
| 105 | + # Plot should have landed on top of previous plot |
| 106 | + if resp_fcn is not None: |
| 107 | + assert cplt2.figure == cplt1.figure |
| 108 | + if plot_fcn != ct.root_locus_plot: |
| 109 | + assert np.all(cplt2.axes == cplt1.axes) |
| 110 | + else: |
| 111 | + warnings.warn("test skipped for root locus plot") |
| 112 | + assert len(cplt2.lines[0]) == len(cplt1.lines[0]) |
| 113 | + |
| 114 | + # Pass axes explicitly |
| 115 | + if resp_fcn is not None: |
| 116 | + cplt3 = resp.plot(**kwargs, **meth_kwargs, ax=cplt1.axes) |
| 117 | + assert cplt3.figure == cplt1.figure |
| 118 | + if plot_fcn != ct.root_locus_plot: |
| 119 | + assert np.all(cplt3.axes == cplt1.axes) |
| 120 | + else: |
| 121 | + warnings.warn("test skipped for root locus plot") |
| 122 | + assert len(cplt3.lines[0]) == len(cplt1.lines[0]) |
| 123 | + |
9 | 124 |
|
10 | 125 | @pytest.mark.usefixtures('mplcleanup') |
11 | 126 | def test_rcParams(): |
|
0 commit comments