[Numpy-discussion] is __array_ufunc__ ready for prime-time?
josef.pktd at gmail.com
josef.pktd at gmail.com
Thu Nov 2 12:43:43 EDT 2017
On Thu, Nov 2, 2017 at 11:51 AM, Marten van Kerkwijk <
m.h.vankerkwijk at gmail.com> wrote:
> Hi Josef,
> astropy's Quantity is well developed and would give similar results to
> pint; all those results make sense if one wants to have consistent
> units. A general library code will actually do the right thing as long
> as it just uses normal mathematical operations with ufuncs - and as
> long as it just duck types! - the unit code will then override and
> properly propagate units to outputs, as can be seen in this example:
> import astropy.units as u
> np.fft.fftfreq(8, 1*u.min)
> # <Quantity [ 0. , 0.125, 0.25 , 0.375,-0.5 ,-0.375,-0.25 ,-0.125] 1 /
> np.fft.fftfreq(8, 1*u.min).var()
> # <Quantity 0.08203125 1 / min2>
> > for example if units are some flows per unit of time and we average, sum
> or integrate over time, then what are the new units? (e.g. pandas time
> The units module will force you to take into account `dt`! This is in
> fact one reason why it is so powerful. So, your example might go
> something like:
> flow = [1., 1.5, 1.5] * u.g / u.s
> dt = [0.5, 0.5, 1.] * u.hr
> np.sum(flow * dt)
> # <Quantity 2.75 g h / s>
> np.sum(flow * dt).to(u.kg)
> # <Quantity 9.9 kg>
> > How do you concatenate and operate arrays with different units?
> This is where Nathaniel's `__array_concatenate__` would come in. For
> regular arrays it is fine to just concatenate, but for almost anything
> else you need a different approach. For quantities, the most logical
> one would be to first create an empty array of the right size with the
> unit of, e.g., the first part to be concatenated, and then set
> sections to the input quantities (where the setter does unit
> conversion and will fail if that is not possible).
"will fail if that is not possible"
rules out inhomogeneous arrays (analogous to structure dtypes)
How to you get a vander matrix for something simple like a polynomial fit?
x[:, None] ** np.arange(3)
> All the best,
> p.s. A fun subject is what to do with logarithmic units, such as the
> magnitudes in astronomy... We have a module for that as well;
similar, scipy.special has ufuncs
what units are those?
Most code that I know (i.e. scipy.stats and statsmodels) does not use only
"normal mathematical operations with ufuncs"
I guess there are a lot of "abnormal" mathematical operations
where just simply propagating the units will not work.
Aside: The problem is more general also for other datastructures.
E.g. statsmodels for most parts uses only numpy ndarrays inside the
algorithm and computations because that provides well defined
behavior. (e.g. pandas behaved too differently in many cases)
I don't have much idea yet about how to change the infrastructure to
allow the use of dask arrays, sparse matrices and similar and possibly
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion