Skip to content

Commit ed80cbe

Browse files
authored
Merge pull request #263 from roryyorke/rory/fix-examples-2
Make all examples Python 3 compatible
2 parents bcf5382 + def37f8 commit ed80cbe

12 files changed

+118
-47
lines changed

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,18 @@ script:
7676
- 'if [ $SLYCOT != "" ]; then python -c "import slycot"; fi'
7777
- coverage run setup.py test
7878

79+
# only run examples if Slycot is install
80+
# set PYTHONPATH for examples
81+
# pmw needed for examples/tfvis.py
82+
# future is needed for Python 2, also for examples/tfvis.py
83+
84+
- if [[ "$SLYCOT" != "" ]]; then
85+
export PYTHONPATH=$PWD;
86+
conda install -c conda-forge pmw future;
87+
cd examples; bash run_examples.sh; cd ..;
88+
fi
89+
90+
# arbitrary change to try to trigger travis build
91+
7992
after_success:
8093
- coveralls

examples/genswitch.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# of a genetic switch. Plots time traces and a phase portrait using
66
# the python-control library.
77

8+
import os
9+
810
import numpy as np
911
import matplotlib.pyplot as mpl
1012
from scipy.integrate import odeint
@@ -50,7 +52,6 @@ def genswitch(y, t, mu=4, n=2):
5052
mpl.figure(3); mpl.clf(); # subplot(221);
5153
mpl.plot(tim1, sol1[:,0], 'b-', tim1, sol1[:,1], 'g--');
5254
# set(pl, 'LineWidth', AM_data_linewidth);
53-
mpl.hold(True);
5455
mpl.plot([tim1[-1], tim1[-1]+1],
5556
[sol1[-1,0], sol2[0,1]], 'ko:',
5657
[tim1[-1], tim1[-1]+1], [sol1[-1,1], sol2[0,0]], 'ko:');
@@ -71,10 +72,11 @@ def genswitch(y, t, mu=4, n=2):
7172
timepts = [0.2, 0.6, 1.2])
7273

7374
# Add the stable equilibrium points
74-
mpl.hold(True);
7575
mpl.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
7676
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3);
7777

7878
mpl.xlabel('Protein A [scaled]');
7979
mpl.ylabel('Protein B [scaled]'); # 'Rotation', 90);
8080

81+
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
82+
mpl.show()

examples/phaseplots.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This file contains examples of phase portraits pulled from "Feedback
55
# Systems" by Astrom and Murray (Princeton University Press, 2008).
66

7+
import os
8+
79
import numpy as np
810
import matplotlib.pyplot as mpl
911
from control.phaseplot import phase_plot
@@ -120,4 +122,5 @@ def saddle_ode(x, t):
120122
mpl.xlabel('$x_1$'); mpl.ylabel('$x_2$');
121123
mpl.title('Undamped system\nLyapunov stable, not asympt. stable')
122124

123-
mpl.show()
125+
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
126+
mpl.show()

examples/pvtol-lqr.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#
44
# This file works through an LQR based design problem, using the
55
# planar vertical takeoff and landing (PVTOL) aircraft example from
6-
# Astrom and Mruray, Chapter 5. It is intended to demonstrate the
6+
# Astrom and Murray, Chapter 5. It is intended to demonstrate the
77
# basic functionality of the python-control package.
88
#
99

10+
import os
11+
1012
from numpy import * # Grab all of the NumPy functions
1113
from matplotlib.pyplot import * # Grab MATLAB plotting functions
1214
from control.matlab import * # MATLAB-like functions
@@ -118,9 +120,9 @@
118120
(Yy, Ty) = step(H1ay, T=linspace(0,10,100));
119121

120122
subplot(221); title("Identity weights")
121-
# plot(T, Y[:,1, 1], '-', T, Y[:,2, 2], '--'); hold(True);
122-
plot(Tx.T, Yx.T, '-', Ty.T, Yy.T, '--'); hold(True);
123-
plot([0, 10], [1, 1], 'k-'); hold(True);
123+
# plot(T, Y[:,1, 1], '-', T, Y[:,2, 2], '--');
124+
plot(Tx.T, Yx.T, '-', Ty.T, Yy.T, '--');
125+
plot([0, 10], [1, 1], 'k-');
124126

125127
axis([0, 10, -0.1, 1.4]);
126128
ylabel('position');
@@ -141,10 +143,10 @@
141143
[Y3, T3] = step(H1cx, T=linspace(0,10,100));
142144

143145
subplot(222); title("Effect of input weights")
144-
plot(T1.T, Y1.T, 'b-'); hold(True);
145-
plot(T2.T, Y2.T, 'b-'); hold(True);
146-
plot(T3.T, Y3.T, 'b-'); hold(True);
147-
plot([0 ,10], [1, 1], 'k-'); hold(True);
146+
plot(T1.T, Y1.T, 'b-');
147+
plot(T2.T, Y2.T, 'b-');
148+
plot(T3.T, Y3.T, 'b-');
149+
plot([0 ,10], [1, 1], 'k-');
148150

149151
axis([0, 10, -0.1, 1.4]);
150152

@@ -190,4 +192,6 @@
190192
xlabel('time');
191193
legend(('x', 'y'), loc='lower right');
192194

193-
show()
195+
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
196+
show()
197+

examples/pvtol-nested-ss.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
bode(L, logspace(-4, 3));
9999

100100
# Add crossover line
101-
subplot(211); hold(True);
101+
subplot(211);
102102
loglog([1e-4, 1e3], [1, 1], 'k-')
103103

104104
# Replot phase starting at -90 degrees
@@ -107,7 +107,6 @@
107107

108108
subplot(212);
109109
semilogx([1e-4, 1e3], [-180, -180], 'k-')
110-
hold(True);
111110
semilogx(w, np.squeeze(phase), 'b-')
112111
axis([1e-4, 1e3, -360, 0]);
113112
xlabel('Frequency [deg]'); ylabel('Phase [deg]');
@@ -118,7 +117,7 @@
118117
# Nyquist plot for complete design
119118
#
120119
figure(7); clf;
121-
axis([-700, 5300, -3000, 3000]); hold(True);
120+
axis([-700, 5300, -3000, 3000]);
122121
nyquist(L, (0.0001, 1000));
123122
axis([-700, 5300, -3000, 3000]);
124123

@@ -127,7 +126,7 @@
127126

128127
# Expanded region
129128
figure(8); clf; subplot(231);
130-
axis([-10, 5, -20, 20]); hold(True);
129+
axis([-10, 5, -20, 20]);
131130
nyquist(L);
132131
axis([-10, 5, -20, 20]);
133132

@@ -145,7 +144,7 @@
145144

146145
figure(9);
147146
(Yvec, Tvec) = step(T, linspace(1, 20));
148-
plot(Tvec.T, Yvec.T); hold(True);
147+
plot(Tvec.T, Yvec.T);
149148

150149
(Yvec, Tvec) = step(Co*S, linspace(1, 20));
151150
plot(Tvec.T, Yvec.T);

examples/robust_mimo.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
and Postlethwaite, 1st Edition.
55
"""
66

7+
import os
8+
79
import numpy as np
810
import matplotlib.pyplot as plt
911

@@ -68,7 +70,7 @@ def analysis():
6870
plt.ylabel('output')
6971
plt.ylim([-1.1,2.1])
7072
plt.legend()
71-
plt.title('o/l response to input [1,0]')
73+
plt.title('o/l response\nto input [1,0]')
7274

7375
plt.subplot(1,3,2)
7476
plt.plot(t,yu2[0],label='$y_1$')
@@ -77,7 +79,7 @@ def analysis():
7779
plt.ylabel('output')
7880
plt.ylim([-1.1,2.1])
7981
plt.legend()
80-
plt.title('o/l response to input [0,1]')
82+
plt.title('o/l response\nto input [0,1]')
8183

8284
plt.subplot(1,3,3)
8385
plt.plot(t,yuz[0],label='$y_1$')
@@ -86,7 +88,7 @@ def analysis():
8688
plt.ylabel('output')
8789
plt.ylim([-1.1,2.1])
8890
plt.legend()
89-
plt.title('o/l response to input [1,-1]')
91+
plt.title('o/l response\nto input [1,-1]')
9092

9193

9294
def synth(wb1,wb2):
@@ -103,7 +105,7 @@ def synth(wb1,wb2):
103105
wp2 = ss(weighting(wb=wb2, m=1.5, a=1e-4))
104106
wp = wp1.append(wp2)
105107
k,_,info = mixsyn(g,wp,wu)
106-
return k, info.gamma
108+
return k, info[0]
107109

108110

109111
def step_opposite(g,t):
@@ -177,4 +179,6 @@ def design():
177179

178180
analysis()
179181
design()
180-
plt.show()
182+
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
183+
plt.show()
184+

examples/robust_siso.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
and Postlethwaite, 1st Edition.
55
"""
66

7+
import os
8+
79
import numpy as np
810
import matplotlib.pyplot as plt
911

@@ -99,4 +101,5 @@
99101
plt.legend()
100102
plt.title('Disturbance response')
101103

102-
plt.show()
104+
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
105+
plt.show()

examples/rss-balred.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python
22

3+
import os
4+
35
import numpy as np
46
import control.modelsimp as msimp
57
import control.matlab as mt
@@ -29,7 +31,6 @@
2931
y, t = mt.step(fsys)
3032
yr, tr = mt.step(rsys)
3133
plt.plot(t.T, y.T)
32-
plt.hold(True)
3334
plt.plot(tr.T, yr.T)
3435

3536
# Repeat balanced reduction, now with 100-dimensional random state space
@@ -42,3 +43,7 @@
4243
yrandr, trandr = mt.impulse(rsysrand)
4344
plt.plot(trand.T, yrand.T, trandr.T, yrandr.T)
4445

46+
47+
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
48+
plt.show()
49+

examples/run_examples.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# Run the examples. Doesn't run Jupyter notebooks.
3+
4+
# The examples must cooperate: they must have no operations that wait
5+
# on user action, like matplotlib.pyplot.show, or starting a GUI event
6+
# loop. Environment variable PYCONTROL_TEST_EXAMPLES is set when the
7+
# examples are being tested; existence of this variable should be used
8+
# to prevent such user-action-waiting operations.
9+
10+
export PYCONTROL_TEST_EXAMPLES=1
11+
12+
example_errors=""
13+
14+
for example in *.py; do
15+
echo "Running ${example}"
16+
if ! python ${example}; then
17+
example_errors="${example_errors} ${example}"
18+
fi
19+
done
20+
21+
if [ -n "${example_errors}" ]; then
22+
echo These examples had errors:
23+
echo "${example_errors}"
24+
exit 1
25+
else
26+
echo All examples ran successfully
27+
fi

examples/slicot-test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Simple test function to test out SLICOT interface
22
# RMM, 28 May 09
33

4+
from __future__ import print_function
5+
46
import numpy as np # Numerical library
57
from scipy import * # Load the scipy functions
68
from control.matlab import * # Load the controls systems library
@@ -19,8 +21,8 @@
1921
# Eigenvalue placement
2022
#from slycot import sb01bd
2123
K = place(A, B, [-3, -2, -1])
22-
print "Pole place: K = ", K
23-
print "Pole place: eigs = ", np.linalg.eig(A - B * K)[0]
24+
print("Pole place: K = ", K)
25+
print("Pole place: eigs = ", np.linalg.eig(A - B * K)[0])
2426

2527
# from slycot import ab01md
2628
# Ac, Bc, ncont, info = ab01md('I', A, B)

examples/test-statefbk.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# test-statefbk.py - Unit tests for state feedback code
22
# RMM, 6 Sep 2010
33

4+
from __future__ import print_function
5+
46
import numpy as np # Numerical library
57
from scipy import * # Load the scipy functions
68
from control.matlab import * # Load the controls systems library
@@ -18,10 +20,10 @@
1820

1921
# Controllability
2022
Wc = ctrb(A, B)
21-
print "Wc = ", Wc
23+
print("Wc = ", Wc)
2224

2325
# Observability
2426
Wo = obsv(A, C)
25-
print "Wo = ", Wo
27+
print("Wo = ", Wo)
2628

2729

0 commit comments

Comments
 (0)