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

Quantities to be initialized from iterators #9245

Open
bsipocz opened this issue Sep 17, 2019 · 1 comment
Open

Quantities to be initialized from iterators #9245

bsipocz opened this issue Sep 17, 2019 · 1 comment

Comments

@bsipocz
Copy link
Member

bsipocz commented Sep 17, 2019

Today I spent some time in the astropy.timeseries.periodograms territory for hunting down the bug for #9236, and while now almost everything works I still have two failures that could easily be fixed if this line would work with Quantities (but of course np.fromiter doesn't work with object dtype):
https://github.com/astropy/astropy/blob/master/astropy/timeseries/periodograms/lombscargle/_statistics.py#L360

I'll do a workaround, but then wondered whether it's a valid use case to support Quantity initialization using iterators.

I suspect this will boil down to the same upstream issue as #5493.

@mhvk
Copy link
Contributor

mhvk commented Sep 17, 2019

Your suspicion is justified to the extent that Quantity relies on np.array to do the coercion of lists, etc., to arrays, and np.array does not take iterators. The logic for np.array is that it is impossible to know the length of an iterator before iterating, so one is stuck anyway with first creating something like a list.

But perhaps we should deviate from that behaviour; it would mean changing the logic inside the isiterable(value) branch in Quantity.__new__. It still would be rather slow, though, and at least the example you point to is probably better done by pre-allocating an array and just filling it in a loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants