From 1dfe75621a03669888eacfb409e0927f7e12f5e7 Mon Sep 17 00:00:00 2001 From: Jesse Averbukh Date: Fri, 23 Sep 2022 09:59:42 -0400 Subject: [PATCH] Implement consistent labels for Imviz --- jdaviz/configs/imviz/plugins/parsers.py | 16 +++++++--------- jdaviz/configs/imviz/tests/test_parser.py | 2 +- jdaviz/core/helpers.py | 5 +++++ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/jdaviz/configs/imviz/plugins/parsers.py b/jdaviz/configs/imviz/plugins/parsers.py index dd57582431..167f1dcea2 100644 --- a/jdaviz/configs/imviz/plugins/parsers.py +++ b/jdaviz/configs/imviz/plugins/parsers.py @@ -12,6 +12,7 @@ from jdaviz.core.registries import data_parser_registry from jdaviz.core.events import SnackbarMessage +from jdaviz.core.helpers import return_data_label from jdaviz.utils import standardize_metadata, PRIHDR_KEY __all__ = ['parse_data'] @@ -41,7 +42,8 @@ def parse_data(app, file_obj, ext=None, data_label=None): """ if isinstance(file_obj, str): if data_label is None: - data_label = os.path.splitext(os.path.basename(file_obj))[0] + # data_label = os.path.splitext(os.path.basename(file_obj))[0] + data_label = return_data_label(app, os.path.splitext(os.path.basename(file_obj))[0]) if file_obj.lower().endswith(('.jpg', '.jpeg', '.png')): from skimage.io import imread from skimage.color import rgb2gray, rgba2rgb @@ -57,7 +59,8 @@ def parse_data(app, file_obj, ext=None, data_label=None): _parse_image(app, pf, data_label, ext=ext) else: if data_label is None: - data_label = f'imviz_data|{str(base64.b85encode(uuid.uuid4().bytes), "utf-8")}' + # data_label = f'imviz_data|{str(base64.b85encode(uuid.uuid4().bytes), "utf-8")}' + data_label = return_data_label(app, 'imviz_data') _parse_image(app, file_obj, data_label, ext=ext) @@ -124,17 +127,12 @@ def _parse_image(app, file_obj, data_label, ext=None): from jdaviz.core.helpers import _next_subset_num if data_label is None: - raise NotImplementedError('data_label should be set by now') + data_label = return_data_label(app, file_obj, ext) data_iter = get_image_data_iterator(app, file_obj, data_label, ext=ext) for data, data_label in data_iter: - - # avoid duplicate data labels in collection - if data_label in app.data_collection.labels: - i = _next_subset_num(data_label, app.data_collection) - data_label = f'{data_label} {i}' - + data_label = return_data_label(app, data_label, ext) app.add_data(data, data_label) # Do not run link_image_data here. We do it at the end in Imviz.load_data() diff --git a/jdaviz/configs/imviz/tests/test_parser.py b/jdaviz/configs/imviz/tests/test_parser.py index 074dd698b6..8b9a09312e 100644 --- a/jdaviz/configs/imviz/tests/test_parser.py +++ b/jdaviz/configs/imviz/tests/test_parser.py @@ -63,7 +63,7 @@ def setup_class(self): self.jwst_asdf_url_2 = 'https://stsci.box.com/shared/static/d5k9z5j05dgfv6ljgie483w21kmpevni.fits' # noqa: E501 def test_no_data_label(self): - with pytest.raises(NotImplementedError, match='should be set'): + with pytest.raises(ValueError, match='app is None'): _parse_image(None, None, None, False) def test_hdulist_no_image(self, imviz_helper): diff --git a/jdaviz/core/helpers.py b/jdaviz/core/helpers.py index 81516c9ec7..fb7574a2a3 100644 --- a/jdaviz/core/helpers.py +++ b/jdaviz/core/helpers.py @@ -707,6 +707,11 @@ def return_data_label(app, loaded_object, ext=None): def return_unique_name(app, data_label): + if app is None: + raise ValueError("app is None, cannot check for duplicate data labels") + if data_label is None: + data_label = "Unknown" + dc = app.data_collection number_of_duplicates = 0 for data in dc: