Skip to content

Commit

Permalink
adapting improvements to work in other helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorris3 committed May 3, 2023
1 parent add8c96 commit 3c2c83e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
22 changes: 14 additions & 8 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion jdaviz/configs/default/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 13 additions & 1 deletion jdaviz/configs/imviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
11 changes: 9 additions & 2 deletions jdaviz/configs/imviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}'
Expand Down

0 comments on commit 3c2c83e

Please sign in to comment.