Skip to content

Commit

Permalink
1141 | Validate canvas width, height (#1183)
Browse files Browse the repository at this point in the history
* 1141 | Validate canvas width, height

* Add tests for canvas extend

* Rename to validate size and fix spacing

* Apply suggestions from code review

---------

Co-authored-by: Jonatan Aponte <jonatan-armando.aponte-duenas@xarvio.com>
Co-authored-by: Jonatan Aponte <jonatanaponte@Jonatans-MBP.fritz.box>
Co-authored-by: Ian Thomas <ianthomas23@gmail.com>
  • Loading branch information
4 people authored Feb 21, 2023
1 parent d9de71f commit 963b7eb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions datashader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,9 +1227,14 @@ def validate_ranges(self, x_range, y_range):
self.x_axis.validate(x_range)
self.y_axis.validate(y_range)

def validate_size(self, width, height):
if width <= 0 or height <= 0:
raise ValueError("Invalid size: plot_width and plot_height must be bigger than 0")

def validate(self):
"""Check that parameter settings are valid for this object"""
self.validate_ranges(self.x_range, self.y_range)
self.validate_size(self.plot_width, self.plot_height)


def bypixel(source, canvas, glyph, agg, *, antialias=False):
Expand Down
18 changes: 18 additions & 0 deletions datashader/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,3 +1564,21 @@ def test_line_antialias_where(npartitions):
sol_where_min[j, i] = agg_where_min[j, i] = nan

assert_eq_ndarray(agg_where_min.data, sol_where_min)


def test_canvas_size():
cvs_list = [
ds.Canvas(plot_width=0, plot_height=6),
ds.Canvas(plot_width=5, plot_height=0),
ds.Canvas(plot_width=0, plot_height=0),
ds.Canvas(plot_width=-1, plot_height=1),
ds.Canvas(plot_width=10, plot_height=-1)
]
msg = r'Invalid size: plot_width and plot_height must be bigger than 0'
df = pd.DataFrame(dict(x=[0, 0.2, 1], y=[0, 0.4, 1], z=[10, 20, 30]))
ddf = dd.from_pandas(df, 1)

for cvs in cvs_list:
with pytest.raises(ValueError, match=msg):
cvs.points(ddf, "x", "y", ds.mean("z"))

17 changes: 17 additions & 0 deletions datashader/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2393,3 +2393,20 @@ def test_line_coordinate_lengths():
for ny in (1, 3):
with pytest.raises(ValueError, match=msg):
cvs.line(source=df, x=["x0", "x1"], y=np.arange(ny), axis=1)


def test_canvas_size():
cvs_list = [
ds.Canvas(plot_width=0, plot_height=6),
ds.Canvas(plot_width=5, plot_height=0),
ds.Canvas(plot_width=0, plot_height=0),
ds.Canvas(plot_width=-1, plot_height=1),
ds.Canvas(plot_width=10, plot_height=-1)
]
msg = r'Invalid size: plot_width and plot_height must be bigger than 0'
df = pd.DataFrame(dict(x=[0, 0.2, 1], y=[0, 0.4, 1], z=[10, 20, 30]))

for cvs in cvs_list:
with pytest.raises(ValueError, match=msg):
cvs.points(df, "x", "y", ds.mean("z"))

0 comments on commit 963b7eb

Please sign in to comment.