Skip to content

Commit aec799c

Browse files
Aplly review comments 2
1 parent 8967070 commit aec799c

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

dpnp/backend/extensions/statistics/histogramdd.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ std::tuple<sycl::event, sycl::event> Histogramdd::call(
281281
{
282282
validate(sample, bins_edges, weights, histogram);
283283

284+
if (sample.get_size() == 0) {
285+
return {sycl::event(), sycl::event()};
286+
}
287+
284288
const int sample_typenum = sample.get_typenum();
285289
const int hist_typenum = histogram.get_typenum();
286290

dpnp/dpnp_iface_histograms.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import dpnp.backend.extensions.statistics._statistics_impl as statistics_ext
5151

5252
# pylint: disable=no-name-in-module
53-
from .dpnp_utils import map_dtype_to_device
53+
from .dpnp_utils import get_usm_allocations, map_dtype_to_device
5454

5555
__all__ = [
5656
"bincount",
@@ -409,9 +409,7 @@ def bincount(x, weights=None, minlength=None):
409409
x_casted, weights_casted, minlength, ntype_casted, usm_type
410410
)
411411

412-
n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type, order="C")
413-
414-
return n
412+
return dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type)
415413

416414

417415
def digitize(x, bins, right=False):
@@ -657,7 +655,7 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
657655
)
658656
_manager.add_event_pair(mem_ev, ht_ev)
659657

660-
n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type, order="C")
658+
n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type)
661659

662660
if density:
663661
db = dpnp.astype(
@@ -811,12 +809,10 @@ def _histdd_make_edges(sample, bins, range, usm_type):
811809

812810

813811
def _histdd_flatten_binedges(bedges_list, edges_count_list, dtype):
814-
queue = bedges_list[0].sycl_queue
815-
usm_type = bedges_list[0].usm_type
816812
total_edges_size = numpy.sum(edges_count_list)
817813

818-
bin_edges_flat = dpnp.empty(
819-
shape=total_edges_size, dtype=dtype, sycl_queue=queue, usm_type=usm_type
814+
bin_edges_flat = dpnp.empty_like(
815+
bedges_list[0], shape=total_edges_size, dtype=dtype
820816
)
821817

822818
offset = numpy.pad(numpy.cumsum(edges_count_list), (1, 0))
@@ -932,13 +928,14 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
932928
----------
933929
sample : {dpnp.ndarray, usm_ndarray}
934930
Input (N, D)-shaped array to be histogrammed.
935-
936931
bins : {sequence, int}, optional
937932
The bin specification:
933+
938934
* A sequence of arrays describing the monotonically increasing bin
939935
edges along each dimension.
940936
* The number of bins for each dimension (nx, ny, ... =bins)
941937
* The number of bins for all dimensions (nx=ny=...=bins).
938+
942939
Default: ``10``
943940
range : {None, sequence}, optional
944941
A sequence of length D, each an optional (lower, upper) tuple giving
@@ -947,26 +944,29 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
947944
An entry of None in the sequence results in the minimum and maximum
948945
values being used for the corresponding dimension.
949946
None is equivalent to passing a tuple of D None values.
947+
950948
Default: ``None``
951949
weights : {dpnp.ndarray, usm_ndarray}, optional
952950
An (N,)-shaped array of values `w_i` weighing each sample
953951
`(x_i, y_i, z_i, ...)`.
954952
Weights are normalized to 1 if density is True. If density is False,
955953
the values of the returned histogram are equal to the sum of the
956954
weights belonging to the samples falling into each bin.
955+
957956
Default: ``None``
958957
density : {bool}, optional
959958
If ``False``, the default, returns the number of samples in each bin.
960959
If ``True``, returns the probability *density* function at the bin,
961960
``bin_count / sample_count / bin_volume``.
961+
962962
Default: ``False``
963963
964964
Returns
965965
-------
966966
H : {dpnp.ndarray}
967967
The multidimensional histogram of sample x. See density and weights
968968
for the different possible semantics.
969-
edges : {list of ndarrays}
969+
edges : {list of dpnp.ndarray}
970970
A list of D arrays describing the bin edges for each dimension.
971971
972972
See Also
@@ -977,36 +977,26 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
977977
Examples
978978
--------
979979
>>> import dpnp as np
980-
>>> r = np.random.normal(size=(100,3))
980+
>>> r = np.random.normal(size=(100, 3))
981981
>>> H, edges = np.histogramdd(r, bins = (5, 8, 4))
982982
>>> H.shape, edges[0].size, edges[1].size, edges[2].size
983983
((5, 8, 4), 6, 9, 5)
984984
985985
"""
986986

987-
if not dpnp.is_supported_array_type(sample):
988-
raise ValueError("sample must be dpnp.ndarray or usm_ndarray")
989-
990-
if weights is not None and not dpnp.is_supported_array_type(weights):
991-
raise ValueError("weights must be dpnp.ndarray or usm_ndarray")
987+
dpnp.check_supported_arrays_type(sample)
988+
if weights is not None:
989+
dpnp.check_supported_arrays_type(weights)
992990

993-
if sample.ndim == 0 and sample.size == 1:
994-
sample = dpnp.reshape(sample, (1, 1))
995-
elif sample.ndim == 1:
991+
if sample.ndim < 2:
996992
sample = dpnp.reshape(sample, (sample.size, 1))
997993
elif sample.ndim > 2:
998994
raise ValueError("sample must have no more than 2 dimensions")
999995

1000996
ndim = sample.shape[1] if sample.size > 0 else 1
1001997

1002998
_arrays = _histdd_extract_arrays(sample, weights, bins)
1003-
usm_type = dpu.get_coerced_usm_type([a.usm_type for a in _arrays])
1004-
queue = dpu.get_execution_queue([a.sycl_queue for a in _arrays])
1005-
1006-
assert usm_type is not None
1007-
1008-
if queue is None:
1009-
raise ValueError("all arrays must be allocated on the same SYCL queue")
999+
usm_type, queue = get_usm_allocations(_arrays)
10101000

10111001
bins = _histdd_normalize_bins(bins, ndim)
10121002
range = _histdd_normalize_range(range, ndim)
@@ -1037,7 +1027,7 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
10371027
)
10381028

10391029
expexted_hist_dtype = _histdd_hist_dtype(queue, weights)
1040-
n = dpnp.asarray(n, dtype=expexted_hist_dtype, usm_type=usm_type, order="C")
1030+
n = dpnp.asarray(n, dtype=expexted_hist_dtype, usm_type=usm_type)
10411031

10421032
if density:
10431033
# calculate the probability density function

dpnp/tests/test_histogram.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,13 @@ def test_bins_another_sycl_queue(self):
771771

772772
def test_sample_array_like(self):
773773
v = [0, 1, 2, 3, 4]
774-
with assert_raises(ValueError):
774+
with assert_raises(TypeError):
775775
dpnp.histogramdd(v)
776776

777777
def test_weights_array_like(self):
778778
v = dpnp.arange(5)
779779
w = [1, 2, 3, 4, 5]
780-
with assert_raises(ValueError):
780+
with assert_raises(TypeError):
781781
dpnp.histogramdd(v, weights=w)
782782

783783
def test_weights_another_sycl_queue(self):

dpnp/tests/third_party/cupy/statistics_tests/test_histogram.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def test_digitize_nd_bins(self):
457457
*testing.product(
458458
{
459459
"weights": [None, 1, 2],
460-
"weights_dtype": [numpy.int32, numpy.float32],
460+
"weights_dtype": [numpy.int32, cupy.default_float_type()],
461461
"density": [True, False],
462462
"bins": [
463463
10,
@@ -473,7 +473,9 @@ def test_digitize_nd_bins(self):
473473
)
474474
class TestHistogramdd:
475475
@testing.for_all_dtypes(no_bool=True, no_complex=True)
476-
@testing.numpy_cupy_allclose(atol=1e-3, rtol=1e-3, type_check=False)
476+
@testing.numpy_cupy_allclose(
477+
atol=1e-3, rtol=1e-3, type_check=has_support_aspect64()
478+
)
477479
def test_histogramdd(self, xp, dtype):
478480
x = testing.shaped_random((100, 3), xp, dtype, scale=100)
479481
if self.bins == "array_list":

0 commit comments

Comments
 (0)