Skip to content

Instantly share code, notes, and snippets.

@jdtsmith
Created May 10, 2022 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdtsmith/3815771bbc065f59a75b08a91521749e to your computer and use it in GitHub Desktop.
Save jdtsmith/3815771bbc065f59a75b08a91521749e to your computer and use it in GitHub Desktop.
BoundedPar
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