Repository navigation
#12355 Issue: Register latex to Cosmology.write - #14701
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.
|
|
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. |
|
@Mubin17, thanks for the PR. This is a very good start!
I'm happy to keep reviewing as the PR matures. |
|
Hi @Mubin17, is there any way I can help with this PR? Thanks again for working on this PR! |
|
@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 |
nstarman
left a comment
There was a problem hiding this comment.
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!
|
@nstarman can you please review these tests before I commit text_latex.py # 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") |
|
From a glance, these tests look good. I would try to implement them in our test suite to see how much coverage they provide! |
nstarman
left a comment
There was a problem hiding this comment.
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. :)
nstarman
left a comment
There was a problem hiding this comment.
I think just one last change then this is good to go! 🎉
The commits should also be squashed down to a few atomic commits (https://docs.github.com/en/desktop/contributing-and-collaborating-using-github-desktop/managing-commits/squashing-commits-in-github-desktop).
Also, I think this PR merits a whatsnew entry (see https://github.com/astropy/astropy/blob/main/docs/whatsnew/6.0.rst)!
|
Please rebase on main to fix merge conflicts. We try to keep a linear history. |
nstarman
left a comment
There was a problem hiding this comment.
Hopefully this helps with the ReadTheDocs CI.
nstarman
left a comment
There was a problem hiding this comment.
Hopefully this helps with the ReadTheDocs CI.
nstarman
left a comment
There was a problem hiding this comment.
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.
|
Thanks! @nstarman for all the help. |
Signed-off-by: Mubin17 <[email protected]>
9993aec to
733510b
Compare
|
I already fear cosmology, but it is somehow even scarier in LaTeX! 😸 Thanks, all! |
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:
Output:
Please let me know if you have any feedback or suggestions for improvements.