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

Added handling for zero data and zero range #648

Merged
merged 2 commits into from
Sep 10, 2018
Merged

Added handling for zero data and zero range #648

merged 2 commits into from
Sep 10, 2018

Conversation

jbednar
Copy link
Member

@jbednar jbednar commented Sep 10, 2018

Previously, Datashader would return ZeroDivision or other errors in all of these cases:

  • No datapoints (common when interactively zooming into a small region, so should be valid)
  • A single datapoint (which caused the x and y ranges to be infinitely small)
  • Multiple datapoints sharing a single x or y value (which caused either the x or y ranges to be infinitely small)

The case with two distinct points has always worked, but the rest give errors:

import pandas as pd, datashader as ds, datashader.transfer_functions as tf
cvs = ds.Canvas(plot_width=5, plot_height=5)
df = pd.DataFrame(dict(x=[1,2], y=[-2,-5]))
print(str(cvs.points(df,'x','y').values))

[[0 0 0 0 1]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [1 0 0 0 0]]

No data points:

df = pd.DataFrame(dict(x=[], y=[]))
print(str(cvs.points(df,'x','y').values))

ValueError: No non-NaN x coordinates found.

A single data point:

df = pd.DataFrame(dict(x=[1], y=[-2]))
print(str(cvs.points(df,'x','y').values))

ZeroDivisionError: float division by zero

Two points, but only 1 x value:

df = pd.DataFrame(dict(x=[1,1], y=[-2,5]))
print(str(cvs.points(df,'x','y').values))

ZeroDivisionError: float division by zero

Two points, but only 1 y value:

df = pd.DataFrame(dict(x=[1,2], y=[-2,-2]))
print(str(cvs.points(df,'x','y').values))

ZeroDivisionError: float division by zero

Float array with one data point:

df = pd.DataFrame(dict(x=[1], y=[2]))
print(str(cvs.points(df,'x','y', agg=ds.sum('x')).values))

ZeroDivisionError: float division by zero

@jbednar
Copy link
Member Author

jbednar commented Sep 10, 2018

After this PR is applied, all these cases are considered legal:

import pandas as pd, datashader as ds, datashader.transfer_functions as tf
cvs = ds.Canvas(plot_width=5, plot_height=5)
df = pd.DataFrame(dict(x=[1,2], y=[-2,-5]))
print(str(cvs.points(df,'x','y').values))

[[0 0 0 0 1]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [1 0 0 0 0]]

No data points:

df = pd.DataFrame(dict(x=[], y=[]))
print(str(cvs.points(df,'x','y').values))

No x values; defaulting to range -0.5,0.5
No y values; defaulting to range -0.5,0.5
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]

A single data point:

df = pd.DataFrame(dict(x=[1], y=[-2]))
print(str(cvs.points(df,'x','y').values))

No x range; defaulting to x-0.5,x+0.5
No y range; defaulting to y-0.5,y+0.5
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]

Two points, but only 1 x value:

df = pd.DataFrame(dict(x=[1,1], y=[-2,5]))
print(str(cvs.points(df,'x','y').values))

No x range; defaulting to x-0.5,x+0.5
[[0 0 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 1 0 0]]

Two points, but only 1 y value:

df = pd.DataFrame(dict(x=[1,2], y=[-2,-2]))
print(str(cvs.points(df,'x','y').values))

No y range; defaulting to y-0.5,y+0.5
[[0 0 0 0 0]
 [0 0 0 0 0]
 [1 0 0 0 1]
 [0 0 0 0 0]
 [0 0 0 0 0]]

Float array with one data point:

df = pd.DataFrame(dict(x=[1], y=[2]))
print(str(cvs.points(df,'x','y', agg=ds.sum('x')).values))

No x range; defaulting to x-0.5,x+0.5
No y range; defaulting to y-0.5,y+0.5
[[nan nan nan nan nan]
 [nan nan nan nan nan]
 [nan nan  1. nan nan]
 [nan nan nan nan nan]
 [nan nan nan nan nan]]

For now I've left in the "No ..." print statements, to show that it's working, but as none of these are meant to be error conditions, I'll remove the printing in the next commit.

@jbednar jbednar merged commit 2e1ed1b into master Sep 10, 2018
@jbednar jbednar deleted the ranges branch September 10, 2018 09:38
jbednar added a commit that referenced this pull request Sep 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant