Skip to content

Commit

Permalink
Implement consistent labels for Imviz
Browse files Browse the repository at this point in the history
  • Loading branch information
javerbukh committed Sep 23, 2022
1 parent d5c4559 commit 64fdad8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
15 changes: 7 additions & 8 deletions jdaviz/configs/imviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand All @@ -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)


Expand Down Expand Up @@ -122,16 +125,12 @@ def get_image_data_iterator(app, file_obj, data_label, ext=None):

def _parse_image(app, file_obj, data_label, ext=None):
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:
data_label = data_label + "_2" # 0th-order solution as proposed in issue #600

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()
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/configs/imviz/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions jdaviz/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,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:
Expand Down

0 comments on commit 64fdad8

Please sign in to comment.