42
42
43
43
import dpctl .utils as dpu
44
44
import numpy
45
- from dpctl .tensor ._type_utils import _can_cast
46
45
47
46
import dpnp
48
47
49
48
# pylint: disable=no-name-in-module
50
49
import dpnp .backend .extensions .statistics ._statistics_impl as statistics_ext
50
+ from dpnp .dpnp_utils .dpnp_utils_common import (
51
+ result_type_for_device ,
52
+ to_supported_dtypes ,
53
+ )
51
54
52
55
# pylint: disable=no-name-in-module
53
- from .dpnp_utils import get_usm_allocations , map_dtype_to_device
56
+ from .dpnp_utils import get_usm_allocations
54
57
55
58
__all__ = [
56
59
"bincount" ,
66
69
_range = range
67
70
68
71
69
- def _result_type_for_device (dtypes , device ):
70
- rt = dpnp .result_type (* dtypes )
71
- return map_dtype_to_device (rt , device )
72
-
73
-
74
72
def _align_dtypes (a_dtype , bins_dtype , ntype , supported_types , device ):
75
- has_fp64 = device .has_aspect_fp64
76
- has_fp16 = device .has_aspect_fp16
77
-
78
- a_bin_dtype = _result_type_for_device ([a_dtype , bins_dtype ], device )
73
+ a_bin_dtype = result_type_for_device ([a_dtype , bins_dtype ], device )
79
74
80
75
# histogram implementation doesn't support uint64 as histogram type
81
76
# we can use int64 instead. Result would be correct even in case of overflow
82
77
if ntype == numpy .uint64 :
83
78
ntype = dpnp .int64
84
79
85
- if (a_bin_dtype , ntype ) in supported_types :
86
- return a_bin_dtype , ntype
87
-
88
- for sample_type , hist_type in supported_types :
89
- if _can_cast (
90
- a_bin_dtype , sample_type , has_fp16 , has_fp64
91
- ) and _can_cast (ntype , hist_type , has_fp16 , has_fp64 ):
92
- return sample_type , hist_type
93
-
94
- # should not happen
95
- return None , None
80
+ return to_supported_dtypes ([a_bin_dtype , ntype ], supported_types , device )
96
81
97
82
98
83
def _ravel_check_a_and_weights (a , weights ):
@@ -524,6 +509,7 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
524
509
If `bins` is a sequence, it defines a monotonically increasing array
525
510
of bin edges, including the rightmost edge, allowing for non-uniform
526
511
bin widths.
512
+
527
513
Default: ``10``.
528
514
range : {None, 2-tuple of float}, optional
529
515
The lower and upper range of the bins. If not provided, range is simply
@@ -532,6 +518,7 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
532
518
affects the automatic bin computation as well. While bin width is
533
519
computed to be optimal based on the actual data within `range`, the bin
534
520
count will fill the entire range including portions containing no data.
521
+
535
522
Default: ``None``.
536
523
density : {None, bool}, optional
537
524
If ``False`` or ``None``, the result will contain the number of samples
@@ -540,6 +527,7 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
540
527
the range is ``1``. Note that the sum of the histogram values will not
541
528
be equal to ``1`` unless bins of unity width are chosen; it is not
542
529
a probability *mass* function.
530
+
543
531
Default: ``None``.
544
532
weights : {None, dpnp.ndarray, usm_ndarray}, optional
545
533
An array of weights, of the same shape as `a`. Each value in `a` only
@@ -549,6 +537,7 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
549
537
Please note that the ``dtype`` of `weights` will also become the
550
538
``dtype`` of the returned accumulator (`hist`), so it must be large
551
539
enough to hold accumulated values as well.
540
+
552
541
Default: ``None``.
553
542
554
543
Returns
@@ -782,29 +771,31 @@ def histogram2d(x, y, bins=10, range=None, density=None, weights=None):
782
771
* A combination [int, array] or [array, int], where int
783
772
is the number of bins and array is the bin edges.
784
773
785
- Default: ``10``
774
+ Default: ``10``.
786
775
range : {None, dpnp.ndarray, usm_ndarray} of shape (2,2), optional
787
776
The leftmost and rightmost edges of the bins along each dimension
788
- (if not specified explicitly in the `bins` parameters):
789
- ``[[xmin, xmax], [ymin, ymax]]``. All values outside of this range
790
- will be considered outliers and not tallied in the histogram.
777
+ If ``None`` the ranges are
778
+ ``[[x.min(), x.max()], [y.min(), y.max()]]``. All values outside
779
+ of this range will be considered outliers and not tallied in the
780
+ histogram.
791
781
792
- Default: ``None``
782
+ Default: ``None``.
793
783
density : {None, bool}, optional
794
784
If ``False`` or ``None``, the default, returns the number of
795
785
samples in each bin.
796
786
If ``True``, returns the probability *density* function at the bin,
797
787
``bin_count / sample_count / bin_volume``.
798
788
799
- Default: ``None``
789
+ Default: ``None``.
800
790
weights : {None, dpnp.ndarray, usm_ndarray} of shape (N,), optional
801
791
An array of values ``w_i`` weighing each sample ``(x_i, y_i)``.
802
792
Weights are normalized to ``1`` if `density` is ``True``.
803
793
If `density` is ``False``, the values of the returned histogram
804
794
are equal to the sum of the weights belonging to the samples
805
795
falling into each bin.
796
+ If ``None`` all samples are assigned a weight of ``1``.
806
797
807
- Default: ``None``
798
+ Default: ``None``.
808
799
Returns
809
800
-------
810
801
H : dpnp.ndarray of shape (nx, ny)
@@ -836,18 +827,27 @@ def histogram2d(x, y, bins=10, range=None, density=None, weights=None):
836
827
Examples
837
828
--------
838
829
>>> import dpnp as np
839
- >>> x = np.random.randn(20)
840
- >>> y = np.random.randn(20)
830
+ >>> x = np.random.randn(20).astype("float32")
831
+ >>> y = np.random.randn(20).astype("float32")
841
832
>>> hist, edges_x, edges_y = np.histogram2d(x, y, bins=(4, 3))
833
+ >>> hist.shape
834
+ (4, 3)
842
835
>>> hist
843
- [[1. 0. 0.]
844
- [0. 0. 0.]
845
- [5. 6. 4.]
846
- [1. 2. 1.]]
836
+ array([[1., 2., 0.],
837
+ [0., 3., 1.],
838
+ [1., 4., 1.],
839
+ [1., 3., 3.]], dtype=float32)
840
+ >>> edges_x.shape
841
+ (5,)
847
842
>>> edges_x
848
- [-5.6575713 -3.5574734 -1.4573755 0.6427226 2.74282 ]
843
+ array([-1.7516936 , -0.96109843, -0.17050326, 0.62009203, 1.4106871 ],
844
+ dtype=float32)
845
+ >>> edges_y.shape
846
+ (4,)
849
847
>>> edges_y
850
- [-1.1889046 -0.07263839 1.0436279 2.159894 ]
848
+ array([-2.6604428 , -0.94615364, 0.76813555, 2.4824247 ], dtype=float32)
849
+
850
+ Please note, that resulting values of histogram and edges would be different
851
851
"""
852
852
853
853
dpnp .check_supported_arrays_type (x , y )
@@ -869,7 +869,7 @@ def histogram2d(x, y, bins=10, range=None, density=None, weights=None):
869
869
usm_type , exec_q = get_usm_allocations ([x , y , bins , range , weights ])
870
870
device = exec_q .sycl_device
871
871
872
- sample_dtype = _result_type_for_device ([x .dtype , y .dtype ], device )
872
+ sample_dtype = result_type_for_device ([x .dtype , y .dtype ], device )
873
873
874
874
# Unlike histogramdd histogram2d accepts 1d bins and
875
875
# apply it to both dimensions
@@ -883,7 +883,7 @@ def histogram2d(x, y, bins=10, range=None, density=None, weights=None):
883
883
bins_dtypes = [sample_dtype ]
884
884
bins_dtypes += [b .dtype for b in bins if hasattr (b , "dtype" )]
885
885
886
- bins_dtype = _result_type_for_device (bins_dtypes , device )
886
+ bins_dtype = result_type_for_device (bins_dtypes , device )
887
887
hist_dtype = _histdd_hist_dtype (exec_q , weights )
888
888
889
889
supported_types = statistics_ext .histogramdd_dtypes ()
@@ -1026,9 +1026,7 @@ def _histdd_hist_dtype(queue, weights):
1026
1026
# hist_dtype is either float or complex, so it is ok
1027
1027
# to calculate it as result type between default_float and
1028
1028
# weights.dtype
1029
- hist_dtype = _result_type_for_device (
1030
- [hist_dtype , weights .dtype ], device
1031
- )
1029
+ hist_dtype = result_type_for_device ([hist_dtype , weights .dtype ], device )
1032
1030
1033
1031
return hist_dtype
1034
1032
@@ -1039,7 +1037,7 @@ def _histdd_sample_dtype(queue, sample, bin_edges_list):
1039
1037
dtypes_ = [bin_edges .dtype for bin_edges in bin_edges_list ]
1040
1038
dtypes_ .append (sample .dtype )
1041
1039
1042
- return _result_type_for_device (dtypes_ , device )
1040
+ return result_type_for_device (dtypes_ , device )
1043
1041
1044
1042
1045
1043
def _histdd_supported_dtypes (sample , bin_edges_list , weights ):
@@ -1089,31 +1087,33 @@ def histogramdd(sample, bins=10, range=None, density=None, weights=None):
1089
1087
* The number of bins for each dimension (nx, ny, ... =bins)
1090
1088
* The number of bins for all dimensions (nx=ny=...=bins).
1091
1089
1092
- Default: ``10``
1090
+ Default: ``10``.
1093
1091
range : {None, sequence}, optional
1094
1092
A sequence of length D, each an optional (lower, upper) tuple giving
1095
1093
the outer bin edges to be used if the edges are not given explicitly in
1096
1094
`bins`.
1097
- An entry of None in the sequence results in the minimum and maximum
1095
+ An entry of `` None`` in the sequence results in the minimum and maximum
1098
1096
values being used for the corresponding dimension.
1099
- None is equivalent to passing a tuple of D None values.
1097
+ `` None`` is equivalent to passing a tuple of D `` None`` values.
1100
1098
1101
- Default: ``None``
1099
+ Default: ``None``.
1102
1100
density : {None, bool}, optional
1103
1101
If ``False`` or ``None``, the default, returns the number of
1104
1102
samples in each bin.
1105
1103
If ``True``, returns the probability *density* function at the bin,
1106
1104
``bin_count / sample_count / bin_volume``.
1107
1105
1108
- Default: ``None``
1109
- weights : {dpnp.ndarray, usm_ndarray}, optional
1106
+ Default: ``None``.
1107
+ weights : {None, dpnp.ndarray, usm_ndarray}, optional
1110
1108
An (N,)-shaped array of values `w_i` weighing each sample
1111
1109
`(x_i, y_i, z_i, ...)`.
1112
- Weights are normalized to 1 if density is True. If density is False,
1113
- the values of the returned histogram are equal to the sum of the
1114
- weights belonging to the samples falling into each bin.
1110
+ Weights are normalized to ``1`` if density is ``True``.
1111
+ If density is ``False``, the values of the returned histogram
1112
+ are equal to the sum of the weights belonging to the samples
1113
+ falling into each bin.
1114
+ If ``None`` all samples are assigned a weight of ``1``.
1115
1115
1116
- Default: ``None``
1116
+ Default: ``None``.
1117
1117
1118
1118
Returns
1119
1119
-------
0 commit comments