Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/arviz_stats/base/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,11 @@ def histogram(self, ary, bins=None, range=None, weights=None, axis=-1, density=N
axes = [ax if ax >= 0 else ary.ndim + ax for ax in axis]
reordered_axes = [i for i in np.arange(ary.ndim) if i not in axes] + list(axes)
if weights is not None:
assert ary.shape == weights.shape
if ary.shape != weights.shape:
raise ValueError(
"`weights` must have the same shape as `ary`. "
f"Got ary.shape={ary.shape}, weights.shape={weights.shape}"
)
weights = np.transpose(weights, axes=reordered_axes)
ary = np.transpose(ary, axes=reordered_axes)
broadcased_shape = ary.shape[: -len(axes)]
Expand Down Expand Up @@ -473,7 +477,13 @@ def histogram(self, ary, bins=None, range=None, weights=None, axis=-1, density=N
ary, bins=bins, range=range, density=density, shape_from_1st=True
)
# ensure broadcasting over range
assert range.shape[:-1] == broadcased_shape
if range.shape[:-1] != broadcased_shape:
expected_shape = broadcased_shape + (2,)
raise ValueError(
"`range` has incompatible shape. "
f"Expected shape {expected_shape}, "
f"got range.shape={range.shape}"
)
if weights is not None:
# ensure broadcasting over weights
histogram_ufunc = make_ufunc(
Expand All @@ -494,7 +504,12 @@ def histogram(self, ary, bins=None, range=None, weights=None, axis=-1, density=N
)
return histogram_ufunc(ary, range, shape_from_1st=True)
# ensure broadcasting over bins
assert bins.shape[:-1] == broadcased_shape
if bins.shape[:-1] != broadcased_shape:
raise ValueError(
"`bins` has incompatible shape. "
f"Expected leading dimensions {broadcased_shape}, "
f"got bins.shape={bins.shape}"
)
if (range is None) or (np.size(range) == 2):
# avoid broadcasting over range
if weights is not None:
Expand All @@ -517,7 +532,13 @@ def histogram(self, ary, bins=None, range=None, weights=None, axis=-1, density=N
)
return histogram_ufunc(ary, bins, shape_from_1st=True)
# ensure broadcasting over range
assert range.shape[:-1] == broadcased_shape
if range.shape[:-1] != broadcased_shape:
expected_shape = broadcased_shape + (2,)
raise ValueError(
"`range` has incompatible shape. "
f"Expected shape {expected_shape}, "
f"got range.shape={range.shape}"
)
if weights is not None:
# ensure broadcasting over weights
histogram_ufunc = make_ufunc(
Expand Down