Created
May 10, 2022 21:23
-
-
Save jdtsmith/3815771bbc065f59a75b08a91521749e to your computer and use it in GitHub Desktop.
BoundedPar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from astropy.table import Table | |
from astropy.table.pprint import TableFormatter | |
def fmt_func(fmt): | |
return lambda val: (f'{val["par"]:{fmt}} ' | |
f'({val["pmn"]:{fmt}}, {val["pmx"]:{fmt}})') | |
PAR_DTYPE = np.dtype([('par', 'f8'), ('pmn', 'f8'), ('pmx', 'f8')]) | |
class BoundedParTableFormatter(TableFormatter): | |
def _pformat_table(self, table, *args, **kwargs): | |
bpcols = [] | |
for col in table.columns.values(): | |
if col.dtype == PAR_DTYPE: | |
bpcols.append((col, col.info.format)) | |
col.info.format = fmt_func(col.info.format or "g") | |
try: | |
return super()._pformat_table(table, *args, **kwargs) | |
finally: | |
for col, fmt in bpcols: | |
col.info.format = fmt | |
class BoundedParTable(Table): | |
TableFormatter = BoundedParTableFormatter | |
name = ['par1', 'par2'] | |
a = np.array([(np.pi, 2, 4.5), (np.pi / 2, 1, 3.1)], dtype=PAR_DTYPE) | |
t = BoundedParTable([name, a], names=['name', 'par']) | |
t.write('pars.ecsv', overwrite=True) | |
t2 = BoundedParTable.read('pars.ecsv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment