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

Ensure pixel_ratio is applied when computing resampling coordinates #6373

Merged
merged 4 commits into from
Sep 17, 2024
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
14 changes: 9 additions & 5 deletions holoviews/operation/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ def _get_sampling(self, element, x, y, ndim=2, default=None):
if y and element.get_dimension_type(y[0]) in datetime_types:
ytype = 'datetime'

# Adjust width and height depending on pixel ratio
pixel_ratio = self._get_pixel_ratio()
width = int(width * pixel_ratio)
height = int(height * pixel_ratio)

# Compute highest allowed sampling density
xspan = xend - xstart
yspan = yend - ystart
Expand All @@ -198,12 +203,11 @@ def _get_sampling(self, element, x, y, ndim=2, default=None):
yunit, height = 0, 0
else:
yunit = float(yspan)/height
xs, ys = (np.linspace(xstart+xunit/2., xend-xunit/2., width),
np.linspace(ystart+yunit/2., yend-yunit/2., height))

pixel_ratio = self._get_pixel_ratio()
width = int(width * pixel_ratio)
height = int(height * pixel_ratio)
xs, ys = (
np.linspace(xstart+xunit/2., xend-xunit/2., width),
np.linspace(ystart+yunit/2., yend-yunit/2., height)
)
return ((xstart, xend), (ystart, yend)), (xs, ys), (width, height), (xtype, ytype)

def _get_pixel_ratio(self):
Expand Down
19 changes: 19 additions & 0 deletions holoviews/tests/operation/test_datashader.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ def test_aggregate_points(self):
vdims=[Dimension('Count', nodata=0)])
self.assertEqual(img, expected)

def test_aggregate_points_empty(self):
points = Points([])
img = aggregate(points, dynamic=False, x_range=(0, 1), y_range=(0, 1),
width=2, height=2, pixel_ratio=1)
expected = Image(([0.25, 0.75], [0.25, 0.75], [[0, 0], [0, 0]]),
vdims=[Dimension('Count', nodata=0)])
self.assertEqual(img, expected)

def test_aggregate_points_empty_with_pixel_ratio(self):
points = Points([])
img = aggregate(points, dynamic=False, x_range=(0, 1), y_range=(0, 1),
width=2, height=2, pixel_ratio=2)
expected = Image((
[0.125, 0.375, 0.625, 0.875],
[0.125, 0.375, 0.625, 0.875],
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
), vdims=[Dimension('Count', nodata=0)])
self.assertEqual(img, expected)

def test_aggregate_points_count_column(self):
points = Points([(0.2, 0.3, np.nan), (0.4, 0.7, 22), (0, 0.99,np.nan)], vdims='z')
img = aggregate(points, dynamic=False, x_range=(0, 1), y_range=(0, 1),
Expand Down