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: warn if missing vector in transformation #242

Merged
merged 2 commits into from
Sep 26, 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
9 changes: 8 additions & 1 deletion src/scippnexus/nxtransformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def offset(self):

@property
def vector(self) -> sc.Variable:
if self.attrs.get('vector') is None:
raise TransformationError('A transformation needs a vector attribute.')
return sc.vector(value=self.attrs.get('vector'))

def __getitem__(self, select: ScippIndex):
Expand Down Expand Up @@ -117,7 +119,12 @@ def make_transformation(
depends_on_to_relative_path(depends_on, self._obj.parent.name)
)
return transform
except (sc.DimensionError, sc.UnitError, TransformationError):
except (sc.DimensionError, sc.UnitError, TransformationError) as e:
msg = (
f"Failed to convert {self.name} into a transformation: {e} "
"Falling back to returning underlying value."
)
warnings.warn(msg, stacklevel=2)
# TODO We should probably try to return some other data structure and
# also insert offset and other attributes.
return value
Expand Down
23 changes: 23 additions & 0 deletions tests/nxtransformations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,26 @@ def test_compute_positions_handles_empty_time_dependent_transform_without_error(
assert position.coords['time'].dtype == sc.DType.datetime64

assert 'position' not in result['detector_0']['data'].coords


def test_compute_transformation_warns_if_transformation_missing_vector_attr(
h5root,
) -> None:
detector = create_detector(h5root)
snx.create_field(
detector, 'depends_on', sc.scalar('/detector_0/transformations/t1')
)
transformations = snx.create_class(detector, 'transformations', NXtransformations)

offset1 = sc.spatial.translation(value=[1, 2, 3], unit='m')
value1 = snx.create_class(transformations, 't1', snx.NXlog)
snx.create_field(value1, 'time', _log.coords['time'] - sc.epoch(unit='s'))
snx.create_field(value1, 'value', _log.data)
value1.attrs['depends_on'] = 't2'
value1.attrs['transformation_type'] = 'rotation'
value1.attrs['offset'] = offset1.values
value1.attrs['offset_units'] = str(offset1.unit)

root = make_group(h5root)
with pytest.warns(UserWarning, match='transformation needs a vector attribute'):
root[()]