Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workarounds for issues with Features column definition and formatting #282

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pahfit/features/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Features(Table):

TableFormatter = BoundedParTableFormatter
MaskedColumn = BoundedMaskedColumn
Column = BoundedMaskedColumn

param_covar = TableAttribute(default=[])
_kind_params = {'starlight': {'temperature', 'tau'},
Expand Down
76 changes: 55 additions & 21 deletions pahfit/features/features_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
# * Special table formatting for bounded (val, min, max) values
def fmt_func(fmt):
def _fmt(v):
if ma.is_masked(v[0]):
return " <n/a> "
if ma.is_masked(v[1]):
return f"{v[0]:{fmt}} (Fixed)"
return f"{v[0]:{fmt}} ({v[1]:{fmt}}, {v[2]:{fmt}})"
try:
if ma.is_masked(v[0]):
return " masked "
if ma.is_masked(v[1]):
return f"{v[0]:{fmt}} (Fixed)"
return f"{v[0]:{fmt}} ({v[1]:{fmt}}, {v[2]:{fmt}})"
except Exception:
# print(f"problem in _fmt: v = {v}, fmt = {fmt}, type(fmt) = {type(fmt)}")
# raise e
return f"{v:0.4g}"

return _fmt

Expand All @@ -24,8 +29,7 @@ class BoundedMaskedColumn(MaskedColumn):

@property
def shape(self):
sh = super().shape
return sh[0:-1] if self._omit_shape and len(sh) > 1 else sh
return super().shape

def is_fixed(self):
return ma.getmask(self)[:, 1:].all(1)
Expand All @@ -37,17 +41,47 @@ class BoundedParTableFormatter(TableFormatter):
'var', 'min', and 'max'. To be set as Table's `TableFormatter'.
"""

def _pformat_table(self, table, *args, **kwargs):
bpcols = []
try:
colsh = [(col, col.shape) for col in table.columns.values()]
BoundedMaskedColumn._omit_shape = True
for col, sh in colsh:
if len(sh) == 2 and sh[1] == 3:
bpcols.append((col, col.info.format))
col.info.format = fmt_func(col.info.format or "g")
return super()._pformat_table(table, *args, **kwargs)
finally:
BoundedMaskedColumn._omit_shape = False
for col, fmt in bpcols:
col.info.format = fmt
def _pformat_col(
self,
col,
max_lines=None,
**kwargs,
):
default_strings, outs = super()._pformat_col(col, max_lines, **kwargs)
has_bounds = len(col.shape) == 2 and col.shape[1] == 3
if not has_bounds:
strings = default_strings
else:
# copy the header strings
strings = default_strings[: outs["n_header"]]

# modify output of the numerical lines
# data index = line index - n_header
after_ellipsis = False
data_strings = default_strings[outs["n_header"] :]
for i, line in enumerate(data_strings):
if "..." in line or "table" in line:
# generic ellipsis line
strings.append(line)
after_ellipsis = True
elif "-- .. --" in line:
# all masked
strings.append(line.replace("-- .. --", "--"))
elif ".. --" in line:
# value but masked bound
strings.append(line.replace(".. --", "(fixed)"))
else:
# deal with case where a "..." row is present, by counting from the end
# show -1 if at the last data string
if after_ellipsis:
d_from_end = len(data_strings) - i
if outs["show_length"]:
# need to add 1 because "length = xx rows" is the last row in this case
d_from_end -= 1
row_index = -d_from_end
else:
row_index = i

strings.append(fmt_func(col.info.format or "0.4g")(col[row_index]))

return strings, outs
Loading