Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 38 additions & 39 deletions packages/essreduce/docs/user-guide/tof/dream.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"import scippnexus as snx\n",
"from scippneutron.chopper import DiskChopper\n",
"from ess.reduce.nexus.types import AnyRun, RawDetector, SampleRun, NeXusDetectorName\n",
"from ess.reduce.time_of_flight import *"
"from ess.reduce.kinematics import *"
]
},
{
Expand Down Expand Up @@ -201,7 +201,7 @@
"metadata": {},
"outputs": [],
"source": [
"from ess.reduce.time_of_flight.fakes import FakeBeamline\n",
"from ess.reduce.kinematics.fakes import FakeBeamline\n",
"\n",
"ess_beamline = FakeBeamline(\n",
" choppers=disk_choppers,\n",
Expand Down Expand Up @@ -299,14 +299,14 @@
"metadata": {},
"outputs": [],
"source": [
"wf = GenericTofWorkflow(run_types=[SampleRun], monitor_types=[])\n",
"wf = GenericWavelengthWorkflow(run_types=[SampleRun], monitor_types=[])\n",
"\n",
"wf[RawDetector[SampleRun]] = raw_data\n",
"wf[DetectorLtotal[SampleRun]] = Ltotal\n",
"wf[NeXusDetectorName] = 'dream_detector'\n",
"wf[LookupTableRelativeErrorThreshold] = {'dream_detector': float(\"inf\")}\n",
"\n",
"wf.visualize(TofDetector[SampleRun])"
"wf.visualize(WavelengthDetector[SampleRun])"
]
},
{
Expand Down Expand Up @@ -342,14 +342,14 @@
"metadata": {},
"outputs": [],
"source": [
"lut_wf = TofLookupTableWorkflow()\n",
"lut_wf = LookupTableWorkflow()\n",
"lut_wf[DiskChoppers[AnyRun]] = disk_choppers\n",
"lut_wf[SourcePosition] = source_position\n",
"lut_wf[LtotalRange] = (\n",
" sc.scalar(5.0, unit=\"m\"),\n",
" sc.scalar(80.0, unit=\"m\"),\n",
")\n",
"lut_wf.visualize(TofLookupTable)"
"lut_wf.visualize(LookupTable)"
]
},
{
Expand Down Expand Up @@ -381,20 +381,19 @@
"def to_event_time_offset(sim):\n",
" # Compute event_time_offset at the detector\n",
" eto = (\n",
" sim.time_of_arrival + ((Ltotal - sim.distance) / sim.speed).to(unit=\"us\")\n",
" sim.time_of_arrival + ((lut_wf.compute(LtotalRange)[1] - sim.distance) / sim.speed).to(unit=\"us\")\n",
" ) % sc.scalar(1e6 / 14.0, unit=\"us\")\n",
" # Compute time-of-flight at the detector\n",
" tof = (Ltotal / sim.speed).to(unit=\"us\")\n",
" # # Compute time-of-flight at the detector\n",
" # tof = (Ltotal / sim.speed).to(unit=\"us\")\n",
" return sc.DataArray(\n",
" data=sim.weight,\n",
" coords={\"wavelength\": sim.wavelength, \"event_time_offset\": eto, \"tof\": tof},\n",
" coords={\"wavelength\": sim.wavelength, \"event_time_offset\": eto},\n",
" )\n",
"\n",
"\n",
"events = to_event_time_offset(sim.readings[\"t0\"])\n",
"fig1 = events.hist(wavelength=300, event_time_offset=300).plot(norm=\"log\")\n",
"fig2 = events.hist(tof=300, event_time_offset=300).plot(norm=\"log\")\n",
"fig1 + fig2"
"fig = events.hist(wavelength=300, event_time_offset=300).plot(norm=\"log\")\n",
"fig"
]
},
{
Expand All @@ -414,10 +413,10 @@
"metadata": {},
"outputs": [],
"source": [
"table = lut_wf.compute(TofLookupTable)\n",
"table = lut_wf.compute(LookupTable)\n",
"\n",
"# Overlay mean on the figure above\n",
"table.array[\"distance\", -1].plot(ax=fig2.ax, color=\"C1\", ls=\"-\", marker=None)"
"table.array[\"distance\", -1].plot(ax=fig.ax, color=\"C1\", ls=\"-\", marker=None)"
]
},
{
Expand Down Expand Up @@ -456,11 +455,11 @@
"outputs": [],
"source": [
"# Set the computed lookup table onto the original workflow\n",
"wf[TofLookupTable] = table\n",
"wf[LookupTable] = table\n",
"\n",
"# Compute time-of-flight of neutron events\n",
"tofs = wf.compute(TofDetector[SampleRun])\n",
"tofs"
"wavs = wf.compute(WavelengthDetector[SampleRun])\n",
"wavs"
]
},
{
Expand All @@ -478,7 +477,7 @@
"metadata": {},
"outputs": [],
"source": [
"tofs.bins.concat().hist(tof=300).plot()"
"# tofs.bins.concat().hist(tof=300).plot()"
]
},
{
Expand All @@ -498,17 +497,17 @@
"metadata": {},
"outputs": [],
"source": [
"from scippneutron.conversion.graph.beamline import beamline\n",
"from scippneutron.conversion.graph.tof import elastic\n",
"# from scippneutron.conversion.graph.beamline import beamline\n",
"# from scippneutron.conversion.graph.tof import elastic\n",
"\n",
"# Perform coordinate transformation\n",
"graph = {**beamline(scatter=False), **elastic(\"tof\")}\n",
"wav_wfm = tofs.transform_coords(\"wavelength\", graph=graph)\n",
"# # Perform coordinate transformation\n",
"# graph = {**beamline(scatter=False), **elastic(\"tof\")}\n",
"# wav_wfm = tofs.transform_coords(\"wavelength\", graph=graph)\n",
"\n",
"# Define wavelength bin edges\n",
"wavs = sc.linspace(\"wavelength\", 0.8, 4.6, 201, unit=\"angstrom\")\n",
"edges = sc.linspace(\"wavelength\", 0.8, 4.6, 201, unit=\"angstrom\")\n",
"\n",
"histogrammed = wav_wfm.hist(wavelength=wavs).squeeze()\n",
"histogrammed = wavs.hist(wavelength=edges).squeeze()\n",
"histogrammed.plot()"
]
},
Expand Down Expand Up @@ -536,7 +535,7 @@
"pp.plot(\n",
" {\n",
" \"wfm\": histogrammed,\n",
" \"ground_truth\": ground_truth.hist(wavelength=wavs),\n",
" \"ground_truth\": ground_truth.hist(wavelength=edges),\n",
" }\n",
")"
]
Expand Down Expand Up @@ -622,8 +621,8 @@
"wf[DetectorLtotal[SampleRun]] = Ltotal\n",
"\n",
"# Compute tofs and wavelengths\n",
"tofs = wf.compute(TofDetector[SampleRun])\n",
"wav_wfm = tofs.transform_coords(\"wavelength\", graph=graph)\n",
"wav_wfm = wf.compute(WavelengthDetector[SampleRun])\n",
"# wav_wfm = tofs.transform_coords(\"wavelength\", graph=graph)\n",
"\n",
"# Compare in plot\n",
"ground_truth = []\n",
Expand All @@ -634,8 +633,8 @@
"figs = [\n",
" pp.plot(\n",
" {\n",
" \"wfm\": wav_wfm[\"detector_number\", i].bins.concat().hist(wavelength=wavs),\n",
" \"ground_truth\": ground_truth[i].hist(wavelength=wavs),\n",
" \"wfm\": wav_wfm[\"detector_number\", i].bins.concat().hist(wavelength=edges),\n",
" \"ground_truth\": ground_truth[i].hist(wavelength=edges),\n",
" },\n",
" title=f\"Pixel {i+1}\",\n",
" )\n",
Expand Down Expand Up @@ -747,7 +746,7 @@
"metadata": {},
"outputs": [],
"source": [
"table = lut_wf.compute(TofLookupTable)\n",
"table = lut_wf.compute(LookupTable)\n",
"table.plot(ymin=65) / (sc.stddevs(table.array) / sc.values(table.array)).plot(norm=\"linear\", ymin=55, vmax=0.05)"
]
},
Expand All @@ -771,11 +770,11 @@
"metadata": {},
"outputs": [],
"source": [
"wf[TofLookupTable] = table\n",
"wf[LookupTable] = table\n",
"\n",
"wf[LookupTableRelativeErrorThreshold] = {'dream_detector': 0.01}\n",
"\n",
"masked_table = wf.compute(ErrorLimitedTofLookupTable[snx.NXdetector])\n",
"masked_table = wf.compute(ErrorLimitedLookupTable[snx.NXdetector])\n",
"masked_table.plot(ymin=65)"
]
},
Expand Down Expand Up @@ -804,18 +803,18 @@
"wf[DetectorLtotal[SampleRun]] = Ltotal\n",
"\n",
"# Compute time-of-flight\n",
"tofs = wf.compute(TofDetector[SampleRun])\n",
"wav_wfm = wf.compute(WavelengthDetector[SampleRun])\n",
"# Compute wavelength\n",
"wav_wfm = tofs.transform_coords(\"wavelength\", graph=graph)\n",
"# wav_wfm = tofs.transform_coords(\"wavelength\", graph=graph)\n",
"\n",
"# Compare to the true wavelengths\n",
"ground_truth = ess_beamline.model_result[\"detector\"].data.flatten(to=\"event\")\n",
"ground_truth = ground_truth[~ground_truth.masks[\"blocked_by_others\"]]\n",
"\n",
"pp.plot(\n",
" {\n",
" \"wfm\": wav_wfm.hist(wavelength=wavs).squeeze(),\n",
" \"ground_truth\": ground_truth.hist(wavelength=wavs),\n",
" \"wfm\": wav_wfm.hist(wavelength=edges).squeeze(),\n",
" \"ground_truth\": ground_truth.hist(wavelength=edges),\n",
" }\n",
")"
]
Expand All @@ -837,7 +836,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.12.12"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions packages/essreduce/src/ess/reduce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import importlib.metadata

from . import nexus, normalization, time_of_flight, uncertainty
from . import nexus, normalization, uncertainty, unwrap

try:
__version__ = importlib.metadata.version("essreduce")
Expand All @@ -12,4 +12,4 @@

del importlib

__all__ = ["nexus", "normalization", "time_of_flight", "uncertainty"]
__all__ = ["nexus", "normalization", "uncertainty", "unwrap"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# Copyright (c) 2025 Scipp contributors (https://github.com/scipp)

"""
Utilities for computing real neutron time-of-flight from chopper settings and
Utilities for computing neutron wavelength from chopper settings and
neutron time-of-arrival at the detectors.
"""

from ..nexus.types import DiskChoppers
from .eto_to_tof import providers
from .lut import (
BeamlineComponentReading,
DistanceResolution,
LookupTableWorkflow,
LtotalRange,
NumberOfSimulatedNeutrons,
PulsePeriod,
Expand All @@ -19,35 +19,38 @@
SimulationSeed,
SourcePosition,
TimeResolution,
TofLookupTableWorkflow,
simulate_chopper_cascade_using_tof,
)
from .to_wavelength import providers
from .types import (
DetectorLtotal,
ErrorLimitedTofLookupTable,
ErrorLimitedLookupTable,
LookupTable,
LookupTableFilename,
LookupTableRelativeErrorThreshold,
MonitorLtotal,
PulseStrideOffset,
TimeOfFlightLookupTable,
TimeOfFlightLookupTableFilename,
ToaDetector,
TofDetector,
TofLookupTable,
TofLookupTableFilename,
TofMonitor,
# ToaDetector,
# TofDetector,
# TofLookupTable,
# TofLookupTableFilename,
# TofMonitor,
WavelengthDetector,
WavelengthMonitor,
)
from .workflow import GenericTofWorkflow
from .workflow import GenericUnwrapWorkflow

__all__ = [
"BeamlineComponentReading",
"DetectorLtotal",
"DiskChoppers",
"DistanceResolution",
"ErrorLimitedTofLookupTable",
"GenericTofWorkflow",
"ErrorLimitedLookupTable",
"GenericUnwrapWorkflow",
"LookupTable",
"LookupTableFilename",
"LookupTableRelativeErrorThreshold",
"LookupTableWorkflow",
"LtotalRange",
"MonitorLtotal",
"NumberOfSimulatedNeutrons",
Expand All @@ -57,15 +60,7 @@
"SimulationResults",
"SimulationSeed",
"SourcePosition",
"TimeOfFlightLookupTable",
"TimeOfFlightLookupTableFilename",
"TimeResolution",
"ToaDetector",
"TofDetector",
"TofLookupTable",
"TofLookupTableFilename",
"TofLookupTableWorkflow",
"TofMonitor",
"WavelengthDetector",
"WavelengthMonitor",
"providers",
Expand Down
Loading
Loading