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

Fix transformation load for time-dependent value with sublogs #231

Merged
merged 1 commit into from
Sep 4, 2024
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
6 changes: 6 additions & 0 deletions src/scippnexus/nxtransformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def make_transformation(
select: ScippIndex,
):
try:
if isinstance(value, sc.DataGroup) and (
isinstance(value.get('value'), sc.DataArray)
):
# Some NXlog groups are split into value, alarm, and connection_status
# sublogs. We only care about the value.
value = value['value']
if isinstance(value, sc.DataGroup):
return value
t = value * self.vector
Expand Down
39 changes: 39 additions & 0 deletions tests/nxtransformations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,45 @@ def test_Transformation_with_multiple_values(h5root):
assert_identical(detector[depends_on][()], expected)


def test_time_dependent_transform_uses_value_sublog(h5root):
detector = create_detector(h5root)
snx.create_field(
detector, 'depends_on', sc.scalar('/detector_0/transformations/t1')
)
transformations = snx.create_class(detector, 'transformations', NXtransformations)
log = sc.DataArray(
sc.array(dims=['time'], values=[1.1, 2.2], unit='m'),
coords={'time': sc.array(dims=['time'], values=[11, 22], unit='s')},
)
log.coords['time'] = sc.epoch(unit='ns') + log.coords['time'].to(unit='ns')
offset = sc.spatial.translation(value=[1, 2, 3], unit='m')
vector = sc.vector(value=[0, 0, 1])
t = log * vector
t.data = sc.spatial.translations(dims=t.dims, values=t.values, unit=t.unit)
value = snx.create_class(transformations, 't1', snx.NXlog)
snx.create_field(value, 'time', log.coords['time'] - sc.epoch(unit='ns'))
snx.create_field(value, 'value', log.data)
# Add alarms with shorter time axis. This will trigger loading as a DataGroup with
# multiple contained DataArrays.
alarm = log['time', :2].copy()
alarm.coords['message'] = sc.array(dims=['time'], values=['alarm 1', 'alarm 2'])
snx.create_field(value, 'alarm_severity', alarm.data)
snx.create_field(value, 'alarm_message', alarm.coords['message'])
snx.create_field(value, 'alarm_time', alarm.coords['time'])
value.attrs['depends_on'] = '.'
value.attrs['transformation_type'] = 'translation'
value.attrs['offset'] = offset.values
value.attrs['offset_units'] = str(offset.unit)
value.attrs['vector'] = vector.value

expected = t * offset
expected.coords['depends_on'] = sc.scalar('.')
detector = make_group(detector)
depends_on = detector['depends_on'][()]
assert depends_on == 'transformations/t1'
assert_identical(detector[depends_on][()], expected)


def test_chain_with_multiple_values(h5root):
detector = create_detector(h5root)
snx.create_field(
Expand Down