Repository navigation
fix: handle monitor event data in normalization #887
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
Open
jokasimr
wants to merge
8
commits into
main
Choose a base branch
from
832
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
211bfca
fix: add case for binned wavelength coord
jokasimr 04f639d
feat: histogram event data monitor before normalization
jokasimr 560a596
fix
jokasimr e1dbf40
Merge main into 832 and resolve workflow fixture conflict
jokasimr 0b67691
feat: add monitor wavelength bins parameter to generic workflow
jokasimr fb19db4
refactor: rename WavelengthBins to MonitorWavelengthBins
jokasimr 493415e
fix: use is_binned
jokasimr 4f0973a
Merge branch 'main' into 832
jokasimr 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
93 changes: 93 additions & 0 deletions
93
packages/essspectroscopy/tests/bifrost/normalization_test.py
This file contains hidden or 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,93 @@ | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # Copyright (c) 2026 Scipp contributors (https://github.com/scipp) | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import scipp as sc | ||
| import scipp.testing | ||
| from ess.bifrost.normalization import normalize_by_monitor_and_proton_charge | ||
| from ess.spectroscopy.types import UncertaintyBroadcastMode | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def detector() -> sc.DataArray: | ||
| events = sc.DataArray( | ||
| sc.ones(dims=['event'], shape=[3], unit='counts', with_variances=True), | ||
| coords={ | ||
| 'incident_wavelength': sc.array( | ||
| dims=['event'], values=[1.5, 2.5, 3.5], unit='angstrom' | ||
| ) | ||
| }, | ||
| ) | ||
| return sc.DataArray( | ||
| sc.bins(data=events, dim='event', begin=sc.index(0), end=sc.index(3)) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(params=[False, True], ids=['scalar', 'time-dependent']) | ||
| def event_monitor(request: pytest.FixtureRequest) -> sc.DataArray: | ||
| events = sc.DataArray( | ||
| sc.ones(dims=['event'], shape=[4], unit='counts', with_variances=True), | ||
| coords={ | ||
| 'incident_wavelength': sc.array( | ||
| dims=['event'], values=[1.5, 1.5, 2.5, 3.5], unit='angstrom' | ||
| ) | ||
| }, | ||
| ) | ||
| if request.param: | ||
| events.coords['time'] = sc.array(dims=['event'], values=[0, 1, 0, 1], unit='s') | ||
| return events.group('time') | ||
| return sc.DataArray( | ||
| sc.bins(data=events, dim='event', begin=sc.index(0), end=sc.index(4)) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ('edges', 'expected_weights'), | ||
| [ | ||
| ([1.0, 3.0, 4.0], [4 / 3, 4 / 3, 2.0]), | ||
| ([1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 2.0]), | ||
| ], | ||
| ) | ||
| def test_normalize_monitor( | ||
| detector: sc.DataArray, | ||
| event_monitor: sc.DataArray, | ||
| edges: list[float], | ||
| expected_weights: list[float], | ||
| ) -> None: | ||
| monitor = event_monitor.hist( | ||
| incident_wavelength=sc.array( | ||
| dims=['incident_wavelength'], values=edges, unit='angstrom' | ||
| ) | ||
| ) | ||
|
|
||
| result = normalize_by_monitor_and_proton_charge( | ||
| detector=detector, | ||
| monitor=monitor, | ||
| proton_charge=sc.DataArray(sc.scalar(2.0, unit='pC')), | ||
| uncertainty_broadcast_mode=UncertaintyBroadcastMode.drop, | ||
| ) | ||
|
|
||
| sc.testing.assert_allclose( | ||
| result.bins.constituents['data'].data, | ||
| sc.array( | ||
| dims=['event'], | ||
| values=expected_weights, | ||
| variances=np.square(expected_weights), | ||
| unit='counts*angstrom/pC', | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def test_event_monitor_requires_bins( | ||
| detector: sc.DataArray, event_monitor: sc.DataArray | ||
| ) -> None: | ||
| with pytest.raises( | ||
| ValueError, match=r'MonitorWavelengthBins\[NormalizationMonitor\]' | ||
| ): | ||
| normalize_by_monitor_and_proton_charge( | ||
| detector=detector, | ||
| monitor=event_monitor, | ||
| proton_charge=sc.DataArray(sc.scalar(2.0, unit='pC')), | ||
| uncertainty_broadcast_mode=UncertaintyBroadcastMode.drop, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
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.
You could also use
That would allow any monitor type and detector.
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.
WavelengthBinscollides with a type already defined in esssans, but we can useComponentinstead ofMonitorTypeThere 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.
I'd rather change the more specialised type to use a longer name as that has a smaller impact than the generic name.
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.
Typically I agree about that. But that would be a breaking change.
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.
See above, I don't think we should have different wavelength binnings in a workflow, unless there is a clear motivation for it.