Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use time-of-flight workflow in Dream data reduction #125

Merged
merged 40 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fa91262
incorporate tof computation in dream reduction workflow
nvaytet Jan 15, 2025
6ec89f9
start making a wrapper provider
nvaytet Jan 16, 2025
ad70023
use the tof workflow in the dream reduction
nvaytet Jan 27, 2025
4f2ef80
start fixing tests
nvaytet Jan 27, 2025
0b57f54
mask zero counts in monitors to avoid infinite values everywhere in d…
nvaytet Jan 29, 2025
85f984d
use nanmin as bin edges can be nan after tof lookup
nvaytet Jan 29, 2025
6b34a2c
update more tests
nvaytet Jan 29, 2025
5d4c819
fix last tests
nvaytet Jan 30, 2025
3150b42
update essreduce version and add tof to deps
nvaytet Jan 30, 2025
2c5aa16
add tof to nightly deps
nvaytet Jan 30, 2025
9a83342
update deps
nvaytet Jan 30, 2025
3bdb149
fix language in notebook
nvaytet Jan 30, 2025
3f83797
add dummy tof provider for powgen to skip the tof workflow step
nvaytet Jan 30, 2025
e214f05
fix powgen tests
nvaytet Jan 30, 2025
3c7e826
fix dream notebook
nvaytet Jan 30, 2025
5927e57
add tof where it was missing
nvaytet Jan 30, 2025
7b145e1
make tof a runtime dependency
nvaytet Jan 31, 2025
0164cf9
explain better in assemble data and make it robust to more pulses
nvaytet Jan 31, 2025
5ff4ec8
merge NaN step in workflow and remove dummy provider in powgen
nvaytet Jan 31, 2025
f245f0e
remove left over providers from __init__
nvaytet Jan 31, 2025
e87d443
fix workflow by removing DetectorData
nvaytet Jan 31, 2025
d7c8ec0
start using new version of tof workflow after update
nvaytet Feb 13, 2025
e8a3267
use correct choppers and make Ltotal separate for detectors and monitors
nvaytet Feb 14, 2025
95147fb
static analysis
nvaytet Feb 14, 2025
ebcefaa
switch to using a filename parameter instead of having to load the ta…
nvaytet Feb 14, 2025
a274a34
fix dream geant4 tests
nvaytet Feb 14, 2025
b55f608
bump essreduce version
nvaytet Feb 17, 2025
bbabb72
merge main branch and update deps to get latest essreduce
nvaytet Feb 17, 2025
6ebbd44
fix init file
nvaytet Feb 17, 2025
7a814df
fix workflow widgets notebook
nvaytet Feb 17, 2025
579d87b
fix metadata stuff from new scippneutron
nvaytet Feb 17, 2025
1e36615
fix orcid_id in tests
nvaytet Feb 17, 2025
55ec664
update type in POWGEN notebook
nvaytet Feb 17, 2025
a9bc401
Merge branch 'main' into tof-workflow
nvaytet Feb 18, 2025
017d31c
fix dependency files
nvaytet Feb 18, 2025
ead136d
Merge branch 'main' into tof-workflow
nvaytet Feb 19, 2025
f2e9894
move notebook to create tof lookup table and add comment about how ta…
nvaytet Feb 20, 2025
b134104
Apply suggestions from code review
nvaytet Feb 20, 2025
85597b8
Merge branch 'tof-workflow' of github.com:scipp/essdiffraction into t…
nvaytet Feb 20, 2025
5c4b3f8
Apply automatic formatting
pre-commit-ci-lite[bot] Feb 20, 2025
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
2 changes: 1 addition & 1 deletion .copier-answers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Diffraction data reduction for the European Spallation Source
max_python: '3.13'
min_python: '3.10'
namespace_package: ess
nightly_deps: scipp,scippnexus,sciline,plopp,scippneutron,essreduce
nightly_deps: scipp,scippnexus,sciline,plopp,scippneutron,essreduce,tof
orgname: scipp
prettyname: ESSdiffraction
projectname: essdiffraction
Expand Down
76 changes: 26 additions & 50 deletions docs/user-guide/dream/dream-data-reduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"outputs": [],
"source": [
"import scipp as sc\n",
"\n",
"from ess import dream, powder\n",
"import ess.dream.data # noqa: F401\n",
"from ess.powder.types import *"
Expand Down Expand Up @@ -71,22 +70,22 @@
"workflow[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.drop\n",
"# Edges for binning in d-spacing\n",
"workflow[DspacingBins] = sc.linspace(\"dspacing\", 0.0, 2.3434, 201, unit=\"angstrom\")\n",
"# Mask in time-of-flight to crop to valid range\n",
"workflow[TofMask] = lambda x: (x < sc.scalar(0.0, unit=\"ns\")) | (\n",
" x > sc.scalar(86e6, unit=\"ns\")\n",
")\n",
"# Empty masks by default\n",
"workflow[TofMask] = None\n",
"workflow[TwoThetaMask] = None\n",
"workflow[WavelengthMask] = None\n",
"# No pixel masks\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow = powder.with_pixel_mask_filenames(workflow, [])\n",
"# Time-of-flight lookup table\n",
"workflow[TimeOfFlightLookupTableFilename] = dream.data.tof_lookup_table_high_flux()"
]
},
{
"cell_type": "markdown",
"id": "6",
"metadata": {},
"source": [
"## Use the workflow\n",
"## Use the reduction workflow\n",
"\n",
"We can visualize the graph for computing the final normalized result for intensity as a function of time-of-flight:"
]
Expand Down Expand Up @@ -152,7 +151,7 @@
"in the documentation of ESSdiffraction.\n",
"See https://scipp.github.io/essdiffraction/\n",
"\"\"\"\n",
"cif_data.save('reduced.cif')"
"cif_data.save(\"reduced.cif\")"
]
},
{
Expand Down Expand Up @@ -192,9 +191,9 @@
"outputs": [],
"source": [
"two_theta = sc.linspace(\"two_theta\", 0.8, 2.4, 301, unit=\"rad\")\n",
"intermediates[MaskedData[SampleRun]].hist(two_theta=two_theta, wavelength=300).plot(\n",
" norm=\"log\"\n",
")"
"intermediates[MaskedData[SampleRun]].hist(\n",
" two_theta=two_theta, wavelength=300\n",
").plot(norm=\"log\")"
]
},
{
Expand All @@ -216,7 +215,7 @@
"outputs": [],
"source": [
"workflow[TwoThetaBins] = sc.linspace(\n",
" dim=\"two_theta\", unit=\"rad\", start=0.8, stop=2.4, num=17\n",
" dim=\"two_theta\", unit=\"rad\", start=0.8, stop=2.4, num=201\n",
")"
]
},
Expand All @@ -237,31 +236,13 @@
"id": "19",
"metadata": {},
"outputs": [],
"source": [
"angle = sc.midpoints(grouped_dspacing.coords[\"two_theta\"])\n",
"sc.plot(\n",
" {\n",
" f\"{angle[group].value:.3f} {angle[group].unit}\": grouped_dspacing[\n",
" \"two_theta\", group\n",
" ].hist()\n",
" for group in range(2, 6)\n",
" }\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20",
"metadata": {},
"outputs": [],
"source": [
"grouped_dspacing.hist().plot(norm=\"log\")"
]
},
{
"cell_type": "markdown",
"id": "21",
"id": "20",
"metadata": {},
"source": [
"## Normalizing by monitor\n",
Expand All @@ -283,7 +264,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "22",
"id": "21",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -292,7 +273,7 @@
},
{
"cell_type": "markdown",
"id": "23",
"id": "22",
"metadata": {},
"source": [
"In addition to the parameters used before, we also need to provide filenames for monitor data and a position of the monitor as that is not saved in the simulation files:"
Expand All @@ -301,14 +282,14 @@
{
"cell_type": "code",
"execution_count": null,
"id": "24",
"id": "23",
"metadata": {},
"outputs": [],
"source": [
"workflow[MonitorFilename[SampleRun]] = dream.data.simulated_monitor_diamond_sample()\n",
"workflow[MonitorFilename[VanadiumRun]] = dream.data.simulated_monitor_vanadium_sample()\n",
"workflow[MonitorFilename[BackgroundRun]] = dream.data.simulated_monitor_empty_can()\n",
"workflow[CaveMonitorPosition] = sc.vector([0.0, 0.0, -4220.0], unit='mm')\n",
"workflow[CaveMonitorPosition] = sc.vector([0.0, 0.0, -4220.0], unit=\"mm\")\n",
"\n",
"# These are the same as at the top of the notebook:\n",
"workflow[Filename[SampleRun]] = dream.data.simulated_diamond_sample()\n",
Expand All @@ -318,18 +299,17 @@
"workflow[NeXusDetectorName] = \"mantle\"\n",
"workflow[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.drop\n",
"workflow[DspacingBins] = sc.linspace(\"dspacing\", 0.0, 2.3434, 201, unit=\"angstrom\")\n",
"workflow[TofMask] = lambda x: (x < sc.scalar(0.0, unit=\"ns\")) | (\n",
" x > sc.scalar(86e6, unit=\"ns\")\n",
")\n",
"workflow[TofMask] = None\n",
"workflow[TwoThetaMask] = None\n",
"workflow[WavelengthMask] = None\n",
"workflow = powder.with_pixel_mask_filenames(workflow, [])"
"workflow = powder.with_pixel_mask_filenames(workflow, [])\n",
"workflow[TimeOfFlightLookupTableFilename] = dream.data.tof_lookup_table_high_flux()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25",
"id": "24",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -339,7 +319,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "26",
"id": "25",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -352,7 +332,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "27",
"id": "26",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -361,7 +341,7 @@
},
{
"cell_type": "markdown",
"id": "28",
"id": "27",
"metadata": {},
"source": [
"Comparing the final, normalized result shows that it agrees with the data that was normalized by proton-charge:"
Expand All @@ -370,14 +350,11 @@
{
"cell_type": "code",
"execution_count": null,
"id": "29",
"id": "28",
"metadata": {},
"outputs": [],
"source": [
"sc.plot({\n",
" 'By proton charge': histogram,\n",
" 'By monitor': normalized_by_monitor.hist()\n",
"})"
"sc.plot({\"By proton charge\": histogram, \"By monitor\": normalized_by_monitor.hist()})"
]
}
],
Expand All @@ -396,8 +373,7 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
147 changes: 147 additions & 0 deletions docs/user-guide/dream/dream-make-tof-lookup-table.ipynb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this notebook supposed to be shown in the docs? If so it needs to be added to the ToC and needs some descriptions. If not, please move it somewhere else, e.g., /tools because it gets executed when building the docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I think I will actually move this to the essreduce docs, that shows in a generic way how to create a lookup table. I think moving it to a tools folder is good for now.

Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# Create a time-of-flight lookup table for DREAM"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1",
"metadata": {},
"outputs": [],
"source": [
"import scipp as sc\n",
"import sciline as sl\n",
"from ess.reduce import time_of_flight\n",
"from ess.dream.beamline import InstrumentConfiguration, choppers"
]
},
{
"cell_type": "markdown",
"id": "2",
"metadata": {},
"source": [
"## Select the choppers"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3",
"metadata": {},
"outputs": [],
"source": [
"disk_choppers = choppers(InstrumentConfiguration.high_flux)"
]
},
{
"cell_type": "markdown",
"id": "4",
"metadata": {},
"source": [
"## Setting up the workflow"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"metadata": {},
"outputs": [],
"source": [
"wf = sl.Pipeline(\n",
" time_of_flight.providers(), params=time_of_flight.default_parameters()\n",
")\n",
"\n",
"wf[time_of_flight.LtotalRange] = sc.scalar(60.0, unit=\"m\"), sc.scalar(80.0, unit=\"m\")\n",
"wf[time_of_flight.SimulationResults] = time_of_flight.simulate_beamline(\n",
" choppers=disk_choppers, neutrons=5_000_000\n",
")\n",
"\n",
"wf[time_of_flight.DistanceResolution] = sc.scalar(0.1, unit=\"m\")\n",
"wf[time_of_flight.TimeResolution] = sc.scalar(250.0, unit='us')\n",
"wf[time_of_flight.LookupTableRelativeErrorThreshold] = 0.02\n",
"# wf[time_of_flight.PulsePeriod] = 1.0 / sc.scalar(14.0, unit=\"Hz\")\n",
"# wf[time_of_flight.PulseStride] = 1\n",
"# wf[time_of_flight.PulseStrideOffset] = None"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"wf[time_of_flight.LookupTableRelativeErrorThreshold] = 0.02\n",
"# wf[time_of_flight.PulsePeriod] = 1.0 / sc.scalar(14.0, unit=\"Hz\")\n",
"# wf[time_of_flight.PulseStride] = 1\n",
"# wf[time_of_flight.PulseStrideOffset] = None"
"wf[time_of_flight.LookupTableRelativeErrorThreshold] = 0.02"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I uncommented them instead.

]
},
{
"cell_type": "markdown",
"id": "6",
"metadata": {},
"source": [
"## Compute the table"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"metadata": {},
"outputs": [],
"source": [
"table = wf.compute(time_of_flight.TimeOfFlightLookupTable)\n",
"table"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8",
"metadata": {},
"outputs": [],
"source": [
"table.squeeze().plot()"
]
},
{
"cell_type": "markdown",
"id": "9",
"metadata": {},
"source": [
"## Save to file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10",
"metadata": {},
"outputs": [],
"source": [
"# Save chopper metadata\n",
"# TODO: storing the choppers as a PyObject is skipped when saving to disk\n",
"table.coords['choppers'] = sc.scalar(disk_choppers)\n",
"# Write to file\n",
"table.save_hdf5('DREAM-high-flux-tof-lookup-table.h5')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
6 changes: 3 additions & 3 deletions docs/user-guide/dream/workflow-widget-dream.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"outputs": [],
"source": [
"from ess.powder.types import DspacingBins, Filename, SampleRun, VanadiumRun\n",
"from ess.powder.types import DspacingBins, Filename, SampleRun, TimeOfFlightLookupTableFilename, VanadiumRun\n",
"import ess.dream.data # noqa: F401\n",
"\n",
"select = widget.children[0].children[0]\n",
Expand All @@ -66,6 +66,7 @@
"# Set parameters\n",
"pbox._input_widgets[Filename[SampleRun]].children[0].value = dream.data.simulated_diamond_sample()\n",
"pbox._input_widgets[Filename[VanadiumRun]].children[0].value = dream.data.simulated_vanadium_sample()\n",
"pbox._input_widgets[TimeOfFlightLookupTableFilename].children[0].value = dream.data.tof_lookup_table_high_flux()\n",
"pbox._input_widgets[DspacingBins].fields[\"stop\"].value = 2.3434\n",
"# Run the workflow\n",
"rbox = wfw.result_box\n",
Expand Down Expand Up @@ -133,8 +134,7 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
Loading