Skip to content

[ESSDIFFRACTION] feat: add provider for dspacing range of instrument - #764

Open
jokasimr wants to merge 4 commits into
mainfrom
745
Open

jokasimr wants to merge 4 commits into
mainfrom
745

Conversation

@jokasimr

Copy link
Copy Markdown
Contributor

Fixes #745

@github-actions github-actions Bot added the essdiffraction Issues for essdiffraction. label Sep 24, 2026
@github-actions github-actions Bot changed the title feat: add provider for dspacing range of instrument [ESSDIFFRACTION] feat: add provider for dspacing range of instrument Sep 24, 2026
@jokasimr
jokasimr requested a review from jl-wynen September 25, 2026 08:54
providers = (
wavelength_range_from_chopper_frames,
dspacing_bins_from_wavelength_and_two_theta,
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we can simply add all these providers into any workflow. As I see it, the providers attributes are a default set of providers that we add to all/most workflows. But wavelength_range_from_chopper_frames only makes sense for 'analytical' unwrap workflows, not when we load an LUT from file. So you should add that provider in the specific workflow functions, not here. (And then we need a different provider for other workflows.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes that makes sense, I'll move it to the beer workflows. And maybe the wavelength range provider needs to change for it to work with the simulated lookup tables as well.

"""
if nbins < 1:
raise ValueError(f'DspacingNBins must be positive, got {nbins}.')
dspacing_range = scn.conversion.tof.dspacing_from_wavelength(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you use the ElasticCoordTransformGraph instead? (Same as computation of DetectorTwoTheta) Currently, this conversion does not react to customisations in that graph.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am a little sceptical that this approach will work robustly in practice.

1. Compute range from chopper params

If you compute WavelengthRange from chopper parameters, you can end up with a range that is larger than what the source and guide can deliver. Then you end up with counts=0 bins even though you did not measure there. So you don't know if the d-spacing really should be zero.

2. Compute range from events

If you have an unwrap workflow other than the 'analytical' workflow, you need an alternative to computing WavelengthRange. If you compute it from the wavelength LUT, you get the same issue as above.

If you compute it from the measured events, you get an inconsistency between the sample run and auxiliary runs (vanadium, empty instrument, etc) or between different sample runs. Since this PR derives DspacingBins only from the sample run, you can get the same issue as in point 1 for the auxiliary runs.
If you would compute the bins for each run separately, you can get problems in the normalisation when the vanadium range is smaller than the sample range.


As far as I can tell, you can only mitigate point 1 via a simulation. And point 2 by including the measured ranges of all runs.

I think the best solution is to have the user enter a fixed wavelength range.
Where does this requirement come from? Is it really too much to ask to specify a range instead of a number of bins?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This requirement comes from the Beer team and Celine.

  1. I don't see why having a range that is slightly larger than what the instrument can deliver would be a problem?

If we cannot measure there the counts will be zero, but the normalized counts will be nan.

As long as we cover the relevant region, having bins with intensity zero, or normalized intensity nan is not a significant issue (besides perhaps the inconvenience of exclude those when exporting).

  1. The way we compute the wavelength range should be agnostic of the approach we use for frame unwrapping, be that analytical or simulated. This is in principle not an issue, but the current implementation here need some adjustments.

In my opinion the simplest way to handle "mismatch" between sample and vanadium etc is either: a) making sure that the same instrument settings lead to the same wavelength and theta ranges, b) re-using the binning from the sample histogram for the normalization factor.

The user will always have the option to select the range themselves. This selection is "best effort" only, but I'd expect it to be sufficient for most cases.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you check with the instrument team how they want to treat out or bounds bins? I don't want to guess and cause painful debugging sessions later.

If we cannot measure there the counts will be zero, but the normalized counts will be nan.

The vanadium normalisation will insert a mask for those bins and the XYE file writer will raise when there is a mask. So the data needs to be post-processed in that case.

a) making sure that the same instrument settings lead to the same wavelength and theta ranges

We get this when deducing the range from the choppers, so that is good because

b) re-using the binning from the sample histogram for the normalization factor.

If we pre-reduce the vanadium and save it to disk (a common case), we might get it as a histogram. In that case, we'd have to rebin and lose some precision. So while the vanadium norm function does this automatically, we should avoid this case if possible.

@jokasimr
jokasimr requested a review from jl-wynen September 30, 2026 12:44

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

essdiffraction Issues for essdiffraction.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ESSDIFFRACTION, BEER] Adapt d-spacing binning

2 participants