xhistogram icon indicating copy to clipboard operation
xhistogram copied to clipboard

ValueError when running histogram with 3 lists of 1-D bins and density=True

Open cspencerjones opened this issue 4 months ago • 0 comments

When I try to run xhistogram to bin my data using 3 separate lists of bins and density=True, I get a Value Error. Here's a minimal example:

import xarray as xr
import numpy as np

nt = 100
da = xr.DataArray(np.random.randn(nt), dims=['time'],
                  name='foo') # all inputs need a name
da2 = xr.DataArray(np.random.randn(nt), dims=['time'],
                  name='foo2')
da3 = xr.DataArray(np.random.randn(nt), dims=['time'],
                  name='foo3')

from xhistogram.xarray import histogram

bins1 = np.linspace(-4, 4, 20)
bins2 = np.linspace(-4, 4, 10)
h = histogram(da, da2, da3, bins=[bins1, bins2, bins1],density=True)
h.plot()

The error is:

ValueError                                Traceback (most recent call last)
Cell In[135], line 5
      3 bins1 = np.linspace(-4, 4, 20)
      4 bins2 = np.linspace(-4, 4, 10)
----> 5 h = histogram(da, da2, da3, bins=[bins1, bins2, bins1],density=True)
      6 h.plot()

File [/srv/conda/envs/notebook/lib/python3.12/site-packages/xhistogram/xarray.py:164](https://leap.2i2c.cloud/srv/conda/envs/notebook/lib/python3.12/site-packages/xhistogram/xarray.py#line=163), in histogram(bins, range, dim, weights, density, block_size, keep_coords, bin_dim_suffix, *args)
    161     dims_to_keep = []
    162     axis = None
--> 164 h_data, bins = _histogram(
    165     *args_data,
    166     weights=weights_data,
    167     bins=bins,
    168     range=range,
    169     axis=axis,
    170     density=density,
    171     block_size=block_size,
    172 )
    174 # create output dims
    175 new_dims = [a.name + bin_dim_suffix for a in args[:N_args]]

File [/srv/conda/envs/notebook/lib/python3.12/site-packages/xhistogram/core.py:454](https://leap.2i2c.cloud/srv/conda/envs/notebook/lib/python3.12/site-packages/xhistogram/core.py#line=453), in histogram(bins, range, axis, weights, density, block_size, *args)
    451     bin_areas = np.outer(*bin_widths)
    452 else:
    453     # Slower, but N-dimensional logic
--> 454     bin_areas = np.prod(np.ix_(*bin_widths))
    456 # Sum over the last n_inputs axes, which correspond to the bins. All other axes
    457 # are "bystander" axes. Sums must be done independently for each bystander axes
    458 # so that nans are dealt with correctly (#51)
    459 bin_axes = tuple(_range(-n_inputs, 0))

File [/srv/conda/envs/notebook/lib/python3.12/site-packages/numpy/_core/fromnumeric.py:3191](https://leap.2i2c.cloud/srv/conda/envs/notebook/lib/python3.12/site-packages/numpy/_core/fromnumeric.py#line=3190), in prod(a, axis, dtype, out, keepdims, initial, where)
   3068 @array_function_dispatch(_prod_dispatcher)
   3069 def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
   3070          initial=np._NoValue, where=np._NoValue):
   3071     """
   3072     Return the product of array elements over a given axis.
   3073 
   (...)
   3189     10
   3190     """
-> 3191     return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
   3192                           keepdims=keepdims, initial=initial, where=where)

File [/srv/conda/envs/notebook/lib/python3.12/site-packages/numpy/_core/fromnumeric.py:86](https://leap.2i2c.cloud/srv/conda/envs/notebook/lib/python3.12/site-packages/numpy/_core/fromnumeric.py#line=85), in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     83         else:
     84             return reduction(axis=axis, out=out, **passkwargs)
---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

It seems like the line causing the trouble is this one

cspencerjones avatar Oct 25 '24 22:10 cspencerjones