Skip to content

Commit

Permalink
Add rename to ECSV
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman committed May 17, 2023
1 parent c9c020c commit 8bc1259
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
39 changes: 28 additions & 11 deletions astropy/cosmology/io/ecsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from .table import from_table, to_table


def read_ecsv(filename, index=None, *, move_to_meta=False, cosmology=None, **kwargs):
"""Read a `~astropy.cosmology.Cosmology` from an ECSV file.
def read_ecsv(
filename, index=None, *, move_to_meta=False, cosmology=None, rename=None, **kwargs
):
"""Read a |Cosmology| from an ECSV file.
Parameters
----------
Expand All @@ -31,10 +33,8 @@ def read_ecsv(filename, index=None, *, move_to_meta=False, cosmology=None, **kwa
(e.g. for ``Cosmology(meta={'key':10}, key=42)``, the ``Cosmology.meta``
will be ``{'key': 10}``).
cosmology : str, `~astropy.cosmology.Cosmology` class, or None (optional, keyword-only)
The cosmology class (or string name thereof) to use when constructing
the cosmology instance. The class also provides default parameter values,
filling in any non-mandatory arguments missing in 'table'.
rename : dict or None (optional keyword-only)
A dictionary mapping column names to fields of the Cosmology.
**kwargs
Passed to :attr:`astropy.table.QTable.read`
Expand All @@ -49,18 +49,30 @@ def read_ecsv(filename, index=None, *, move_to_meta=False, cosmology=None, **kwa

# build cosmology from table
return from_table(
table, index=index, move_to_meta=move_to_meta, cosmology=cosmology
table,
index=index,
move_to_meta=move_to_meta,
cosmology=cosmology,
rename=rename,
)


def write_ecsv(
cosmology, file, *, overwrite=False, cls=QTable, cosmology_in_meta=True, **kwargs
cosmology,
file,
*,
overwrite=False,
cls=QTable,
cosmology_in_meta=True,
rename=None,
**kwargs
):
"""Serialize the cosmology into a ECSV.
Parameters
----------
cosmology : `~astropy.cosmology.Cosmology` subclass instance
cosmology : |Cosmology|
The cosmology instance to convert to a mapping.
file : path-like or file-like
Location to save the serialized cosmology.
Expand All @@ -69,9 +81,12 @@ def write_ecsv(
cls : type (optional, keyword-only)
Astropy :class:`~astropy.table.Table` (sub)class to use when writing.
Default is :class:`~astropy.table.QTable`.
cosmology_in_meta : bool
cosmology_in_meta : bool (optional, keyword-only)
Whether to put the cosmology class in the Table metadata (if `True`,
default) or as the first column (if `False`).
rename : dict or None (optional keyword-only)
A dictionary mapping fields of the Cosmology to columns of the table.
**kwargs
Passed to ``cls.write``
Expand All @@ -80,7 +95,9 @@ def write_ecsv(
TypeError
If kwarg (optional) 'cls' is not a subclass of `astropy.table.Table`
"""
table = to_table(cosmology, cls=cls, cosmology_in_meta=cosmology_in_meta)
table = to_table(
cosmology, cls=cls, cosmology_in_meta=cosmology_in_meta, rename=rename
)

kwargs["format"] = "ascii.ecsv"
table.write(file, overwrite=overwrite, **kwargs)
Expand Down
23 changes: 23 additions & 0 deletions astropy/cosmology/io/tests/test_ecsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ def test_readwrite_ecsv_instance(
got = read(fp)
assert got == cosmo

def test_readwrite_ecsv_rename(
self, cosmo_cls, cosmo, read, write, tmp_path, add_cu
):
"""Test rename argument to read/write."""
fp = tmp_path / "test_readwrite_ecsv_rename.ecsv"
rename = {"name": "cosmo_name"}

write(fp, format="ascii.ecsv", rename=rename)

tbl = QTable.read(fp, format="ascii.ecsv")

assert "name" not in tbl.colnames
assert "cosmo_name" in tbl.colnames

# Errors if reading
with pytest.raises(TypeError, match="there are unused parameters"):
read(fp)

# Roundtrips
inv_rename = {v: k for k, v in rename.items()}
got = read(fp, rename=inv_rename)
assert got == cosmo

def test_readwrite_ecsv_subclass_partial_info(
self, cosmo_cls, cosmo, read, write, tmp_path, add_cu
):
Expand Down

0 comments on commit 8bc1259

Please sign in to comment.