Skip to content

Commit

Permalink
Aplly review comments 2
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderKalistratov committed Dec 4, 2024
1 parent 8967070 commit 60776e8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
4 changes: 4 additions & 0 deletions dpnp/backend/extensions/statistics/histogramdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ std::tuple<sycl::event, sycl::event> Histogramdd::call(
{
validate(sample, bins_edges, weights, histogram);

if (sample.get_size() == 0) {
return {sycl::event(), sycl::event()};
}

const int sample_typenum = sample.get_typenum();
const int hist_typenum = histogram.get_typenum();

Expand Down
44 changes: 18 additions & 26 deletions dpnp/dpnp_iface_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import dpnp.backend.extensions.statistics._statistics_impl as statistics_ext

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

__all__ = [
"bincount",
Expand Down Expand Up @@ -409,7 +409,7 @@ def bincount(x, weights=None, minlength=None):
x_casted, weights_casted, minlength, ntype_casted, usm_type
)

n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type, order="C")
n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type)

return n

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

n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type, order="C")
n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type)

if density:
db = dpnp.astype(
Expand Down Expand Up @@ -811,12 +811,10 @@ def _histdd_make_edges(sample, bins, range, usm_type):


def _histdd_flatten_binedges(bedges_list, edges_count_list, dtype):
queue = bedges_list[0].sycl_queue
usm_type = bedges_list[0].usm_type
total_edges_size = numpy.sum(edges_count_list)

bin_edges_flat = dpnp.empty(
shape=total_edges_size, dtype=dtype, sycl_queue=queue, usm_type=usm_type
bin_edges_flat = dpnp.empty_like(
bedges_list[0], shape=total_edges_size, dtype=dtype
)

offset = numpy.pad(numpy.cumsum(edges_count_list), (1, 0))
Expand Down Expand Up @@ -932,13 +930,14 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
----------
sample : {dpnp.ndarray, usm_ndarray}
Input (N, D)-shaped array to be histogrammed.
bins : {sequence, int}, optional
The bin specification:
* A sequence of arrays describing the monotonically increasing bin
edges along each dimension.
* The number of bins for each dimension (nx, ny, ... =bins)
* The number of bins for all dimensions (nx=ny=...=bins).
Default: ``10``
range : {None, sequence}, optional
A sequence of length D, each an optional (lower, upper) tuple giving
Expand All @@ -947,26 +946,29 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
An entry of None in the sequence results in the minimum and maximum
values being used for the corresponding dimension.
None is equivalent to passing a tuple of D None values.
Default: ``None``
weights : {dpnp.ndarray, usm_ndarray}, optional
An (N,)-shaped array of values `w_i` weighing each sample
`(x_i, y_i, z_i, ...)`.
Weights are normalized to 1 if density is True. If density is False,
the values of the returned histogram are equal to the sum of the
weights belonging to the samples falling into each bin.
Default: ``None``
density : {bool}, optional
If ``False``, the default, returns the number of samples in each bin.
If ``True``, returns the probability *density* function at the bin,
``bin_count / sample_count / bin_volume``.
Default: ``False``
Returns
-------
H : {dpnp.ndarray}
The multidimensional histogram of sample x. See density and weights
for the different possible semantics.
edges : {list of ndarrays}
edges : {list of dpnp.ndarray}
A list of D arrays describing the bin edges for each dimension.
See Also
Expand All @@ -977,36 +979,26 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
Examples
--------
>>> import dpnp as np
>>> r = np.random.normal(size=(100,3))
>>> r = np.random.normal(size=(100, 3))
>>> H, edges = np.histogramdd(r, bins = (5, 8, 4))
>>> H.shape, edges[0].size, edges[1].size, edges[2].size
((5, 8, 4), 6, 9, 5)
"""

if not dpnp.is_supported_array_type(sample):
raise ValueError("sample must be dpnp.ndarray or usm_ndarray")

if weights is not None and not dpnp.is_supported_array_type(weights):
raise ValueError("weights must be dpnp.ndarray or usm_ndarray")
dpnp.check_supported_arrays_type(sample)
if weights is not None:
dpnp.check_supported_arrays_type(weights)

if sample.ndim == 0 and sample.size == 1:
sample = dpnp.reshape(sample, (1, 1))
elif sample.ndim == 1:
if sample.ndim < 2:
sample = dpnp.reshape(sample, (sample.size, 1))
elif sample.ndim > 2:
raise ValueError("sample must have no more than 2 dimensions")

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

_arrays = _histdd_extract_arrays(sample, weights, bins)
usm_type = dpu.get_coerced_usm_type([a.usm_type for a in _arrays])
queue = dpu.get_execution_queue([a.sycl_queue for a in _arrays])

assert usm_type is not None

if queue is None:
raise ValueError("all arrays must be allocated on the same SYCL queue")
usm_type, queue = get_usm_allocations(_arrays)

bins = _histdd_normalize_bins(bins, ndim)
range = _histdd_normalize_range(range, ndim)
Expand Down Expand Up @@ -1037,7 +1029,7 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False):
)

expexted_hist_dtype = _histdd_hist_dtype(queue, weights)
n = dpnp.asarray(n, dtype=expexted_hist_dtype, usm_type=usm_type, order="C")
n = dpnp.asarray(n, dtype=expexted_hist_dtype, usm_type=usm_type)

if density:
# calculate the probability density function
Expand Down
4 changes: 2 additions & 2 deletions dpnp/tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,13 +771,13 @@ def test_bins_another_sycl_queue(self):

def test_sample_array_like(self):
v = [0, 1, 2, 3, 4]
with assert_raises(ValueError):
with assert_raises(TypeError):
dpnp.histogramdd(v)

def test_weights_array_like(self):
v = dpnp.arange(5)
w = [1, 2, 3, 4, 5]
with assert_raises(ValueError):
with assert_raises(TypeError):
dpnp.histogramdd(v, weights=w)

def test_weights_another_sycl_queue(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def test_digitize_nd_bins(self):
*testing.product(
{
"weights": [None, 1, 2],
"weights_dtype": [numpy.int32, numpy.float32],
"weights_dtype": [numpy.int32, cupy.default_float_type()],
"density": [True, False],
"bins": [
10,
Expand All @@ -473,7 +473,9 @@ def test_digitize_nd_bins(self):
)
class TestHistogramdd:
@testing.for_all_dtypes(no_bool=True, no_complex=True)
@testing.numpy_cupy_allclose(atol=1e-3, rtol=1e-3, type_check=False)
@testing.numpy_cupy_allclose(
atol=1e-3, rtol=1e-3, type_check=has_support_aspect64()
)
def test_histogramdd(self, xp, dtype):
x = testing.shaped_random((100, 3), xp, dtype, scale=100)
if self.bins == "array_list":
Expand Down

0 comments on commit 60776e8

Please sign in to comment.