Skip to content

Commit

Permalink
require name for Chopper constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
nvaytet committed Nov 27, 2024
1 parent ec9a76f commit 452d315
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/tof/chopper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Chopper:
The frequency of the chopper. Must be positive.
distance:
The distance from the source to the chopper.
name:
The name of the chopper.
phase:
The phase of the chopper. Because the phase offset implemented as a time delay
on real beamline choppers, it is applied in the opposite direction
Expand All @@ -46,8 +48,6 @@ class Chopper:
The centers of the chopper cutouts.
widths:
The widths of the chopper cutouts.
name:
The name of the chopper.
Notes
-----
Expand All @@ -59,13 +59,13 @@ def __init__(
*,
frequency: sc.Variable,
distance: sc.Variable,
phase: sc.Variable,
name: str,
phase: sc.Variable = sc.scalar(0.0, unit='rad'),
open: Optional[sc.Variable] = None,
close: Optional[sc.Variable] = None,
centers: Optional[sc.Variable] = None,
widths: Optional[sc.Variable] = None,
direction: Direction = Clockwise,
name: str = "",
):
if frequency <= (0.0 * frequency.unit):
raise ValueError(f"Chopper frequency must be positive, got {frequency:c}.")
Expand Down
2 changes: 1 addition & 1 deletion src/tof/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Detector:
The name of the detector.
"""

def __init__(self, distance: sc.Variable, name: str = "detector"):
def __init__(self, distance: sc.Variable, name: str):
self.distance = distance.to(dtype=float, copy=False)
self.name = name

Expand Down
22 changes: 22 additions & 0 deletions tests/chopper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_angular_speed():
close=10.0 * deg,
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)
assert chopper.omega == two_pi * f

Expand All @@ -34,6 +35,7 @@ def test_open_close_times_one_rotation():
close=sc.array(dims=['cutout'], values=[20.0], unit='deg'),
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)

topen, tclose = chopper.open_close_times(0 * sec)
Expand All @@ -55,6 +57,7 @@ def test_open_close_times_three_rotations():
close=sc.array(dims=['cutout'], values=[20.0], unit='deg'),
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)

topen, tclose = chopper.open_close_times(0.21 * sec)
Expand Down Expand Up @@ -85,6 +88,7 @@ def test_open_close_angles_scalars_converted_to_arrays():
close=20.0 * deg,
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)
topen, tclose = chopper.open_close_times(0.0 * sec)
assert sc.identical(topen[1], (10.0 * deg).to(unit='rad') / (two_pi * f))
Expand All @@ -99,6 +103,7 @@ def test_phase():
close=sc.array(dims=['cutout'], values=[20.0], unit='deg'),
phase=0.0 * deg,
distance=10.0 * meter,
name='choppe1',
)
topen1, tclose1 = chopper1.open_close_times(0.0 * sec)
chopper2 = tof.Chopper(
Expand All @@ -107,6 +112,7 @@ def test_phase():
close=sc.array(dims=['cutout'], values=[20.0], unit='deg'),
phase=30.0 * deg,
distance=10.0 * meter,
name='chopper2',
)
topen2, tclose2 = chopper2.open_close_times(0.0 * sec)
assert sc.allclose(topen2, topen1 + (30.0 * deg).to(unit='rad') / chopper2.omega)
Expand All @@ -124,13 +130,15 @@ def test_phase_int():
close=cl,
phase=30.0 * deg,
distance=d,
name='chopper1',
)
chopper2 = tof.Chopper(
frequency=f,
open=op,
close=cl,
phase=30 * deg,
distance=d,
name='chopper2',
)
topen1, tclose1 = chopper1.open_close_times(0.0 * sec)
topen2, tclose2 = chopper2.open_close_times(0.0 * sec)
Expand All @@ -146,6 +154,7 @@ def test_frequency_must_be_positive():
close=10.0 * deg,
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)


Expand All @@ -159,6 +168,7 @@ def test_open_close_times_counter_rotation():
close=sc.array(dims=['cutout'], values=[20.0, 130.0], unit='deg'),
phase=ph,
distance=d,
name='chopper1',
)
chopper2 = tof.Chopper(
frequency=f,
Expand All @@ -171,6 +181,7 @@ def test_open_close_times_counter_rotation():
phase=ph,
distance=d,
direction=tof.AntiClockwise,
name='chopper2',
)

topen1, tclose1 = chopper1.open_close_times(0.0 * sec)
Expand All @@ -188,6 +199,7 @@ def test_open_close_times_counter_rotation_with_phase():
phase=0.0 * deg,
distance=10.0 * meter,
direction=tof.AntiClockwise,
name='chopper1',
)
topen1, tclose1 = chopper1.open_close_times(0.0 * sec)
chopper2 = tof.Chopper(
Expand All @@ -197,6 +209,7 @@ def test_open_close_times_counter_rotation_with_phase():
phase=30.0 * deg,
distance=10.0 * meter,
direction=tof.AntiClockwise,
name='chopper2',
)
topen2, tclose2 = chopper2.open_close_times(0.0 * sec)
assert sc.allclose(
Expand All @@ -215,6 +228,7 @@ def test_open_close_anticlockwise_multiple_rotations():
distance=10.0 * meter,
phase=0 * deg,
direction=tof.AntiClockwise,
name='chopper',
)

two_rotations_open, two_rotations_close = chopper.open_close_times(0.0 * sec)
Expand Down Expand Up @@ -249,6 +263,7 @@ def test_bad_direction_raises():
phase=ph,
distance=d,
direction=tof.Clockwise,
name='chopper',
)
tof.Chopper(
frequency=f,
Expand All @@ -257,6 +272,7 @@ def test_bad_direction_raises():
phase=ph,
distance=d,
direction=tof.AntiClockwise,
name='chopper',
)
with pytest.raises(
ValueError, match="Chopper direction must be Clockwise or AntiClockwise"
Expand All @@ -268,6 +284,7 @@ def test_bad_direction_raises():
phase=ph,
distance=d,
direction='clockwise',
name='chopper',
)
with pytest.raises(
ValueError, match="Chopper direction must be Clockwise or AntiClockwise"
Expand All @@ -279,6 +296,7 @@ def test_bad_direction_raises():
phase=ph,
distance=d,
direction='anti-clockwise',
name='chopper',
)
with pytest.raises(
ValueError, match="Chopper direction must be Clockwise or AntiClockwise"
Expand All @@ -290,6 +308,7 @@ def test_bad_direction_raises():
phase=ph,
distance=d,
direction=1,
name='chopper',
)


Expand All @@ -303,6 +322,7 @@ def test_chopper_create_from_centers_and_widths():
widths=widths,
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)
assert sc.allclose(chopper.open, centers - widths / 2)
assert sc.allclose(chopper.close, centers + widths / 2)
Expand All @@ -313,6 +333,7 @@ def test_chopper_create_from_centers_and_widths():
close=sc.array(dims=['cutout'], values=[20.0, 54.0], unit='deg'),
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)
assert sc.allclose(chopper.open, expected.open)
assert sc.allclose(chopper.close, expected.close)
Expand All @@ -330,4 +351,5 @@ def test_chopper_create_raises_when_both_edges_and_centers_are_supplied():
widths=10.0 * deg,
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)

0 comments on commit 452d315

Please sign in to comment.