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

Add tests for handling of NaNs in where reductions #1241

Merged
merged 1 commit into from
Jun 28, 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
34 changes: 18 additions & 16 deletions datashader/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
'cat_int': np.array([10]*5 + [11]*5 + [12]*5 + [13]*5)})
df_pd.cat = df_pd.cat.astype('category')
df_pd.cat2 = df_pd.cat2.astype('category')
df_pd.at[2,'f32'] = nan
df_pd.at[2,'f64'] = nan
df_pd.at[2,'plusminus'] = nan
df_pd.at[2, 'f32'] = nan
df_pd.at[2, 'f64'] = nan
df_pd.at[6, 'reverse'] = nan
df_pd.at[2, 'plusminus'] = nan

_ddf = dd.from_pandas(df_pd, npartitions=2)

Expand Down Expand Up @@ -558,7 +559,7 @@ def test_where_max_n(ddf, npartitions):
[14, 12, 10, 11, 13, -1]],
[[ 8, 6, 5, 7, 9, -1],
[18, 16, 15, 17, 19, -1]]])
sol_reverse = np.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = np.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 7):
# Using row index.
Expand Down Expand Up @@ -587,7 +588,7 @@ def test_where_min_n(ddf, npartitions):
[13, 11, 10, 12, 14, -1]],
[[ 9, 7, 5, 6, 8, -1],
[19, 17, 15, 16, 18, -1]]])
sol_reverse = np.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = np.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 7):
# Using row index.
Expand Down Expand Up @@ -658,7 +659,7 @@ def test_where_first_n(ddf, npartitions):
[10, 11, 12, 13, 14, -1]],
[[ 5, 6, 7, 8, 9, -1],
[15, 16, 17, 18, 19, -1]]])
sol_reverse = np.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = np.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 7):
# Using row index.
Expand Down Expand Up @@ -687,7 +688,7 @@ def test_where_last_n(ddf, npartitions):
[14, 13, 12, 11, 10, -1]],
[[ 9, 8, 7, 6, 5, -1],
[19, 18, 17, 16, 15, -1]]])
sol_reverse = np.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = np.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 7):
# Using row index.
Expand Down Expand Up @@ -720,7 +721,8 @@ def test_summary_where_n(ddf, npartitions):
[14, 12, 10, 11, 13]],
[[ 8, 6, 5, 7, 9],
[18, 16, 15, 17, 19]]])
sol_max_n_reverse = np.where(sol_max_n_rowindex < 0, np.nan, 20 - sol_max_n_rowindex)
sol_max_n_reverse = np.where(np.logical_or(sol_max_n_rowindex < 0, sol_max_n_rowindex == 6),
np.nan, 20 - sol_max_n_rowindex)

agg = c.points(ddf, 'x', 'y', ds.summary(
count=ds.count(),
Expand Down Expand Up @@ -2234,7 +2236,7 @@ def test_categorical_where_max(ddf, npartitions):
assert ddf.npartitions == npartitions
sol_rowindex = xr.DataArray([[[4, 1, -1, 3], [12, 13, 14, 11]], [[8, 5, 6, 7], [16, 17, 18, 15]]],
coords=coords + [['a', 'b', 'c', 'd']], dims=dims + ['cat2'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

# Using row index
agg = c.points(ddf, 'x', 'y', ds.by('cat2', ds.where(ds.max('plusminus'))))
Expand All @@ -2254,7 +2256,7 @@ def test_categorical_where_min(ddf, npartitions):
assert ddf.npartitions == npartitions
sol_rowindex = xr.DataArray([[[0, 1, -1, 3], [12, 13, 10, 11]], [[8, 9, 6, 7], [16, 17, 18, 19]]],
coords=coords + [['a', 'b', 'c', 'd']], dims=dims + ['cat2'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

# Using row index
agg = c.points(ddf, 'x', 'y', ds.by('cat2', ds.where(ds.min('plusminus'))))
Expand All @@ -2274,7 +2276,7 @@ def test_categorical_where_first(ddf, npartitions):
assert ddf.npartitions == npartitions
sol_rowindex = xr.DataArray([[[0, 1, -1, 3], [12, 13, 10, 11]], [[8, 5, 6, 7], [16, 17, 18, 15]]],
coords=coords + [['a', 'b', 'c', 'd']], dims=dims + ['cat2'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

# Using row index
agg = c.points(ddf, 'x', 'y', ds.by('cat2', ds.where(ds.first('plusminus'))))
Expand All @@ -2294,7 +2296,7 @@ def test_categorical_where_last(ddf, npartitions):
assert ddf.npartitions == npartitions
sol_rowindex = xr.DataArray([[[4, 1, -1, 3], [12, 13, 14, 11]], [[8, 9, 6, 7], [16, 17, 18, 19]]],
coords=coords + [['a', 'b', 'c', 'd']], dims=dims + ['cat2'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

# Using row index
agg = c.points(ddf, 'x', 'y', ds.by('cat2', ds.where(ds.last('plusminus'))))
Expand All @@ -2318,7 +2320,7 @@ def test_categorical_where_max_n(ddf, npartitions):
[[[8, -1, -1], [5, 9, -1], [6, -1, -1], [7, -1, -1]],
[[16, -1, -1], [17, -1, -1], [18, -1, -1], [15, 19, -1]]]],
coords=coords + [['a', 'b', 'c', 'd'], [0, 1, 2]], dims=dims + ['cat2', 'n'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 4):
# Using row index
Expand Down Expand Up @@ -2351,7 +2353,7 @@ def test_categorical_where_min_n(ddf, npartitions):
[[[8, -1, -1], [9, 5, -1], [6, -1, -1], [7, -1, -1]],
[[16, -1, -1], [17, -1, -1], [18, -1, -1], [19, 15, -1]]]],
coords=coords + [['a', 'b', 'c', 'd'], [0, 1, 2]], dims=dims + ['cat2', 'n'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 4):
# Using row index
Expand Down Expand Up @@ -2384,7 +2386,7 @@ def test_categorical_where_first_n(ddf, npartitions):
[[[8, -1, -1], [5, 9, -1], [6, -1, -1], [7, -1, -1]],
[[16, -1, -1], [17, -1, -1], [18, -1, -1], [15, 19, -1]]]],
coords=coords + [['a', 'b', 'c', 'd'], [0, 1, 2]], dims=dims + ['cat2', 'n'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 4):
# Using row index
Expand Down Expand Up @@ -2413,7 +2415,7 @@ def test_categorical_where_last_n(ddf, npartitions):
[[[8, -1, -1], [9, 5, -1], [6, -1, -1], [7, -1, -1]],
[[16, -1, -1], [17, -1, -1], [18, -1, -1], [19, 15, -1]]]],
coords=coords + [['a', 'b', 'c', 'd'], [0, 1, 2]], dims=dims + ['cat2', 'n'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 4):
# Using row index
Expand Down
48 changes: 25 additions & 23 deletions datashader/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
df_pd.cat = df_pd.cat.astype('category')
df_pd.cat2 = df_pd.cat2.astype('category')
df_pd.onecat = df_pd.onecat.astype('category')
df_pd.at[2,'f32'] = nan
df_pd.at[2,'f64'] = nan
df_pd.at[2,'plusminus'] = nan
# x 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
# y 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
# i32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# f32 0 1 nan 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# reverse 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
# plusminus 0 -1 nan -3 4 -5 6 -7 8 -9 10 -11 12 -13 14 -15 16 -17 18 -19
# cat2 a b c d a b c d a b c d a b c d a b c d
df_pd.at[2, 'f32'] = nan
df_pd.at[2, 'f64'] = nan
df_pd.at[6, 'reverse'] = nan
df_pd.at[2, 'plusminus'] = nan
# x 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
# y 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
# i32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# f32 0 1 nan 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# reverse 20 19 18 17 16 15 nan 13 12 11 10 9 8 7 6 5 4 3 2 1
# plusminus 0 -1 nan -3 4 -5 6 -7 8 -9 10 -11 12 -13 14 -15 16 -17 18 -19
# cat2 a b c d a b c d a b c d a b c d a b c d

test_gpu = bool(int(os.getenv("DATASHADER_TEST_GPU", 0)))

Expand Down Expand Up @@ -505,7 +506,7 @@ def test_where_first_n(df):
[10, 11, 12, 13, 14, -1]],
[[ 5, 6, 7, 8, 9, -1],
[15, 16, 17, 18, 19, -1]]])
sol_reverse = np.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 7):
# Using row index.
Expand All @@ -529,7 +530,7 @@ def test_where_last_n(df):
[14, 13, 12, 11, 10, -1]],
[[ 9, 8, 7, 6, 5, -1],
[19, 18, 17, 16, 15, -1]]])
sol_reverse = np.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 7):
# Using row index.
Expand All @@ -553,7 +554,7 @@ def test_where_max_n(df):
[14, 12, 10, 11, 13, -1]],
[[ 8, 6, 5, 7, 9, -1],
[18, 16, 15, 17, 19, -1]]])
sol_reverse = np.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 7):
# Using row index.
Expand All @@ -577,7 +578,7 @@ def test_where_min_n(df):
[13, 11, 10, 12, 14, -1]],
[[ 9, 7, 5, 6, 8, -1],
[19, 17, 15, 16, 18, -1]]])
sol_reverse = np.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 7):
# Using row index.
Expand Down Expand Up @@ -605,7 +606,8 @@ def test_summary_where_n(df):
[14, 12, 10, 11, 13]],
[[ 8, 6, 5, 7, 9],
[18, 16, 15, 17, 19]]])
sol_max_n_reverse = np.where(sol_max_n_rowindex < 0, np.nan, 20 - sol_max_n_rowindex)
sol_max_n_reverse = np.where(np.logical_or(sol_max_n_rowindex < 0, sol_max_n_rowindex == 6),
np.nan, 20 - sol_max_n_rowindex)

agg = c.points(df, 'x', 'y', ds.summary(
count=ds.count(),
Expand Down Expand Up @@ -2846,7 +2848,7 @@ def test_canvas_size():
def test_categorical_where_max(df):
sol_rowindex = xr.DataArray([[[4, 1, -1, 3], [12, 13, 14, 11]], [[8, 5, 6, 7], [16, 17, 18, 15]]],
coords=coords + [['a', 'b', 'c', 'd']], dims=dims + ['cat2'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

# Using row index
agg = c.points(df, 'x', 'y', ds.by('cat2', ds.where(ds.max('plusminus'))))
Expand All @@ -2861,7 +2863,7 @@ def test_categorical_where_max(df):
def test_categorical_where_min(df):
sol_rowindex = xr.DataArray([[[0, 1, -1, 3], [12, 13, 10, 11]], [[8, 9, 6, 7], [16, 17, 18, 19]]],
coords=coords + [['a', 'b', 'c', 'd']], dims=dims + ['cat2'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

# Using row index
agg = c.points(df, 'x', 'y', ds.by('cat2', ds.where(ds.min('plusminus'))))
Expand All @@ -2876,7 +2878,7 @@ def test_categorical_where_min(df):
def test_categorical_where_first(df):
sol_rowindex = xr.DataArray([[[0, 1, -1, 3], [12, 13, 10, 11]], [[8, 5, 6, 7], [16, 17, 18, 15]]],
coords=coords + [['a', 'b', 'c', 'd']], dims=dims + ['cat2'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

# Using row index
agg = c.points(df, 'x', 'y', ds.by('cat2', ds.where(ds.first('plusminus'))))
Expand All @@ -2891,7 +2893,7 @@ def test_categorical_where_first(df):
def test_categorical_where_last(df):
sol_rowindex = xr.DataArray([[[4, 1, -1, 3], [12, 13, 14, 11]], [[8, 9, 6, 7], [16, 17, 18, 19]]],
coords=coords + [['a', 'b', 'c', 'd']], dims=dims + ['cat2'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

# Using row index
agg = c.points(df, 'x', 'y', ds.by('cat2', ds.where(ds.last('plusminus'))))
Expand All @@ -2910,7 +2912,7 @@ def test_categorical_where_max_n(df):
[[[8, -1, -1], [5, 9, -1], [6, -1, -1], [7, -1, -1]],
[[16, -1, -1], [17, -1, -1], [18, -1, -1], [15, 19, -1]]]],
coords=coords + [['a', 'b', 'c', 'd'], [0, 1, 2]], dims=dims + ['cat2', 'n'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 4):
# Using row index
Expand Down Expand Up @@ -2938,7 +2940,7 @@ def test_categorical_where_min_n(df):
[[[8, -1, -1], [9, 5, -1], [6, -1, -1], [7, -1, -1]],
[[16, -1, -1], [17, -1, -1], [18, -1, -1], [19, 15, -1]]]],
coords=coords + [['a', 'b', 'c', 'd'], [0, 1, 2]], dims=dims + ['cat2', 'n'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 4):
# Using row index
Expand Down Expand Up @@ -2966,7 +2968,7 @@ def test_categorical_where_first_n(df):
[[[8, -1, -1], [5, 9, -1], [6, -1, -1], [7, -1, -1]],
[[16, -1, -1], [17, -1, -1], [18, -1, -1], [15, 19, -1]]]],
coords=coords + [['a', 'b', 'c', 'd'], [0, 1, 2]], dims=dims + ['cat2', 'n'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 4):
# Using row index
Expand Down Expand Up @@ -2994,7 +2996,7 @@ def test_categorical_where_last_n(df):
[[[8, -1, -1], [9, 5, -1], [6, -1, -1], [7, -1, -1]],
[[16, -1, -1], [17, -1, -1], [18, -1, -1], [19, 15, -1]]]],
coords=coords + [['a', 'b', 'c', 'd'], [0, 1, 2]], dims=dims + ['cat2', 'n'])
sol_reverse = xr.where(sol_rowindex < 0, np.nan, 20 - sol_rowindex)
sol_reverse = xr.where(np.logical_or(sol_rowindex < 0, sol_rowindex == 6), np.nan, 20 - sol_rowindex)

for n in range(1, 4):
# Using row index
Expand Down