Skip to content
New issue

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

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

Already on GitHub? # to your account

Mosviz: nirspec level 2 loader #2144

Closed
Jdaviz-Triage-Bot opened this issue Apr 11, 2023 · 5 comments · Fixed by #2146
Closed

Mosviz: nirspec level 2 loader #2144

Jdaviz-Triage-Bot opened this issue Apr 11, 2023 · 5 comments · Fixed by #2146
Labels
bug Something isn't working mosviz

Comments

@Jdaviz-Triage-Bot
Copy link

Jdaviz-Triage-Bot commented Apr 11, 2023

Reporter: Camilla Pacifici

According to the docs, nirspec level 2 should be recognized by mosviz:
https://jdaviz.readthedocs.io/en/latest/mosviz/import_data.html#automatic-directory-loading

But I get this traceback

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[83], line 2
      1 mosviz = Mosviz()
----> 2 mosviz.load_data(file_calx1d, instrument="nirspec")
      3 mosviz.show()

File ~/opt/miniconda3/envs/jdaviz3.4/lib/python3.10/site-packages/jdaviz/configs/mosviz/helper.py:503, in Mosviz.load_data(self, spectra_1d, spectra_2d, images, spectra_1d_label, spectra_2d_label, images_label, *args, **kwargs)
    500     allow_link_table = False
    502 elif spectra_1d:
--> 503     self.load_1d_spectra(spectra_1d, spectra_1d_label)
    504     allow_link_table = False
    506 elif spectra_2d:

File ~/opt/miniconda3/envs/jdaviz3.4/lib/python3.10/site-packages/jdaviz/configs/mosviz/helper.py:608, in Mosviz.load_1d_spectra(self, data_obj, data_labels)
    593 def load_1d_spectra(self, data_obj, data_labels=None):
    594     """
    595     Load and parse a set of 1D spectra objects.
    596 
   (...)
    606         for each item in ``data_obj`` if  ``data_obj`` is a list.
    607     """
--> 608     super().load_data(data_obj, parser_reference="mosviz-spec1d-parser",
    609                       data_labels=data_labels)
    610     self._add_redshift_column()

File ~/opt/miniconda3/envs/jdaviz3.4/lib/python3.10/site-packages/jdaviz/core/helpers.py:109, in ConfigHelper.load_data(self, data, data_label, parser_reference, **kwargs)
    107 if data_label:
    108     kwargs['data_label'] = data_label
--> 109 self.app.load_data(data, parser_reference=parser_reference, **kwargs)

File ~/opt/miniconda3/envs/jdaviz3.4/lib/python3.10/site-packages/jdaviz/app.py:546, in Application.load_data(self, file_obj, parser_reference, **kwargs)
    543         parser = data_parser_registry.members.get(data_parser)
    545 if parser is not None:
--> 546     parser(self, file_obj, **kwargs)
    547 else:
    548     self._application_handler.load_data(file_obj)

File ~/opt/miniconda3/envs/jdaviz3.4/lib/python3.10/site-packages/jdaviz/configs/mosviz/plugins/parsers.py:237, in mos_spec1d_parser(app, data_obj, data_labels, table_viewer_reference_name)
    233 with app.data_collection.delay_link_manager_update():
    235     for i, (cur_data, cur_label) in enumerate(zip(data_obj, data_labels)):
    236         # Make metadata layout conform with other viz.
--> 237         cur_data.meta = standardize_metadata(cur_data.meta)
    238         cur_data.meta['mosviz_row'] = i
    240         app.add_data(cur_data, cur_label, notify_done=False)

AttributeError: 'str' object has no attribute 'meta'

A single pair cal/x1d can be found here: https://stsci.box.com/s/903uu5lgafp3hg3ayac9efyhu1lrs57n
(Data are publicly available)

🐱


DISCLAIMER: This issue was autocreated by the Jdaviz Issue Creation Bot on behalf of the reporter. If any information is incorrect, please contact Duy Nguyen

@pllim pllim added mosviz bug Something isn't working labels Apr 11, 2023
@pllim
Copy link
Contributor

pllim commented Apr 11, 2023

I think you were accidentally passing in 2D spec into positional argument expecting 1D spec. Safer to invoke the keywords explicitly.

Still, with the latest stable specutils v1.10.0, I get the following error when calling this with your data. It looks like specutils is intentionally saying that this unrectified data is not supported. How do you expect it to be visualized without calibration?

mosviz.load_data(spectra_1d="single/jw02136001001_03137_00001_nrs1_x1d.fits",
                 spectra_2d="single/jw02136001001_03137_00001_nrs1_cal.fits",
                 instrument="nirspec")
File jdaviz/configs/mosviz/helper.py:489, in Mosviz.load_data(self, spectra_1d, spectra_2d, images, spectra_1d_label, spectra_2d_label, images_label, *args, **kwargs)
    487 elif spectra_1d is not None and spectra_2d is not None:
    488     self.load_1d_spectra(spectra_1d, spectra_1d_label)
--> 489     self.load_2d_spectra(spectra_2d, spectra_2d_label)
    490     self.load_metadata()
    492 elif spectra_1d and images:

File jdaviz/configs/mosviz/helper.py:627, in Mosviz.load_2d_spectra(self, data_obj, data_labels)
    612 def load_2d_spectra(self, data_obj, data_labels=None):
    613     """
    614     Load and parse a set of 2D spectra objects.
    615 
   (...)
    625         for each item in ``data_obj`` if  ``data_obj`` is a list.
    626     """
--> 627     super().load_data(data_obj, parser_reference="mosviz-spec2d-parser",
    628                       data_labels=data_labels)
    629     self._add_redshift_column()

File jdaviz/core/helpers.py:109, in ConfigHelper.load_data(self, data, data_label, parser_reference, **kwargs)
    107 if data_label:
    108     kwargs['data_label'] = data_label
--> 109 self.app.load_data(data, parser_reference=parser_reference, **kwargs)

File jdaviz/app.py:552, in Application.load_data(self, file_obj, parser_reference, **kwargs)
    549         parser = data_parser_registry.members.get(data_parser)
    551 if parser is not None:
--> 552     parser(self, file_obj, **kwargs)
    553 else:
    554     self._application_handler.load_data(file_obj)

File jdaviz/configs/mosviz/plugins/parsers.py:317, in mos_spec2d_parser(app, data_obj, data_labels, add_to_table, show_in_viewer, ext, transpose)
    315 if len(data_obj) == 1 and _check_is_file(data_obj[0]):
    316     if identify_jwst_s2d_multi_fits("test", data_obj[0]):
--> 317         data_obj = SpectrumList.read(data_obj[0])
    319 # If we're given a string, repeat it for each object
    320 if isinstance(data_labels, str):

File astropy/nddata/mixins/ndio.py:59, in NDDataRead.__call__(self, *args, **kwargs)
     58 def __call__(self, *args, **kwargs):
---> 59     return self.registry.read(self._cls, *args, **kwargs)

File astropy/io/registry/core.py:218, in UnifiedInputRegistry.read(self, cls, format, cache, *args, **kwargs)
    213     format = self._get_valid_format(
    214         "read", cls, path, fileobj, args, kwargs
    215     )
    217 reader = self.get_reader(format, cls)
--> 218 data = reader(*args, **kwargs)
    220 if not isinstance(data, cls):
    221     # User has read with a subclass where only the parent class is
    222     # registered.  This returns the parent class, so try coercing
    223     # to desired subclass.
    224     try:

File specutils/io/default_loaders/jwst_reader.py:425, in jwst_s2d_multi_loader(filename, **kwargs)
    407 @data_loader(
    408     "JWST s2d multi", identifier=identify_jwst_s2d_multi_fits,
    409     dtype=SpectrumList, extensions=['fits'], priority=10,
    410 )
    411 def jwst_s2d_multi_loader(filename, **kwargs):
    412     """
    413     Loader for JWST s2d 2D rectified spectral data in FITS format.
    414 
   (...)
    423         The spectra contained in the file.
    424     """
--> 425     return _jwst_s2d_loader(filename, **kwargs)

File specutils/io/default_loaders/jwst_reader.py:495, in _jwst_s2d_loader(filename, **kwargs)
    493     # Make sure all rows are the same
    494     if not (lam == wavelength_array).all():
--> 495         raise RuntimeError("This 2D or 3D spectrum is not rectified "
    496                            "and cannot be loaded into a Spectrum1D object.")
    497 elif dispaxis == 2:
    498     flux_array = hdu.data.T

RuntimeError: This 2D or 3D spectrum is not rectified and cannot be loaded into a Spectrum1D object.

@camipacifici
Copy link
Contributor

camipacifici commented Apr 12, 2023

I was passing the directory with both files as instructed in the docs
"The NIRSpec parser expects a directory with either level 2 files"

I confirm I get the same error as you (the rectified error) if I pass the files separately with their keyword.

It loads if I pass it the rectified level 2 s2d file, although it gives me a bunch of warnings about redshift values in disagreement between the 1D and 2D and the Identifier shows negative numbers (no clue if this is a data problem).

I added the s2d file to the "single" folder: https://stsci.box.com/s/903uu5lgafp3hg3ayac9efyhu1lrs57n

@pllim
Copy link
Contributor

pllim commented Apr 12, 2023

If you want to pass in directory, you have to explicitly use the directory= keyword.

@camipacifici
Copy link
Contributor

camipacifici commented Apr 12, 2023

Tried passing in a single image as well, and I get:

ValueError                                Traceback (most recent call last)
Cell In[9], line 2
      1 mosviz = Mosviz()
----> 2 mosviz.load_data(spectra_1d="../data/mastDownload/jones_calx1d_files/single/jw02136001001_03137_00001_nrs1_x1d.fits",
      3                  spectra_2d="../data/mastDownload/jones_s2d_level2/jw02136001001_03137_00001_nrs1_s2d.fits",
      4                  images="/Users/cpacifici/Dropbox/SINCRONIZZARE/Documents/WORK/CANDELS/hlsp_candels_hst_wfc3_egs-tot-60mas_f160w_v1.0_drz.fits",
      5                  instrument="nirspec")
      6 mosviz.show()

File ~/opt/miniconda3/envs/pr2146/lib/python3.10/site-packages/jdaviz/configs/mosviz/helper.py:483, in Mosviz.load_data(self, spectra_1d, spectra_2d, images, spectra_1d_label, spectra_2d_label, images_label, *args, **kwargs)
    480 else:
    481     self.load_images(images, images_label)
--> 483 self.load_2d_spectra(spectra_2d, spectra_2d_label)
    484 self.load_1d_spectra(spectra_1d, spectra_1d_label)
    485 self.load_metadata()

File ~/opt/miniconda3/envs/pr2146/lib/python3.10/site-packages/jdaviz/configs/mosviz/helper.py:627, in Mosviz.load_2d_spectra(self, data_obj, data_labels)
    612 def load_2d_spectra(self, data_obj, data_labels=None):
    613     """
    614     Load and parse a set of 2D spectra objects.
    615 
   (...)
    625         for each item in ``data_obj`` if  ``data_obj`` is a list.
    626     """
--> 627     super().load_data(data_obj, parser_reference="mosviz-spec2d-parser",
    628                       data_labels=data_labels)
    629     self._add_redshift_column()

File ~/opt/miniconda3/envs/pr2146/lib/python3.10/site-packages/jdaviz/core/helpers.py:109, in ConfigHelper.load_data(self, data, data_label, parser_reference, **kwargs)
    107 if data_label:
    108     kwargs['data_label'] = data_label
--> 109 self.app.load_data(data, parser_reference=parser_reference, **kwargs)

File ~/opt/miniconda3/envs/pr2146/lib/python3.10/site-packages/jdaviz/app.py:552, in Application.load_data(self, file_obj, parser_reference, **kwargs)
    549         parser = data_parser_registry.members.get(data_parser)
    551 if parser is not None:
--> 552     parser(self, file_obj, **kwargs)
    553 else:
    554     self._application_handler.load_data(file_obj)

File ~/opt/miniconda3/envs/pr2146/lib/python3.10/site-packages/jdaviz/configs/mosviz/plugins/parsers.py:362, in mos_spec2d_parser(app, data_obj, data_labels, add_to_table, show_in_viewer, ext, transpose)
    359         app.data_collection[label] = data
    361     if add_to_table:
--> 362         _add_to_table(
    363             app, data_labels, '2D Spectra',
    364             table_viewer_reference_name=table_viewer_reference_name
    365         )
    367 if show_in_viewer:
    368     if len(data_labels) > 1:

File ~/opt/miniconda3/envs/pr2146/lib/python3.10/site-packages/jdaviz/configs/mosviz/plugins/parsers.py:64, in _add_to_table(app, data, comp_label, table_viewer_reference_name)
     62 mos_table = app.data_collection['MOS Table']
     63 if comp_label not in mos_table.component_ids():
---> 64     mos_table.add_component(data, comp_label)

File ~/opt/miniconda3/envs/pr2146/lib/python3.10/site-packages/glue/core/data.py:1077, in Data.add_component(self, component, label)
   1074     component.set_parent(self)
   1076 if not self._check_can_add(component):
-> 1077     raise ValueError("The dimensions of component %s are "
   1078                      "incompatible with the dimensions of this data: "
   1079                      "%r vs %r" % (label, component.shape, self.shape))
   1081 if isinstance(label, ComponentID):
   1082     component_id = label

ValueError: The dimensions of component 2D Spectra are incompatible with the dimensions of this data: (50,) vs (1,)

@camipacifici
Copy link
Contributor

Ah, you are right about the directory keyword, sorry. My mistake!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working mosviz
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants