Skip to content

Commit 59bd89d

Browse files
committed
Apply Python linting fixes across src/ and exercise files
- Fix import sorting, unused imports, and undefined names - Modernize syntax (f-strings, raw strings for regex) - Add src/compat/ module with replacements for deprecated scitools - Update pyproject.toml with ruff configuration - Apply consistent formatting to exercise files in doc/.src/chapters/
1 parent 36f0915 commit 59bd89d

75 files changed

Lines changed: 1022 additions & 523 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc/.src/chapters/diffu/exer-diffu/axisymm_flow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def solver_theta(I, a, R, Nr, D, T, theta=0.5, u_L=None, u_R=0, user_action=None
6363
r*alpha is needed midway between spatial mesh points, - use
6464
arithmetic mean of successive mesh values (i.e. of r_i*alpha_i)
6565
"""
66-
t0 = time.clock()
66+
t0 = time.perf_counter()
6767

6868
r = linspace(0, R, Nr + 1) # mesh points in space
6969
dr = r[1] - r[0]
@@ -163,7 +163,7 @@ def solver_theta(I, a, R, Nr, D, T, theta=0.5, u_L=None, u_R=0, user_action=None
163163
# Switch variables before next step
164164
u_1, u = u, u_1
165165

166-
t1 = time.clock()
166+
t1 = time.perf_counter()
167167
# return u_1, since u and u_1 are switched
168168
return u_1, t, t1 - t0
169169

doc/.src/chapters/diffu/exer-diffu/surface_osc.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,16 @@
3535

3636
print(sol[0].shape)
3737
print(sol[1].shape)
38-
import scitools.std as plt
38+
import matplotlib.pyplot as plt
3939

4040
counter = 0
4141
for u0, u1 in zip(sol[0][2:], sol[1][2:], strict=False):
4242
x0 = sol[0][0]
4343
x1 = sol[1][0]
44-
plt.plot(
45-
x0,
46-
u0,
47-
"r-",
48-
x1,
49-
u1,
50-
"b-",
51-
legend=["short", "long"],
52-
savefig="tmp_%04d.png" % counter,
53-
axis=[x1[0], x1[-1], -1.1, 1.1],
54-
)
44+
plt.clf()
45+
plt.plot(x0, u0, "r-", label="short")
46+
plt.plot(x1, u1, "b-", label="long")
47+
plt.legend()
48+
plt.axis([x1[0], x1[-1], -1.1, 1.1])
49+
plt.savefig("tmp_%04d.png" % counter)
5550
counter += 1

doc/.src/chapters/diffu/exer-diffu/welding.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,21 @@ def run(gamma, beta=10, delta=40, scaling=1, animate=False):
2727

2828
import time
2929

30-
import scitools.std as plt
30+
import matplotlib.pyplot as plt
3131

3232
plot_arrays = []
3333

3434
def process_u(u, x, t, n):
3535
global ymax
3636
if animate:
37-
plt.plot(
38-
x,
39-
u,
40-
"r-",
41-
x,
42-
f(x, t[n]) / delta,
43-
"b-",
44-
axis=[0, L, ymin, ymax],
45-
title=f"t={t[n]:f}",
46-
xlabel="x",
47-
ylabel=f"u and f/{delta:g}",
48-
)
37+
plt.clf()
38+
plt.plot(x, u, "r-", x, f(x, t[n]) / delta, "b-")
39+
plt.axis([0, L, ymin, ymax])
40+
plt.title(f"t={t[n]:f}")
41+
plt.xlabel("x")
42+
plt.ylabel(f"u and f/{delta:g}")
43+
plt.draw()
44+
plt.pause(0.001)
4945
if t[n] == 0:
5046
time.sleep(1)
5147
plot_arrays.append(x)
@@ -65,18 +61,10 @@ def process_u(u, x, t, n):
6561
x = plot_arrays[0]
6662
plt.figure()
6763
for u, f in plot_arrays[1:]:
68-
plt.plot(
69-
x,
70-
u,
71-
"r-",
72-
x,
73-
f,
74-
"b--",
75-
axis=[x[0], x[-1], 0, ymax],
76-
xlabel="$x$",
77-
ylabel=rf"$u, \ f/{delta:g}$",
78-
)
79-
plt.hold("on")
64+
plt.plot(x, u, "r-", x, f, "b--")
65+
plt.axis([x[0], x[-1], 0, ymax])
66+
plt.xlabel("$x$")
67+
plt.ylabel(rf"$u, \ f/{delta:g}$")
8068
plt.legend(
8169
[
8270
"$u,\\ t=0.2$",

doc/.src/chapters/trunc/exer-trunk/trunc_vib_ic_fw.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def visualize(u, t, I, w):
2929
plot(t, u, "r--o")
3030
t_fine = linspace(0, t[-1], 1001) # very fine mesh for u_e
3131
u_e = exact_solution(t_fine, I, w)
32-
hold("on")
3332
plot(t_fine, u_e, "b-")
3433
legend(["numerical", "exact"], loc="upper left")
3534
xlabel("t")
@@ -100,8 +99,6 @@ def main():
10099
parser.add_argument("--dt", type=float, default=0.05)
101100
parser.add_argument("--num_periods", type=int, default=5)
102101
parser.add_argument("--savefig", action="store_true")
103-
# Hack to allow --SCITOOLS options (read when importing scitools.std)
104-
parser.add_argument("--SCITOOLS_easyviz_backend", default="matplotlib")
105102
a = parser.parse_args()
106103
I, w, dt, num_periods, savefig = a.I, a.w, a.dt, a.num_periods, a.savefig
107104
P = 2 * pi / w # one period
@@ -122,7 +119,6 @@ def plot_empirical_freq_and_amplitude(u, t, I, w):
122119
a = amplitudes(minima, maxima)
123120
figure()
124121
plot(range(len(p)), 2 * pi / p, "r-")
125-
hold("on")
126122
plot(range(len(a)), a, "b-")
127123
plot(range(len(p)), [w] * len(p), "r--")
128124
plot(range(len(a)), [I] * len(a), "b--")
@@ -144,8 +140,9 @@ def visualize_front(u, t, I, w, savefig=False):
144140
curves as they evolve in time.
145141
Makes it easy to plot very long time series.
146142
"""
147-
import scitools.std as st
148-
from scitools.MovingPlotWindow import MovingPlotWindow
143+
import matplotlib.pyplot as plt
144+
145+
from compat.moving_plot_window import MovingPlotWindow
149146

150147
P = 2 * pi / w # one period
151148
umin = 1.2 * u.min()
@@ -156,21 +153,18 @@ def visualize_front(u, t, I, w, savefig=False):
156153
for n in range(1, len(u)):
157154
if plot_manager.plot(n):
158155
s = plot_manager.first_index_in_plot
159-
st.plot(
160-
t[s : n + 1],
161-
u[s : n + 1],
162-
"r-1",
163-
t[s : n + 1],
164-
I * cos(w * t)[s : n + 1],
165-
"b-1",
166-
title=f"t={t[n]:6.3f}",
167-
axis=plot_manager.axis(),
168-
show=not savefig,
169-
) # drop window if savefig
156+
plt.clf()
157+
plt.plot(t[s : n + 1], u[s : n + 1], "r-")
158+
plt.plot(t[s : n + 1], I * cos(w * t)[s : n + 1], "b-")
159+
plt.title(f"t={t[n]:6.3f}")
160+
plt.axis(plot_manager.axis())
170161
if savefig:
171162
filename = "tmp_vib%04d.png" % n
172-
st.savefig(filename)
163+
plt.savefig(filename)
173164
print("making plot file", filename, f"at t={t[n]:g}")
165+
else:
166+
plt.draw()
167+
plt.pause(0.001)
174168
plot_manager.update(n)
175169

176170

@@ -182,7 +176,7 @@ def visualize_front_ascii(u, t, I, w, fps=10):
182176
"""
183177
import time
184178

185-
from scitools.avplotter import Plotter
179+
from compat.ascii_plotter import Plotter
186180

187181
P = 2 * pi / w
188182
umin = 1.2 * u.min()

doc/.src/chapters/vib/exer-vib/binary_star.py

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# import matplotlib.pyplot as plt
1+
import matplotlib.pyplot as plt
22
import numpy as np
33
import odespy
4-
import scitools.std as plt
54

65

76
def solver(alpha, ic, T, dt=0.05):
@@ -38,9 +37,10 @@ def demo_circular():
3837
# mass A is at (1, 0) with vel. (0, 1)
3938
ic = [1, 0, 0, 1, 0, 0, 0, 0]
4039
x_A, x_B, y_A, y_B, t = solver(alpha=0.001, ic=ic, T=2 * np.pi, dt=0.01)
41-
plt.plot(
42-
x_A, x_B, "r2-", y_A, y_B, "b2-", legend=["A", "B"], daspectmode="equal"
43-
) # x and y axis have same scaling
40+
plt.plot(x_A, x_B, "r-", label="A")
41+
plt.plot(y_A, y_B, "b-", label="B")
42+
plt.gca().set_aspect("equal")
43+
plt.legend()
4444
plt.savefig("tmp_circular.png")
4545
plt.savefig("tmp_circular.pdf")
4646
plt.show()
@@ -63,39 +63,24 @@ def demo_two_stars(animate=True):
6363
if animate:
6464
# Animate motion and draw the objects' paths in time
6565
for i in range(len(x_A)):
66-
plt.plot(
67-
x_A[: i + 1],
68-
x_B[: i + 1],
69-
"r-",
70-
y_A[: i + 1],
71-
y_B[: i + 1],
72-
"b-",
73-
[x_A[0], x_A[i]],
74-
[x_B[0], x_B[i]],
75-
"r2o",
76-
[y_A[0], y_A[i]],
77-
[y_B[0], y_B[i]],
78-
"b4o",
79-
daspectmode="equal", # axes aspect
80-
legend=["A", "B", "A", "B"],
81-
axis=[-1, 1, -1, 1],
82-
savefig="tmp_%04d.png" % i,
83-
title=f"t={t[i]:.2f}",
84-
)
66+
plt.clf()
67+
plt.plot(x_A[: i + 1], x_B[: i + 1], "r-", label="A")
68+
plt.plot(y_A[: i + 1], y_B[: i + 1], "b-", label="B")
69+
plt.plot([x_A[0], x_A[i]], [x_B[0], x_B[i]], "ro")
70+
plt.plot([y_A[0], y_A[i]], [y_B[0], y_B[i]], "bo")
71+
plt.gca().set_aspect("equal")
72+
plt.legend()
73+
plt.axis([-1, 1, -1, 1])
74+
plt.title(f"t={t[i]:.2f}")
75+
plt.savefig("tmp_%04d.png" % i)
8576
else:
8677
# Make a simple static plot of the solution
87-
plt.plot(
88-
x_A,
89-
x_B,
90-
"r-",
91-
y_A,
92-
y_B,
93-
"b-",
94-
daspectmode="equal",
95-
legend=["A", "B"],
96-
axis=[-1, 1, -1, 1],
97-
savefig="tmp_two_stars.png",
98-
)
78+
plt.plot(x_A, x_B, "r-", label="A")
79+
plt.plot(y_A, y_B, "b-", label="B")
80+
plt.gca().set_aspect("equal")
81+
plt.legend()
82+
plt.axis([-1, 1, -1, 1])
83+
plt.savefig("tmp_two_stars.png")
9984
# plt.axes().set_aspect('equal') # mpl
10085
plt.show()
10186

@@ -107,4 +92,4 @@ def demo_two_stars(animate=True):
10792
demo_circular()
10893
else:
10994
demo_two_stars(True)
110-
raw_input()
95+
input()

doc/.src/chapters/vib/exer-vib/elastic_pendulum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import matplotlib.pyplot as plt
12
import numpy as np
23
import odespy
3-
import scitools.std as plt
44

55

66
def simulate(

doc/.src/chapters/vib/exer-vib/elastic_pendulum_drag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import matplotlib.pyplot as plt
12
import numpy as np
23
import odespy
3-
import scitools.std as plt
44

55

66
def simulate_drag(

doc/.src/chapters/vib/exer-vib/sliding_box.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3-
import scitools.std as plt
43

54

65
def plot_spring():
@@ -10,7 +9,6 @@ def plot_spring():
109
for alpha in alpha_values:
1110
print(alpha, s(u))
1211
plt.plot(u, s(u))
13-
plt.hold("on")
1412
plt.legend([rf"$\alpha={alpha:g}$" for alpha in alpha_values])
1513
plt.xlabel("u")
1614
plt.ylabel("Spring response $s(u)$")
@@ -51,7 +49,6 @@ def f(u, t, beta, gamma):
5149
for beta in beta_values:
5250
u, t = simulate(beta, gamma, 0, 6, 60)
5351
plt.plot(t, u)
54-
plt.hold("on")
5552
plt.legend([rf"$\beta={beta:g}$" for beta in beta_values])
5653
plt.title(rf"$\gamma={gamma:g}$")
5754
plt.xlabel("$t$")
@@ -60,4 +57,4 @@ def f(u, t, beta, gamma):
6057
plt.savefig(filestem + ".png")
6158
plt.savefig(filestem + ".pdf")
6259
plt.show()
63-
raw_input()
60+
input()

doc/.src/chapters/vib/exer-vib/test_vib_undamped_exact_discrete_sol.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,13 @@ def u_numerical_exact(t):
3030
u_e = u_numerical_exact(t)
3131
error = abs(u_e - u).max()
3232
# Make a plot in a file, but not on the screen
33-
from scitools.std import plot
34-
35-
plot(
36-
t,
37-
u,
38-
"bo",
39-
t,
40-
u_e,
41-
"r-",
42-
legend=("numerical", "exact"),
43-
show=False,
44-
savefig="tmp.png",
45-
)
33+
import matplotlib.pyplot as plt
34+
35+
plt.plot(t, u, "bo", label="numerical")
36+
plt.plot(t, u_e, "r-", label="exact")
37+
plt.legend()
38+
plt.savefig("tmp.png")
39+
plt.close()
4640

4741
assert error < 1e-14
4842

doc/.src/chapters/vib/exer-vib/vib_EulerCromer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def demo():
8585
w = 0.5
8686
P = 2 * pi / w
8787
T = 4 * P
88-
import scitools.std as plt
88+
import matplotlib.pyplot as plt
8989

9090
from vib import solver as solver2
9191

@@ -96,7 +96,7 @@ def demo():
9696
plt.plot(t, u, "r-", t2, u2, "b-")
9797
plt.legend(["Euler-Cromer", "centered scheme"])
9898
plt.title(f"dt={dt:.3g}")
99-
raw_input()
99+
input()
100100
plt.savefig("tmp_%d" % k + ".png")
101101
plt.savefig("tmp_%d" % k + ".pdf")
102102
dt /= 2

0 commit comments

Comments
 (0)