Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmo : not accept array-like, only array? #12166

Closed
nstarman opened this issue Sep 13, 2021 · 3 comments
Closed

Cosmo : not accept array-like, only array? #12166

nstarman opened this issue Sep 13, 2021 · 3 comments
Assignees
Labels
API change PRs and issues that change an existing API, possibly requiring a deprecation period cosmology Refactoring

Comments

@nstarman
Copy link
Member

nstarman commented Sep 13, 2021

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 through np.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) and numpy.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 the c backend, hopefully with mypyc so everything can be written in python and transpiled to performant c, 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

if isiterable(z):
z = np.asarray(z)
return self._Om0 * (1. + z) ** 3 * self.inv_efunc(z) ** 2

Checks like the above will not be necessary.

@nstarman nstarman added cosmology API change PRs and issues that change an existing API, possibly requiring a deprecation period labels Sep 13, 2021
@nstarman nstarman added this to To do (?) in Cosmology, the Expansion via automation Sep 13, 2021
@mhvk
Copy link
Contributor

mhvk commented Sep 13, 2021

I generally agree with anything that sets expectations for when things work (numbers, ndarray, Quantity here), and doesn't try to test on input and coerce - rather just duck type for things that do not fall in the list of accepted items (e.g., a dask array may well work, but let it be up to that class to be sufficiently close to ndarray). In your example, I would indeed think that one should just be able to directly do the calculation. (Actually, in that particular example, it would seem true even now that whatever is done is done in inv_efunc...)

@aconley
Copy link
Contributor

aconley commented Sep 13, 2021 via email

@nstarman nstarman self-assigned this Sep 14, 2021
@nstarman
Copy link
Member Author

@mhvk @aconley. Thanks for the input! (and for agreeing with me 😆).
I have a PR ready, but I'll wait until #12145 is in, as that closes a related bug.

Cosmology, the Expansion automation moved this from To do (?) to Done Sep 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change PRs and issues that change an existing API, possibly requiring a deprecation period cosmology Refactoring
Projects
Status: Done
Development

No branches or pull requests

3 participants