Skip to content

Commit 04a9bbf

Browse files
Merge pull request #752 from jarrodmillman/mypy
Add MyPy type checking
2 parents 2330236 + 51e25a5 commit 04a9bbf

File tree

103 files changed

+330
-214
lines changed

Some content is hidden

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

103 files changed

+330
-214
lines changed

.pre-commit-config.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,38 @@ repos:
3838
args: ["--fix", "--show-fixes", "--exit-non-zero-on-fix"]
3939
- id: ruff-format
4040

41+
- repo: https://github.com/pre-commit/mirrors-mypy
42+
rev: "v1.10.0"
43+
hooks:
44+
- id: mypy
45+
additional_dependencies:
46+
- types-aiofiles
47+
- types-requests
48+
- pandas-stubs
49+
- types-pillow
50+
- matplotlib
51+
exclude: |
52+
(?x)(
53+
^build/
54+
| ^pyximages/
55+
| conf\.py$
56+
| .*/setup.*\.py$
57+
| .*/demo.py$
58+
| .*/auto_examples/
59+
| advanced/mathematical_optimization/examples/plot_gradient_descent\.py$
60+
| advanced/mathematical_optimization/examples/helper/compare_optimizers\.py$
61+
| advanced/advanced_numpy/examples/view-colors\.py$
62+
| advanced/advanced_numpy/examples/stride-diagonals\.py$
63+
| advanced/interfacing_with_c/cython_numpy/test_cos_doubles\.py$
64+
| advanced/interfacing_with_c/numpy_shared/test_cos_doubles\.py$
65+
| advanced/interfacing_with_c/swig.*\.py$
66+
| advanced/advanced_numpy/examples/myobject_test\.py$
67+
| advanced/interfacing_with_c/numpy_shared/test_cos_doubles\.py$
68+
| advanced/interfacing_with_c/numpy_c_api/test_cos_module_np\.py$
69+
| intro/numpy/solutions/2_a_call_fortran\.py$
70+
| advanced/advanced_numpy/examples/mandelplot\.py$
71+
)
72+
4173
- repo: https://github.com/codespell-project/codespell
4274
rev: 193cd7d27cd571f79358af09a8fb8997e54f8fff # frozen: v2.3.0
4375
hooks:

advanced/advanced_numpy/examples/pilbuffer-answer.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,21 @@
77
"""
88

99
import numpy as np
10-
import Image
10+
from PIL import Image
1111

1212
# Let's make a sample image, RGBA format
1313

14-
x = np.zeros((200, 200, 4), dtype=np.int8)
14+
x = np.zeros((200, 200, 4), dtype=np.uint8)
1515

16-
x[:, :, 0] = 254 # red
16+
x[:, :, 0] = 255 # red
1717
x[:, :, 3] = 255 # opaque
1818

1919
data = x.view(np.int32) # Check that you understand why this is OK!
2020

2121
img = Image.frombuffer("RGBA", (200, 200), data)
2222
img.save("test.png")
2323

24-
#
2524
# Modify the original data, and save again.
26-
#
27-
# It turns out that PIL, which knows next to nothing about NumPy,
28-
# happily shares the same data.
29-
#
3025

31-
x[:, :, 1] = 254
26+
x[:, :, 1] = 255
3227
img.save("test2.png")

advanced/advanced_numpy/examples/pilbuffer.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
"""
2-
Exercise: using the buffer protocole
3-
====================================
2+
Exercise: using the buffer protocol
3+
===================================
44
5-
Skeletton of the code to do an exercise using the buffer protocole.
5+
Skeleton of the code to do an exercise using the buffer protocole.
66
"""
77

88
import numpy as np
9-
import Image
9+
from PIL import Image
1010

1111
# Let's make a sample image, RGBA format
1212

13-
x = np.zeros((200, 200, 4), dtype=np.int8)
13+
x = np.zeros((200, 200, 4), dtype=np.uint8)
1414

15-
# TODO: fill `x` with fully opaque red [255, 0, 0, 255]
15+
# TODO: fill `data` with fully opaque red [255, 0, 0, 255]
1616

17-
# TODO: RGBA images consist of 32-bit integers whose bytes are [RR,GG,BB,AA]
18-
# How to get that from ``x``?
17+
# TODO: `x` is an array of bytes (8-bit integers)
18+
# What we need for PIL to understand this data is RGBA array,
19+
# where each element is a 32-bit integer, with bytes [RR,GG,BB,AA].
20+
# How do we convert `x` to such an array, called `data`?
1921

2022
data = ...
2123

advanced/advanced_numpy/examples/plots/plot_maskedstats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import matplotlib.pyplot as plt
1111

1212
data = np.loadtxt("../../../../data/populations.txt")
13-
populations = np.ma.masked_array(data[:, 1:])
13+
populations = np.ma.masked_array(data[:, 1:]) # type: ignore[var-annotated]
1414
year = data[:, 0]
1515

1616
bad_years = ((year >= 1903) & (year <= 1910)) | ((year >= 1917) & (year <= 1918))

advanced/debugging/to_debug_solution.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ def compare_optimizers(optimizers):
5757
"""
5858
random_a = -1.3 + rng.random(size=100)
5959
random_b = 0.3 + rng.random(size=100)
60-
param_grid = product(FUNCTIONS, random_a, random_b)
61-
values = []
62-
for value in param_grid:
63-
values.append(value)
64-
param_grid = values
60+
param_grid = list(product(FUNCTIONS, random_a, random_b))
6561
print("Benching 1D root-finder optimizers from scipy.optimize:")
6662
for optimizer in OPTIMIZERS:
6763
print(

advanced/debugging/wiener_filtering.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def iterated_wiener(noisy_img, size=3):
4646
face = sp.datasets.face(gray=True)
4747
noisy_face = face + 20 * rng.integers(3, size=face.shape) - 30
4848

49-
plt.matshow(face[cut], cmap=plt.cm.gray)
50-
plt.matshow(noisy_face[cut], cmap=plt.cm.gray)
49+
plt.matshow(face[cut], cmap="gray")
50+
plt.matshow(noisy_face[cut], cmap="gray")
5151

5252
denoised_face = iterated_wiener(noisy_face)
53-
plt.matshow(denoised_face[cut], cmap=plt.cm.gray)
53+
plt.matshow(denoised_face[cut], cmap="gray")
5454

5555
plt.show()

advanced/image_processing/examples/plot_GMM.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
plt.text(0.57, 0.8, "histogram", fontsize=20, transform=plt.gca().transAxes)
4848
plt.yticks([])
4949
plt.subplot(133)
50-
plt.imshow(binary_img, cmap=plt.cm.gray, interpolation="nearest")
50+
plt.imshow(binary_img, cmap="gray", interpolation="nearest")
5151
plt.axis("off")
5252

5353
plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1)

advanced/image_processing/examples/plot_block_mean.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
block_mean.shape = (sx // 4, sy // 6)
2020

2121
plt.figure(figsize=(5, 5))
22-
plt.imshow(block_mean, cmap=plt.cm.gray)
22+
plt.imshow(block_mean, cmap="gray")
2323
plt.axis("off")
2424

2525
plt.show()

advanced/image_processing/examples/plot_blur.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515

1616
plt.figure(figsize=(9, 3))
1717
plt.subplot(131)
18-
plt.imshow(blurred_face, cmap=plt.cm.gray)
18+
plt.imshow(blurred_face, cmap="gray")
1919
plt.axis("off")
2020
plt.subplot(132)
21-
plt.imshow(very_blurred, cmap=plt.cm.gray)
21+
plt.imshow(very_blurred, cmap="gray")
2222
plt.axis("off")
2323
plt.subplot(133)
24-
plt.imshow(local_mean, cmap=plt.cm.gray)
24+
plt.imshow(local_mean, cmap="gray")
2525
plt.axis("off")
2626

2727
plt.subplots_adjust(wspace=0, hspace=0.0, top=0.99, bottom=0.01, left=0.01, right=0.99)

advanced/image_processing/examples/plot_clean_morpho.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
l = 128
3737

3838
plt.subplot(141)
39-
plt.imshow(binary_img[:l, :l], cmap=plt.cm.gray)
39+
plt.imshow(binary_img[:l, :l], cmap="gray")
4040
plt.axis("off")
4141
plt.subplot(142)
42-
plt.imshow(open_img[:l, :l], cmap=plt.cm.gray)
42+
plt.imshow(open_img[:l, :l], cmap="gray")
4343
plt.axis("off")
4444
plt.subplot(143)
45-
plt.imshow(close_img[:l, :l], cmap=plt.cm.gray)
45+
plt.imshow(close_img[:l, :l], cmap="gray")
4646
plt.axis("off")
4747
plt.subplot(144)
48-
plt.imshow(mask[:l, :l], cmap=plt.cm.gray)
48+
plt.imshow(mask[:l, :l], cmap="gray")
4949
plt.contour(close_img[:l, :l], [0.5], linewidths=2, colors="r")
5050
plt.axis("off")
5151

advanced/image_processing/examples/plot_denoising.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
plt.axis("off")
3535
plt.title("Median filter", fontsize=20)
3636
plt.subplot(144)
37-
plt.imshow(np.abs(im - im_med), cmap=plt.cm.hot, interpolation="nearest")
37+
plt.imshow(np.abs(im - im_med), cmap="hot", interpolation="nearest")
3838
plt.axis("off")
3939
plt.title("Error", fontsize=20)
4040

advanced/image_processing/examples/plot_display_face.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
plt.figure(figsize=(10, 3.6))
1414

1515
plt.subplot(131)
16-
plt.imshow(f, cmap=plt.cm.gray)
16+
plt.imshow(f, cmap="gray")
1717

1818
plt.subplot(132)
19-
plt.imshow(f, cmap=plt.cm.gray, vmin=30, vmax=200)
19+
plt.imshow(f, cmap="gray", vmin=30, vmax=200)
2020
plt.axis("off")
2121

2222
plt.subplot(133)
23-
plt.imshow(f, cmap=plt.cm.gray)
23+
plt.imshow(f, cmap="gray")
2424
plt.contour(f, [50, 200])
2525
plt.axis("off")
2626

advanced/image_processing/examples/plot_face_denoise.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
plt.figure(figsize=(12, 2.8))
2424

2525
plt.subplot(131)
26-
plt.imshow(noisy, cmap=plt.cm.gray, vmin=40, vmax=220)
26+
plt.imshow(noisy, cmap="gray", vmin=40, vmax=220)
2727
plt.axis("off")
2828
plt.title("noisy", fontsize=20)
2929
plt.subplot(132)
30-
plt.imshow(gauss_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
30+
plt.imshow(gauss_denoised, cmap="gray", vmin=40, vmax=220)
3131
plt.axis("off")
3232
plt.title("Gaussian filter", fontsize=20)
3333
plt.subplot(133)
34-
plt.imshow(med_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
34+
plt.imshow(med_denoised, cmap="gray", vmin=40, vmax=220)
3535
plt.axis("off")
3636
plt.title("Median filter", fontsize=20)
3737

advanced/image_processing/examples/plot_face_tv_denoise.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
plt.figure(figsize=(12, 2.8))
2525

2626
plt.subplot(131)
27-
plt.imshow(noisy, cmap=plt.cm.gray, vmin=40, vmax=220)
27+
plt.imshow(noisy, cmap="gray", vmin=40, vmax=220)
2828
plt.axis("off")
2929
plt.title("noisy", fontsize=20)
3030
plt.subplot(132)
31-
plt.imshow(tv_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
31+
plt.imshow(tv_denoised, cmap="gray", vmin=40, vmax=220)
3232
plt.axis("off")
3333
plt.title("TV denoising", fontsize=20)
3434

3535
tv_denoised = denoise_tv_chambolle(noisy, weight=50)
3636
plt.subplot(133)
37-
plt.imshow(tv_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
37+
plt.imshow(tv_denoised, cmap="gray", vmin=40, vmax=220)
3838
plt.axis("off")
3939
plt.title("(more) TV denoising", fontsize=20)
4040

advanced/image_processing/examples/plot_find_edges.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
plt.figure(figsize=(16, 5))
2525
plt.subplot(141)
26-
plt.imshow(im, cmap=plt.cm.gray)
26+
plt.imshow(im, cmap="gray")
2727
plt.axis("off")
2828
plt.title("square", fontsize=20)
2929
plt.subplot(142)

advanced/image_processing/examples/plot_find_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
roi = im[slice_x, slice_y]
3636

3737
plt.figure(figsize=(4, 2))
38-
plt.axes([0, 0, 1, 1])
38+
plt.axes((0, 0, 1, 1))
3939
plt.imshow(roi)
4040
plt.axis("off")
4141

advanced/image_processing/examples/plot_geom_face.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@
2323

2424

2525
plt.subplot(151)
26-
plt.imshow(face, cmap=plt.cm.gray)
26+
plt.imshow(face, cmap="gray")
2727
plt.axis("off")
2828
plt.subplot(152)
29-
plt.imshow(crop_face, cmap=plt.cm.gray)
29+
plt.imshow(crop_face, cmap="gray")
3030
plt.axis("off")
3131
plt.subplot(153)
32-
plt.imshow(flip_ud_face, cmap=plt.cm.gray)
32+
plt.imshow(flip_ud_face, cmap="gray")
3333
plt.axis("off")
3434
plt.subplot(154)
35-
plt.imshow(rotate_face, cmap=plt.cm.gray)
35+
plt.imshow(rotate_face, cmap="gray")
3636
plt.axis("off")
3737
plt.subplot(155)
38-
plt.imshow(rotate_face_noreshape, cmap=plt.cm.gray)
38+
plt.imshow(rotate_face_noreshape, cmap="gray")
3939
plt.axis("off")
4040

4141
plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1)

advanced/image_processing/examples/plot_granulo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def granulometry(data, sizes=None):
4444
plt.figure(figsize=(6, 2.2))
4545

4646
plt.subplot(121)
47-
plt.imshow(mask, cmap=plt.cm.gray)
47+
plt.imshow(mask, cmap="gray")
4848
opened = sp.ndimage.binary_opening(mask, structure=disk_structure(10))
4949
opened_more = sp.ndimage.binary_opening(mask, structure=disk_structure(14))
5050
plt.contour(opened, [0.5], colors="b", linewidths=2)

advanced/image_processing/examples/plot_greyscale_dilation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323

2424
plt.figure(figsize=(12.5, 3))
2525
plt.subplot(141)
26-
plt.imshow(im, interpolation="nearest", cmap=plt.cm.nipy_spectral)
26+
plt.imshow(im, interpolation="nearest", cmap="nipy_spectral")
2727
plt.axis("off")
2828
plt.subplot(142)
29-
plt.imshow(bigger_points, interpolation="nearest", cmap=plt.cm.nipy_spectral)
29+
plt.imshow(bigger_points, interpolation="nearest", cmap="nipy_spectral")
3030
plt.axis("off")
3131
plt.subplot(143)
32-
plt.imshow(dist, interpolation="nearest", cmap=plt.cm.nipy_spectral)
32+
plt.imshow(dist, interpolation="nearest", cmap="nipy_spectral")
3333
plt.axis("off")
3434
plt.subplot(144)
35-
plt.imshow(dilate_dist, interpolation="nearest", cmap=plt.cm.nipy_spectral)
35+
plt.imshow(dilate_dist, interpolation="nearest", cmap="nipy_spectral")
3636
plt.axis("off")
3737

3838
plt.subplots_adjust(wspace=0, hspace=0.02, top=0.99, bottom=0.01, left=0.01, right=0.99)

advanced/image_processing/examples/plot_histo_segmentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
plt.text(0.57, 0.8, "histogram", fontsize=20, transform=plt.gca().transAxes)
4040
plt.yticks([])
4141
plt.subplot(133)
42-
plt.imshow(binary_img, cmap=plt.cm.gray, interpolation="nearest")
42+
plt.imshow(binary_img, cmap="gray", interpolation="nearest")
4343
plt.axis("off")
4444

4545
plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1)

advanced/image_processing/examples/plot_interpolation_face.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
plt.figure(figsize=(8, 4))
1414

1515
plt.subplot(1, 2, 1)
16-
plt.imshow(f[320:340, 510:530], cmap=plt.cm.gray)
16+
plt.imshow(f[320:340, 510:530], cmap="gray")
1717
plt.axis("off")
1818

1919
plt.subplot(1, 2, 2)
20-
plt.imshow(f[320:340, 510:530], cmap=plt.cm.gray, interpolation="nearest")
20+
plt.imshow(f[320:340, 510:530], cmap="gray", interpolation="nearest")
2121
plt.axis("off")
2222

2323
plt.subplots_adjust(wspace=0.02, hspace=0.02, top=1, bottom=0, left=0, right=1)

advanced/image_processing/examples/plot_measure_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
plt.figure(figsize=(6, 3))
3434

3535
plt.subplot(121)
36-
plt.imshow(label_im, cmap=plt.cm.nipy_spectral)
36+
plt.imshow(label_im, cmap="nipy_spectral")
3737
plt.axis("off")
3838
plt.subplot(122)
39-
plt.imshow(label_clean, vmax=nb_labels, cmap=plt.cm.nipy_spectral)
39+
plt.imshow(label_clean, vmax=nb_labels, cmap="nipy_spectral")
4040
plt.axis("off")
4141

4242
plt.subplots_adjust(wspace=0.01, hspace=0.01, top=1, bottom=0, left=0, right=1)

advanced/image_processing/examples/plot_numpy_array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
face[range(400), range(400)] = 255
2323

2424
plt.figure(figsize=(3, 3))
25-
plt.axes([0, 0, 1, 1])
26-
plt.imshow(face, cmap=plt.cm.gray)
25+
plt.axes((0, 0, 1, 1))
26+
plt.imshow(face, cmap="gray")
2727
plt.axis("off")
2828

2929
plt.show()

advanced/image_processing/examples/plot_propagation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
plt.figure(figsize=(9.5, 3))
2424
plt.subplot(131)
25-
plt.imshow(square, cmap=plt.cm.gray, interpolation="nearest")
25+
plt.imshow(square, cmap="gray", interpolation="nearest")
2626
plt.axis("off")
2727
plt.subplot(132)
28-
plt.imshow(open_square, cmap=plt.cm.gray, interpolation="nearest")
28+
plt.imshow(open_square, cmap="gray", interpolation="nearest")
2929
plt.axis("off")
3030
plt.subplot(133)
31-
plt.imshow(reconstruction, cmap=plt.cm.gray, interpolation="nearest")
31+
plt.imshow(reconstruction, cmap="gray", interpolation="nearest")
3232
plt.axis("off")
3333

3434
plt.subplots_adjust(wspace=0, hspace=0.02, top=0.99, bottom=0.01, left=0.01, right=0.99)

0 commit comments

Comments
 (0)