Skip to content

Commit

Permalink
Merge pull request #247 from scipp/transform-sizes
Browse files Browse the repository at this point in the history
Add Transform.sizes property for nicer downstream behavior
  • Loading branch information
SimonHeybrock authored Nov 4, 2024
2 parents 23656d1 + c279f79 commit c8c1205
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/scippnexus/nxtransformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ class Transform:
value: sc.Variable | sc.DataArray | sc.DataGroup
vector: sc.Variable
depends_on: DependsOn
offset: sc.Variable | None
offset: sc.Variable | None = None

@property
def sizes(self) -> dict[str, int]:
"""Convenience property to access sizes of the value."""
return self.value.sizes

def __post_init__(self):
if self.transformation_type not in ['translation', 'rotation']:
Expand Down
38 changes: 38 additions & 0 deletions tests/transform_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest
import scipp as sc

from scippnexus import DependsOn
from scippnexus.nxtransformations import Transform, TransformationError


@pytest.fixture()
def depends_on() -> DependsOn:
return DependsOn(parent='/', value='.')


@pytest.fixture()
def z_vector() -> sc.Variable:
return sc.vector(value=[0, 0, 1], unit='m')


def test_init_raises_if_transformation_type_is_invalid(depends_on, z_vector) -> None:
with pytest.raises(TransformationError, match='transformation_type'):
Transform(
name='t1',
transformation_type='trans',
value=sc.ones(dims=['x', 'y', 'z'], shape=(2, 3, 4)),
vector=z_vector,
depends_on=depends_on,
)


def test_sizes_returns_sizes_of_value(depends_on, z_vector) -> None:
value = sc.ones(dims=['x', 'y', 'z'], shape=(2, 3, 4))
transform = Transform(
name='t1',
transformation_type='translation',
value=value,
vector=z_vector,
depends_on=depends_on,
)
assert transform.sizes == {'x': 2, 'y': 3, 'z': 4}

0 comments on commit c8c1205

Please sign in to comment.