Skip to content

Quantities should always allow comparison with zero - #1254

Merged
mdboom merged 4 commits into
astropy:masterfrom
mhvk:units-zero-always-ok
Nov 4, 2013
Merged

mdboom merged 4 commits into
astropy:masterfrom
mhvk:units-zero-always-ok

Conversation

@mhvk

@mhvk mhvk commented Oct 7, 2013

Copy link
Copy Markdown
Contributor

Also while working on #929 (where I needed to intercept a where(q==0,...), I noticed that comparisons of the form q==0, q>0 raise an exception unless q is dimensionless. They probably shouldn't as this has a well-defined answer (as 0 can have any unit it wants!). Since quantity is so much in flux right now, I won't do a PR, but just note that one way of simplifying this is by defining a difference function which returns a plain array for testing in the various comparisons. E.g., things can be made to work with.

    def _diff(self, other, raise_on_incompatible=True):
        try:
            return self.value - other.to(self.unit).value
        except AttributeError:
            if np.all(other*0. == other):
                return self.value
            try:
                return self.to(1).value - other
            except(UnitsException,TypeError):
                if raise_on_incompatible:
                    raise TypeError("Quantity object cannot be compared to an "
                                    "object of type {0}"
                                    .format(other.__class__))
        except UnitsException:
            if raise_on_incompatible:
                raise UnitsException("Can only compare quantities with "
                                     "compatible dimensions.")
        return False

(the raise_on_incompatible=False option is there for ==, which should simply return False in this case).

@embray

embray commented Jul 12, 2013

Copy link
Copy Markdown
Member

This sounds reasonable to me, though I'm not sure--could definitely use further discussion.

@mdboom

mdboom commented Jul 26, 2013

Copy link
Copy Markdown
Contributor

Yeah -- seems reasonable. We've deliberately excluded offset units (such as Celcius and Fahrenheit would require to convert to Kelvin), so this assertion should hold true.

What's the use case, however? Why not just require the slightly more pedantic case of putting a unit on 0, which gives the added benefit of checking that the unit is compatible?

@mhvk

mhvk commented Jul 26, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - the use case was that I was trying to make some more numpy functions work, and realised that np.sinc did not work because internally it does an np.where(q==0,...) (to avoid a division by zero), which throws an error when comparing q to 0 (see first message of this issue). I then thought the comparison is generally valid, so we might as well allow it. Indeed, it temporarily entered the general unit check for two-argument ufunc's, and could trivially go back there. But I also see the logic of just being a bit more pedantic and insisting on 0 also having units until a better use case pops up.

@mhvk

mhvk commented Sep 21, 2013

Copy link
Copy Markdown
Contributor Author

Still feel this is sensible, but no hurry.

@taldcroft

Copy link
Copy Markdown
Member

Since this isn't a priority for 0.3, I'm reassigning to 0.4.

@astrofrog

Copy link
Copy Markdown
Member

After implementing support for astropy.units into one of my codes, I can definitely see the value for this and also for comparing with unitless np.inf and -np.inf. If feel silly to have to do e.g.

q[2] = 0. * u.m
q[3] = np.inf * u.cm

since the units are irrelevant.

@mhvk

mhvk commented Oct 7, 2013

Copy link
Copy Markdown
Contributor Author

@astrofrog - I still had my "zero can have arbitrary units" PR around, so now updated it to include inf and nan. With this, your examples work. The PR includes some test cases for comparisons, but not much beyond it. PRs to my PR most welcome.

@mhvk

mhvk commented Oct 18, 2013

Copy link
Copy Markdown
Contributor Author

@astrofrog - no hurry, but if you have time to look at this comparison-with-zero/inf PR, that would be great.

@astrofrog

Copy link
Copy Markdown
Member

Will do later today hopefully, and @mdboom should look too.

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

It might make more sense to include np.all here, rather than place the burden on the callers of this function.

Also, maybe add a docstring. (I know it's private, but it might not be clear what its purpose is a few months from now).

@mdboom

mdboom commented Oct 18, 2013

Copy link
Copy Markdown
Contributor

Looks good, aside from my minor comments.

@mhvk

mhvk commented Oct 18, 2013

Copy link
Copy Markdown
Contributor Author

@mdboom - thanks, all comments implemented.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mhvk - can you explain these changes?

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.

Zero has become special with this PR: previously, the assertion was correct since c.x[0], which has units of kpc, is not equal to 0, which does not have these units. However, with the present PR, 0 can have any units, and thus one does have c.x[0] == 0

(I must admit that I'm not 100% sure that the above was the intent of the test...)

@mhvk

mhvk commented Nov 3, 2013

Copy link
Copy Markdown
Contributor Author

@astrofrog, @mdboom - looking over old branches, I encountered this one. I think I had all your comments addressed, so it should be good to go. (I rebased on current master to make it mergeable, so travis will run once more as well.)

mdboom added a commit that referenced this pull request Nov 4, 2013
Quantities should always allow comparison with zero
@mdboom
mdboom merged commit 9acd4cf into astropy:master Nov 4, 2013
@mhvk
mhvk deleted the units-zero-always-ok branch November 4, 2013 21:26
@embray

embray commented Nov 5, 2013

Copy link
Copy Markdown
Member

If this is merged shouldn't it be 0.3?

@mdboom

mdboom commented Nov 5, 2013

Copy link
Copy Markdown
Contributor

Yes, 0.3.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants