Skip to content

Commit 975ef9b

Browse files
committed
add test with pulse skipping chopper phase offset by 180 deg
1 parent b9ed7b7 commit 975ef9b

File tree

2 files changed

+71
-23
lines changed

2 files changed

+71
-23
lines changed

src/ess/reduce/time_of_flight/fakes.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -174,25 +174,6 @@ def get_monitor(self, name: str) -> sc.DataGroup:
174174
radius=sc.scalar(30.0, unit="cm"),
175175
)
176176

177-
pulse_skipping = DiskChopper(
178-
frequency=sc.scalar(-7.0, unit="Hz"),
179-
beam_position=sc.scalar(0.0, unit="deg"),
180-
phase=sc.scalar(0.0, unit="deg"),
181-
axle_position=sc.vector(value=[0, 0, 30.0], unit="m"),
182-
slit_begin=sc.array(
183-
dims=["cutout"],
184-
values=np.array([40.0]),
185-
unit="deg",
186-
),
187-
slit_end=sc.array(
188-
dims=["cutout"],
189-
values=np.array([140.0]),
190-
unit="deg",
191-
),
192-
slit_height=sc.scalar(10.0, unit="cm"),
193-
radius=sc.scalar(30.0, unit="cm"),
194-
)
195-
196177

197178
def wfm_choppers():
198179
return {
@@ -218,3 +199,24 @@ def psc_choppers():
218199
)
219200
for name, ch in wfm_choppers().items()
220201
}
202+
203+
204+
def pulse_skipping_chopper():
205+
return DiskChopper(
206+
frequency=sc.scalar(-7.0, unit="Hz"),
207+
beam_position=sc.scalar(0.0, unit="deg"),
208+
phase=sc.scalar(0.0, unit="deg"),
209+
axle_position=sc.vector(value=[0, 0, 30.0], unit="m"),
210+
slit_begin=sc.array(
211+
dims=["cutout"],
212+
values=np.array([40.0]),
213+
unit="deg",
214+
),
215+
slit_end=sc.array(
216+
dims=["cutout"],
217+
values=np.array([140.0]),
218+
unit="deg",
219+
),
220+
slit_height=sc.scalar(10.0, unit="cm"),
221+
radius=sc.scalar(30.0, unit="cm"),
222+
)

tests/time_of_flight/unwrap_test.py

+50-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_standard_unwrap_histogram_mode(dist, dim) -> None:
148148
def test_pulse_skipping_unwrap() -> None:
149149
distance = sc.scalar(100.0, unit="m")
150150
choppers = fakes.psc_choppers()
151-
choppers["pulse_skipping"] = fakes.pulse_skipping
151+
choppers["pulse_skipping"] = fakes.pulse_skipping_chopper()
152152

153153
beamline = fakes.FakeBeamline(
154154
choppers=choppers,
@@ -189,10 +189,56 @@ def test_pulse_skipping_unwrap() -> None:
189189
assert sc.isclose(mon.data.nansum(), tofs.data.nansum(), rtol=sc.scalar(1.0e-3))
190190

191191

192+
def test_pulse_skipping_unwrap_180_phase_shift() -> None:
193+
distance = sc.scalar(100.0, unit="m")
194+
choppers = fakes.psc_choppers()
195+
choppers["pulse_skipping"] = fakes.pulse_skipping_chopper()
196+
choppers["pulse_skipping"].phase.value += 180.0
197+
198+
beamline = fakes.FakeBeamline(
199+
choppers=choppers,
200+
monitors={"detector": distance},
201+
run_length=sc.scalar(1.0, unit="s"),
202+
events_per_pulse=100_000,
203+
seed=4,
204+
)
205+
mon, ref = beamline.get_monitor("detector")
206+
207+
sim = time_of_flight.simulate_beamline(
208+
choppers=choppers, neutrons=300_000, pulses=2, seed=1234
209+
)
210+
211+
pl = sl.Pipeline(
212+
time_of_flight.providers(), params=time_of_flight.default_parameters()
213+
)
214+
215+
pl[time_of_flight.RawData] = mon
216+
pl[time_of_flight.SimulationResults] = sim
217+
pl[time_of_flight.LtotalRange] = distance, distance
218+
pl[time_of_flight.PulseStride] = 2
219+
pl[time_of_flight.PulseStrideOffset] = 1 # Start the stride at the second pulse
220+
221+
tofs = pl.compute(time_of_flight.TofData)
222+
223+
# Convert to wavelength
224+
graph = {**beamline_graph(scatter=False), **elastic_graph("tof")}
225+
wavs = tofs.transform_coords("wavelength", graph=graph).bins.concat().value
226+
227+
diff = abs(
228+
(wavs.coords["wavelength"] - ref.coords["wavelength"])
229+
/ ref.coords["wavelength"]
230+
)
231+
# All errors should be small
232+
assert np.nanpercentile(diff.values, 100) < 0.01
233+
# Make sure that we have not lost too many events (we lose some because they may be
234+
# given a NaN tof from the lookup).
235+
assert sc.isclose(mon.data.nansum(), tofs.data.nansum(), rtol=sc.scalar(1.0e-3))
236+
237+
192238
def test_pulse_skipping_unwrap_when_all_neutrons_arrive_after_second_pulse() -> None:
193239
distance = sc.scalar(150.0, unit="m")
194240
choppers = fakes.psc_choppers()
195-
choppers["pulse_skipping"] = fakes.pulse_skipping
241+
choppers["pulse_skipping"] = fakes.pulse_skipping_chopper()
196242

197243
beamline = fakes.FakeBeamline(
198244
choppers=choppers,
@@ -237,7 +283,7 @@ def test_pulse_skipping_unwrap_when_all_neutrons_arrive_after_second_pulse() ->
237283
def test_pulse_skipping_unwrap_when_first_half_of_first_pulse_is_missing() -> None:
238284
distance = sc.scalar(100.0, unit="m")
239285
choppers = fakes.psc_choppers()
240-
choppers["pulse_skipping"] = fakes.pulse_skipping
286+
choppers["pulse_skipping"] = fakes.pulse_skipping_chopper()
241287

242288
beamline = fakes.FakeBeamline(
243289
choppers=choppers,
@@ -307,7 +353,7 @@ def test_pulse_skipping_unwrap_when_first_half_of_first_pulse_is_missing() -> No
307353
def test_pulse_skipping_unwrap_histogram_mode() -> None:
308354
distance = sc.scalar(100.0, unit="m")
309355
choppers = fakes.psc_choppers()
310-
choppers["pulse_skipping"] = fakes.pulse_skipping
356+
choppers["pulse_skipping"] = fakes.pulse_skipping_chopper()
311357

312358
beamline = fakes.FakeBeamline(
313359
choppers=choppers,

0 commit comments

Comments
 (0)