Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose units symbol & name on KeyData objects #449

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions extra_data/keydata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Optional, Tuple

import h5py
import numpy as np

from .exceptions import TrainIDError, NoDataError
Expand Down Expand Up @@ -108,24 +109,24 @@

@property
def units(self):
dset = self.files[0].file[self.hdf5_data_path]
base_unit = dset.attrs.get('unitSymbol', None)
attrs = self.attributes()
base_unit = attrs.get('unitSymbol', None)
if base_unit is None:
return None

Check warning on line 115 in extra_data/keydata.py

View check run for this annotation

Codecov / codecov/patch

extra_data/keydata.py#L115

Added line #L115 was not covered by tests

prefix = dset.attrs.get('metricPrefixSymbol', '')
prefix = attrs.get('metricPrefixSymbol', '')
if prefix == 'u':
prefix = 'μ' # We are not afraid of unicode
JamesWrigley marked this conversation as resolved.
Show resolved Hide resolved
return prefix + base_unit

@property
def units_name(self):
dset = self.files[0].file[self.hdf5_data_path]
base_unit = dset.attrs.get('unitName', None)
attrs = self.attributes()
base_unit = attrs.get('unitName', None)
if base_unit is None:
return None

Check warning on line 127 in extra_data/keydata.py

View check run for this annotation

Codecov / codecov/patch

extra_data/keydata.py#L127

Added line #L127 was not covered by tests

prefix = dset.attrs.get('metricPrefixName', '')
prefix = attrs.get('metricPrefixName', '')
return prefix + base_unit

@property
Expand All @@ -151,6 +152,18 @@
from pathlib import Path
return [Path(p) for p in paths]

def attributes(self):
dset = self.files[0].file[self.hdf5_data_path]
if len(dset.attrs) == 0 and dset.is_virtual:
# Virtual datasets were initially created without these attributes.
# Find a source file. Not using source_file_paths as it can give [].
_, filename, _, _ = dset.virtual_sources()[0]
# Not using FileAccess: no need for train or source lists.
f = h5py.File(filename, 'r')
JamesWrigley marked this conversation as resolved.
Show resolved Hide resolved
dset = f[self.hdf5_data_path]

return dict(dset.attrs)

def select_trains(self, trains):
"""Select a subset of trains in this data as a new :class:`KeyData` object.

Expand Down
10 changes: 10 additions & 0 deletions extra_data/tests/test_keydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ def test_file_no_trains(run_with_file_no_trains):
assert xpos.shape == (64,)


def test_attributes(mock_sa3_control_data):
run = H5File(mock_sa3_control_data)
xgm_intensity = run['SA3_XTD10_XGM/XGM/DOOCS:output', 'data.intensityTD']
attrs = xgm_intensity.attributes()

assert isinstance(attrs, dict)
assert attrs['metricPrefixName'] == 'micro'
assert attrs['unitSymbol'] == 'J'


def test_units(mock_sa3_control_data):
run = H5File(mock_sa3_control_data)
xgm_intensity = run['SA3_XTD10_XGM/XGM/DOOCS:output', 'data.intensityTD']
Expand Down
2 changes: 2 additions & 0 deletions extra_data/tests/test_voview.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def test_use_voview(mock_spb_raw_run, tmp_path):
assert {p.name for p in xgm_intens[:30].source_file_paths} == {
'RAW-R0238-DA01-S00000.h5'
}
assert xgm_intens.units == 'μJ'
assert xgm_intens.units_name == 'microjoule'



Expand Down