Skip to content

#12355 Issue: Register latex to Cosmology.write - #14701

Merged
nstarman merged 2 commits into
astropy:mainfrom
Mubin17:Mubin17/Issue#12355
May 17, 2023
Merged

nstarman merged 2 commits into
astropy:mainfrom
Mubin17:Mubin17/Issue#12355

Conversation

@Mubin17

@Mubin17 Mubin17 commented Apr 27, 2023 •

Copy link
Copy Markdown
Contributor

Description

This pull request is to address Issue #12355

Fixes #12355

I've added a new write_latex() method to the Cosmology package that allows you to export a Cosmology object to a Latex table.

Test:

from astropy.cosmology import Planck18
from astropy.cosmology.io import latex 

latex.write_latex(Planck18, "dd.tex", overwrite=True, latex_names=True)

Output:

\begin{table}
\begin{tabular}{cccccccc}
cosmology & name & $$H_0 
{[\mathrm{km\,s^{-1}\,Mpc^{-1}}]}$$ & Om0 & $$T_{0} 
{[\mathrm{K}]}$$ & Neff & $$m_{nu} 
{[\mathrm{eV}]}$$ & Ob0 \\
FlatLambdaCDM & Planck18 & 67.66 & 0.30966 & 2.7255 & 3.046 & 0.0 .. 0.06 & 0.04897 \\
\end{tabular}
\end{table}

Please let me know if you have any feedback or suggestions for improvements.

@Mubin17
Mubin17 requested a review from a team as a code owner April 27, 2023 12:00
@Mubin17
Mubin17 requested a review from nstarman April 27, 2023 12:00
@github-actions

Copy link
Copy Markdown
Contributor

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.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@pllim pllim added this to the v6.0 milestone Apr 27, 2023
@pllim

pllim commented Apr 27, 2023

Copy link
Copy Markdown
Member

Thanks! If this patch were to proceed, would need change log and test too. I'll let cosmology maintainer decide if it also need a what's new entry. But let's wait to hear back first.

@nstarman

nstarman commented Apr 27, 2023 •

Copy link
Copy Markdown
Member

@Mubin17, thanks for the PR. This is a very good start!
@pllim is correct that this needs a change-log and tests.
A few additional things

  1. The module latex needs to be imported in __init__.py. This is to enable Cosmology.write to pick up on registered methods.
  2. The write method need to be registered with Cosmology.write (see
    # ===================================================================
    # Register
    convert_registry.register_reader("mapping", Cosmology, from_mapping)
    convert_registry.register_writer("mapping", Cosmology, to_mapping)
    convert_registry.register_identifier("mapping", Cosmology, mapping_identify)
    for example). Currently this function can only be accessed through io, which is not documented / should be private.

I'm happy to keep reviewing as the PR matures.

Comment thread astropy/cosmology/io/latex.py Resolved
Comment thread astropy/cosmology/io/latex.py Resolved
Comment thread astropy/cosmology/io/latex.py Resolved
Comment thread astropy/cosmology/io/latex.py Resolved
@nstarman

nstarman commented May 2, 2023

Copy link
Copy Markdown
Member

Hi @Mubin17, is there any way I can help with this PR? Thanks again for working on this PR!

@Mubin17

Mubin17 commented May 3, 2023 •

Copy link
Copy Markdown
Contributor Author

@nstarman Apologies for being MIA the past couple of days, but I have updated the write_latex() method and it seems to be working fine now. Before I move on to tests I think I should add the read_latex() and latex_identify() methods to the Cosmology package as well. Please let me know if it is necessary or if I should jump on to tests. I came up with the code below by referencing the read_html_table().

def read_latex(filename, index=None, *, move_to_meta=False, cosmology=None,**kwargs):
    # Check that the format is 'ascii.html' (or not specified)
    format = kwargs.pop("format", "latex")
    if format != "latex":
        raise ValueError(f"format must be 'latex', not {format}")
    
    with u.add_enabled_units(cu):
        table = QTable.read(filename, format="latex", **kwargs)

    table_columns = set(table.colnames)
    for name, latex in _FORMAT_TABLE.items():
        if latex in table_columns:
            table.rename_column(latex, name)
            
    # Build the cosmology from table, using the private backend.
    return from_table(table, index=index, move_to_meta=move_to_meta, cosmology=cosmology)

However, upon execution, it throws this error:

ValueError: '.. 0.06' did not parse as unit: Syntax error parsing unit '.. 0.06' If this is meant to be a custom unit, define it with 'u.def_unit'. To have it recognized inside a file reader or other code, enable it with 'u.add_enabled_units'. For details, see https://docs.astropy.org/en/latest/units/combining_and_defining.html

@Mubin17
Mubin17 requested a review from nstarman May 5, 2023 07:17

@nstarman nstarman 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.

Before I move on to tests I think I should add the read_latex() and latex_identify() methods

Let's keep this PR atomic, only writing for now. I have a couple of suggestions, mostly about units. I think this PR is ready for some tests, it's looking quite good!

Comment thread astropy/cosmology/io/latex.py Resolved
Comment thread astropy/cosmology/io/latex.py Resolved
Comment thread astropy/cosmology/io/latex.py Resolved
Comment thread astropy/cosmology/io/latex.py Resolved
@Mubin17

Mubin17 commented May 7, 2023

Copy link
Copy Markdown
Contributor Author

@nstarman can you please review these tests before I commit text_latex.py
Please let me know if you have any suggestions.

# Licensed under a 3-clause BSD style license - see LICENSE.rst

# THIRD PARTY
import pytest

# LOCAL
import astropy.units as u
from astropy.cosmology.io.latex import _FORMAT_TABLE, write_latex
from astropy.table import QTable
from astropy.cosmology import Planck15

class TestWriteLatex:
    def setup_class(self):
        # Create a cosmology object to use for testing
        self.cosmo = Planck15

    def test_write_latex(self, tmp_path):
        # Write the cosmology to a temporary file
        filename = tmp_path / "test_write_latex.tex"
        write_latex(self.cosmo, filename)

        # Read the table back in using QTable.read() and check that the data is correct
        table = QTable.read(filename, format="latex")
        
        for column_name in table.colnames[2:]:  
            assert column_name in _FORMAT_TABLE.values()

    def test_write_latex_bad_cls(self, tmp_path):
        # Test that passing an invalid class to write_latex() raises a TypeError
        filename = tmp_path / "test_write_latex_bad_cls.tex"
        with pytest.raises(TypeError):
            write_latex(self.cosmo, filename, cls=str)

    def test_write_latex_invalid_path(self):
        # Test that passing an invalid path to write_latex() raises a IOError
        invalid_path = ""

        with pytest.raises(IOError):
            write_latex(self.cosmo, invalid_path, overwrite=True)

    def test_write_latex_invalid_format(self, tmp_path):
        filename = tmp_path / "test_write_latex_invalid_format.tex"
        # Test using non-Latex format
        with pytest.raises(ValueError):
            write_latex(self.cosmo, filename, format="ascii.html")

Comment thread astropy/cosmology/io/latex.py Resolved
@nstarman

nstarman commented May 8, 2023 •

Copy link
Copy Markdown
Member

From a glance, these tests look good. I would try to implement them in our test suite to see how much coverage they provide!
Also take a look at the test_html file for some more tests to adapt. It's probably easiest to copy-paste-adapt from test_html.py.
Last, the test needs to be hooked up in test_connect. Ping me if anything is confusing!

@Mubin17
Mubin17 requested a review from nstarman May 9, 2023 12:22
Comment thread astropy/cosmology/tests/test_connect.py Resolved
@Mubin17
Mubin17 requested a review from nstarman May 11, 2023 04:41

@nstarman nstarman 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.

The main bit of the CI is failing, for what appears to be unrelated reasons. Fixing the changeling should also hopefully trigger a full CI run. :)

@Mubin17
Mubin17 requested a review from nstarman May 12, 2023 13:43

@nstarman nstarman 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.

@Mubin17
Mubin17 requested a review from nstarman May 15, 2023 20:06
@Mubin17

Mubin17 commented May 15, 2023

Copy link
Copy Markdown
Contributor Author

@nstarman Does the merge conflict arise because my branch is 291 commits behind astropy:main? If yes, should I do sync_fork?

@nstarman

nstarman commented May 15, 2023 •

Copy link
Copy Markdown
Member

Please rebase on main to fix merge conflicts. We try to keep a linear history.

@nstarman nstarman 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.

Hopefully this helps with the ReadTheDocs CI.

Comment thread docs/whatsnew/6.0.rst Resolved
Comment thread docs/whatsnew/6.0.rst Resolved

@nstarman nstarman 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.

Hopefully this helps with the ReadTheDocs CI.

@Mubin17
Mubin17 requested a review from nstarman May 17, 2023 01:09
@Mubin17 Mubin17 closed this May 17, 2023
@Mubin17 Mubin17 reopened this May 17, 2023

@nstarman nstarman 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.

Thanks @Mubin17 for contributing this PR! This looks good to go!
One last bit of house-cleaning is to squash commits. If you want to do that, great, otherwise I can squash down to a few commits.
Thanks again @Mubin17. If you're interested in contributing the LaTeX reader, I'd be happy to work with you.

@Mubin17

Mubin17 commented May 17, 2023

Copy link
Copy Markdown
Contributor Author

Thanks! @nstarman for all the help.
I am not comfortable with squashing commits, so if you can do that it would be great!
Yes, I would love to contribute to the LaTex reader. I will open a new PR soon.
Thanks again.

@Mubin17
Mubin17 requested a review from nstarman May 17, 2023 04:53
@nstarman
nstarman force-pushed the Mubin17/Issue#12355 branch from 9993aec to 733510b Compare May 17, 2023 16:27
@nstarman
nstarman enabled auto-merge May 17, 2023 16:28
@nstarman
nstarman merged commit fd63a62 into astropy:main May 17, 2023
@pllim

pllim commented May 17, 2023

Copy link
Copy Markdown
Member

I already fear cosmology, but it is somehow even scarier in LaTeX! 😸

Thanks, all!

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.

Register latex to Cosmology.write

3 participants