Skip to content

Commit c99f72f

Browse files
billtubbsmurrayrm
authored andcommitted
Updated examples to be PEP compliant (#307)
Multiple changes to make examples/ more PEP8 compliant: * Updated for Python 3 * Edited to comply with Python PEPs * Removed MATLAB-like formatting such as semi-colons, syntax and indenting PEP compliant (using PyCharm), changed imports from `from ___ import *` to things like `import matplotlib.pyplot as plt` and `import numpy as np`, removed tabs * Removed spacing around `*` and `/` operators for consistency and as per PEP 8. * Refactored name 'ord' which conflicts with built-in. * Reformatted comments * Removed tab characters * Replaced np.matrix with np.array objects
1 parent 8a11cd3 commit c99f72f

17 files changed

+1262
-506
lines changed

examples/bdalg-matlab.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
sys1ss = ss(A1, B1, C1, 0)
1111
sys1tf = ss2tf(sys1ss)
1212

13-
sys2tf = tf([1, 0.5], [1, 5]);
14-
sys2ss = tf2ss(sys2tf);
13+
sys2tf = tf([1, 0.5], [1, 5])
14+
sys2ss = tf2ss(sys2tf)
1515

1616
# Series composition
17-
series1 = sys1ss + sys2ss;
17+
series1 = sys1ss + sys2ss

examples/bode-and-nyquist-plots.ipynb

+78-45
Large diffs are not rendered by default.

examples/check-controllability-and-observability.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66

77
from __future__ import print_function
88

9-
from scipy import * # Load the scipy functions
9+
import numpy as np # Load the scipy functions
1010
from control.matlab import * # Load the controls systems library
1111

1212
# Parameters defining the system
1313

1414
m = 250.0 # system mass
15-
k = 40.0 # spring constant
16-
b = 60.0 # damping constant
15+
k = 40.0 # spring constant
16+
b = 60.0 # damping constant
1717

1818
# System matrices
19-
A = matrix([[1, -1, 1.],
20-
[1, -k / m, -b / m],
21-
[1, 1, 1]])
19+
A = np.array([[1, -1, 1.],
20+
[1, -k/m, -b/m],
21+
[1, 1, 1]])
2222

23-
B = matrix([[0],
24-
[1 / m],
25-
[1]])
23+
B = np.array([[0],
24+
[1/m],
25+
[1]])
2626

27-
C = matrix([[1., 0, 1.]])
27+
C = np.array([[1., 0, 1.]])
2828

2929
sys = ss(A, B, C, 0)
3030

examples/genswitch.py

+45-44
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,76 @@
88
import os
99

1010
import numpy as np
11-
import matplotlib.pyplot as mpl
11+
import matplotlib.pyplot as plt
1212
from scipy.integrate import odeint
1313
from control import phase_plot, box_grid
1414

1515
# Simple model of a genetic switch
16-
#
16+
1717
# This function implements the basic model of the genetic switch
1818
# Parameters taken from Gardner, Cantor and Collins, Nature, 2000
1919
def genswitch(y, t, mu=4, n=2):
20-
return (mu / (1 + y[1]**n) - y[0], mu / (1 + y[0]**n) - y[1])
20+
return mu/(1 + y[1]**n) - y[0], mu/(1 + y[0]**n) - y[1]
2121

2222
# Run a simulation from an initial condition
2323
tim1 = np.linspace(0, 10, 100)
2424
sol1 = odeint(genswitch, [1, 5], tim1)
2525

26-
# Extract the equlibirum points
27-
mu = 4; n = 2; # switch parameters
28-
eqpt = np.empty(3);
29-
eqpt[0] = sol1[0,-1]
30-
eqpt[1] = sol1[1,-1]
31-
eqpt[2] = 0; # fzero(@(x) mu/(1+x^2) - x, 2);
26+
# Extract the equilibrium points
27+
mu = 4; n = 2 # switch parameters
28+
eqpt = np.empty(3)
29+
eqpt[0] = sol1[0, -1]
30+
eqpt[1] = sol1[1, -1]
31+
eqpt[2] = 0 # fzero(@(x) mu/(1+x^2) - x, 2)
3232

3333
# Run another simulation showing switching behavior
34-
tim2 = np.linspace(11, 25, 100);
35-
sol2 = odeint(genswitch, sol1[-1,:] + [2, -2], tim2)
34+
tim2 = np.linspace(11, 25, 100)
35+
sol2 = odeint(genswitch, sol1[-1, :] + [2, -2], tim2)
3636

3737
# First plot out the curves that define the equilibria
3838
u = np.linspace(0, 4.5, 46)
39-
f = np.divide(mu, (1 + u**n)) # mu / (1 + u^n), elementwise
39+
f = np.divide(mu, (1 + u**n)) # mu/(1 + u^n), element-wise
4040

41-
mpl.figure(1); mpl.clf();
42-
mpl.axis([0, 5, 0, 5]); # box on;
43-
mpl.plot(u, f, '-', f, u, '--') # 'LineWidth', AM_data_linewidth);
44-
mpl.legend(('z1, f(z1)', 'z2, f(z2)')) # legend(lgh, 'boxoff');
45-
mpl.plot([0, 3], [0, 3], 'k-') # 'LineWidth', AM_ref_linewidth);
46-
mpl.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
47-
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3);
48-
mpl.xlabel('z1, f(z2)');
49-
mpl.ylabel('z2, f(z1)');
41+
plt.figure(1); plt.clf()
42+
plt.axis([0, 5, 0, 5]) # box on;
43+
plt.plot(u, f, '-', f, u, '--') # 'LineWidth', AM_data_linewidth)
44+
plt.legend(('z1, f(z1)', 'z2, f(z2)')) # legend(lgh, 'boxoff')
45+
plt.plot([0, 3], [0, 3], 'k-') # 'LineWidth', AM_ref_linewidth)
46+
plt.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
47+
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3)
48+
plt.xlabel('z1, f(z2)')
49+
plt.ylabel('z2, f(z1)')
5050

5151
# Time traces
52-
mpl.figure(3); mpl.clf(); # subplot(221);
53-
mpl.plot(tim1, sol1[:,0], 'b-', tim1, sol1[:,1], 'g--');
54-
# set(pl, 'LineWidth', AM_data_linewidth);
55-
mpl.plot([tim1[-1], tim1[-1]+1],
56-
[sol1[-1,0], sol2[0,1]], 'ko:',
57-
[tim1[-1], tim1[-1]+1], [sol1[-1,1], sol2[0,0]], 'ko:');
58-
# set(pl, 'LineWidth', AM_data_linewidth, 'MarkerSize', AM_data_markersize);
59-
mpl.plot(tim2, sol2[:,0], 'b-', tim2, sol2[:,1], 'g--');
60-
# set(pl, 'LineWidth', AM_data_linewidth);
61-
mpl.axis([0, 25, 0, 5]);
52+
plt.figure(3); plt.clf() # subplot(221)
53+
plt.plot(tim1, sol1[:, 0], 'b-', tim1, sol1[:, 1], 'g--')
54+
# set(pl, 'LineWidth', AM_data_linewidth)
55+
plt.plot([tim1[-1], tim1[-1] + 1],
56+
[sol1[-1, 0], sol2[0, 1]], 'ko:',
57+
[tim1[-1], tim1[-1] + 1], [sol1[-1, 1], sol2[0, 0]], 'ko:')
58+
# set(pl, 'LineWidth', AM_data_linewidth, 'MarkerSize', AM_data_markersize)
59+
plt.plot(tim2, sol2[:, 0], 'b-', tim2, sol2[:, 1], 'g--')
60+
# set(pl, 'LineWidth', AM_data_linewidth)
61+
plt.axis([0, 25, 0, 5])
6262

63-
mpl.xlabel('Time {\itt} [scaled]');
64-
mpl.ylabel('Protein concentrations [scaled]');
65-
mpl.legend(('z1 (A)', 'z2 (B)')) # 'Orientation', 'horizontal');
66-
# legend(legh, 'boxoff');
63+
plt.xlabel('Time {\itt} [scaled]')
64+
plt.ylabel('Protein concentrations [scaled]')
65+
plt.legend(('z1 (A)', 'z2 (B)')) # 'Orientation', 'horizontal')
66+
# legend(legh, 'boxoff')
6767

6868
# Phase portrait
69-
mpl.figure(2); mpl.clf(); # subplot(221);
70-
mpl.axis([0, 5, 0, 5]); # set(gca, 'DataAspectRatio', [1, 1, 1]);
71-
phase_plot(genswitch, X0 = box_grid([0, 5, 6], [0, 5, 6]), T = 10,
72-
timepts = [0.2, 0.6, 1.2])
69+
plt.figure(2)
70+
plt.clf() # subplot(221)
71+
plt.axis([0, 5, 0, 5]) # set(gca, 'DataAspectRatio', [1, 1, 1])
72+
phase_plot(genswitch, X0=box_grid([0, 5, 6], [0, 5, 6]), T=10,
73+
timepts=[0.2, 0.6, 1.2])
7374

7475
# Add the stable equilibrium points
75-
mpl.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
76-
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3);
76+
plt.plot(eqpt[0], eqpt[1], 'k.', eqpt[1], eqpt[0], 'k.',
77+
eqpt[2], eqpt[2], 'k.') # 'MarkerSize', AM_data_markersize*3)
7778

78-
mpl.xlabel('Protein A [scaled]');
79-
mpl.ylabel('Protein B [scaled]'); # 'Rotation', 90);
79+
plt.xlabel('Protein A [scaled]')
80+
plt.ylabel('Protein B [scaled]') # 'Rotation', 90)
8081

8182
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
82-
mpl.show()
83+
plt.show()

0 commit comments

Comments
 (0)