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

Make hms/dms namedtuples and add absdms #1988

Merged
merged 12 commits into from Jan 28, 2014
Merged

Conversation

eteq
Copy link
Member

@eteq eteq commented Jan 22, 2014

This small change adjusts Angle so that the hms and dms attributes yield namedtuples instead of regular tuples. That enables usage along these lines:

'{0.h} {0.m} {0.s:.3f}'.format(coordinate.hms)

It also adds an absdms attribute that gives the absolute value of the dms elements. This is valuable for string formatting purposes (this was prompted by an on-list discussion about how to make IAU-style formatted strings.) Note that there is no abshms because I know of know convention where hour angle is negative. But it could be easily added in.

@mdboom
Copy link
Contributor

mdboom commented Jan 22, 2014

👍

@@ -217,7 +222,7 @@ def hms(self):
The angle's value in hours, as a ``(h, m, s)`` tuple
Copy link
Member

Choose a reason for hiding this comment

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

You could be more precise in the description, e.g. "tuple" -> "named tuple" or even "tuple" -> "(h, m, s) tuple" -> "named tuple with (h, m, s) members".

@cdeil
Copy link
Member

cdeil commented Jan 23, 2014

👍

>>> a.dms
(57.0, 17.0, 44.806247...)
DMS_tuple(d=57.0, m=17.0, s=44.806247...)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add an example with a negative declination here or to the Angle.dms docstring?
I was surprised that m and s are negative in addition to d in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

an excellent idea! done in the version I'll push up soon

@cdeil
Copy link
Member

cdeil commented Jan 23, 2014

I've written a function to create IAU designation strings here that covers my needs:
https://github.com/gammapy/gammapy/pull/64/files

Comments on how to improve the implementation are absolutely welcome.

If you want this for astropy now (as a coordinate member function?) let me know and I'll give it a try and make it more general / add more tests / docs.

I did not find the Angle.absdms method useful in the implementation, because I need d with sign, but m and s without sign. So I'm not sure it's a useful addition to Angle.

This is primarily intented for use with `dms` to generate string
representations of coordinates that are correct for negative angles.
"""
d, m, s = util.degrees_to_dms(self.degree)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would take the absolute value of self.degree rather than the absolute value of what is returned by the function.

Copy link
Member Author

Choose a reason for hiding this comment

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

good point - I was concerned some implementations might come out subtly different, but right now it comes out the same, which is what really matters (fixed in a version I'm about to psuh up)

@mhvk
Copy link
Contributor

mhvk commented Jan 23, 2014

Like the idea, but wonder about absdms, as I feel it is better to always return something that represents the actual value (people can do abs(angle).dms already). How about something like sdms or signed_dms, which returns a 4-part tuple in which the first item is the sign. This would also cover @cdeil's usage. (Actually, I would even consider to have dms return that, but perhaps that it just because I cannot see how I would use three negative numbers.)

@mdboom
Copy link
Contributor

mdboom commented Jan 23, 2014

👍 to the idea of a function that returns (sign, d, m, s) (where d, m and s are all positive).

The current behavior is so that the obvious mathematical expression works:

d + (m / 60.0) + (s / 360.0)

If, for example, only d were signed, you'd have to do something like:

d + (m / 60.0) * sign(d) + (s / 360.0) * sign(d)

In short, dms is mathematically-convenient, not formatting-convenient.

@cdeil
Copy link
Member

cdeil commented Jan 27, 2014

Here's the coordinate_iau_format utility function I came up with:
https://gammapy.readthedocs.org/en/latest/api/gammapy.catalog.utils.coordinate_iau_format.html
https://github.com/gammapy/gammapy/blob/master/gammapy/catalog/utils.py
https://github.com/gammapy/gammapy/blob/master/gammapy/catalog/tests/test_utils.py

For the declination I ended up using this code, which could be made a bit more readable (by using tuple names instead of indices) and maybe simpler (if sign becomes available as an extra field or maybe even d and m are returned as int instead of float) if this PR gets merged.

dec_sign = '+' if dec.deg >= 0 else '-'
dec_d = int(abs(dec.dms[0]))
dec_m = int(abs(dec.dms[1]))
dec_s = abs(dec.dms[2])

@eteq
Copy link
Member Author

eteq commented Jan 27, 2014

Good idea, @mhvk - I like the signed_dms approach better, tool. I know there was debate at one point suggesting this be how dms works, but having two in the way your suggesting nicely works around that. So I just pushed up a new version that implements it that way. (and @cdeil, does that change look good to you, too?)

@@ -12,6 +12,10 @@ New Features

- ``astropy.coordinates``

- Updated `Angle.dms` and `Angle.hms` to return `namedtuple`s instead of
regular tuples, and added `Angle.absdms` attribute that gives the absolute
Copy link
Member

Choose a reason for hiding this comment

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

Change absdms -> signed_dms

@mhvk
Copy link
Contributor

mhvk commented Jan 28, 2014

Apart from my minor comment about the documentation (and @cdell's corrections), this looks good!

@@ -23,6 +24,11 @@

TWOPI = math.pi * 2.0 # no need to calculate this all the time

# these are used by the `hms` and `dms` attributes
HMS_tuple = namedtuple('HMS_tuple', ('h', 'm', 's'))
Copy link
Member

Choose a reason for hiding this comment

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

Just a minor question - is there a reason why not use hms_tuple, dms_tuple, and signed_dms_tuple for the names of the tuple types?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really. I was thinking about them as something sort of like classes, and hence the PEP8 first-letter-cap convention led to those. It's pretty ambiguous, though, so if you think lower-case is better, I'm happy to do that instead, @astrofrog .

Copy link
Member

Choose a reason for hiding this comment

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

I think the reason I felt unsure is because currently it seems like a mix of the two conventions. I'd either go the full way and treat it as a class (HMSTuple) or use the normal variable convention (hms_tuple). I have no strong preference :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I see your point. In that case I mildly prefer hms_tuple so I'll switch it all to the attribute/variable-style convention.

@eteq
Copy link
Member Author

eteq commented Jan 28, 2014

Alright, I think I've implemented all the suggestions here, so if the tests pass I'll go ahead and merge this later today.

@eteq
Copy link
Member Author

eteq commented Jan 28, 2014

Oh, and @cdeil - The coordinate_iau_format is definitely something we'll want in Astropy, but I think it belongs in the SkyCoordinate that's discussed in APE5. So once APE5 is sorted out and we start implementing it, that's a great place to put it.

@eteq
Copy link
Member Author

eteq commented Jan 28, 2014

Tests passing, so I'll go ahead and merge. Thanks all of you for the valuable feedback!

eteq added a commit that referenced this pull request Jan 28, 2014
Make hms/dms namedtuples and add `signed_dms`
@eteq eteq merged commit 96f4dd4 into astropy:master Jan 28, 2014
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.

None yet

5 participants