Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,38 @@ repos:
args: ["--fix", "--show-fixes", "--exit-non-zero-on-fix"]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.10.0"
hooks:
- id: mypy
additional_dependencies:
- types-aiofiles
- types-requests
- pandas-stubs
- types-pillow
- matplotlib
exclude: |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if pre-commit could reuse the config and thus this exclude list from pyproject.toml?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the part I could not get to work :/ I don't know why it doesn't!

(?x)(
^build/
| ^pyximages/
| conf\.py$
| .*/setup.*\.py$
| .*/demo.py$
| .*/auto_examples/
| advanced/mathematical_optimization/examples/plot_gradient_descent\.py$
| advanced/mathematical_optimization/examples/helper/compare_optimizers\.py$
| advanced/advanced_numpy/examples/view-colors\.py$
| advanced/advanced_numpy/examples/stride-diagonals\.py$
| advanced/interfacing_with_c/cython_numpy/test_cos_doubles\.py$
| advanced/interfacing_with_c/numpy_shared/test_cos_doubles\.py$
| advanced/interfacing_with_c/swig.*\.py$
| advanced/advanced_numpy/examples/myobject_test\.py$
| advanced/interfacing_with_c/numpy_shared/test_cos_doubles\.py$
| advanced/interfacing_with_c/numpy_c_api/test_cos_module_np\.py$
| intro/numpy/solutions/2_a_call_fortran\.py$
| advanced/advanced_numpy/examples/mandelplot\.py$
)

- repo: https://github.com/codespell-project/codespell
rev: 193cd7d27cd571f79358af09a8fb8997e54f8fff # frozen: v2.3.0
hooks:
Expand Down
13 changes: 4 additions & 9 deletions advanced/advanced_numpy/examples/pilbuffer-answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,21 @@
"""

import numpy as np
import Image
from PIL import Image

# Let's make a sample image, RGBA format

x = np.zeros((200, 200, 4), dtype=np.int8)
x = np.zeros((200, 200, 4), dtype=np.uint8)

x[:, :, 0] = 254 # red
x[:, :, 0] = 255 # red
x[:, :, 3] = 255 # opaque

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

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

#
# Modify the original data, and save again.
#
# It turns out that PIL, which knows next to nothing about NumPy,
# happily shares the same data.
#

x[:, :, 1] = 254
x[:, :, 1] = 255
img.save("test2.png")
18 changes: 10 additions & 8 deletions advanced/advanced_numpy/examples/pilbuffer.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
"""
Exercise: using the buffer protocole
====================================
Exercise: using the buffer protocol
===================================

Skeletton of the code to do an exercise using the buffer protocole.
Skeleton of the code to do an exercise using the buffer protocole.
"""

import numpy as np
import Image
from PIL import Image

# Let's make a sample image, RGBA format

x = np.zeros((200, 200, 4), dtype=np.int8)
x = np.zeros((200, 200, 4), dtype=np.uint8)

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

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

data = ...

Expand Down
2 changes: 1 addition & 1 deletion advanced/advanced_numpy/examples/plots/plot_maskedstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import matplotlib.pyplot as plt

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

bad_years = ((year >= 1903) & (year <= 1910)) | ((year >= 1917) & (year <= 1918))
Expand Down
6 changes: 1 addition & 5 deletions advanced/debugging/to_debug_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ def compare_optimizers(optimizers):
"""
random_a = -1.3 + rng.random(size=100)
random_b = 0.3 + rng.random(size=100)
param_grid = product(FUNCTIONS, random_a, random_b)
values = []
for value in param_grid:
values.append(value)
param_grid = values
param_grid = list(product(FUNCTIONS, random_a, random_b))
print("Benching 1D root-finder optimizers from scipy.optimize:")
for optimizer in OPTIMIZERS:
print(
Expand Down
6 changes: 3 additions & 3 deletions advanced/debugging/wiener_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def iterated_wiener(noisy_img, size=3):
face = sp.datasets.face(gray=True)
noisy_face = face + 20 * rng.integers(3, size=face.shape) - 30

plt.matshow(face[cut], cmap=plt.cm.gray)
plt.matshow(noisy_face[cut], cmap=plt.cm.gray)
plt.matshow(face[cut], cmap="gray")
plt.matshow(noisy_face[cut], cmap="gray")

denoised_face = iterated_wiener(noisy_face)
plt.matshow(denoised_face[cut], cmap=plt.cm.gray)
plt.matshow(denoised_face[cut], cmap="gray")

plt.show()
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_GMM.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
plt.text(0.57, 0.8, "histogram", fontsize=20, transform=plt.gca().transAxes)
plt.yticks([])
plt.subplot(133)
plt.imshow(binary_img, cmap=plt.cm.gray, interpolation="nearest")
plt.imshow(binary_img, cmap="gray", interpolation="nearest")
plt.axis("off")

plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1)
Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_block_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
block_mean.shape = (sx // 4, sy // 6)

plt.figure(figsize=(5, 5))
plt.imshow(block_mean, cmap=plt.cm.gray)
plt.imshow(block_mean, cmap="gray")
plt.axis("off")

plt.show()
6 changes: 3 additions & 3 deletions advanced/image_processing/examples/plot_blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.imshow(blurred_face, cmap=plt.cm.gray)
plt.imshow(blurred_face, cmap="gray")
plt.axis("off")
plt.subplot(132)
plt.imshow(very_blurred, cmap=plt.cm.gray)
plt.imshow(very_blurred, cmap="gray")
plt.axis("off")
plt.subplot(133)
plt.imshow(local_mean, cmap=plt.cm.gray)
plt.imshow(local_mean, cmap="gray")
plt.axis("off")

plt.subplots_adjust(wspace=0, hspace=0.0, top=0.99, bottom=0.01, left=0.01, right=0.99)
Expand Down
8 changes: 4 additions & 4 deletions advanced/image_processing/examples/plot_clean_morpho.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@
l = 128

plt.subplot(141)
plt.imshow(binary_img[:l, :l], cmap=plt.cm.gray)
plt.imshow(binary_img[:l, :l], cmap="gray")
plt.axis("off")
plt.subplot(142)
plt.imshow(open_img[:l, :l], cmap=plt.cm.gray)
plt.imshow(open_img[:l, :l], cmap="gray")
plt.axis("off")
plt.subplot(143)
plt.imshow(close_img[:l, :l], cmap=plt.cm.gray)
plt.imshow(close_img[:l, :l], cmap="gray")
plt.axis("off")
plt.subplot(144)
plt.imshow(mask[:l, :l], cmap=plt.cm.gray)
plt.imshow(mask[:l, :l], cmap="gray")
plt.contour(close_img[:l, :l], [0.5], linewidths=2, colors="r")
plt.axis("off")

Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_denoising.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
plt.axis("off")
plt.title("Median filter", fontsize=20)
plt.subplot(144)
plt.imshow(np.abs(im - im_med), cmap=plt.cm.hot, interpolation="nearest")
plt.imshow(np.abs(im - im_med), cmap="hot", interpolation="nearest")
plt.axis("off")
plt.title("Error", fontsize=20)

Expand Down
6 changes: 3 additions & 3 deletions advanced/image_processing/examples/plot_display_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
plt.figure(figsize=(10, 3.6))

plt.subplot(131)
plt.imshow(f, cmap=plt.cm.gray)
plt.imshow(f, cmap="gray")

plt.subplot(132)
plt.imshow(f, cmap=plt.cm.gray, vmin=30, vmax=200)
plt.imshow(f, cmap="gray", vmin=30, vmax=200)
plt.axis("off")

plt.subplot(133)
plt.imshow(f, cmap=plt.cm.gray)
plt.imshow(f, cmap="gray")
plt.contour(f, [50, 200])
plt.axis("off")

Expand Down
6 changes: 3 additions & 3 deletions advanced/image_processing/examples/plot_face_denoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
plt.figure(figsize=(12, 2.8))

plt.subplot(131)
plt.imshow(noisy, cmap=plt.cm.gray, vmin=40, vmax=220)
plt.imshow(noisy, cmap="gray", vmin=40, vmax=220)
plt.axis("off")
plt.title("noisy", fontsize=20)
plt.subplot(132)
plt.imshow(gauss_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
plt.imshow(gauss_denoised, cmap="gray", vmin=40, vmax=220)
plt.axis("off")
plt.title("Gaussian filter", fontsize=20)
plt.subplot(133)
plt.imshow(med_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
plt.imshow(med_denoised, cmap="gray", vmin=40, vmax=220)
plt.axis("off")
plt.title("Median filter", fontsize=20)

Expand Down
6 changes: 3 additions & 3 deletions advanced/image_processing/examples/plot_face_tv_denoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
plt.figure(figsize=(12, 2.8))

plt.subplot(131)
plt.imshow(noisy, cmap=plt.cm.gray, vmin=40, vmax=220)
plt.imshow(noisy, cmap="gray", vmin=40, vmax=220)
plt.axis("off")
plt.title("noisy", fontsize=20)
plt.subplot(132)
plt.imshow(tv_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
plt.imshow(tv_denoised, cmap="gray", vmin=40, vmax=220)
plt.axis("off")
plt.title("TV denoising", fontsize=20)

tv_denoised = denoise_tv_chambolle(noisy, weight=50)
plt.subplot(133)
plt.imshow(tv_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
plt.imshow(tv_denoised, cmap="gray", vmin=40, vmax=220)
plt.axis("off")
plt.title("(more) TV denoising", fontsize=20)

Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_find_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

plt.figure(figsize=(16, 5))
plt.subplot(141)
plt.imshow(im, cmap=plt.cm.gray)
plt.imshow(im, cmap="gray")
plt.axis("off")
plt.title("square", fontsize=20)
plt.subplot(142)
Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_find_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
roi = im[slice_x, slice_y]

plt.figure(figsize=(4, 2))
plt.axes([0, 0, 1, 1])
plt.axes((0, 0, 1, 1))
plt.imshow(roi)
plt.axis("off")

Expand Down
10 changes: 5 additions & 5 deletions advanced/image_processing/examples/plot_geom_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@


plt.subplot(151)
plt.imshow(face, cmap=plt.cm.gray)
plt.imshow(face, cmap="gray")
plt.axis("off")
plt.subplot(152)
plt.imshow(crop_face, cmap=plt.cm.gray)
plt.imshow(crop_face, cmap="gray")
plt.axis("off")
plt.subplot(153)
plt.imshow(flip_ud_face, cmap=plt.cm.gray)
plt.imshow(flip_ud_face, cmap="gray")
plt.axis("off")
plt.subplot(154)
plt.imshow(rotate_face, cmap=plt.cm.gray)
plt.imshow(rotate_face, cmap="gray")
plt.axis("off")
plt.subplot(155)
plt.imshow(rotate_face_noreshape, cmap=plt.cm.gray)
plt.imshow(rotate_face_noreshape, cmap="gray")
plt.axis("off")

plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1)
Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_granulo.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def granulometry(data, sizes=None):
plt.figure(figsize=(6, 2.2))

plt.subplot(121)
plt.imshow(mask, cmap=plt.cm.gray)
plt.imshow(mask, cmap="gray")
opened = sp.ndimage.binary_opening(mask, structure=disk_structure(10))
opened_more = sp.ndimage.binary_opening(mask, structure=disk_structure(14))
plt.contour(opened, [0.5], colors="b", linewidths=2)
Expand Down
8 changes: 4 additions & 4 deletions advanced/image_processing/examples/plot_greyscale_dilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@

plt.figure(figsize=(12.5, 3))
plt.subplot(141)
plt.imshow(im, interpolation="nearest", cmap=plt.cm.nipy_spectral)
plt.imshow(im, interpolation="nearest", cmap="nipy_spectral")
plt.axis("off")
plt.subplot(142)
plt.imshow(bigger_points, interpolation="nearest", cmap=plt.cm.nipy_spectral)
plt.imshow(bigger_points, interpolation="nearest", cmap="nipy_spectral")
plt.axis("off")
plt.subplot(143)
plt.imshow(dist, interpolation="nearest", cmap=plt.cm.nipy_spectral)
plt.imshow(dist, interpolation="nearest", cmap="nipy_spectral")
plt.axis("off")
plt.subplot(144)
plt.imshow(dilate_dist, interpolation="nearest", cmap=plt.cm.nipy_spectral)
plt.imshow(dilate_dist, interpolation="nearest", cmap="nipy_spectral")
plt.axis("off")

plt.subplots_adjust(wspace=0, hspace=0.02, top=0.99, bottom=0.01, left=0.01, right=0.99)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
plt.text(0.57, 0.8, "histogram", fontsize=20, transform=plt.gca().transAxes)
plt.yticks([])
plt.subplot(133)
plt.imshow(binary_img, cmap=plt.cm.gray, interpolation="nearest")
plt.imshow(binary_img, cmap="gray", interpolation="nearest")
plt.axis("off")

plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1)
Expand Down
4 changes: 2 additions & 2 deletions advanced/image_processing/examples/plot_interpolation_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
plt.figure(figsize=(8, 4))

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

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

plt.subplots_adjust(wspace=0.02, hspace=0.02, top=1, bottom=0, left=0, right=1)
Expand Down
4 changes: 2 additions & 2 deletions advanced/image_processing/examples/plot_measure_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
plt.figure(figsize=(6, 3))

plt.subplot(121)
plt.imshow(label_im, cmap=plt.cm.nipy_spectral)
plt.imshow(label_im, cmap="nipy_spectral")
plt.axis("off")
plt.subplot(122)
plt.imshow(label_clean, vmax=nb_labels, cmap=plt.cm.nipy_spectral)
plt.imshow(label_clean, vmax=nb_labels, cmap="nipy_spectral")
plt.axis("off")

plt.subplots_adjust(wspace=0.01, hspace=0.01, top=1, bottom=0, left=0, right=1)
Expand Down
4 changes: 2 additions & 2 deletions advanced/image_processing/examples/plot_numpy_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
face[range(400), range(400)] = 255

plt.figure(figsize=(3, 3))
plt.axes([0, 0, 1, 1])
plt.imshow(face, cmap=plt.cm.gray)
plt.axes((0, 0, 1, 1))
plt.imshow(face, cmap="gray")
plt.axis("off")

plt.show()
6 changes: 3 additions & 3 deletions advanced/image_processing/examples/plot_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

plt.figure(figsize=(9.5, 3))
plt.subplot(131)
plt.imshow(square, cmap=plt.cm.gray, interpolation="nearest")
plt.imshow(square, cmap="gray", interpolation="nearest")
plt.axis("off")
plt.subplot(132)
plt.imshow(open_square, cmap=plt.cm.gray, interpolation="nearest")
plt.imshow(open_square, cmap="gray", interpolation="nearest")
plt.axis("off")
plt.subplot(133)
plt.imshow(reconstruction, cmap=plt.cm.gray, interpolation="nearest")
plt.imshow(reconstruction, cmap="gray", interpolation="nearest")
plt.axis("off")

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