Skip to content

Commit

Permalink
fix: warn if missing vector in transformation (#242)
Browse files Browse the repository at this point in the history
* fix: warn if missing vector in transformation
  • Loading branch information
jokasimr authored Sep 26, 2024
1 parent 94779e6 commit cd17d10
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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[()]

0 comments on commit cd17d10

Please sign in to comment.