From 3c2c83e5a8f61000f8d35a672f8882a802030c63 Mon Sep 17 00:00:00 2001 From: "Brett M. Morris" Date: Wed, 3 May 2023 15:55:00 -0400 Subject: [PATCH] adapting improvements to work in other helpers --- jdaviz/app.py | 22 ++++++++++++------- .../tests/test_metadata_viewer.py | 6 +++-- jdaviz/configs/default/plugins/viewers.py | 5 ++++- jdaviz/configs/imviz/helper.py | 14 +++++++++++- jdaviz/configs/imviz/plugins/parsers.py | 11 ++++++++-- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/jdaviz/app.py b/jdaviz/app.py index 6f24fa399b..f1b8763835 100644 --- a/jdaviz/app.py +++ b/jdaviz/app.py @@ -404,7 +404,10 @@ def _on_layers_changed(self, msg): wcs_only_refdata_icon = 'mdi-compass-outline' wcs_only_not_refdata_icon = 'mdi-compass-off-outline' - n_wcs_layers = len([icon.startswith('mdi') for icon in self.state.layer_icons]) + n_wcs_layers = ( + len([icon.startswith('mdi') for icon in self.state.layer_icons]) + if is_wcs_only else 0 + ) if layer_name not in self.state.layer_icons: if is_wcs_only: self.state.layer_icons = {**self.state.layer_icons, @@ -759,13 +762,15 @@ def get_data_from_viewer(self, viewer_reference, data_label=None, elif len(layer_data.shape) == 2: layer_data = layer_data.get_object(cls=CCDData) - is_wcs_only = ( - np.all(np.isnan(layer_data.data)) and - layer_data.meta.get('WCS-ONLY', False) - ) + is_wcs_only = ( + # then check if it's all NaNs: + np.all(np.isnan(layer_data.data)) and + # finally check that metadata confirms this is a WCS-ONLY object: + layer_data.meta.get('WCS-ONLY', False) + ) - if is_wcs_only: - continue + if is_wcs_only: + continue data[label] = layer_data @@ -1743,7 +1748,8 @@ def set_data_visibility(self, viewer_reference, data_label, visible=True, replac # remove wcs-only data from selected items, # add to wcs_only_layers: for layer in viewer.layers: - if layer.layer.data.label == data_label and layer.layer.meta.get('WCS-ONLY', False): + is_wcs_only = getattr(layer.layer, 'meta', {}).get('WCS-ONLY', False) + if layer.layer.data.label == data_label and is_wcs_only: layer.visible = False viewer_item['wcs_only_layers'].append(data_label) selected_items.pop(data_id) diff --git a/jdaviz/configs/default/plugins/metadata_viewer/tests/test_metadata_viewer.py b/jdaviz/configs/default/plugins/metadata_viewer/tests/test_metadata_viewer.py index 33ec26697e..022163696b 100644 --- a/jdaviz/configs/default/plugins/metadata_viewer/tests/test_metadata_viewer.py +++ b/jdaviz/configs/default/plugins/metadata_viewer/tests/test_metadata_viewer.py @@ -37,7 +37,7 @@ def test_view_dict(imviz_helper): assert mv.has_metadata assert mv.metadata == [ ('BAR', '10.0', ''), ('BOZO', 'None', ''), ('EXTNAME', 'SCI', ''), - ('EXTVER', '1', ''), ('FOO', '', '')] + ('EXTVER', '1', ''), ('FOO', '', ''), ('WCS-ONLY', 'False', '')] mv.dataset_selected = 'has_nested_meta[DATA]' assert not mv.has_primary @@ -46,7 +46,9 @@ def test_view_dict(imviz_helper): assert mv.has_metadata assert mv.metadata == [ ('EXTNAME', 'ASDF', ''), ('REF.bar', '10.0', ''), - ('REF.foo.1', '', ''), ('REF.foo.2.0', '1', ''), ('REF.foo.2.1', '2', '')] + ('REF.foo.1', '', ''), ('REF.foo.2.0', '1', ''), ('REF.foo.2.1', '2', ''), + ('WCS-ONLY', 'False', '') + ] mv.dataset_selected = 'has_primary[DATA,1]' assert mv.has_primary diff --git a/jdaviz/configs/default/plugins/viewers.py b/jdaviz/configs/default/plugins/viewers.py index 27bf6091df..a645258141 100644 --- a/jdaviz/configs/default/plugins/viewers.py +++ b/jdaviz/configs/default/plugins/viewers.py @@ -117,7 +117,10 @@ def _get_layer_info(layer): visible_layers = {} for layer in self.state.layers[::-1]: - layer_is_wcs_only = layer.layer.meta.get('WCS-ONLY', False) + layer_is_wcs_only = ( + hasattr(layer.layer, 'meta') and + layer.layer.meta.get('WCS-ONLY', False) + ) if layer.visible and not layer_is_wcs_only: prefix_icon, suffix = _get_layer_info(layer) visible_layers[layer.layer.label] = {'color': _get_layer_color(layer), diff --git a/jdaviz/configs/imviz/helper.py b/jdaviz/configs/imviz/helper.py index c03c6f004e..e6efc06e47 100644 --- a/jdaviz/configs/imviz/helper.py +++ b/jdaviz/configs/imviz/helper.py @@ -320,7 +320,19 @@ def get_reference_image_data(app): viewer_reference = app._get_first_viewer_reference_name(require_image_viewer=True) viewer = app.get_viewer(viewer_reference) refdata = viewer.state.reference_data - iref = app.data_collection.index(refdata) if refdata in app.data_collection else None + + if refdata is not None: + iref = app.data_collection.index(refdata) + return refdata, iref + + # if reference data not found above, fall back on old method: + for i, data in enumerate(app.data_collection): + if layer_is_image_data(data): + iref = i + refdata = data + break + if refdata is None: + raise ValueError(f'No valid reference data found in collection: {app.data_collection}') return refdata, iref diff --git a/jdaviz/configs/imviz/plugins/parsers.py b/jdaviz/configs/imviz/plugins/parsers.py index 20abae34fd..7a86cfb18e 100644 --- a/jdaviz/configs/imviz/plugins/parsers.py +++ b/jdaviz/configs/imviz/plugins/parsers.py @@ -379,7 +379,10 @@ def _nddata_to_glue_data(ndd, data_label): if ndd.data.ndim != 2: raise ValueError(f'Imviz cannot load this NDData with ndim={ndd.data.ndim}') - for attrib in ['data', 'mask', 'uncertainty']: + for attrib, sub_attrib in zip( + ['data', 'mask', 'uncertainty'], + ['data', None, 'array'] + ): arr = getattr(ndd, attrib) if arr is None: continue @@ -389,7 +392,11 @@ def _nddata_to_glue_data(ndd, data_label): cur_data.coords = ndd.wcs raw_arr = arr - wcs_only = np.all(np.isnan(raw_arr)) + if sub_attrib is not None: + base_arr = getattr(raw_arr, sub_attrib) + else: + base_arr = raw_arr + wcs_only = np.all(np.isnan(base_arr)) cur_data.meta.update({'WCS-ONLY': wcs_only}) cur_label = f'{data_label}'