Skip to content

Units equivalency context manager -- mostly for dimensionless angles - #1593

Merged
mdboom merged 23 commits into
astropy:masterfrom
mhvk:units-angles-dimensionless-context
Oct 24, 2013
Merged

mdboom merged 23 commits into
astropy:masterfrom
mhvk:units-angles-dimensionless-context

Conversation

@mhvk

@mhvk mhvk commented Oct 17, 2013

Copy link
Copy Markdown
Contributor

This is a follow-up on #1161, which introduced an equivalency that allows radian to be dimensionless. That PR still required the equivalency to be explicitly given, which does not address all uses (in particular, the example below). The present PR adds equivalencies to the UnitRegistry class, and with that the ability to set an equivalency context. E.g.,

import astropy.units as u; import numpy as np
with u.set_enabled_equivalencies_context(u.angles_dimensionless()):
    omega = u.cycle/(2.*u.s)
    t = 1.*u.min
    c = np.exp(1j*omega*t)
c

yields

<Quantity (1-2.1558735510086122e-14j) >

@mdboom: one change beyond introducing, testing, and documenting this, is that the default for equivalencies for all user-facing methods is changed to None, so that [] can be used to override any global equivalencies in effect.

Comment thread astropy/units/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.

This will also reset the enabled units. I think we only want to set self._equivalencies = set() here. Likewise set_enabled_units will reset the enabled equivalencies. These should be independent.

@mhvk

mhvk commented Oct 17, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - I do think it is important to be able to turn off the context temporarily (and this is needed in decompose to avoid roundtrip). but we could do it the other way around: we could always add to the list and have the default remain equivalencies=[]; then, we could newly determine that equivalencies=None means "never use equivalencies at all". Maybe I just go ahead and do that... (as well as the reset part).

@mhvk

mhvk commented Oct 17, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - OK, I changed None vs [], and now add global equivalencies by default. Definitely cleaner

In addition, I thought I should really pre-normalize equivalencies in the registry -- which was a bit of a can of worms, since Unit does not yet exist when UnitsRegistry is defined. I ended up making _normalize_equivalency a function outside of the Unit class, with an call to it from within that also adds the global equivalencies if required. Hope this makes sense.

Comment thread docs/units/equivalencies.rst 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.

This looks good, but I think it also makes sense to document the set_enabled_equivalencies functions at the top level of this file. It one wanted to do it for another equivalency than this one, it might be hard to find it here.

@mdboom

mdboom commented Oct 17, 2013

Copy link
Copy Markdown
Contributor

Apart from my documentation comment, I think this is looking quite good.

@mhvk

mhvk commented Oct 17, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - yes, indeed, much better -- especially if equivalency moves out of quantity, this will be much more generally useful. Now done (plus some clean-up of that page; sorry if that makes it quite a bit less easy to check).

Comment thread docs/units/equivalencies.rst 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.

the default "off"

@mdboom

mdboom commented Oct 17, 2013

Copy link
Copy Markdown
Contributor

That doc looks much better. I think this is in good shape, and as you say, makes the removal of equivalencies from Quantity feasible, since there's something good to replace it with.

@mhvk

mhvk commented Oct 18, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - OK, thanks! (and spelling corrected).

Comment thread docs/units/equivalencies.rst 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.

I think this is causing the Travis tests to fail. Since this is an external reference, I think it needs to look like:

`supplementary to derived unit <http://www.bipm.org/en/CGPM/db/20/8/>`__

Likewise for the other links below.

@mhvk

mhvk commented Oct 19, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom, @astrofrog - the travis sphinx failure would seem to be related to modelling rather than this PR. Perhaps a problem with a random number? I thought I'd pass it on rather than uploading a no-op change to get it to rebuild.

@mdboom

mdboom commented Oct 21, 2013

Copy link
Copy Markdown
Contributor

@mhvk: At this point, this needs a rebase anyway. Would you mind doing that? That should also trigger a rebuild of Travis...

@mhvk

mhvk commented Oct 22, 2013

Copy link
Copy Markdown
Contributor Author

@astrofrog, @mdboom - I rebased and pushed but Travis doesn't seem to start. Any idea?

Note: somewhat sillily, I rebased this one rather than #1161; do you want to consider both together here? I'm not sure how to get #1161 in order without redoing the edits.

@astrofrog

Copy link
Copy Markdown
Member

@mhvk - I think Travis was just under heavy load. I restarted the build manually.

@mhvk

mhvk commented Oct 23, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - with some hopefully final corrections, the documentation will now hopefully build; I hope this can still be merged before #1662...

@mhvk

mhvk commented Oct 23, 2013

Copy link
Copy Markdown
Contributor Author

Finally managed to get the context manager documentation bug-free as well. @mdboom: would you consider this to be ready to go in?

Comment thread docs/units/equivalencies.rst 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.

Maybe that helps convert between angles and dimensionless units? Otherwise the sentence feels a bit incomplete.

@mdboom

mdboom commented Oct 23, 2013

Copy link
Copy Markdown
Contributor

This looks really good and ready to merge, aside from the very minor comments.

However... we're working on top of each other a bit today. I just filed #1662 which simplifies the use of the context manager and combines add_enabled_units and add_enabled_units_context into one. I wonder if you wouldn't mind rebasing this branch on that branch and doing the analogous simplification for the equivalencies context managers. (Or I can do that as a PR against this one once #1662 is on master and this has been rebased on master). I just think those context managers are really ugly if you can't use them both ways.

@mhvk

mhvk commented Oct 23, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - I tried rebasing and while I am not yet done, it seems less bad than I feared. In any case, fine to go ahead as far as #1662 is concerned.

@mhvk

mhvk commented Oct 23, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - OK, it was not bad at all -- I do like the new way of doing it, much cleaner! Note that I took the liberty of adding some comments also to your set_enable_units, etc., as it did take me a while to figure out what was going on.

Comment thread astropy/units/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.

equivalencies=equivalencies ?

@mdboom

mdboom commented Oct 24, 2013

Copy link
Copy Markdown
Contributor

We're getting this doc error:

/home/travis/build/astropy/astropy/build/lib.linux-x86_64-2.7/astropy/units/core.py:docstring of astropy.units.core.set_enabled_equivalencies:26: ERROR: Inconsistent literal block quoting.

Other than that, I think this is good to go, and thanks for handling the 11th hour update to the less complicated context managers.

@mhvk

mhvk commented Oct 24, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - this finally passes everything. So, once #1161 and #1662 are in, this can go in as well.

@mhvk

mhvk commented Oct 24, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - p.s. you comment on the wrong initialisation in the _UnitRegistry made me realise that part should be tested as well. I now added tests that add_enabled_equivalencies and set_enabled_equivalencies do what they are supposed to.

@astrofrog

Copy link
Copy Markdown
Member

@mhvk - it looks like this needs rebasing?

@mhvk

mhvk commented Oct 24, 2013

Copy link
Copy Markdown
Contributor Author

@astrofrog - hmm, odd, maybe because of #1634? (actually, how does one see whether it needs rebasing?) Since #1161 and #1662 may also suffer from this, perhaps it is best for me to wait until those are in, and then rebase.

@astrofrog

Copy link
Copy Markdown
Member

@mhvk - if it says "We can’t automatically merge this pull request." it means you need to rebase. Sometimes it's something as simple as a CHANGE.rst addition conflicting.

@mhvk

mhvk commented Oct 24, 2013

Copy link
Copy Markdown
Contributor Author

@astrofrog - I don't see that -- but checking on my own repository, I do see it there, so I guess this is simply hidden from people who do not have merge rights. I guess the way around it is to simply try it locally, a la

git fetch upstream
git checkout upstream/master
git checkout -b merge-trial
git merge units-angles-dimensionless-context

Oddly enough, though, that actually works...

Anyway, as said, #1161 and #1662 should really go in first (unless you want to do it in one fell swoop). Since apparently I cannot test this locally, is #1161 OK?

@astrofrog

Copy link
Copy Markdown
Member

Oh that's annoying (only committers can see the message).

@mdboom
mdboom merged commit 0974796 into astropy:master Oct 24, 2013
@mdboom

mdboom commented Oct 24, 2013

Copy link
Copy Markdown
Contributor

Merged (manually).

@mhvk
mhvk deleted the units-angles-dimensionless-context branch October 24, 2013 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants