glue-viz / glue-astronomy

Plugin to add astronomy-specific functionality to glue
https://glue-astronomy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
11 stars 13 forks source link

CASA images may have bad WCS #57

Open keflavich opened 2 years ago

keflavich commented 2 years ago

I loaded a CASA .image in glue and tried to do a PV cut. I got the following traceback:

Traceback (most recent call last):
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/plugins/tools/pv_slicer/qt/pv_slicer.py", line 237, in _slice_from_path
    result = extract_pv_slice(cube, path=p, wcs=cube_wcs, order=0)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/pvextractor.py", line 76, in extract_pv_slice
    wcs = sanitize_wcs(wcs)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/utils/wcs_utils.py", line 39, in sanitize_wcs
    pc = np.matrix(mywcs.wcs.get_pc())
AttributeError: 'astropy.wcs.Wcsprm' object has no attribute 'wcs'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/adamginsburg/.local/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 270, in process
    func(*args, **kwargs)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/viewers/matplotlib/toolbar_mode.py", line 207, in key
    self._finish_roi(self._last_event)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/viewers/matplotlib/toolbar_mode.py", line 101, in _finish_roi
    self._roi_callback(self)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/plugins/tools/pv_slicer/qt/pv_slicer.py", line 41, in _extract_callback
    self._build_from_vertices(vx, vy)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/plugins/tools/pv_slicer/qt/pv_slicer.py", line 44, in _build_from_vertices
    pv_slice, x, y, wcs = _slice_from_path(vx, vy, self.viewer.state.reference_data,
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/plugins/tools/pv_slicer/qt/pv_slicer.py", line 240, in _slice_from_path
    result = extract_pv_slice(cube, path=p, wcs=None, order=0)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/pvextractor.py", line 79, in extract_pv_slice
    scale = get_spatial_scale(wcs)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/utils/wcs_utils.py", line 10, in get_spatial_scale
    wcs = wcs.sub([WCSSUB_CELESTIAL])
AttributeError: 'NoneType' object has no attribute 'sub'

I'm not certain, but I think this implies that mywcs is already the wcs object that should be getting used....

...looking closer, maybe this is a pvextractor problem? I'll try to track it down further but I want to post.

keflavich commented 2 years ago

I think this line is the problem: https://github.com/glue-viz/glue/blob/ed71979f8e0e41f993a2363b3b5a8f8c3167a130/glue/plugins/tools/pv_slicer/qt/pv_slicer.py#L210 because data.coords is itself the wcs object

keflavich commented 2 years ago

Hm, simply replacing the WCS gives: NotImplementedError: Don't yet support nd fancy indexing

astrofrog commented 2 years ago

Could you post the full traceback? I think this is likely to be an issue with pvextractor not working well with dask arrays, but I'd need to see the traceback to know for sure. I can try and investigate this on Thursday.

keflavich commented 2 years ago

I think that is the full traceback? I was able to reproduce it this morning though so I'll try to get you a more complete MWE. But, just try doing a PV extraction on any CASA .image, maybe?

astrofrog commented 2 years ago

Sorry I meant the lack of traceback in https://github.com/glue-viz/glue-astronomy/issues/57#issuecomment-975825066 - but I will try myself on Thursday with a cube I have.

keflavich commented 2 years ago

Oh right, sorry. I'll see if I can get that.

keflavich commented 2 years ago
Traceback (most recent call last):
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/plugins/tools/pv_slicer/qt/pv_slicer.py", line 240, in _slice_from_path
    result = extract_pv_slice(cube, path=p, wcs=cube_wcs, order=0)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/pvextractor.py", line 97, in extract_pv_slice
    pv_slice = extract_slice(cube, path, wcs=wcs, spacing=pixel_spacing,
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/geometry/slices.py", line 42, in extract_slice
    slice = extract_line_slice(cube, x, y, order=order)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/geometry/line_slices.py", line 43, in extract_line_slice
    total_slice[:,ok] = cube[:, y[ok].astype(int), x[ok].astype(int)]
  File "/orange/adamginsburg/miniconda3/envs/python39/lib/python3.9/site-packages/dask/array/core.py", line 1754, in __getitem__
    dsk, chunks = slice_array(out, self.name, self.chunks, index2, self.itemsize)
  File "/orange/adamginsburg/miniconda3/envs/python39/lib/python3.9/site-packages/dask/array/slicing.py", line 174, in slice_array
    dsk_out, bd_out = slice_with_newaxes(out_name, in_name, blockdims, index, itemsize)
  File "/orange/adamginsburg/miniconda3/envs/python39/lib/python3.9/site-packages/dask/array/slicing.py", line 196, in slice_with_newaxes
    dsk, blockdims2 = slice_wrap_lists(out_name, in_name, blockdims, index2, itemsize)
  File "/orange/adamginsburg/miniconda3/envs/python39/lib/python3.9/site-packages/dask/array/slicing.py", line 242, in slice_wrap_lists
    raise NotImplementedError("Don't yet support nd fancy indexing")
NotImplementedError: Don't yet support nd fancy indexing

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/adamginsburg/.local/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 270, in process
    func(*args, **kwargs)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/viewers/matplotlib/toolbar_mode.py", line 207, in key
    self._finish_roi(self._last_event)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/viewers/matplotlib/toolbar_mode.py", line 101, in _finish_roi
    self._roi_callback(self)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/plugins/tools/pv_slicer/qt/pv_slicer.py", line 41, in _extract_callback
    self._build_from_vertices(vx, vy)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/plugins/tools/pv_slicer/qt/pv_slicer.py", line 44, in _build_from_vertices
    pv_slice, x, y, wcs = _slice_from_path(vx, vy, self.viewer.state.reference_data,
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/plugins/tools/pv_slicer/qt/pv_slicer.py", line 243, in _slice_from_path
    result = extract_pv_slice(cube, path=p, wcs=None, order=0)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/pvextractor.py", line 79, in extract_pv_slice
    scale = get_spatial_scale(wcs)
  File "/blue/adamginsburg/adamginsburg/repos/glue/glue/external/pvextractor/utils/wcs_utils.py", line 10, in get_spatial_scale
    wcs = wcs.sub([WCSSUB_CELESTIAL])
AttributeError: 'NoneType' object has no attribute 'sub'
keflavich commented 2 years ago

I added these lines:

    if isinstance(cube_wcs, wcs.Wcsprm):
        cube_wcs = data.coords

after line 210 (also had to import wcs from astropy)