Skip to content

Commit b6312e3

Browse files
committed
Addressed review comments
1 parent dd6e322 commit b6312e3

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

dpnp/dpnp_iface_histograms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _get_outer_edges(a, range):
114114
first_edge, last_edge = a.min(), a.max()
115115
if not (dpnp.isfinite(first_edge) and dpnp.isfinite(last_edge)):
116116
raise ValueError(
117-
"autodetected range of [{first_edge}, {last_edge}] "
117+
f"autodetected range of [{first_edge}, {last_edge}] "
118118
"is not finite"
119119
)
120120

@@ -135,6 +135,7 @@ def _get_bin_edges(a, bins, range, usm_type):
135135
sycl_queue = a.sycl_queue
136136

137137
if isinstance(bins, str):
138+
# TODO: implement support of string bins
138139
raise NotImplementedError("only integer and array bins are implemented")
139140

140141
if numpy.ndim(bins) == 0:
@@ -318,7 +319,7 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
318319
cum_n = _search_sorted_inclusive(sa, bin_edges)
319320
else:
320321
zero = dpnp.zeros(
321-
1, dtype=ntype, sycl_queue=a.sycl_queue, usm_type=a.usm_type
322+
1, dtype=ntype, sycl_queue=a.sycl_queue, usm_type=usm_type
322323
)
323324
sorting_index = dpnp.argsort(a)
324325
sa = a[sorting_index]

tests/test_histogram.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dpctl
12
import numpy
23
import pytest
34
from numpy.testing import (
@@ -283,6 +284,18 @@ def test_invalid_range(self, xp):
283284
with assert_raises_regex(ValueError, "max must be larger than"):
284285
xp.histogram(vals, range=[0.1, 0.01])
285286

287+
@pytest.mark.parametrize("xp", [numpy, dpnp])
288+
@pytest.mark.parametrize("inf_val", [-numpy.inf, numpy.inf])
289+
def test_infinite_edge(self, xp, inf_val):
290+
v = xp.array([0.5, 1.5, inf_val])
291+
min, max = v.min(), v.max()
292+
293+
# both first and last ranges must be finite
294+
with assert_raises_regex(
295+
ValueError, f"autodetected range of \[{min}, {max}\] is not finite"
296+
):
297+
xp.histogram(v)
298+
286299
def test_bin_edge_cases(self):
287300
v = dpnp.array([337, 404, 739, 806, 1007, 1811, 2012])
288301

@@ -322,7 +335,7 @@ def test_unsigned_monotonicity_check(self, xp):
322335
arr = xp.array([2])
323336
bins = xp.array([1, 3, 1], dtype="uint64")
324337
with assert_raises(ValueError):
325-
_, _ = xp.histogram(arr, bins=bins)
338+
xp.histogram(arr, bins=bins)
326339

327340
def test_nan_values(self):
328341
one_nan = numpy.array([0, 1, numpy.nan])
@@ -356,3 +369,23 @@ def test_signed_overflow_bounds(self, dtype):
356369
result_hist, result_edges = dpnp.histogram(iv, bins=2)
357370
assert_array_equal(result_hist, expected_hist)
358371
assert_allclose(result_edges, expected_edges)
372+
373+
def test_string_bins_not_implemented(self):
374+
v = dpnp.arange(5)
375+
376+
# numpy support string bins, but not dpnp
377+
_, _ = numpy.histogram(v.asnumpy(), bins="auto")
378+
with assert_raises(NotImplementedError):
379+
dpnp.histogram(v, bins="auto")
380+
381+
def test_bins_another_sycl_queue(self):
382+
v = dpnp.arange(7, 12, sycl_queue=dpctl.SyclQueue())
383+
bins = dpnp.arange(4, sycl_queue=dpctl.SyclQueue())
384+
with assert_raises(ValueError):
385+
dpnp.histogram(v, bins=bins)
386+
387+
def test_weights_another_sycl_queue(self):
388+
v = dpnp.arange(5, sycl_queue=dpctl.SyclQueue())
389+
w = dpnp.arange(7, 12, sycl_queue=dpctl.SyclQueue())
390+
with assert_raises(ValueError):
391+
dpnp.histogram(v, weights=w)

0 commit comments

Comments
 (0)