-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'simpler-bounded-params' into dev
- Loading branch information
Showing
3 changed files
with
47 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,45 @@ | ||
import numpy.ma as ma | ||
from astropy.table import MaskedColumn | ||
import numpy as np | ||
from astropy.table.pprint import TableFormatter | ||
|
||
|
||
# * 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}})" | ||
|
||
def fmt_func(fmt: str): | ||
"""Format bounded variables specially.""" | ||
if fmt.startswith('%'): | ||
fmt = fmt[1:] | ||
|
||
def _fmt(x): | ||
ret = f"{x['val']:{fmt}}" | ||
if np.isnan(x['min']) and np.isnan(x['max']): | ||
return ret + " (fixed)" | ||
else: | ||
mn = ("-∞" if np.isnan(x['min']) or x['min'] == -np.inf | ||
else f"{x['min']:{fmt}}") | ||
mx = ("∞" if np.isnan(x['max']) or x['max'] == np.inf | ||
else f"{x['max']:{fmt}}") | ||
return f"{ret} ({mn}, {mx})" | ||
return _fmt | ||
|
||
|
||
class BoundedMaskedColumn(MaskedColumn): | ||
"""Masked column which can be toggled to group rows into one item | ||
for formatting. To be set as Table's `MaskedColumn'. | ||
""" | ||
|
||
_omit_shape = False | ||
|
||
@property | ||
def shape(self): | ||
sh = super().shape | ||
return sh[0:-1] if self._omit_shape and len(sh) > 1 else sh | ||
|
||
def is_fixed(self): | ||
return ma.getmask(self)[:, 1:].all(1) | ||
|
||
|
||
class BoundedParTableFormatter(TableFormatter): | ||
"""Format bounded parameters. | ||
Bounded parameters are 3-field structured arrays, with fields | ||
'var', 'min', and 'max'. To be set as Table's `TableFormatter'. | ||
'val', 'min', and 'max'. To be set as Table's `TableFormatter'. | ||
""" | ||
|
||
def _pformat_table(self, table, *args, **kwargs): | ||
bpcols = [] | ||
tlfmt = table.meta.get('pahfit_format') | ||
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: | ||
for col in table.columns.values(): | ||
if len(col.dtype) == 3: # bounded! | ||
bpcols.append((col, col.info.format)) | ||
col.info.format = fmt_func(col.info.format or "g") | ||
fmt = col.meta.get('pahfit_format') or tlfmt or "g" | ||
col.info.format = fmt_func(fmt) | ||
return super()._pformat_table(table, *args, **kwargs) | ||
finally: | ||
BoundedMaskedColumn._omit_shape = False | ||
for col, fmt in bpcols: | ||
col.info.format = fmt | ||
|
||
def _name_and_structure(self, name, *args): | ||
"Simplified column name: no val, min, max needed." | ||
return name |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,28 @@ | ||
"""pahfit.util General pahfit.features utility functions.""" | ||
import numpy as np | ||
import numpy.ma as ma | ||
|
||
|
||
def bounded_is_missing(val): | ||
"""Return a mask array indicating which of the bounded values | ||
are missing. A missing bounded value has a masked value.""" | ||
return ma.getmask(val)[..., 0] | ||
return getattr(val['val'], 'mask', None) or np.zeros_like(val['val'], dtype=bool) | ||
|
||
|
||
def bounded_is_fixed(val): | ||
"""Return a mask array indicating which of the bounded values | ||
are fixed. A fixed bounded value has masked bounds.""" | ||
return ma.getmask(val)[..., -2:].all(-1) | ||
return np.isnan(val['min']) & np.isnan(val['max']) | ||
|
||
|
||
def bounded_min(val): | ||
"""Return the minimum of each bounded value passed. | ||
Either the lower bound, or, if no such bound is set, the value itself.""" | ||
lower = val[..., 1] | ||
return np.where(lower, lower, val[..., 0]) | ||
lower = val['min'] | ||
return np.where(lower, lower, val['val']) | ||
|
||
|
||
def bounded_max(val): | ||
"""Return the maximum of each bounded value passed. | ||
Either the upper bound, or, if no such bound is set, the value itself.""" | ||
upper = val[..., 2] | ||
return np.where(upper, upper, val[..., 0]) | ||
upper = val['max'] | ||
return np.where(upper, upper, val['val']) |