Skip to content

Angular diameter distance for positive curvature - #4723

Closed
joergdietrich wants to merge 11 commits into
astropy:masterfrom
joergdietrich:ADD_negative_Ok
Closed

joergdietrich wants to merge 11 commits into
astropy:masterfrom
joergdietrich:ADD_negative_Ok

Conversation

@joergdietrich

Copy link
Copy Markdown
Contributor

This is the PR promised in #4661 to make angular_diameter_distance_z1z2 work reliably in positive curvature models. Along the way it creates the new methods comoving_distance_z1z2 and comoving_transverse_distance_z1z2 on which angular_diameter_distance_z1z2 is now based instead of Peebles' (1993) eq. 13.70.

comoving_distance and comoving_transverse_distance are now special cases of the corresponding _z1z2 methods. The performance impact is one additional function call for these methods and amounts to 2-4 µs on my laptop for a total execution time of 78 µs for a scalar input. For array like input, the perfomance difference is negligible because the code spends most of the time in the for loop of the vectorized function call.

vectorize_if_needed now deals with functions with more than one input argument (arbitrary combinations of scalar and array like)

angular_diameter_distance_z1z2 is 50% faster on scalar input because the integral over 1/E(z) is computed only once instead of twice. Again, the performance difference vanishes if the function needs to be vectorized.

@joergdietrich joergdietrich reopened this Mar 23, 2016
@pllim pllim added cosmology Affects-dev PRs and issues that do not impact an existing Astropy release labels Mar 23, 2016
@pllim pllim added this to the v1.2.0 milestone Mar 23, 2016
Comment thread astropy/cosmology/core.py Outdated
return self.comoving_transverse_distance_z1z2(0, z)

def comoving_transverse_distance_z1z2(self, z1, z2):
"""Comoving transverse distance in Mpc between to redshifts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo 'to'

@nhmc

nhmc commented Mar 25, 2016

Copy link
Copy Markdown
Contributor

I left some inline comments pointing out docstring typos.

Otherwise this looks excellent to me. I much prefer having this generalised comoving distance method (and the new related methods) allowing two input redshifts, instead of the previous approximation used for angular_diameter_distance_z1z2.

@aconley should take a look at this too, though, as he's more of a cosmology guru than me.

@joergdietrich

Copy link
Copy Markdown
Contributor Author

Fixed the typos. Thanks!

The previous expression wasn't an approximation. It's analytically exact but numerically unstable.

@aconley

aconley commented Mar 25, 2016

Copy link
Copy Markdown
Contributor

The functionality looks good to me.

But it does seem a little strange to me that we now have _z1z2 versions of some functions but
not others. And I am a bit leery about adding even more functions to the comology class --
it's getting hard to keep track. Should we instead consider adding default min z arguments
instead of new functions?

For example:
luminosity_distance(self, z1=0, z2)
comoving_transverse_distance(self, z1=0, z2)
absorption_distance(self, z1=0, z2)
angular_diameter_distance(self, z1=0, z2)

we'd have to keep angular_diameter_distance_z1z2 for backwards compatibility,
but it would avoid further fattening of the interface.

I'd be happy to make that change by making a pull against @joergdietrich -s branch
if that seems useful.

@joergdietrich

Copy link
Copy Markdown
Contributor Author

Unfortunately, that's a syntax error in Python. Non-default arguments can't follow default arguments.
luminosity_distance(self, z2, z1=0) is counter-intuitive. Maybe luminosity_distance(self, z1, z2=None) is better, which takes computes d_lum(0, z1) if z2 is None and d_lum(z1, z2) if not.

Doing things this way would also allow us to keep the old behavior of raising ValueError for angular_diameter_distance_z1z2 if any z2 < z1 and going ahead with the new, more sensible approach to return negative distances if z2 < z1 in angular_diameter_distance(z1, z2) calls.

@aconley

aconley commented Mar 26, 2016

Copy link
Copy Markdown
Contributor

Oh, right, nuts.

At that point I think you call them distance(self, zmax, zmin=0).

@aconley

aconley commented Mar 26, 2016

Copy link
Copy Markdown
Contributor

Will anybody actually use any of the z1z2s except the angular diameter one?

With that in mind, a simpler, fix it now, solution would be to remove (or make private
by adding a leading _) your new z1z2 functions. That solves the immediate problem, but doesn't
force us to commit to supporting the _z1z2 API (which I clearly don't like) in the future
(except for the one that already exists).

@nhmc

nhmc commented Mar 28, 2016

Copy link
Copy Markdown
Contributor

@joergdietrich Thanks for clarifying (i.e. it's an exact analytic expression, but numerically unstable).

I don't think the API is a problem here. We can always just replace the existing positional argument with a z1 keyword argument: luminosity_distance(self, z1=None, z2=None)
Then you can pass both as positional or keyword arguments, whatever the user prefers. An error can be raised by hand if z1 is not passed.

But I agree with @aconley's concern about method bloat, and I like his idea to just make the new z1z2 functions private by adding a leading underscore for now.

@joergdietrich

Copy link
Copy Markdown
Contributor Author

I like @nhmc's suggestion to make both arguments keyword arguments, although in the end very few methods actually benefit from having both z1 and z2 in practice. Occasionally lookback_time(z1, z2) may be interesting. So I'm okay with making the new methods private for now.

@aconley

aconley commented Mar 30, 2016

Copy link
Copy Markdown
Contributor

age(z1, z2) would be convenient to have, although easy enough to calculate
without the overloaded version.

But not a suggestion for now.

On Wed, Mar 30, 2016 at 9:58 AM, Jörg Dietrich [email protected]
wrote:

I like @nhmc https://github.com/nhmc's suggestion to make both
arguments keyword arguments, although in the end very few methods actually
benefit from having both z1 and z2 in practice. Occasionally
lookback_time(z1, z2) may be interesting. So I'm okay with making the new
methods private for now.

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#4723 (comment)

@joergdietrich

Copy link
Copy Markdown
Contributor Author

New commit to make them private for now.

@joergdietrich joergdietrich reopened this Mar 30, 2016
Comment thread astropy/cosmology/core.py
if isiterable(x):
return np.vectorize(func)(x)
if any(map(isiterable, x)):
return np.vectorize(func)(*x)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is lazy in python 2. any is, but map isn't.
So this isn't quite as efficient as it may seem. I'm not sure we care though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Python 2 there is itertools.imap, and six provides a wrapper : six.map

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Already in the morning I can answer the question "what have I learned today?".

@aconley

aconley commented Mar 31, 2016

Copy link
Copy Markdown
Contributor

Looks good to me.

@aconley

aconley commented Apr 9, 2016

Copy link
Copy Markdown
Contributor

@nhmc - are we waiting on something to accept this?

@nhmc

nhmc commented Apr 9, 2016

Copy link
Copy Markdown
Contributor

No, it looks ready to merge to me. Sorry for not commenting sooner. Thanks again @joergdietrich.

@aconley

aconley commented Apr 9, 2016

Copy link
Copy Markdown
Contributor

Yes, thanks for adding this capability. I know lensing folk will find this quite helpful.

@pllim

pllim commented Apr 11, 2016

Copy link
Copy Markdown
Member

One last request before merge -- squash all commits into one. Thanks!

@joergdietrich

Copy link
Copy Markdown
Contributor Author

Use the squash button when merging? I know it will take me too much time to figure out how to squash commits already pushed upstream.

@pllim

pllim commented Apr 11, 2016

Copy link
Copy Markdown
Member

Haha... Funny you brought it up. There was a lengthy discussion about that button, resulting in it being disabled. (See #4423 for part of that discussion and astropy-dev mailing list for the rest.)

@joergdietrich

Copy link
Copy Markdown
Contributor Author

Well, your choice. I don't have the time for another fight with git.

@aconley

aconley commented Apr 11, 2016 via email

Copy link
Copy Markdown
Contributor

pllim pushed a commit to pllim/astropy that referenced this pull request Apr 11, 2016
pllim pushed a commit to pllim/astropy that referenced this pull request Apr 11, 2016
@pllim

pllim commented Apr 11, 2016

Copy link
Copy Markdown
Member

@aconley , I was going to do it for him but turns out it is quite painful because halfway through the commits, he merged master into his PR. So rebase -i goes wild. I am going to do a brute-force squashing by copying his changes to my own branch and then merge that one. PR coming soon.

@pllim

pllim commented Apr 11, 2016

Copy link
Copy Markdown
Member

I am closing this one now. See #4769 for the squashed version. I eyeballed the changes and made sure I didn't introduce anything new, and added @joergdietrich as the author, so everything should be okay...

@pllim pllim closed this Apr 11, 2016
pllim added a commit that referenced this pull request Apr 11, 2016
Angular diameter distance for positive curvature (#4723)
ntessore pushed a commit to glass-dev/cosmology that referenced this pull request Dec 7, 2020
ntessore pushed a commit to glass-dev/cosmology that referenced this pull request Dec 7, 2020
ntessore pushed a commit to glass-dev/cosmology that referenced this pull request Dec 8, 2020
ntessore pushed a commit to glass-dev/cosmology that referenced this pull request Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Affects-dev PRs and issues that do not impact an existing Astropy release cosmology Duplicate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants