Cosmo : not accept array-like, only array? #12166
Labels
API change
PRs and issues that change an existing API, possibly requiring a deprecation period
cosmology
Refactoring
Projects
Description
Many of the functions in
astropy.cosmology
accept any array-like input and have to perform a check to distinguish between scalars and arrays. While this is faster than just passing everything throughnp.array()
(as discussed in #12145 (comment)), the check is still a performance bottleneck.
It seems to me that there is not much added value in Cosmology functions accepting any array-like input rather than actual ndarrays.
I propose limiting the accepted input types to
numbers.Number
(e.g. int, float) andnumpy.ndarray
(includes Quantity) and eliminating array-like inputs, like lists and tuples.Looking forward, this is part of a longer plan to "vectorize" Cosmology, both the methods (like with #11893), but also have the entire class work with arrays of parameters, not only scalar values. With only Number and ndarray (incl. Quantity) as valid inputs it will eventually be possible to eliminate workaround methods like
astropy.cosmology.utils.vectorize_if_needed
which are likewise performance bottlenecks. Also at some point I would like to extend thec
backend, hopefully withmypyc
so everything can be written in python and transpiled to performantc
, and simple type inputs make for much easier / better code.I would appreciate some second opinions as this is a non-backward compatible change. I think / hope its a painless one, especially since most people just use numpy arrays anyway, but it's still a change. v5.0 seems the right time, if any.
tag: @mhvk @adrn @aconley
astropy/astropy/cosmology/flrw.py
Lines 417 to 419 in 23404ee
Checks like the above will not be necessary.
The text was updated successfully, but these errors were encountered: