Skip to content

Commit

Permalink
MAINT: Use newer numpy random API in examples and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
WarrenWeckesser committed Nov 8, 2023
1 parent 369a161 commit 45ea8a0
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 45 deletions.
20 changes: 10 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ The script::
ncols = 5*w
kernel = np.exp(-np.linspace(-2, 2, 35)**2)
kernel = kernel/kernel.sum()
np.random.seed(123)
x = np.random.randn(nrows, ncols, 3)
rng = np.random.default_rng(seed=121263137472525314065)
x = rng.standard_normal((nrows, ncols, 3))
x = np.apply_along_axis(lambda z: np.convolve(z, kernel, mode='same'), 0, x)
x = np.apply_along_axis(lambda z: np.convolve(z, kernel, mode='same'), 1, x)

Expand Down Expand Up @@ -165,13 +165,13 @@ The script::
img_height = 200
img = np.zeros((img_height, img_width, 3), dtype=np.uint8)

np.random.seed(222)
rng = np.random.default_rng(seed=121263137472525314065)
for _ in range(40):
width = np.random.randint(5, img_width // 5)
height = np.random.randint(5, img_height // 5)
row = np.random.randint(5, img_height - height - 5)
col = np.random.randint(5, img_width - width - 5)
color = np.random.randint(80, 256, size=2)
width = rng.integers(5, img_width // 5)
height = rng.integers(5, img_height // 5)
row = rng.integers(5, img_height - height - 5)
col = rng.integers(5, img_width - width - 5)
color = rng.integers(80, 256, size=2)
img[row:row+height, col:col+width, 1:] = color

write_png('example4.png', img, use_palette=True)
Expand Down Expand Up @@ -248,8 +248,8 @@ The script::
height = 40
width = 250
num_frames = 30
np.random.seed(12345)
w = np.random.randn(num_frames, height, width, 3)
rng = np.random.default_rng(seed=121263137472525314065)
w = rng.standard_normal((num_frames, height, width, 3))
for k in range(3):
w[..., k] = smoother(w[..., k])

Expand Down
Binary file modified examples/example3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
ncols = 5*w
kernel = np.exp(-np.linspace(-2, 2, 35)**2)
kernel = kernel/kernel.sum()
np.random.seed(123)
x = np.random.randn(nrows, ncols, 3)
rng = np.random.default_rng(seed=121263137472525314065)
x = rng.standard_normal((nrows, ncols, 3))
x = np.apply_along_axis(lambda z: np.convolve(z, kernel, mode='same'), 0, x)
x = np.apply_along_axis(lambda z: np.convolve(z, kernel, mode='same'), 1, x)

Expand Down
Binary file modified examples/example4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions examples/example4.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
img_height = 200
img = np.zeros((img_height, img_width, 3), dtype=np.uint8)

np.random.seed(222)
rng = np.random.default_rng(seed=121263137472525314065)
for _ in range(40):
width = np.random.randint(5, img_width // 5)
height = np.random.randint(5, img_height // 5)
row = np.random.randint(5, img_height - height - 5)
col = np.random.randint(5, img_width - width - 5)
color = np.random.randint(80, 256, size=2)
width = rng.integers(5, img_width // 5)
height = rng.integers(5, img_height // 5)
row = rng.integers(5, img_height - height - 5)
col = rng.integers(5, img_width - width - 5)
color = rng.integers(80, 256, size=2)
img[row:row+height, col:col+width, 1:] = color

write_png('example4.png', img, use_palette=True)
Binary file modified examples/example6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/example6.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def smoother(w):
height = 40
width = 250
num_frames = 30
np.random.seed(12345)
w = np.random.randn(num_frames, height, width, 3)
rng = np.random.default_rng(seed=121263137472525314065)
w = rng.standard_normal((num_frames, height, width, 3))
for k in range(3):
w[..., k] = smoother(w[..., k])

Expand Down
49 changes: 24 additions & 25 deletions tests/test_write_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,15 @@ class TestWritePng(unittest.TestCase):
def test_write_png_nbit_grayscale(self):
# Test the creation of grayscale images for bit depths of 1, 2, 4
# 8 and 16, with or without a `transparent` color selected.
np.random.seed(123)
rng = np.random.default_rng(seed=121263137472525314065)
for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
for bitdepth in [1, 2, 4, 8, 16]:
for transparent in [None, 0]:
for interlace in [0, 1]:
dt = np.uint16 if bitdepth == 16 else np.uint8
maxval = 2**bitdepth
sz = (3, 11)
img = np.random.randint(0, maxval, size=sz).astype(dt)
img = rng.integers(0, maxval, size=sz).astype(dt)
if transparent is not None:
img[2:4, 2:] = transparent

Expand Down Expand Up @@ -486,16 +486,15 @@ def test_write_png_with_alpha(self):
# and 6, resp.), with bit depths 8 and 16.
w = 25
h = 15
np.random.seed(12345)
rng = np.random.default_rng(seed=121263137472525314065)
for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
for color_type in [4, 6]:
num_channels = 2 if color_type == 4 else 4
for bit_depth in [8, 16]:
for interlace in [0, 1]:
dt = np.uint8 if bit_depth == 8 else np.uint16
sz = (h, w, num_channels)
img = np.random.randint(0, 2**bit_depth,
size=sz).astype(dt)
img = rng.integers(0, 2**bit_depth, size=sz).astype(dt)
f = io.BytesIO()
numpngw.write_png(f, img, filter_type=filter_type,
interlace=interlace)
Expand Down Expand Up @@ -529,15 +528,15 @@ def test_write_png_RGB(self):
# a `transparent` color selected, and with bit depth 8 and 16.
w = 24
h = 10
np.random.seed(12345)
rng = np.random.default_rng(seed=121263137472525314065)
for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
for transparent in [None, (0, 0, 0)]:
for bit_depth in [8, 16]:
for interlace in [0, 1]:
dt = np.uint16 if bit_depth == 16 else np.uint8
maxval = 2**bit_depth
img = np.random.randint(0, maxval,
size=(h, w, 3)).astype(dt)
img = rng.integers(0, maxval,
size=(h, w, 3)).astype(dt)
if transparent:
img[2:4, 2:4] = transparent

Expand Down Expand Up @@ -643,7 +642,8 @@ def test_write_png_max_chunk_len(self):
w = 250
h = 150
max_chunk_len = 500
img = np.random.randint(0, 256, size=(h, w)).astype(np.uint8)
rng = np.random.default_rng(seed=121263137472525314065)
img = rng.integers(0, 256, size=(h, w)).astype(np.uint8)
f = io.BytesIO()
numpngw.write_png(f, img, max_chunk_len=max_chunk_len)

Expand Down Expand Up @@ -681,8 +681,8 @@ def test_write_png_max_chunk_len(self):
self.assertEqual(file_contents, b"")

def test_write_png_timestamp_gamma_chromaticity(self):
np.random.seed(123)
img = np.random.randint(0, 256, size=(10, 10)).astype(np.uint8)
rng = np.random.default_rng(seed=121263137472525314065)
img = rng.integers(0, 256, size=(10, 10)).astype(np.uint8)
f = io.BytesIO()
timestamp = (1452, 4, 15, 8, 9, 10)
gamma = 2.2
Expand Down Expand Up @@ -721,12 +721,12 @@ def test_write_png_bkgd(self):
# Test creation of RGB images (color type 2), with a background color.
w = 16
h = 8
np.random.seed(123)
rng = np.random.default_rng(seed=121263137472525314065)
for bit_depth in [8, 16]:
maxval = 2**bit_depth
bg = (maxval - 1, maxval - 2, maxval - 3)
dt = np.uint16 if bit_depth == 16 else np.uint8
img = np.random.randint(0, maxval, size=(h, w, 3)).astype(dt)
img = rng.integers(0, maxval, size=(h, w, 3)).astype(dt)

f = io.BytesIO()
numpngw.write_png(f, img, background=bg, filter_type=0)
Expand Down Expand Up @@ -756,12 +756,12 @@ def test_write_png_sbit(self):
# Test creation of sBIT chunks for color_type 0 and 2.
w = 7
h = 5
np.random.seed(123)
rng = np.random.default_rng(seed=121263137472525314065)
for bit_depth in [8, 16]:
for size in [(h, w), (h, w, 3)]:
maxval = 2**bit_depth
dt = np.uint16 if bit_depth == 16 else np.uint8
img = np.random.randint(0, maxval, size=size).astype(dt)
img = rng.integers(0, maxval, size=size).astype(dt)

color_type = 0 if len(size) == 2 else 2

Expand Down Expand Up @@ -802,7 +802,6 @@ def test_write_png_bkgd_palette(self):
# when use_palette is True.
w = 6
h = 8
np.random.seed(123)
for bg_in_img in [True, False]:
bit_depth = 8
maxval = 2**bit_depth
Expand Down Expand Up @@ -975,8 +974,8 @@ def test_basic(self):
w = 22
h = 10
bitdepth = 8
np.random.seed(123)
img = np.random.randint(0, 256, size=(h, w)).astype(np.uint8)
rng = np.random.default_rng(seed=121263137472525314065)
img = rng.integers(0, 256, size=(h, w)).astype(np.uint8)

f = io.BytesIO()
numpngw.write_png(f, img, filter_type=1)
Expand Down Expand Up @@ -1008,9 +1007,9 @@ def test_write_apng_8bit_RGBA(self):
num_frames = 4
w = 25
h = 15
np.random.seed(12345)
rng = np.random.default_rng(seed=121263137472525314065)
seq_size = (num_frames, h, w, 4)
seq = np.random.randint(0, 256, size=seq_size).astype(np.uint8)
seq = rng.integers(0, 256, size=seq_size).astype(np.uint8)
f = io.BytesIO()
numpngw.write_apng(f, seq)

Expand Down Expand Up @@ -1063,9 +1062,9 @@ def test_default_image(self):
num_frames = 2
w = 16
h = 8
np.random.seed(12345)
rng = np.random.default_rng(seed=121263137472525314065)
seq_size = (num_frames, h, w, 4)
seq = np.random.randint(0, 256, size=seq_size).astype(np.uint8)
seq = rng.integers(0, 256, size=seq_size).astype(np.uint8)
default_image = np.zeros((h, w, 4), dtype=np.uint8)

f = io.BytesIO()
Expand Down Expand Up @@ -1118,7 +1117,7 @@ def test_write_apng_bkgd(self):
# Also test the chromaticity argument.
w = 16
h = 8
np.random.seed(123)
rng = np.random.default_rng(seed=121263137472525314065)
num_frames = 3
chromaticity = [[0.500, 0.750],
[0.125, 0.960],
Expand All @@ -1128,8 +1127,8 @@ def test_write_apng_bkgd(self):
maxval = 2**bit_depth
bg = (maxval - 1, maxval - 2, maxval - 3)
dt = np.uint16 if bit_depth == 16 else np.uint8
seq = np.random.randint(0, maxval,
size=(num_frames, h, w, 3)).astype(dt)
seq = rng.integers(0, maxval,
size=(num_frames, h, w, 3)).astype(dt)

f = io.BytesIO()
numpngw.write_apng(f, seq, background=bg, filter_type=0,
Expand Down

0 comments on commit 45ea8a0

Please sign in to comment.