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

Spectrum1D.__str__ formatting error #1073

Closed
bmorris3 opened this issue Aug 10, 2023 · 1 comment · Fixed by #1123
Closed

Spectrum1D.__str__ formatting error #1073

bmorris3 opened this issue Aug 10, 2023 · 1 comment · Fixed by #1123
Labels
bug data objects Core data objects like Spectrum1D or SpectralCollection
Milestone

Comments

@bmorris3
Copy link
Contributor

bmorris3 commented Aug 10, 2023

If I make a Spectrum1D and print it with:

import numpy as np
from specutils import Spectrum1D
import astropy.units as u

flux = np.arange(2**3).reshape((2, 2, 2)) * u.dn

s3d = Spectrum1D(flux=flux)
print(s3d)

I get the following traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File ~/miniconda3/envs/roman2/lib/python3.11/site-packages/astropy/units/quantity.py:1535, in Quantity.__format__(self, format_spec)
   1534 try:
-> 1535     return self.to_string(format=format_spec)
   1536 except ValueError:
   1537     # We might have a unit format not implemented in `to_string()`.

File ~/miniconda3/envs/roman2/lib/python3.11/site-packages/astropy/units/quantity.py:1451, in Quantity.to_string(self, unit, precision, format, subfmt)
   1450 if format not in formats:
-> 1451     raise ValueError(f"Unknown format '{format}'")
   1452 elif format is None:

ValueError: Unknown format '.5'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 print(s3d)

File ~/miniconda3/envs/roman2/lib/python3.11/site-packages/specutils/spectra/spectrum1d.py:750, in Spectrum1D.__str__(self)
    748     for i, flux in enumerate(self.flux):
    749         label = 'flux{:2}'.format(i)
--> 750         result += self._format_array_summary(label, flux) + '\n'
    751 else:
    752     result += self._format_array_summary('flux', self.flux) + '\n'

File ~/miniconda3/envs/roman2/lib/python3.11/site-packages/specutils/spectra/spectrum1d.py:734, in Spectrum1D._format_array_summary(self, label, array)
    732     mean = np.mean(array)
    733     s = "{:17} [ {:.5}, ..., {:.5} ],  mean={:.5}"
--> 734     return s.format(label+':', array[0], array[-1], mean)
    735 else:
    736     return "{:17} [ ],  mean= n/a".format(label+':')

File ~/miniconda3/envs/roman2/lib/python3.11/site-packages/astropy/units/quantity.py:1545, in Quantity.__format__(self, format_spec)
   1543 # Can the value be formatted on its own?
   1544 try:
-> 1545     return f"{format(self.value, format_spec)}{self._unitstr:s}"
   1546 except ValueError:
   1547     # Format the whole thing as a single string.
   1548     return format(f"{self.value}{self._unitstr:s}", format_spec)

TypeError: unsupported format string passed to numpy.ndarray.__format__

Using specutils v1.11.0, py 3.9.12, platform: osx-arm64.

@rosteen rosteen added bug data objects Core data objects like Spectrum1D or SpectralCollection labels Aug 29, 2023
@rosteen rosteen added this to the v1.x milestone Aug 29, 2023
@bmorris3
Copy link
Contributor Author

Thank you @rosteen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug data objects Core data objects like Spectrum1D or SpectralCollection
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants