PAHFIT / pahfit

Model Decomposition for Near- to Mid-Infrared Spectroscopy of Astronomical Sources
https://pahfit.readthedocs.io/
18 stars 26 forks source link

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

Closed drvdputt closed 4 months ago

drvdputt commented 10 months ago

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 "._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 commented 5 months ago

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

jdtsmith commented 4 months ago

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

drvdputt commented 4 months ago

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