Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle upcoming numpy deprecations #1026

Merged
merged 1 commit into from
Jan 16, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion sources/vips/large_image_source_vips/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 14 additions & 14 deletions test/test_source_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@
}

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
tile_overlap_ratio = 0.5


# 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):
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down