Skip to content

Cosmology units - #1237

Merged
mdboom merged 6 commits into
astropy:masterfrom
aconley:cosmology_units
Jul 26, 2013
Merged

mdboom merged 6 commits into
astropy:masterfrom
aconley:cosmology_units

Conversation

@aconley

@aconley aconley commented Jul 8, 2013

Copy link
Copy Markdown
Contributor

Address issue #406 by adding units to astropy.cosmology. Passes all unit tests on python3.2. Unfortunately, I don't have a working sphinx installation, so I haven't been able to test if the doc update worked.

Comment thread astropy/cosmology/core.py Outdated

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.

You could replace this line with

self._hubble_distance = (1 / self._H0).to(u.Mpc)

And then remove c_kms completely.

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.

That doesn't work -- not surprisingly, since H0 is inverse time units, not distance -- unless there has been some change to units since I branched so that it automatically inserts factors of c as needed.

H0 = 70 * u.km / u.s / u.Mpc
(1/H0).to(u.Mpc)
ERROR: UnitsException: 'Mpc s / km' (time) and 'Mpc' (length) are not convertible [astropy.units.core]

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.

Sorry, my mistake. I should have written

self._hubble_distance = (const.c / self._H0).to(u.Mpc)

which should then work, and you can still remove c_kms.

@nhmc

nhmc commented Jul 11, 2013

Copy link
Copy Markdown
Contributor

Apart from the comments above, this looks great to me.

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 know if we do this now or as a follow-on, but we should probably consider making Distance a Quantity subclass (as we have done for Angle in #1006). This would give make most of the properties that convert the value to other units redundant (i.e. they could be removed), as well as make conversions much easier.

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.

Probably a good idea... but in a different pull request.

@mdboom

mdboom commented Jul 16, 2013

Copy link
Copy Markdown
Contributor

Cool -- this is probably the most major use of the units API thus far, and it's nice to see how it plays out in real code. Reading this, I still don't like the order dependence of the __mul__ operator, but it's the least of all evils, I guess.

Thanks -- these are all mostly minor comments. We probably can (and should) put off inheriting Distance from Quantity in a subsequent pull request.

@aconley

aconley commented Jul 16, 2013

Copy link
Copy Markdown
Contributor Author

Ok, all done except the Distance subclassing, which I think belongs elsewhere.

distmod does return a u.mag Quantity rather than just a floating point number (or ndarray). As discussed elsewhere (#406) this isn't very useful right now, but, hey, maybe at some future date it will be.

@astrofrog

Copy link
Copy Markdown
Member

@aconley - now that #929 is merged, the order of the operations should no longer matter. Could you rebase this and make sure that the tests are still passing, since the Quantity class underwent significant refactoring in #929?

Alex Conley added 6 commits July 26, 2013 10:07
* Distances, etc. now returned as astropy.units.Quantity objects
* Some cosmological variables (Tcmb0, H0, etc.) stored as quantities
* Unit tests updated to reflect use of quantities.
* coordinates.Distance makes use of cosmology object, so those uses
  updated.
* Following suggestion by @nhmc, use constants.c directly instead
  of c_kms
* For consistency, distmod also returns a quantity object.
* Mostly minor documentation and line break changes.
* use Quantity(blah, u.Mpc) instead of Quantity(blah, 'Mpc')
@aconley

aconley commented Jul 26, 2013

Copy link
Copy Markdown
Contributor Author

@astrofrog -- done. It passes all the cosmology related tests on python 2.7 and 3.2.

@mdboom

mdboom commented Jul 26, 2013

Copy link
Copy Markdown
Contributor

Can you file a new issue for the breakage on 3.2? Travis is testing master on 3.2 just fine at the moment, so I suspect there is some environmental difference going on...

EDIT: request -> issue

@aconley

aconley commented Jul 26, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom: I was just in the process of doing that -- but now I can't seem to replicate it. Maybe some cruft left over from a previous build? Anyways, ignore that.

mdboom added a commit that referenced this pull request Jul 26, 2013
@mdboom
mdboom merged commit 94f9553 into astropy:master Jul 26, 2013
ntessore pushed a commit to glass-dev/cosmology that referenced this pull request Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants