Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Lint fixes on benchmarks and examples/*.py #1135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2025

Conversation

roryyorke
Copy link
Contributor

@roryyorke roryyorke commented Mar 2, 2025

I don't think these are too controversial.

CI failures

I see two failures on CI , but I can't see that these changes caused them:

first, in doctest-linux: https://github.com/roryyorke/python-control/actions/runs/13612156165/job/38050709282

Traceback
=========

      File "/home/runner/miniconda3/envs/doctest-env/lib/python3.12/site-packages/sphinx/events.py", line 415, in emit
        raise ExtensionError(
    sphinx.errors.ExtensionError: Handler <function html_collect_pages at 0x7f36a35016c0> for event 'html-collect-pages' threw an exception (exception: module 'sphinx.util' has no attribute 'console')

which seems to be sphinx-doc/sphinx#13352.

Then in install-examples: https://github.com/roryyorke/python-control/actions/runs/13612156159/job/38050709274

ModuleNotFoundError: No module named 'autoreload'

I'll install jupyterlab in my dev environment and see if I can reproduce this. I can't see that changing *.py files in examples could cause this.

tfvis.py and Pwm Pmw

When I ran tfvis.py under Python 3.12, I got

AttributeError: module 'collections' has no attribute 'Callable'

in Pwm Pmw code.

Callable was moved to collections.abc in Python 3.10 (October 2021). The hack below lets the script run, but unless pwm Pmw is updated we should consider retiring this example.

modified   examples/tfvis.py
@@ -48,6 +48,9 @@ from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
 from control.matlab import logspace
 from numpy import array, conj, polymul, real, size
 
+import collections.abc
+collections.Callable = collections.abc.Callable
+
 
 def make_poly(facts):
     """ Create polynomial from factors """

Warnings in examples

Some examples raise control deprecation warnings, e.g., genswitch.py uses phase_plot instead of phase_plane_plot. I started looking into this, but it got a bit involved.

Lint on notebooks

ruff can also run on Jupyter notebooks. Some fixes are obvious:

examples/bode-and-nyquist-plots.ipynb:cell 2:2:17: F401 [*] `scipy` imported but unused
  |
1 | import numpy as np
2 | import scipy as sp
  |                 ^^ F401
3 | import matplotlib.pyplot as plt
4 | import control as ct
  |
  = help: Remove unused import: `scipy`

but I'm less sure about changing these * imports; they are convenient, and make things more familiar for Matlab users:

examples/pvtol-lqr-nested.ipynb:cell 3:2:1: F403 `from matplotlib.pyplot import *` used; unable to detect undefined names
  |
1 | from numpy import *             # Grab all of the NumPy functions
2 | from matplotlib.pyplot import * # Grab MATLAB plotting functions
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ F403
3 | from control.matlab import *    # MATLAB-like functions
4 | %matplotlib inline
  |

@murrayrm
Copy link
Member

murrayrm commented Mar 2, 2025

The CI errors are also showing up in #1133, so probably something changed on the GitHub Actions end of things. I'll set up a new issue to track that.

@coveralls
Copy link

coveralls commented Mar 2, 2025

Coverage Status

coverage: 94.745%. remained the same
when pulling 5653531 on roryyorke:rory/lint-examples
into ad996f9 on python-control:main.

@roryyorke
Copy link
Contributor Author

The CI errors are also showing up in #1133, so probably something changed on the GitHub Actions end of things.

Thanks. I can't reproduce the notebook conversion failure locally. I've attached the output of mamba list to record versions of everything installed.

I see there's already a bug on Pmw for collections.Callable, filed in 2020: https://sourceforge.net/p/pmw/bugs/52/

packages.txt

@murrayrm murrayrm added this to the 0.10.2 milestone Mar 3, 2025
@murrayrm
Copy link
Member

murrayrm commented Mar 8, 2025

Sorry for the delay in reviewing this. All of the changes look good to me.

Some comments on your comments:

tfvis.py and Pmw

Callable was moved to collections.abc in Python 3.10 (October 2021). The hack below lets the script run, but unless pwm Pmw is updated we should consider retiring this example.

I think it would be fine to move this to examples/legacy. To a large extent the functionality demonstrated in the script is captured in sisotool.

Warnings in examples

Some examples raise control deprecation warnings, e.g., genswitch.py uses phase_plot instead of phase_plane_plot. I started looking into this, but it got a bit involved.

Perhaps flag some of these as separate issues? For the specific case of genswitch.py (and perhaps others), I likely have new versions that I did for FBS2e (I'm slowly converting all examples and figures to use python-control).

Lint on notebooks

... I'm less sure about changing these * imports; they are convenient, and make things more familiar for Matlab users ...

It would be nice to run lint on the notebooks. I agree that for MATLAB users, it will be convenient to use from control.matlab import * and perhaps we should just flag that one as OK wherever it appears. But I believe that the main control toolbox should always use a prefix (preferably ct for consistency).

@murrayrm murrayrm merged commit 394e1c2 into python-control:main Mar 8, 2025
40 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants