-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add "JSON" generator to facilitate testing of live workflows in downstream packages #115
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp) | ||
"""Generators for "JSON" data from a NeXus file, for the purpose of testing.""" | ||
|
||
from collections.abc import Generator | ||
from typing import Any | ||
|
||
import scipp as sc | ||
|
||
|
||
def _as_str_attr(value: Any, name: str) -> dict: | ||
val = str(value) | ||
return {"string_size": len(val), "type": "string", "name": name, "values": val} | ||
|
||
|
||
def _variable_to_json(var: sc.Variable, name: str): | ||
attrs = [] | ||
if var.dtype == sc.DType.datetime64: | ||
offset = var.min() | ||
attrs.append(_as_str_attr(offset.value, "offset")) | ||
var = var - offset | ||
if var.unit is not None: | ||
attrs.append(_as_str_attr(var.unit, "units")) | ||
return { | ||
"module": "dataset", | ||
"config": { | ||
"name": name, | ||
"values": var.values, | ||
"size": list(var.shape), | ||
"type": str(var.dtype), | ||
}, | ||
"attributes": attrs, | ||
} | ||
|
||
|
||
_event_index_0 = sc.array(dims=('dummy',), values=[0], unit=None) | ||
|
||
|
||
def _event_data_pulse_to_json(pulse: sc.DataArray) -> dict: | ||
content = pulse.value.coords | ||
event_time_zero = sc.concat([pulse.coords['event_time_zero']], 'dummy') | ||
event_time_offset = content['event_time_offset'] | ||
event_id = content.get('event_id') | ||
# I think we always have a pixel_id in the flatbuffer, so monitors just get ones? | ||
if event_id is None: | ||
event_id = sc.ones(sizes=event_time_offset.sizes, dtype='int32', unit=None) | ||
children = [ | ||
_variable_to_json(event_time_zero, name='event_time_zero'), | ||
_variable_to_json(event_time_offset, name='event_time_offset'), | ||
_variable_to_json(event_id, name='event_id'), | ||
_variable_to_json(_event_index_0, name='event_index'), | ||
] | ||
group = { | ||
"type": "group", | ||
"name": "events_0", | ||
"children": children, | ||
"attributes": [_as_str_attr("NXevent_data", name="NX_class")], | ||
} | ||
return group | ||
|
||
|
||
def event_data_generator(data: sc.DataArray) -> Generator[dict, None, None]: | ||
""" | ||
Generate JSON data for event data from a NeXus file. | ||
|
||
Parameters | ||
---------- | ||
data: | ||
A data array with event data, equivalent to what ScippNexus would load from an | ||
NXevent_data group in a NeXus file. | ||
|
||
Yields | ||
------ | ||
: | ||
A dict of data for a single event data pulse that can be wrapped in a | ||
:py:class:`ess.reduce.nexus.json_nexus.JSONGroup`. | ||
""" | ||
for pulse in data: | ||
yield _event_data_pulse_to_json(pulse) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp) | ||
import pytest | ||
import scippnexus as snx | ||
from scipp.testing import assert_identical | ||
|
||
from ess.reduce import data | ||
from ess.reduce.nexus.json_generator import event_data_generator | ||
from ess.reduce.nexus.json_nexus import json_nexus_group | ||
|
||
|
||
def test_event_data_generator_monitor_events_round_trip() -> None: | ||
filename = data.loki_tutorial_sample_run_60250() | ||
monitor = snx.load(filename, root='entry/instrument/monitor_1/monitor_1_events') | ||
generator = event_data_generator(monitor) | ||
for i in range(len(monitor)): | ||
group = json_nexus_group(next(generator)) | ||
assert_identical(group[()], monitor[i : i + 1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test that the generator has finished after the loop? I.e., that |
||
with pytest.raises(StopIteration): | ||
next(generator) | ||
|
||
|
||
def test_event_data_generator_detector_events_round_trip() -> None: | ||
filename = data.loki_tutorial_sample_run_60250() | ||
detector = snx.load( | ||
filename, root='entry/instrument/larmor_detector/larmor_detector_events' | ||
) | ||
generator = event_data_generator(detector) | ||
for i in range(100): | ||
group = json_nexus_group(next(generator)) | ||
assert_identical(group[()], detector[i : i + 1]) | ||
|
||
|
||
def test_event_data_generator_without_event_id_yields_ones() -> None: | ||
filename = data.loki_tutorial_sample_run_60250() | ||
base = snx.load(filename, root='entry/instrument/monitor_1/monitor_1_events') | ||
monitor = base.bins.drop_coords('event_id') | ||
generator = event_data_generator(monitor) | ||
for i in range(100): | ||
group = json_nexus_group(next(generator)) | ||
assert_identical(group[()], base[i : i + 1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a dict, not a data array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, do you mean the return value? This docstring line is about the input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I meant the return value. My bad.
Can you add an explanation of it? In particular, there is no explanation of how data is encoded.