Skip to content

Commit 752979b

Browse files
Aplly review comments IntelPython#2
1 parent 8967070 commit 752979b

3 files changed

Lines changed: 21 additions & 27 deletions

File tree

dpnp/dpnp_iface_histograms.py

Lines changed: 17 additions & 23 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,7 +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")
412+
n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type)
413413

414414
return n
415415

@@ -657,7 +657,7 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
657657
)
658658
_manager.add_event_pair(mem_ev, ht_ev)
659659

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

662662
if density:
663663
db = dpnp.astype(
@@ -811,12 +811,10 @@ def _histdd_make_edges(sample, bins, range, usm_type):
811811

812812

813813
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
816814
total_edges_size = numpy.sum(edges_count_list)
817815

818-
bin_edges_flat = dpnp.empty(
819-
shape=total_edges_size, dtype=dtype, sycl_queue=queue, usm_type=usm_type
816+
bin_edges_flat = dpnp.empty_like(
817+
bedges_list[0], shape=total_edges_size, dtype=dtype
820818
)
821819

822820
offset = numpy.pad(numpy.cumsum(edges_count_list), (1, 0))
@@ -932,13 +930,14 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
932930
----------
933931
sample : {dpnp.ndarray, usm_ndarray}
934932
Input (N, D)-shaped array to be histogrammed.
935-
936933
bins : {sequence, int}, optional
937934
The bin specification:
935+
938936
* A sequence of arrays describing the monotonically increasing bin
939937
edges along each dimension.
940938
* The number of bins for each dimension (nx, ny, ... =bins)
941939
* The number of bins for all dimensions (nx=ny=...=bins).
940+
942941
Default: ``10``
943942
range : {None, sequence}, optional
944943
A sequence of length D, each an optional (lower, upper) tuple giving
@@ -947,26 +946,29 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
947946
An entry of None in the sequence results in the minimum and maximum
948947
values being used for the corresponding dimension.
949948
None is equivalent to passing a tuple of D None values.
949+
950950
Default: ``None``
951951
weights : {dpnp.ndarray, usm_ndarray}, optional
952952
An (N,)-shaped array of values `w_i` weighing each sample
953953
`(x_i, y_i, z_i, ...)`.
954954
Weights are normalized to 1 if density is True. If density is False,
955955
the values of the returned histogram are equal to the sum of the
956956
weights belonging to the samples falling into each bin.
957+
957958
Default: ``None``
958959
density : {bool}, optional
959960
If ``False``, the default, returns the number of samples in each bin.
960961
If ``True``, returns the probability *density* function at the bin,
961962
``bin_count / sample_count / bin_volume``.
963+
962964
Default: ``False``
963965
964966
Returns
965967
-------
966968
H : {dpnp.ndarray}
967969
The multidimensional histogram of sample x. See density and weights
968970
for the different possible semantics.
969-
edges : {list of ndarrays}
971+
edges : {list of dpnp.ndarray}
970972
A list of D arrays describing the bin edges for each dimension.
971973
972974
See Also
@@ -977,18 +979,16 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
977979
Examples
978980
--------
979981
>>> import dpnp as np
980-
>>> r = np.random.normal(size=(100,3))
982+
>>> r = np.random.normal(size=(100, 3))
981983
>>> H, edges = np.histogramdd(r, bins = (5, 8, 4))
982984
>>> H.shape, edges[0].size, edges[1].size, edges[2].size
983985
((5, 8, 4), 6, 9, 5)
984986
985987
"""
986988

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")
989+
dpnp.check_supported_arrays_type(sample)
990+
if weights is not None:
991+
dpnp.check_supported_arrays_type(weights)
992992

993993
if sample.ndim == 0 and sample.size == 1:
994994
sample = dpnp.reshape(sample, (1, 1))
@@ -1000,13 +1000,7 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
10001000
ndim = sample.shape[1] if sample.size > 0 else 1
10011001

10021002
_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")
1003+
usm_type, queue = get_usm_allocations(_arrays)
10101004

10111005
bins = _histdd_normalize_bins(bins, ndim)
10121006
range = _histdd_normalize_range(range, ndim)
@@ -1037,7 +1031,7 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
10371031
)
10381032

10391033
expexted_hist_dtype = _histdd_hist_dtype(queue, weights)
1040-
n = dpnp.asarray(n, dtype=expexted_hist_dtype, usm_type=usm_type, order="C")
1034+
n = dpnp.asarray(n, dtype=expexted_hist_dtype, usm_type=usm_type)
10411035

10421036
if density:
10431037
# 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: 2 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,7 @@ 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(atol=1e-3, rtol=1e-3, type_check=has_support_aspect64())
477477
def test_histogramdd(self, xp, dtype):
478478
x = testing.shaped_random((100, 3), xp, dtype, scale=100)
479479
if self.bins == "array_list":

0 commit comments

Comments
 (0)