Skip to content

Commit

Permalink
Metadata Viewer: API to return dict, not list
Browse files Browse the repository at this point in the history
  • Loading branch information
pllim committed Nov 14, 2024
1 parent f715ffe commit d48be0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MetadataViewer(PluginTemplateMixin, DatasetSelectMixin):
Dataset to expose the metadata.
* :attr:`show_primary`:
Whether to show MEF primary header metadata instead.
* :attr:`metadata`:
* :attr:`meta`:
Read-only metadata. If the data is loaded from a multi-extension FITS file,
this can be the extension header or the primary header, depending on
``show_primary`` setting.
Expand All @@ -39,6 +39,7 @@ class MetadataViewer(PluginTemplateMixin, DatasetSelectMixin):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.meta = {}
# override the default filters on dataset entries to require metadata in entries
self.dataset.add_filter('not_from_plugin')

Expand All @@ -47,14 +48,15 @@ def __init__(self, *args, **kwargs):

@property
def user_api(self):
return PluginUserApi(self, expose=('dataset', 'show_primary'), readonly=('metadata',))
return PluginUserApi(self, expose=('dataset', 'show_primary'), readonly=('meta',))

def reset(self):
self.has_metadata = False
self.has_primary = False
self.show_primary = False
self.has_comments = False
self.metadata = []
self.meta = {}

@observe("dataset_selected")
def show_metadata(self, event):
Expand Down Expand Up @@ -101,7 +103,8 @@ def find_public_metadata(self, meta, primary_only=False):
d = flatten_nested_dict(meta)
# Some FITS keywords cause "# ipykernel cannot clean for JSON" messages.
# Also, we want to hide internal metadata that starts with underscore.
badkeys = ['COMMENT', 'HISTORY', ''] + [k for k in d if k.startswith('_')]
badkeys = (['COMMENT', 'HISTORY', ''] + [k for k in d if k.startswith('_')]
+ [k for k in d if k.startswith('original_')])
for badkey in badkeys:
if badkey in d:
del d[badkey]
Expand All @@ -125,6 +128,7 @@ def get_comment(key):
public_meta = sorted(zip(d.keys(), map(str, d.values()), map(get_comment, d.keys())))
if len(public_meta) > 0:
self.metadata = public_meta
self.meta = d
self.has_metadata = True
self.has_comments = has_comments
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ def test_view_dict(imviz_helper):
('BITPIX', '8', 'array data type'), ('EXTEND', 'True', ''),
('NAXIS', '0', 'number of array dimensions'),
('SIMPLE', 'True', 'conforms to FITS standard')]
assert sorted(mv.meta.keys()) == ['APERTURE', 'BITPIX', 'NAXIS', 'SIMPLE']

mv.dataset_selected = 'no_meta'
assert not mv.has_primary
assert not mv.show_primary
assert not mv.has_comments
assert not mv.has_metadata
assert mv.metadata == []
assert mv.meta == {}


def test_view_invalid(imviz_helper):
Expand All @@ -94,3 +96,4 @@ def test_view_invalid(imviz_helper):
assert not mv.has_comments
assert not mv.has_metadata
assert mv.metadata == []
assert mv.meta == {}

0 comments on commit d48be0f

Please sign in to comment.