Skip to content

Commit d18fae1

Browse files
Merge pull request #176 from scipp/restructure-workflow-for-streaming
Restructure workflow for streaming
2 parents f15b313 + af3dd1c commit d18fae1

File tree

25 files changed

+366
-229
lines changed

25 files changed

+366
-229
lines changed

docs/user-guide/common/vanadium_processing.ipynb

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
"\n",
7373
"workflow[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.drop\n",
7474
"\n",
75-
"workflow[TofMask] = lambda x: (x < sc.scalar(0.0, unit=\"us\")) | (x > sc.scalar(16666.67, unit=\"us\"))\n",
75+
"workflow[TofMask] = lambda x: (x < sc.scalar(0.0, unit=\"us\")) | (\n",
76+
" x > sc.scalar(16666.67, unit=\"us\")\n",
77+
")\n",
7678
"workflow[TwoThetaMask] = None\n",
7779
"workflow[WavelengthMask] = None\n",
7880
"\n",
@@ -114,7 +116,7 @@
114116
"metadata": {},
115117
"outputs": [],
116118
"source": [
117-
"peaked_data.hist().plot()"
119+
"peaked_data.plot()"
118120
]
119121
},
120122
{
@@ -150,42 +152,24 @@
150152
"cell_type": "markdown",
151153
"id": "10",
152154
"metadata": {},
153-
"source": [
154-
"We need to histogram the data to perform fits:"
155-
]
156-
},
157-
{
158-
"cell_type": "code",
159-
"execution_count": null,
160-
"id": "11",
161-
"metadata": {},
162-
"outputs": [],
163-
"source": [
164-
"peak_histogram = peaked_data.hist()"
165-
]
166-
},
167-
{
168-
"cell_type": "markdown",
169-
"id": "12",
170-
"metadata": {},
171155
"source": [
172156
"The fits require a bin-center coordinate, so convert from bin-edges:"
173157
]
174158
},
175159
{
176160
"cell_type": "code",
177161
"execution_count": null,
178-
"id": "13",
162+
"id": "11",
179163
"metadata": {},
180164
"outputs": [],
181165
"source": [
182-
"to_fit = peak_histogram.copy(deep=False)\n",
166+
"to_fit = peaked_data.copy(deep=False)\n",
183167
"to_fit.coords['dspacing'] = sc.midpoints(to_fit.coords['dspacing'])"
184168
]
185169
},
186170
{
187171
"cell_type": "markdown",
188-
"id": "14",
172+
"id": "12",
189173
"metadata": {},
190174
"source": [
191175
"Perform the fits:"
@@ -194,7 +178,7 @@
194178
{
195179
"cell_type": "code",
196180
"execution_count": null,
197-
"id": "15",
181+
"id": "13",
198182
"metadata": {},
199183
"outputs": [],
200184
"source": [
@@ -203,7 +187,7 @@
203187
},
204188
{
205189
"cell_type": "markdown",
206-
"id": "16",
190+
"id": "14",
207191
"metadata": {},
208192
"source": [
209193
"Remove the fitted peaks to obtain the incoherent scattering.\n",
@@ -218,18 +202,18 @@
218202
{
219203
"cell_type": "code",
220204
"execution_count": null,
221-
"id": "17",
205+
"id": "15",
222206
"metadata": {},
223207
"outputs": [],
224208
"source": [
225209
"incoherent = scn.peaks.remove_peaks(sc.values(to_fit), fit_results)\n",
226-
"incoherent.coords['dspacing'] = peak_histogram.coords['dspacing']\n",
210+
"incoherent.coords['dspacing'] = peaked_data.coords['dspacing']\n",
227211
"incoherent.plot()"
228212
]
229213
},
230214
{
231215
"cell_type": "markdown",
232-
"id": "18",
216+
"id": "16",
233217
"metadata": {},
234218
"source": [
235219
"We can further inspect the results.\n",
@@ -262,7 +246,7 @@
262246
{
263247
"cell_type": "code",
264248
"execution_count": null,
265-
"id": "19",
249+
"id": "17",
266250
"metadata": {},
267251
"outputs": [],
268252
"source": [
@@ -272,29 +256,36 @@
272256
" fit_results: list[scn.peaks.FitResult],\n",
273257
" peak_estimates: sc.Variable,\n",
274258
" *,\n",
275-
" xlim: tuple[sc.Variable, sc.Variable] | None=None,\n",
259+
" xlim: tuple[sc.Variable, sc.Variable] | None = None,\n",
276260
"):\n",
277261
" if xlim is not None:\n",
262+
"\n",
278263
" def in_range(x: sc.Variable) -> bool:\n",
279264
" return sc.isfinite(x) and (xlim[0] <= x) and (x < xlim[1])\n",
280-
" data = data[data.dim, xlim[0]:xlim[1]]\n",
281-
" removed = removed[removed.dim, xlim[0]:xlim[1]]\n",
282-
" fit_results, peak_estimates = zip(*(\n",
283-
" (r, e)\n",
284-
" for r, e in zip(fit_results, peak_estimates, strict=True)\n",
285-
" if in_range(r.window[0]) and in_range(r.window[1])\n",
286-
" ), strict=True)\n",
265+
"\n",
266+
" data = data[data.dim, xlim[0] : xlim[1]]\n",
267+
" removed = removed[removed.dim, xlim[0] : xlim[1]]\n",
268+
" fit_results, peak_estimates = zip(\n",
269+
" *(\n",
270+
" (r, e)\n",
271+
" for r, e in zip(fit_results, peak_estimates, strict=True)\n",
272+
" if in_range(r.window[0]) and in_range(r.window[1])\n",
273+
" ),\n",
274+
" strict=True,\n",
275+
" )\n",
287276
"\n",
288277
" # The actual data\n",
289278
" plot_data = {'data': data, 'removed': removed}\n",
290279
" linestyles = {}\n",
291280
" markers = {}\n",
292-
" colors = {'data': 'C0','removed': 'C2'}\n",
281+
" colors = {'data': 'C0', 'removed': 'C2'}\n",
293282
"\n",
294283
" # Overlay with fit models evaluated at optimized parameters\n",
295284
" for i, result in enumerate(fit_results):\n",
296285
" if all(not sc.isnan(param).value for param in result.popt.values()):\n",
297-
" best_fit = data[data.dim, result.window[0] : result.window[1]].copy(deep=False)\n",
286+
" best_fit = data[data.dim, result.window[0] : result.window[1]].copy(\n",
287+
" deep=False\n",
288+
" )\n",
298289
" best_fit.coords[best_fit.dim] = sc.midpoints(best_fit.coords[best_fit.dim])\n",
299290
" best_fit.data = result.eval_model(best_fit.coords[best_fit.dim])\n",
300291
"\n",
@@ -340,12 +331,12 @@
340331
{
341332
"cell_type": "code",
342333
"execution_count": null,
343-
"id": "20",
334+
"id": "18",
344335
"metadata": {},
345336
"outputs": [],
346337
"source": [
347338
"peak_removal_diagnostic(\n",
348-
" peak_histogram,\n",
339+
" peaked_data,\n",
349340
" incoherent,\n",
350341
" fit_results,\n",
351342
" peak_estimates,\n",
@@ -355,12 +346,12 @@
355346
{
356347
"cell_type": "code",
357348
"execution_count": null,
358-
"id": "21",
349+
"id": "19",
359350
"metadata": {},
360351
"outputs": [],
361352
"source": [
362353
"peak_removal_diagnostic(\n",
363-
" peak_histogram,\n",
354+
" peaked_data,\n",
364355
" incoherent,\n",
365356
" fit_results,\n",
366357
" peak_estimates,\n",
@@ -370,7 +361,7 @@
370361
},
371362
{
372363
"cell_type": "markdown",
373-
"id": "22",
364+
"id": "20",
374365
"metadata": {},
375366
"source": [
376367
"The resulting data array `incoherent` can be saved and used in the main workflow [POWGEN_data_reduction](../sns-instruments/POWGEN_data_reduction.rst) to replace `FocussedDataDspacing[VanadiumRun]`."

docs/user-guide/dream/dream-advanced-powder-reduction.ipynb

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@
140140
"metadata": {},
141141
"outputs": [],
142142
"source": [
143-
"grouped_dspacing.hist().plot(title=grouped_dspacing.coords['detector'].value.capitalize(),\n",
144-
" norm=\"log\")"
143+
"grouped_dspacing.hist().plot(\n",
144+
" title=grouped_dspacing.coords['detector'].value.capitalize(), norm=\"log\"\n",
145+
")"
145146
]
146147
},
147148
{
@@ -170,7 +171,9 @@
170171
"metadata": {},
171172
"outputs": [],
172173
"source": [
173-
"workflow = dream.DreamGeant4Workflow(run_norm=powder.RunNormalization.monitor_integrated)\n",
174+
"workflow = dream.DreamGeant4Workflow(\n",
175+
" run_norm=powder.RunNormalization.monitor_integrated\n",
176+
")\n",
174177
"\n",
175178
"workflow[Filename[SampleRun]] = dream.data.simulated_diamond_sample()\n",
176179
"workflow[Filename[VanadiumRun]] = dream.data.simulated_vanadium_sample()\n",
@@ -326,14 +329,8 @@
326329
"metadata": {},
327330
"outputs": [],
328331
"source": [
329-
"intermediates = workflow.compute(\n",
330-
" (\n",
331-
" DataWithScatteringCoordinates[SampleRun],\n",
332-
" MaskedData[SampleRun],\n",
333-
" )\n",
334-
")\n",
335-
"\n",
336-
"intermediates[DataWithScatteringCoordinates[SampleRun]]"
332+
"intermediates = workflow.compute((CountsWavelength[SampleRun], MaskedData[SampleRun]))\n",
333+
"intermediates[CountsWavelength[SampleRun]]"
337334
]
338335
},
339336
{
@@ -344,9 +341,9 @@
344341
"outputs": [],
345342
"source": [
346343
"two_theta = sc.linspace(\"two_theta\", 0.8, 2.4, 301, unit=\"rad\")\n",
347-
"intermediates[MaskedData[SampleRun]].hist(\n",
348-
" two_theta=two_theta, wavelength=300\n",
349-
").plot(norm=\"log\")"
344+
"intermediates[MaskedData[SampleRun]].hist(two_theta=two_theta, wavelength=300).plot(\n",
345+
" norm=\"log\"\n",
346+
")"
350347
]
351348
},
352349
{
@@ -419,12 +416,13 @@
419416
"source": [
420417
"detector_names = [\"mantle\", \"endcap_forward\", \"endcap_backward\", \"high_resolution\"]\n",
421418
"parameter_table = pd.DataFrame(\n",
422-
" {NeXusDetectorName: detector_names},\n",
423-
" index=detector_names\n",
419+
" {NeXusDetectorName: detector_names}, index=detector_names\n",
424420
").rename_axis(index='detector')\n",
425421
"\n",
426422
"all_detector_workflow = workflow.copy()\n",
427-
"mapped = all_detector_workflow[EmptyCanSubtractedIofDspacing[SampleRun]].map(parameter_table)\n",
423+
"mapped = all_detector_workflow[EmptyCanSubtractedIofDspacing[SampleRun]].map(\n",
424+
" parameter_table\n",
425+
")\n",
428426
"all_detector_workflow[EmptyCanSubtractedIofDspacing[SampleRun]] = mapped.reduce(\n",
429427
" func=powder.grouping.collect_detectors\n",
430428
")"
@@ -446,9 +444,7 @@
446444
"outputs": [],
447445
"source": [
448446
"all_detector_workflow.visualize(\n",
449-
" EmptyCanSubtractedIofDspacing[SampleRun],\n",
450-
" graph_attr={\"rankdir\": \"LR\"},\n",
451-
" compact=True\n",
447+
" EmptyCanSubtractedIofDspacing[SampleRun], graph_attr={\"rankdir\": \"LR\"}, compact=True\n",
452448
")"
453449
]
454450
},
@@ -533,14 +529,17 @@
533529
" sc.linspace(dim=\"two_theta\", unit=\"rad\", start=2.91, stop=3.11, num=51),\n",
534530
"]\n",
535531
"parameter_table = pd.DataFrame(\n",
536-
" {NeXusDetectorName: detector_names,\n",
537-
" TwoThetaBins: two_theta_bins,\n",
538-
" },\n",
539-
" index=detector_names\n",
532+
" {\n",
533+
" NeXusDetectorName: detector_names,\n",
534+
" TwoThetaBins: two_theta_bins,\n",
535+
" },\n",
536+
" index=detector_names,\n",
540537
").rename_axis(index='detector')\n",
541538
"\n",
542539
"all_detector_workflow = workflow.copy()\n",
543-
"mapped = all_detector_workflow[EmptyCanSubtractedIofDspacingTwoTheta[SampleRun]].map(parameter_table)\n",
540+
"mapped = all_detector_workflow[EmptyCanSubtractedIofDspacingTwoTheta[SampleRun]].map(\n",
541+
" parameter_table\n",
542+
")\n",
544543
"all_detector_workflow[EmptyCanSubtractedIofDspacingTwoTheta[SampleRun]] = mapped.reduce(\n",
545544
" func=powder.grouping.collect_detectors\n",
546545
")"
@@ -556,7 +555,7 @@
556555
"all_detector_workflow.visualize(\n",
557556
" EmptyCanSubtractedIofDspacingTwoTheta[SampleRun],\n",
558557
" graph_attr={\"rankdir\": \"LR\"},\n",
559-
" compact=True\n",
558+
" compact=True,\n",
560559
")"
561560
]
562561
},

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies = [
3838
"plopp>=25.03.0",
3939
"pythreejs",
4040
"sciline>=25.04.1",
41-
"scipp>=24.09.1",
41+
"scipp>=25.05.1",
4242
"scippneutron>=25.02.0",
4343
"scippnexus>=23.12.0",
4444
"tof>=25.01.2",

requirements/base.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ numpy
99
plopp>=25.03.0
1010
pythreejs
1111
sciline>=25.04.1
12-
scipp>=24.09.1
12+
scipp>=25.05.1
1313
scippneutron>=25.02.0
1414
scippnexus>=23.12.0
1515
tof>=25.01.2

requirements/base.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# SHA1:b1c1e81e8ad714d967dc65bce97a390ae3880cc6
1+
# SHA1:81114073dd5e4b765405b54b3976f92d6a96439b
22
#
3-
# This file was generated by pip-compile-multi.
3+
# This file is autogenerated by pip-compile-multi
44
# To update, run:
55
#
6-
# requirements upgrade
6+
# pip-compile-multi
77
#
88
annotated-types==0.7.0
99
# via pydantic
@@ -35,7 +35,7 @@ exceptiongroup==1.3.0
3535
# via ipython
3636
executing==2.2.0
3737
# via stack-data
38-
fonttools==4.58.0
38+
fonttools==4.58.1
3939
# via matplotlib
4040
fsspec==2025.5.1
4141
# via dask
@@ -51,7 +51,7 @@ importlib-metadata==8.7.0
5151
# via dask
5252
ipydatawidgets==4.3.5
5353
# via pythreejs
54-
ipython==8.36.0
54+
ipython==8.37.0
5555
# via ipywidgets
5656
ipywidgets==8.1.7
5757
# via
@@ -76,7 +76,7 @@ matplotlib==3.10.3
7676
# plopp
7777
matplotlib-inline==0.1.7
7878
# via ipython
79-
mpltoolbox==25.4.0
79+
mpltoolbox==25.5.0
8080
# via scippneutron
8181
networkx==3.4.2
8282
# via cyclebane
@@ -136,7 +136,7 @@ sciline==25.5.2
136136
# via
137137
# -r base.in
138138
# essreduce
139-
scipp==25.5.0
139+
scipp==25.5.1
140140
# via
141141
# -r base.in
142142
# essreduce
@@ -191,5 +191,5 @@ wcwidth==0.2.13
191191
# via prompt-toolkit
192192
widgetsnbextension==4.0.14
193193
# via ipywidgets
194-
zipp==3.21.0
194+
zipp==3.22.0
195195
# via importlib-metadata

requirements/basetest.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# SHA1:c4f3e9aaa3abd10fcdf497bea14415857f62cc89
22
#
3-
# This file was generated by pip-compile-multi.
3+
# This file is autogenerated by pip-compile-multi
44
# To update, run:
55
#
6-
# requirements upgrade
6+
# pip-compile-multi
77
#
88
certifi==2025.4.26
99
# via requests

0 commit comments

Comments
 (0)