dask-contrib / dask-awkward

Native Dask collection for awkward arrays, and the library to use it.
https://dask-awkward.readthedocs.io
BSD 3-Clause "New" or "Revised" License
61 stars 19 forks source link

Unexpected results in division of scalars #525

Closed alexander-held closed 1 month ago

alexander-held commented 4 months ago

In the following example, dividing a scalar by an expression that reduces down to a scalar yields unexpected results:

import numpy as np
import dask_awkward as dak
import awkward as ak

data = ak.Array(np.arange(10))
a = dak.from_awkward(data, npartitions=1)

print(f"sum: {(ak.sum(a)).compute()}")
print(f"1/sum: {(1/ak.sum(a)).compute()}")
print(f"0.5/sum: {(0.5/ak.sum(a)).compute()}")

output:

sum: 45
1/sum: 45.0
0.5/sum: 90.0

A way to make this work correctly instead is exponentiation: print(f"sum**(-1.0): {(ak.sum(a)**(-1.0)).compute()}").

When instead using dask.array via

import dask.array as da

data = np.arange(10)
a = da.from_array(data)

the results are as expected:

sum: 45
1/sum: 0.022222222222222223
0.5/sum: 0.011111111111111112

I'm using dask_awkward version 2024.6.0.

martindurant commented 4 months ago

https://github.com/dask-contrib/dask-awkward/pull/516 I think fixed this

jpivarski commented 1 month ago

Yes, version 2024.9.0 from PyPI has:

>>> import numpy as np
>>> import dask_awkward as dak
>>> import awkward as ak
>>> 
>>> data = ak.Array(np.arange(10))
>>> a = dak.from_awkward(data, npartitions=1)
>>> 
>>> print(f"sum: {(ak.sum(a)).compute()}")
sum: 45
>>> print(f"1/sum: {(1/ak.sum(a)).compute()}")
1/sum: 0.022222222222222223
>>> print(f"0.5/sum: {(0.5/ak.sum(a)).compute()}")
0.5/sum: 0.011111111111111112

It's fixed.