From 6d9a7c3a8cc2a1c7245255c4a09dac759feb83b3 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Mon, 16 Jan 2023 10:30:07 -0500 Subject: [PATCH] Handle upcoming numpy deprecations --- CHANGELOG.md | 1 + .../vips/large_image_source_vips/__init__.py | 3 +- test/test_source_hdf5.py | 28 +++++++++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b50d96ef..4ecb56f78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Better parse svs pixel size in tiff and tifffile sources ([#1021](../../pull/1021)) - Add geojson to known mime types ([#1022](../../pull/1022)) - Handle upcoming matplotlib deprecations ([#1025](../../pull/1025)) +- Handle upcoming numpy deprecations ([#1026](../../pull/1026)) ## 1.19.1 diff --git a/sources/vips/large_image_source_vips/__init__.py b/sources/vips/large_image_source_vips/__init__.py index d983f8edf..428237f1b 100644 --- a/sources/vips/large_image_source_vips/__init__.py +++ b/sources/vips/large_image_source_vips/__init__.py @@ -338,7 +338,8 @@ def addTile(self, tile, x=0, y=0, mask=None, interpretation=None): newarr = numpy.zeros( (tile.shape[0], tile.shape[1], tile.shape[2] + 1), dtype=tile.dtype) newarr[:, :, :tile.shape[2]] = tile - newarr[:, :, -1] = 255 + newarr[:, :, -1] = min(numpy.iinfo( + tile.dtype).max, 255) if tile.dtype.kind in 'iu' else 255 tile = newarr if mask is not None: if len(mask.shape) == 3: diff --git a/test/test_source_hdf5.py b/test/test_source_hdf5.py index d318dbd82..46030b037 100644 --- a/test/test_source_hdf5.py +++ b/test/test_source_hdf5.py @@ -27,14 +27,14 @@ } possible_data_ranges = [ - [0, 1, float], - [0, 2**8, numpy.uint8], - [0, 2**16, numpy.uint16], - [0, 2**32, numpy.uint32], - [-2**7, 2**7, numpy.int8], - [-2**15, 2**15, numpy.int16], - [-2**31, 2**31, numpy.int32], - [-1, 1, float] + [0, 1, 2, float], + [0, 2**8, -1, numpy.uint8], + [0, 2**16, -2, numpy.uint16], + [0, 2**32, -4, numpy.uint32], + [-2**7, 2**7, -1, numpy.int8], + [-2**15, 2**15, -2, numpy.int16], + [-2**31, 2**31, -4, numpy.int32], + [-1, 1, 2, float] ] max_tile_size = 100 @@ -42,10 +42,10 @@ # https://stackoverflow.com/questions/18915378/rounding-to-significant-figures-in-numpy -def signif(x): +def signif(x, minval, maxval, digits): if x == 0: return 0 - return round(x, -2) + return max(min(round(x, digits), max(1, maxval - 1)), minval) def get_dims(x, y, s, max=False): @@ -72,7 +72,7 @@ def random_tile(data_range): tile = numpy.random.rand(*tile_shape) tile *= (data_range[1] - data_range[0]) tile += data_range[0] - tile = tile.astype(data_range[2]) # apply dtype + tile = tile.astype(data_range[3]) # apply dtype mask = numpy.random.randint(2, size=tile_shape[:-1]) return (tile, mask) @@ -154,10 +154,10 @@ def testImageGeneration(data_range): # trim unused space from expected expected = expected[:max_x, :max_y] - # round to -2 precision + # round to specified precision precision_vector = numpy.vectorize(signif) - expected = precision_vector(expected) - result = precision_vector(result) + expected = precision_vector(expected, data_range[0], data_range[1], data_range[2]) + result = precision_vector(result, data_range[0], data_range[1], data_range[2]) # ignore alpha values for now expected = expected.take(indices=range(0, -1), axis=-1)