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

Add check for time unit of NXlog.time before attempting datetime conv. #130

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/scippnexus/v2/nxdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
asvariable,
base_definitions_dict,
)
from .field import Field
from .field import Field, _is_time
from .nxevent_data import NXevent_data


Expand Down Expand Up @@ -331,7 +331,7 @@ def __init__(self, attrs: Dict[str, Any], children: Dict[str, Union[Field, Group
def assemble(self,
dg: sc.DataGroup) -> Union[sc.DataGroup, sc.DataArray, sc.Dataset]:
if (time := dg.get('time')) is not None:
if time.dtype != sc.DType.datetime64:
if time.dtype != sc.DType.datetime64 and _is_time(time):
dg['time'] = convert_time_to_datetime64(time,
start=sc.epoch(unit=time.unit))
return super().assemble(dg)
Expand Down
14 changes: 14 additions & 0 deletions tests/nxdata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ def test_NXlog_data_is_loaded_as_time_dependent_data_array(nxroot):
assert_identical(loaded, da)


@pytest.mark.parametrize("time_unit", ['m', None])
def test_NXlog_with_nontime_time_axis_can_be_loaded(nxroot, time_unit):
da = sc.DataArray(
data=sc.array(dims=['time'], unit='K', values=[1, 2, 3]),
coords={'time': sc.array(dims=['time'], unit=time_unit, values=[1, 2, 3])})
data = nxroot.create_class('data1', NXdata)
log = data.create_class('data', NXlog)
log['time'] = da.coords['time']
log['value'] = da.data
loaded = data[()]
assert_identical(loaded, da)
assert loaded.coords['time'].dtype != sc.DType.datetime64


def test_field_dims_match_NXdata_dims(h5root):
da = sc.DataArray(
sc.array(dims=['xx', 'yy'], unit='m', values=[[1, 2, 3], [4, 5, 6]]))
Expand Down