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
Allow fraction and inline options for unit.to_string() #14449
Allow fraction and inline options for unit.to_string() #14449
Conversation
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
👋 Thank you for your draft pull request! Do you know that you can use |
65b8eb0
to
9c97e24
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another nice improvement. I like seeing methods exchanged for super()
.
Also, is this the right venue to change to tuple
? Sorry to keep banging on this drum, but this is one of many many ways we can speed up the unit internals.
We should consider implementing these extra options as components in unit format names, i.e.
|
@eerovaher - @olebole and I discussed this in his initial PR, which introduced I also prefer not to tie our hands too much in how we use |
9c97e24
to
5d9b61f
Compare
OK, encouraged by @nstarman's sense, I tried having only a Obviously, if we agree on either direction, I'll squash/rebase appropriately. |
5d9b61f
to
f1517fc
Compare
I think it's important that the already existing Another thing to consider, beyond the scope of this pull request but very much related to the idea of consistency between unit formats, is that because for |
@eerovaher - I agree that we should be careful before changing how we do quantity formatting (unit formatting is rarely useful on its own). For the regular user, all we do here is to ensure that quantities formatted as On inline for latex by default: right now, quantities use the equivalent of astropy/astropy/units/quantity.py Line 1532 in b5b3f40
But for this PR, probably best to stick to the premise that it does not change things for the two most common cases, of using an notebook (and thus |
OK, with all your help, I think this PR to introduce the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is great work! I like it as well!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this is a large improvement. My comments are minor and mostly about adding more docstring. Feel free to ignore the code speed bumps.
astropy/units/core.py
Outdated
- `False` : display unit bases with negative powers as they are; | ||
- 'inline' or `True` : use a single-line fraction; | ||
- 'multiline' : use a multiline fraction (available for the | ||
``latex``, ``console`` and ``unicode`` formats only). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
``latex``, ``console`` and ``unicode`` formats only). | |
``"latex"``, ``"console"`` and ``"unicode"`` formats only). |
Also, are these available as astropy.units.format.Base
objects?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, should be strings. The formats are documented at https://docs.astropy.org/en/latest/units/index.html#module-astropy.units.format, but the class docstring are not really useful as is -- clearly, only used indirectly... Anyway, I'll leave them as strings for now!
parts = [] | ||
if m not in ("", "1"): | ||
parts.append(m) | ||
if ex: | ||
if not ex.startswith("-"): | ||
ex = "+" + ex | ||
parts.append(f"10{cls._format_superscript(ex)}") | ||
return cls._times.join(parts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parts = [] | |
if m not in ("", "1"): | |
parts.append(m) | |
if ex: | |
if not ex.startswith("-"): | |
ex = "+" + ex | |
parts.append(f"10{cls._format_superscript(ex)}") | |
return cls._times.join(parts) | |
if ex and not ex.startswith("-"): | |
ex = "+" + ex | |
return cls._times.join( | |
((m,) if m in ("", "1") else ()) | |
+ ((f"10{cls._format_superscript(ex)}",) if ex else ()) | |
) |
Should be a mild speed bump as it reduces intermediates and extending lists
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes it quite a bit less readable, and not obviously faster given the creation and addition of tuples... Since it is not in a fast path anyway, I prefer to just stick with what was there...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM.
909ccaf
to
242b236
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mhvk!
This pull request continues the work started by @olebole in #14393 in giving the user options for how to represent a unit in a given format. It deviates from #14393 in using
fraction
as the primary option argument, to indicate whether or not bases raised to negative powers should be shown as is (fraction=False
) or a fraction should be used (fraction=True
).In addition, aninline
option allows the choice between using a solidus (inline=True
) or a displayed fraction (inline=False
). The latter is only available forlatex
,console
, andunicode
formats.EDIT: I changed to follow option 1 below and only have a
fraction={False|True|'inline'|'display'}
argument.Opening as a draft since there are some implementation questions:
fraction
option, with possible valuesFalse
,'inline'
and'display'
, withTrue
as a short-cut tothe default fraction style'inline'
. This is more concise and perhaps better (I'm wavering!);inline
option is ignored for formats that do not support it. Should it raise an error instead? EDIT: yes, since machine-readable formats should be strict about what they allow, and raise if an option would make the format no longer readable.To do:
p.s. Just in case: the
inline
option was just introduced and is only in-dev
- hence the replacement of the #14393 changelog fragment with a new one (EDIT: and theskip-changelog-checks
entry since that is misled by the deletion of14393.feature.rst
).p.s.2 For my own convenience, on top of #14437; will rebase once that is in.