Skip to content

Commit c0df490

Browse files
YooSunYoungSimonHeybrockpre-commit-ci-lite[bot]aaronfinkeMridulS
authored
NXLauetof format and workflow/IO helpers.
* Lauetof export interface. * Raw data metadata as dataclass * Allow arbitrary metadata and export time of flight from the coordinate. * Specify unit Co-authored-by: Simon Heybrock <[email protected]> * Add docstring to export methods. * Add missing attributes. * Remove comments * Write unit only when applicable. * Update comment. * Validity check when appending histogram on top of metadata (#116) * Separate metatadata from event data for easy export. * Add raw data metadata retrieval part. * Lauetof export interface. * Raw data metadata as dataclass * Allow arbitrary metadata and export time of flight from the coordinate. * Separate metatadata from event data for easy export. * Add raw data metadata retrieval part. * Satety check in the export function. * Add warning filter. * Apply automatic formatting * Apply automatic formatting * Fix typo * Move functions to more proper module. * Lauetof export interface. * Raw data metadata as dataclass * Allow arbitrary metadata and export time of flight from the coordinate. * Specify unit Co-authored-by: Simon Heybrock <[email protected]> * Add docstring to export methods. * Add missing attributes. * Remove comments * Fix typo. * Apply automatic formatting --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Simon Heybrock <[email protected]> * Wrap detector processing step. * Lower pin of essreduce to use accumulators. * Specify dtype of string. [skip ci] * Use essreduce accumulator. * Export NXsource. * Fix type hint * Apply automatic formatting * Data reduction wrapper interface (#122) * Executable module. * Save crystal rotation. * Fix crystal rotation. * Remove all zero lines, not just the first one (#123) * remove all zero lines, not just the first one * Apply automatic formatting * Update src/ess/nmx/mcstas/load.py * Apply automatic formatting --------- Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Sunyoung Yoo <[email protected]> * Apply suggestions from code review Co-authored-by: Mridul Seth <[email protected]> * Apply automatic formatting * Add bitshuffle support, compress binned datasets using bitshuffle/LZ4 (#125) * add bitshuffle support, compress binned datasets using bitshuffle/LZ4 * Apply automatic formatting * Add docstring and option. --------- Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: YooSunyoung <[email protected]> * Update default space group in mtz io module (#126) * Executable module. * Save crystal rotation. * Fix crystal rotation. * Remove all zero lines, not just the first one (#123) * remove all zero lines, not just the first one * Apply automatic formatting * Update src/ess/nmx/mcstas/load.py * Apply automatic formatting --------- Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Sunyoung Yoo <[email protected]> * Apply suggestions from code review Co-authored-by: Mridul Seth <[email protected]> * Apply automatic formatting * Add bitshuffle support, compress binned datasets using bitshuffle/LZ4 (#125) * add bitshuffle support, compress binned datasets using bitshuffle/LZ4 * Apply automatic formatting * Add docstring and option. --------- Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: YooSunyoung <[email protected]> * Update mtz_io.py make default space group P1 (lowest symmetry space group) * Update default space group in tests. --------- Co-authored-by: YooSunyoung <[email protected]> Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Mridul Seth <[email protected]> --------- Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Mridul Seth <[email protected]> --------- Co-authored-by: Simon Heybrock <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: Aaron Finke <[email protected]> Co-authored-by: Mridul Seth <[email protected]>
1 parent 08aee47 commit c0df490

File tree

9 files changed

+765
-77
lines changed

9 files changed

+765
-77
lines changed

docs/user-guide/workflow_chunk.ipynb

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,38 @@
134134
" raw_data_metadatas[detector_i] = results[NMXRawDataMetadata]\n",
135135
"\n",
136136
"# We take the min/maximum values of the scale factor\n",
137-
"min_toa = min(dg['min_toa'] for dg in raw_data_metadatas.values())\n",
138-
"max_toa = max(dg['max_toa'] for dg in raw_data_metadatas.values())\n",
139-
"max_probability = max(dg['max_probability'] for dg in raw_data_metadatas.values())\n",
137+
"min_toa = min(meta.min_toa for meta in raw_data_metadatas.values())\n",
138+
"max_toa = max(meta.max_toa for meta in raw_data_metadatas.values())\n",
139+
"max_probability = max(meta.max_probability for meta in raw_data_metadatas.values())\n",
140140
"\n",
141141
"toa_bin_edges = sc.linspace(dim='t', start=min_toa, stop=max_toa, num=51)\n",
142142
"scale_factor = mcstas_weight_to_probability_scalefactor(\n",
143143
" max_counts=wf.compute(MaximumCounts), max_probability=max_probability\n",
144144
")"
145145
]
146146
},
147+
{
148+
"cell_type": "markdown",
149+
"metadata": {},
150+
"source": [
151+
"## Compute Metadata\n",
152+
"\n",
153+
"Other metadata does not require any chunk-based computation.\n",
154+
"\n",
155+
"Therefore we export the metadata first and append detector data later."
156+
]
157+
},
147158
{
148159
"cell_type": "markdown",
149160
"metadata": {},
150161
"source": [
151162
"## Compute Final Output\n",
152163
"\n",
153-
"Now with the `scale_factor: McStasWeight2CountScaleFactor`, we can compute the final output chunk by chunk.\n",
164+
"Now with all the metadata, we can compute the final output chunk by chunk.\n",
165+
"\n",
166+
"We will also compute static parameters in advance so that stream processor does not compute them every time another chunk is added.\n",
154167
"\n",
155-
"We will also compute static parameters in advance so that stream processor does not compute them every time another chunk is added."
168+
"We will as well export the reduced data detector by detector."
156169
]
157170
},
158171
{
@@ -181,68 +194,47 @@
181194
"metadata": {},
182195
"outputs": [],
183196
"source": [
184-
"from functools import partial\n",
185-
"from ess.reduce.streaming import StreamProcessor, EternalAccumulator\n",
197+
"from ess.nmx.nexus import NXLauetofWriter\n",
186198
"\n",
187-
"# Stream processor building helper\n",
188-
"final_stream_processor = partial(\n",
189-
" StreamProcessor,\n",
190-
" dynamic_keys=(RawEventProbability,),\n",
191-
" target_keys=(NMXReducedDataGroup,),\n",
192-
" accumulators={NMXReducedCounts: EternalAccumulator},\n",
193-
")"
194-
]
195-
},
196-
{
197-
"cell_type": "code",
198-
"execution_count": null,
199-
"metadata": {},
200-
"outputs": [],
201-
"source": [
202-
"from ess.nmx.types import DetectorName\n",
203-
"from ess.nmx.mcstas.load import raw_event_data_chunk_generator\n",
204-
"from ess.nmx.streaming import calculate_number_of_chunks\n",
205-
"from ipywidgets import IntProgress\n",
206199
"\n",
207-
"CHUNK_SIZE = 10 # Number of event rows to process at once\n",
208-
"# Increase this number to speed up the processing\n",
209-
"NUM_DETECTORS = 3\n",
210-
"final_wf[TimeBinSteps] = sc.linspace(\n",
211-
" 't', 0.1, 0.15, 51, unit='s'\n",
212-
") # Time bin edges can be calculated from the data\n",
213-
"# But streaming processor only sees the first chunk\n",
214-
"# So we need to set it manually before processing the first chunk\n",
215-
"# It is a bit cumbersome since we have to know the range of the time bins in advance\n",
216-
"\n",
217-
"# Loop over the detectors\n",
218-
"file_path = final_wf.compute(FilePath)\n",
219-
"for detector_i in range(0, NUM_DETECTORS):\n",
220-
" temp_wf = final_wf.copy()\n",
221-
" temp_wf[DetectorIndex] = detector_i\n",
222-
" # First compute static information\n",
223-
" detector_name = temp_wf.compute(DetectorName)\n",
224-
" temp_wf[PixelIds] = temp_wf.compute(PixelIds)\n",
200+
"def temp_generator(file_path, detector_name):\n",
225201
" max_chunk_id = calculate_number_of_chunks(\n",
226202
" file_path, detector_name=detector_name, chunk_size=CHUNK_SIZE\n",
227203
" )\n",
228204
" cur_detector_progress_bar = IntProgress(\n",
229205
" min=0, max=max_chunk_id, description=f\"Detector {detector_i}\"\n",
230206
" )\n",
231207
" display(cur_detector_progress_bar)\n",
232-
"\n",
233-
" # Build the stream processor\n",
234-
" processor = final_stream_processor(temp_wf)\n",
235208
" for da in raw_event_data_chunk_generator(\n",
236209
" file_path=file_path, detector_name=detector_name, chunk_size=CHUNK_SIZE\n",
237210
" ):\n",
238-
" if any(da.sizes.values()) == 0:\n",
239-
" continue\n",
240-
" else:\n",
241-
" results = processor.add_chunk({RawEventProbability: da})\n",
211+
" yield da\n",
242212
" cur_detector_progress_bar.value += 1\n",
243213
"\n",
244-
" result = results[NMXReducedDataGroup]\n",
245-
" display(result)\n"
214+
"\n",
215+
"# When a panel is added to the writer,\n",
216+
"# the writer will start processing the data from the generator\n",
217+
"# and store the results in memory\n",
218+
"# The writer will then write the data to the file\n",
219+
"# when ``save`` is called\n",
220+
"writer = NXLauetofWriter(\n",
221+
" chunk_generator=temp_generator,\n",
222+
" chunk_insert_key=RawEventProbability,\n",
223+
" workflow=final_wf,\n",
224+
" output_filename=\"test.h5\",\n",
225+
" overwrite=True,\n",
226+
" extra_meta={\"McStasWeight2CountScaleFactor\": scale_factor},\n",
227+
")"
228+
]
229+
},
230+
{
231+
"cell_type": "code",
232+
"execution_count": null,
233+
"metadata": {},
234+
"outputs": [],
235+
"source": [
236+
"for detector_i in range(3):\n",
237+
" display(writer.add_panel(detector_id=detector_i))"
246238
]
247239
}
248240
],

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ dependencies = [
4242
"pandas",
4343
"gemmi",
4444
"defusedxml",
45+
"bitshuffle",
4546
]
4647

4748
dynamic = ["version"]
4849

50+
[project.scripts]
51+
essnmx_reduce_mcstas = "ess.nmx.mcstas.executables:main"
52+
4953
[project.optional-dependencies]
5054
test = [
5155
"pytest",

src/ess/nmx/mcstas/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp)
3+
from ..types import MaximumCounts
4+
5+
default_parameters = {MaximumCounts: 10000}
36

47

58
def McStasWorkflow():
@@ -27,5 +30,6 @@ def McStasWorkflow():
2730
reduce_raw_event_probability,
2831
raw_event_probability_to_counts,
2932
format_nmx_reduced_data,
30-
)
33+
),
34+
params=default_parameters,
3135
)

0 commit comments

Comments
 (0)