Skip to content

Commit 7c2bcb1

Browse files
authored
Merge pull request #180 from scipp/wrapped-tof-lookup
Make a wrapped lookup table to time-of-flight
2 parents 01125a2 + fa28370 commit 7c2bcb1

File tree

19 files changed

+802
-949
lines changed

19 files changed

+802
-949
lines changed

docs/user-guide/tof/dream.ipynb

+39-42
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239
"raw_data = ess_beamline.get_monitor(\"detector\")[0]\n",
240240
"\n",
241241
"# Visualize\n",
242-
"raw_data.hist(event_time_offset=300).sum(\"pulse\").plot()"
242+
"raw_data.hist(event_time_offset=300).squeeze().plot()"
243243
]
244244
},
245245
{
@@ -290,8 +290,10 @@
290290
" time_of_flight.providers(), params=time_of_flight.default_parameters()\n",
291291
")\n",
292292
"workflow[time_of_flight.RawData] = raw_data\n",
293-
"workflow[time_of_flight.LtotalRange] = (sc.scalar(75.5, unit='m'),\n",
294-
" sc.scalar(78.0, unit='m'))\n",
293+
"workflow[time_of_flight.LtotalRange] = (\n",
294+
" sc.scalar(75.5, unit=\"m\"),\n",
295+
" sc.scalar(78.0, unit=\"m\"),\n",
296+
")\n",
295297
"\n",
296298
"workflow.visualize(time_of_flight.TofData)"
297299
]
@@ -314,8 +316,7 @@
314316
"outputs": [],
315317
"source": [
316318
"workflow[time_of_flight.SimulationResults] = time_of_flight.simulate_beamline(\n",
317-
" choppers=disk_choppers,\n",
318-
" neutrons=2_000_000\n",
319+
" choppers=disk_choppers, neutrons=2_000_000\n",
319320
")"
320321
]
321322
},
@@ -343,17 +344,24 @@
343344
"outputs": [],
344345
"source": [
345346
"sim = workflow.compute(time_of_flight.SimulationResults)\n",
346-
"# Compute time-of-arrival at the detector\n",
347-
"tarrival = sim.time_of_arrival + ((Ltotal - sim.distance) / sim.speed).to(unit=\"us\")\n",
348-
"# Compute time-of-flight at the detector\n",
349-
"tflight = (Ltotal / sim.speed).to(unit=\"us\")\n",
350-
"\n",
351-
"events = sc.DataArray(\n",
352-
" data=sim.weight,\n",
353-
" coords={\"wavelength\": sim.wavelength, \"toa\": tarrival, \"tof\": tflight},\n",
354-
")\n",
355-
"fig1 = events.hist(wavelength=300, toa=300).plot(norm=\"log\")\n",
356-
"fig2 = events.hist(tof=300, toa=300).plot(norm=\"log\")\n",
347+
"\n",
348+
"\n",
349+
"def to_event_time_offset(sim):\n",
350+
" # Compute event_time_offset at the detector\n",
351+
" eto = (\n",
352+
" sim.time_of_arrival + ((Ltotal - sim.distance) / sim.speed).to(unit=\"us\")\n",
353+
" ) % sc.scalar(1e6 / 14.0, unit=\"us\")\n",
354+
" # Compute time-of-flight at the detector\n",
355+
" tof = (Ltotal / sim.speed).to(unit=\"us\")\n",
356+
" return sc.DataArray(\n",
357+
" data=sim.weight,\n",
358+
" coords={\"wavelength\": sim.wavelength, \"event_time_offset\": eto, \"tof\": tof},\n",
359+
" )\n",
360+
"\n",
361+
"\n",
362+
"events = to_event_time_offset(sim)\n",
363+
"fig1 = events.hist(wavelength=300, event_time_offset=300).plot(norm=\"log\")\n",
364+
"fig2 = events.hist(tof=300, event_time_offset=300).plot(norm=\"log\")\n",
357365
"fig1 + fig2"
358366
]
359367
},
@@ -374,10 +382,10 @@
374382
"metadata": {},
375383
"outputs": [],
376384
"source": [
377-
"table = workflow.compute(time_of_flight.TimeOfFlightLookupTable)\n",
385+
"table = workflow.compute(time_of_flight.TimeOfFlightLookupTable).squeeze()\n",
378386
"\n",
379387
"# Overlay mean on the figure above\n",
380-
"table[\"distance\", 13].plot(ax=fig2.ax, color=\"C1\", ls='-', marker=None)"
388+
"table[\"distance\", 13].plot(ax=fig2.ax, color=\"C1\", ls=\"-\", marker=None)"
381389
]
382390
},
383391
{
@@ -447,7 +455,8 @@
447455
"# Define wavelength bin edges\n",
448456
"wavs = sc.linspace(\"wavelength\", 0.8, 4.6, 201, unit=\"angstrom\")\n",
449457
"\n",
450-
"wav_wfm.hist(wavelength=wavs).sum(\"pulse\").plot()"
458+
"histogrammed = wav_wfm.hist(wavelength=wavs).squeeze()\n",
459+
"histogrammed.plot()"
451460
]
452461
},
453462
{
@@ -473,7 +482,7 @@
473482
"\n",
474483
"pp.plot(\n",
475484
" {\n",
476-
" \"wfm\": wav_wfm.hist(wavelength=wavs).sum(\"pulse\"),\n",
485+
" \"wfm\": histogrammed,\n",
477486
" \"ground_truth\": ground_truth.hist(wavelength=wavs),\n",
478487
" }\n",
479488
")"
@@ -530,16 +539,12 @@
530539
"outputs": [],
531540
"source": [
532541
"raw_data = sc.concat(\n",
533-
" [ess_beamline.get_monitor(key)[0] for key in monitors.keys()],\n",
542+
" [ess_beamline.get_monitor(key)[0].squeeze() for key in monitors.keys()],\n",
534543
" dim=\"detector_number\",\n",
535544
")\n",
536545
"\n",
537546
"# Visualize\n",
538-
"pp.plot(\n",
539-
" sc.collapse(\n",
540-
" raw_data.hist(event_time_offset=300).sum(\"pulse\"), keep=\"event_time_offset\"\n",
541-
" )\n",
542-
")"
547+
"pp.plot(sc.collapse(raw_data.hist(event_time_offset=300), keep=\"event_time_offset\"))"
543548
]
544549
},
545550
{
@@ -655,22 +660,14 @@
655660
"source": [
656661
"# Update workflow\n",
657662
"workflow[time_of_flight.SimulationResults] = time_of_flight.simulate_beamline(\n",
658-
" choppers=disk_choppers,\n",
659-
" neutrons=2_000_000\n",
663+
" choppers=disk_choppers, neutrons=2_000_000\n",
660664
")\n",
661665
"workflow[time_of_flight.RawData] = ess_beamline.get_monitor(\"detector\")[0]\n",
662666
"\n",
663667
"sim = workflow.compute(time_of_flight.SimulationResults)\n",
664-
"# Compute time-of-arrival at the detector\n",
665-
"tarrival = sim.time_of_arrival + ((Ltotal - sim.distance) / sim.speed).to(unit=\"us\")\n",
666-
"# Compute time-of-flight at the detector\n",
667-
"tflight = (Ltotal / sim.speed).to(unit=\"us\")\n",
668-
"\n",
669-
"events = sc.DataArray(\n",
670-
" data=sim.weight,\n",
671-
" coords={\"wavelength\": sim.wavelength, \"toa\": tarrival, \"tof\": tflight},\n",
672-
")\n",
673-
"events.hist(wavelength=300, toa=300).plot(norm=\"log\")"
668+
"\n",
669+
"events = to_event_time_offset(sim)\n",
670+
"events.hist(wavelength=300, event_time_offset=300).plot(norm=\"log\")"
674671
]
675672
},
676673
{
@@ -696,7 +693,7 @@
696693
"metadata": {},
697694
"outputs": [],
698695
"source": [
699-
"table = workflow.compute(time_of_flight.TimeOfFlightLookupTable)\n",
696+
"table = workflow.compute(time_of_flight.TimeOfFlightLookupTable).squeeze()\n",
700697
"table.plot() / (sc.stddevs(table) / sc.values(table)).plot(norm=\"log\")"
701698
]
702699
},
@@ -720,9 +717,9 @@
720717
"metadata": {},
721718
"outputs": [],
722719
"source": [
723-
"workflow[time_of_flight.LookupTableRelativeErrorThreshold] = 1.0e-2\n",
720+
"workflow[time_of_flight.LookupTableRelativeErrorThreshold] = 0.01\n",
724721
"\n",
725-
"workflow.compute(time_of_flight.MaskedTimeOfFlightLookupTable).plot()"
722+
"workflow.compute(time_of_flight.TimeOfFlightLookupTable).squeeze().plot()"
726723
]
727724
},
728725
{
@@ -757,7 +754,7 @@
757754
"\n",
758755
"pp.plot(\n",
759756
" {\n",
760-
" \"wfm\": wav_wfm.hist(wavelength=wavs).sum(\"pulse\"),\n",
757+
" \"wfm\": wav_wfm.hist(wavelength=wavs).squeeze(),\n",
761758
" \"ground_truth\": ground_truth.hist(wavelength=wavs),\n",
762759
" }\n",
763760
")"

0 commit comments

Comments
 (0)