Skip to content

xref physical types#11595

Merged
adrn merged 13 commits into
astropy:mainfrom
nstarman:xref_physical_type
Apr 30, 2021
Merged

xref physical types#11595
adrn merged 13 commits into
astropy:mainfrom
nstarman:xref_physical_type

Conversation

@nstarman

@nstarman nstarman commented Apr 18, 2021

Copy link
Copy Markdown
Member

Now that #11118 is in (🥳 ), it's time for followups. One of the more interesting followups came from discussion with @adrn on methods to annotate physical types for Quantity and the like. @namurphy's great #11204 is in and #11606 is done, so I thought I'd give this a go.

See https://astropy--11595.org.readthedocs.build/en/11595/ for the compiled docs.

Part of #11555
Requires : #11618 (done)

What this does (edited)

  • convert the name of each physical type in the table of physical types in the docs to an ReST link target. These can be referenced (with sphinx :ref:) both in the docs and in parameters of docstrings by...
  • Adding the link target to the target map in docs/conf.py::numpydoc_xref_aliases.
  • An explanation is added to the glossary.rst
  • implements a non-exhaustive addition of physical type annotations in the docs. Some may be wrong!

Example

Screen Shot 2021-04-27 at 17 46 04

Links to the appropriate row in

Screen Shot 2021-04-27 at 17 46 31

And the best part is that the link just looks like ... ['length']!

Subpackage review sign-off sheet

@adonath @adamginsburg @StuartLittlefair @astrojuanlu @hamogu @dhomeier @saimn @matteobachetti @eslavich @tboch @tomdonaldson @perrygreenfield @mwcraig @taldcroft @adrn @mhvk @pllim @bsipocz @eteq @larrybradley @Cadair @astrofrog @mcara @nden

Please check and add your github handle after an approving review

@pep8speaks

pep8speaks commented Apr 18, 2021

Copy link
Copy Markdown

Hello @nstarman 👋! It looks like you've made some changes in your pull request, so I've checked the code again for style.

There are no PEP8 style issues with this pull request - thanks! 🎉

Comment last updated at 2021-04-30 15:16:26 UTC

Comment thread astropy/units/physical.py Outdated
Comment thread docs/conf.py Outdated
Comment thread astropy/units/physical_types.csv Outdated
@mhvk

mhvk commented Apr 19, 2021

Copy link
Copy Markdown
Contributor

@nstarman - I haven't looked in detail yet, but about storage I wonder whether you considered repurposing the way units are represented in the docs, which is basically by automatically generating the docstring for each module (see generate_units_summary in utils.py). I think I'd prefer that over a .csv file.

Regardless, maybe best to do that kind of docs change as a separate PR from using the physical types in the docs?

On that: would #10655 enable moving towards allowing ~astropy.units.Quantity['length']?

@nstarman

Copy link
Copy Markdown
Member Author

#not_v4.3

@mhvk

mhvk commented Apr 21, 2021

Copy link
Copy Markdown
Contributor

See #11618 for just getting the physical types as a table in the docs.

@nstarman

Copy link
Copy Markdown
Member Author

Great. I'll label this as requiring 11618, and wait until that is merged. This PR will expand upon 11618 to add ReST link targets in the table.

@nstarman nstarman marked this pull request as draft April 26, 2021 01:05
@nstarman nstarman force-pushed the xref_physical_type branch from cca95a2 to 01d41c1 Compare April 27, 2021 04:58
@nstarman nstarman marked this pull request as ready for review April 27, 2021 05:52
@nstarman

nstarman commented Apr 27, 2021

Copy link
Copy Markdown
Member Author

Ok. Trying to figure out how to get rid of that ugly spare column in the physical types table, but the link targets are up and running!
Now for adding docstring references all over the place.

@nstarman nstarman changed the title xref-physical_types xref physical types Apr 27, 2021
Comment thread astropy/units/physical.py Outdated
@nstarman nstarman force-pushed the xref_physical_type branch 2 times, most recently from 8d25e18 to a5f1e04 Compare April 27, 2021 21:16
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
@nstarman nstarman force-pushed the xref_physical_type branch from a5f1e04 to ebd861b Compare April 27, 2021 21:31
@nstarman nstarman requested a review from mhvk April 27, 2021 22:00
@nstarman nstarman force-pushed the xref_physical_type branch from ebd861b to 1cda3ce Compare April 28, 2021 00:00
nstarman and others added 10 commits April 30, 2021 11:14
- Also fix parameter type for angular speed parameters
- Add [‘dimensionless’] to altaz’s relative_humidity — from review by @astrofrog

Co-authored-by: Adrian Price-Whelan <adrianmpw@gmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Co-authored-by: Adrian Price-Whelan <adrianmpw@gmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Co-authored-by: Adrian Price-Whelan <adrianmpw@gmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
@nstarman nstarman force-pushed the xref_physical_type branch from 5534afe to 6a7da08 Compare April 30, 2021 15:15
@namurphy

Copy link
Copy Markdown
Contributor

This PR looks great! I can't wait for us to be able to do the same thing for PlasmaPy.

@adrn adrn left a comment

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.

Very minor nitpick, otherwise ready to merge! (pending RTD check)

Comment thread docs/glossary.rst Outdated
@adrn

adrn commented Apr 30, 2021

Copy link
Copy Markdown
Member

@namurphy your comment makes me wonder: @nstarman are the xref physical types here usable by affiliated packages through intersphinx?

Would be good to remember that we should probably document this PR in two places: (1) in the documentation part of the contributing guide (here?) to remind documentation contributions to include physical types when quantities/units are involved, and (2) to guide affiliated package maintainers with how to use xref physical types in their own docs, either through intersphinx or by copy-pasting the relevant part of the conf.py file modified here. Make a new issue to keep track of these things (for a follow-up)?

@nstarman

Copy link
Copy Markdown
Member Author

@namurphy your comment makes me wonder: @nstarman are the xref physical types here usable by affiliated packages through intersphinx?

That's a good question. They should be according to https://docs.readthedocs.io/en/stable/guides/intersphinx.html. One of my goals is to improve the overall ease of intersphinxing with astropy and using the glossary and xref aliases. Part of that goal is to move some of this and other code to sphinx_astropy, and pre-set up the inter sphinx... but until then, something like the following might work for numpydoc_xref in affiliate packages:

  1. add astropy to intersphinx
  2. map in your conf.py
from astropy.units.physical import _units_and_physical_types
numpydoc_xref_physical_type_aliases = {}
for _, ptypes in _units_and_physical_types:
    ptypes = {ptypes} if isinstance(ptypes, str) else ptypes
    for ptype in ptypes:
        key = f"'{ptype}'"
        val = f":ref:`:ref: '{ptype}' <astropy:{ptype}>`"   # <== intersphinxed link here
        numpydoc_xref_physical_type_aliases[key] = val

numpydoc_xref_aliases.update(numpydoc_xref_physical_type_aliases)

Would be good to remember that we should probably document this PR in two places: (1) in the documentation part of the contributing guide (here?) to remind documentation contributions to include physical types when quantities/units are involved, and (2) to guide affiliated package maintainers with how to use xref physical types in their own docs, either through intersphinx or by copy-pasting the relevant part of the conf.py file modified here. Make a new issue to keep track of these things (for a follow-up)?

👍 #11675

Comment thread docs/glossary.rst Outdated
Co-authored-by: Adrian Price-Whelan <adrianmpw@gmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
@nstarman nstarman force-pushed the xref_physical_type branch from 9f03106 to cc9fd90 Compare April 30, 2021 17:58
@namurphy

Copy link
Copy Markdown
Contributor

@nstarman — thank you! I've got too much pandemic brain fog to think much about it at the moment, but that looks like a good way to go about it for the time being. I'm hoping to look into this more after 4.3 is released.

A slight hesitation is that _units_and_physical_types is not part of Astropy's public API, so I'm wondering if it would be worthwhile to put a comment in physical.py that says not to change the name of that dict since it may end up being used in other packages for this purpose.

cc: @rocco8773

@nstarman

nstarman commented Apr 30, 2021

Copy link
Copy Markdown
Member Author

A slight hesitation is that _units_and_physical_types is not part of Astropy's public API, so I'm wondering if it would be worthwhile to put a comment in physical.py that says not to change the name of that dict since it may end up being used in other packages for this purpose.

@namurphy, my thought for other packages is to essentially move the short script (if @adrn or others confirm it works) in #11595 (comment) to sphinx_astropy and export numpydoc_xref_physical_type_aliases (perhaps renamed to include "astropy") into v1 so that in 3rd party packages' conf.py it gets imported. Then anyone who wants the physical-type xrefs can do numpydoc_xref_aliases.update(numpydoc_xref_physical_type_aliases). If they wanted different names, e.g. ['astropy-length'] instead of ['length'] if they support more than one unit package, then that can be done by
numpydoc_xref_aliases.update({"'astropy-"+k[1:]: v for k, v in numpydoc_xref_physical_type_aliases.items()}).
This would hide _units_and_physical_types and make xref plugin quite easy. I think implementing #11595 (comment) directly in 3rd party packages should remain an experimental implementation and the next release of sphinx_astropy should codify the implementation for 3rd party packages.

Alternatively/additionally, a good addition to physical.py might be to add each physical type to the module (like how cosmology realizations are added in cosmology/realizations.py) and have a set of all their names.
available_physical_types= _name_physical_mapping.keys() would conveniently stay updated as people define new physical types. Were this implemented, then that would change #11595 (comment) and this PR to use all publicly-scoped functions.


astropy/sphinx-astropy#39

@adrn

adrn commented Apr 30, 2021

Copy link
Copy Markdown
Member

@namurphy I think using _units_and_physical_types is a temporary solution: we would ideally make it easier for other packages to get access to the physical type definitions directly in the future. So maybe better to put the comment in your docs :)

Edit: As I clicked comment here, I saw @nstarman commented above - yes to what he says!

@adrn

adrn commented Apr 30, 2021

Copy link
Copy Markdown
Member

I think this is ready. Thanks again @nstarman - this is a great addition to the documentation!

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.

8 participants