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

Crash in astropy.table.pprint when using lines-only science pack #266

Closed
drvdputt opened this issue Nov 21, 2023 · 3 comments
Closed

Crash in astropy.table.pprint when using lines-only science pack #266

drvdputt opened this issue Nov 21, 2023 · 3 comments

Comments

@drvdputt
Copy link
Contributor

Bug description

The code crashes when trying to print Features.

Reproducing

This is a strange bug, reproducible with very minimal examples

This crashes:

from pahfit.model import Model
m = Model.from_yaml('minimal_bad.yaml')
print(m)

with the following minimal yaml file

H2_lines:
    kind: line
    wavelength:
        H2_S(8):    5.0530

On the other hand, if a stellar component is added, everything is fine

starlight:
    kind: starlight
    temperature: 5000

H2_lines:
    kind: line
    wavelength:
        H2_S(8):    5.0530

Exceptions and tracebacks

Click for full output

Traceback (most recent call last):
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/pprint.py", line 90, in auto_format_func
out = format_func(format
, val)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/pprint.py", line 88, in
format_func = lambda format_, val: format_(val)
^^^^^^^^^^^^
File "/Users/dvandeputte/Repositories/pahfit/pahfit/features/features_format.py", line 16, in _fmt
raise e
File "/Users/dvandeputte/Repositories/pahfit/pahfit/features/features_format.py", line 9, in _fmt
if ma.is_masked(v) or ma.is_masked(v[0]):
~^^^
IndexError: invalid index to scalar variable.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/pprint.py", line 535, in _pformat_col_iter
yield format_col_str(idx)
^^^^^^^^^^^^^^^^^^^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/pprint.py", line 523, in format_col_str
left = format_func(col_format, col[(idx,) + multidim0])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/pprint.py", line 103, in _auto_format_func
raise ValueError(f"Format function for value {val} failed.") from err
ValueError: Format function for value 0.0 failed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/column.py", line 779, in format
self.pformat(max_lines=1)
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/column.py", line 883, in pformat
lines, outs = _pformat_col(
^^^^^^^^^^^^^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/pprint.py", line 294, in _pformat_col
col_strs = [
^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/pprint.py", line 294, in
col_strs = [
^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/pprint.py", line 537, in _pformat_col_iter
raise ValueError(
ValueError: Unable to parse format string "<function fmt_func.._fmt at 0x103640540>" for entry "[ 0. 0. inf]" in column "power"

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/dvandeputte/Downloads/pahfit_debug/debug.py", line 3, in
print(m)
File "/Users/dvandeputte/Repositories/pahfit/pahfit/model.py", line 133, in repr
return self._status_message() + self.features.repr()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/table.py", line 1632, in repr
return self.base_repr(html=False, max_width=None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/table.py", line 1606, in base_repr
data_lines, outs = self.formatter._pformat_table(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dvandeputte/Repositories/pahfit/pahfit/features/features_format.py", line 51, in _pformat_table
col.info.format = fmt_func(col.info.format or "g")
^^^^^^^^^^^^^^^
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/utils/data_info.py", line 244, in set
setattr(instance._parent, self.attr, value)
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/column.py", line 1262, in setattr
super().setattr(item, value)
File "/Users/dvandeputte/opt/miniconda3/envs/pahfitcube/lib/python3.11/site-packages/astropy/table/column.py", line 783, in format
raise ValueError(
ValueError: Invalid format for column 'power': could not display values in this column using this format

Cause

I don't know how to solve this, but I was able to narrow it down to a certain behavior. We’re using a formatting function that expects 3-tuples as input, and this function is passed to the pretty printer for the table. But astropy.table.pprint, does something automagically with the dimensionality, and ends up passing the individual elements to our function, instead of tuples.
With the working example pack, we get a BoundedMaskedColumn (a type that we derive from the regular MaskedColumn), while its a regular Column for the lines only case.
For some reason, pprint behaves differently in this case, and it probably depends on the details of BoundedMaskedColumn. I have not tried this with a regular MaskedColumn.

Workaround

A temporary workaround would be to add a starlight component to the yaml file, and then remove it in python (by setting model.features = model.features[1:]). This forces the relevant columns to be BoundedMaskedColumn.

@jdtsmith
Copy link
Contributor

jdtsmith commented May 8, 2024

Won't forcing all columns to be BoundedMaskedColumn's do the trick here?

@jdtsmith
Copy link
Contributor

jdtsmith commented May 13, 2024

Hopefully the new simpler Tabulate underlying format #283 fixed this; @drvdputt ?

@drvdputt
Copy link
Contributor Author

Yes, this issue was of the same origin as #274. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants