Skip to content
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

Support event-mode I(Q) #65

Merged
merged 13 commits into from
Feb 6, 2024
4 changes: 3 additions & 1 deletion docs/examples/loki-direct-beam.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"\n",
"params[CorrectForGravity] = True\n",
"params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"params[sans.ReturnEvents] = False\n",
"\n",
"params[QBins] = sc.linspace(dim='Q', start=0.01, stop=0.3, num=101, unit='1/angstrom')"
]
Expand Down Expand Up @@ -391,7 +392,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
40 changes: 33 additions & 7 deletions docs/examples/sans2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
" dims=['wavelength'], values=[0.7, 17.1], unit='angstrom'\n",
")\n",
"pipeline[CorrectForGravity] = True\n",
"pipeline[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound"
"pipeline[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"pipeline[sans.ReturnEvents] = True"
]
},
{
Expand Down Expand Up @@ -158,7 +159,25 @@
"outputs": [],
"source": [
"result = iofq.compute()\n",
"result.plot()"
"result.hist().plot(scale={'Q': 'log'}, norm='log')"
]
},
{
"cell_type": "markdown",
"id": "28532aa7",
"metadata": {},
"source": [
"As the result was computed in event-mode, we can also use a different $Q$-binning, without re-reducing the data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0748a1a",
"metadata": {},
"outputs": [],
"source": [
"result.hist(Q=60).plot(scale={'Q': 'log'}, norm='log')"
]
},
{
Expand All @@ -182,7 +201,7 @@
"result_drop = pipeline.compute(BackgroundSubtractedIofQ)\n",
"# Reset the UnsertaintyBroadcastMode to the old value\n",
"pipeline[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"sc.DataGroup(upper_bound=result, dropped=result_drop).plot(norm='log')"
"sc.DataGroup(upper_bound=result, dropped=result_drop).hist().plot(norm='log')"
]
},
{
Expand Down Expand Up @@ -246,8 +265,15 @@
"\n",
"display(plot_flat_detector_xy(results[MaskedData[SampleRun]].sum('tof'), norm='log'))\n",
"\n",
"parts = {str(key): results[key] for key in parts}\n",
"parts = {key: val if val.bins is None else val.hist() for key, val in parts.items()}\n",
"wavelength = pipeline.compute(WavelengthBins)\n",
"display(\n",
" results[CleanSummedQ[SampleRun, Numerator]]\n",
" .hist(wavelength=wavelength)\n",
" .transpose()\n",
" .plot(norm='log')\n",
")\n",
"display(results[CleanSummedQ[SampleRun, Denominator]].plot(norm='log'))\n",
"parts = {str(key): results[key].sum('wavelength') for key in parts}\n",
"display(sc.plot(parts, norm='log'))\n",
"\n",
"iofqs = {str(key): results[key] for key in iofqs}\n",
Expand Down Expand Up @@ -291,7 +317,7 @@
")\n",
"pipeline.set_param_table(param_table)\n",
"results = pipeline.compute(sciline.Series[Mode, BackgroundSubtractedIofQ])\n",
"sc.DataGroup(results).plot(norm='log')"
"sc.DataGroup(results).hist().plot(norm='log')"
]
},
{
Expand Down Expand Up @@ -354,7 +380,7 @@
"metadata": {},
"outputs": [],
"source": [
"pp.plot(sc.collapse(result, keep='Q'), norm='log')"
"pp.plot(sc.collapse(result.hist(), keep='Q'), norm='log')"
]
}
],
Expand Down
16 changes: 14 additions & 2 deletions docs/examples/zoom.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
" dims=['wavelength'], values=[0.7, 17.1], unit='angstrom'\n",
")\n",
"params[CorrectForGravity] = True\n",
"params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound"
"params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"params[sans.ReturnEvents] = False"
]
},
{
Expand Down Expand Up @@ -246,8 +247,19 @@
" )\n",
")\n",
"\n",
"wavelength = pipeline.compute(WavelengthBins)\n",
"display(\n",
" results[CleanSummedQ[SampleRun, Numerator]]\n",
" .hist(wavelength=wavelength)\n",
" .transpose()\n",
" .plot(norm='log')\n",
")\n",
"display(results[CleanSummedQ[SampleRun, Denominator]].plot(norm='log'))\n",
"parts = {str(key): results[key] for key in parts}\n",
"parts = {key: val if val.bins is None else val.hist() for key, val in parts.items()}\n",
"parts = {\n",
" key: val.sum('wavelength') if val.bins is None else val.hist()\n",
" for key, val in parts.items()\n",
"}\n",
"display(sc.plot(parts, norm='log', scale={'Q': 'log'}))\n",
"\n",
"iofqs = {str(key): results[key] for key in iofqs}\n",
Expand Down
5 changes: 5 additions & 0 deletions src/esssans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from .common import transmission_from_background_run, transmission_from_sample_run
from .direct_beam import direct_beam
from .types import BackgroundSubtractedIofQ, IofQ, ReturnEvents, SampleRun
Copy link
Member

Choose a reason for hiding this comment

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

Not sure why we are now importing those on the top level? Was it to get away from the from types import *?
If so, should this be reflected in the notebooks?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. types is meaningless for the user. We should name (and categorize things) in a meaningful way. Yes, there are a lot of things that need updating, I wanted to do that separately, in particular given that there is also other ongoing work that may conflict.

Copy link
Member

Choose a reason for hiding this comment

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

Should we sit down and discuss what would be the best way to do this, so we're all on the same page?

Copy link
Member Author

Choose a reason for hiding this comment

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

Definitely!


providers = (
*conversions.providers,
Expand All @@ -41,6 +42,10 @@
del importlib

__all__ = [
SimonHeybrock marked this conversation as resolved.
Show resolved Hide resolved
'BackgroundSubtractedIofQ',
'IofQ',
'SampleRun',
'ReturnEvents',
Copy link
Member

Choose a reason for hiding this comment

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

Above, you import BackgroundSubtractedIofQ, IofQ, ReturnEvents, SampleRun
Should all of them be listed here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

'beam_center_finder',
'common',
'conversions',
Expand Down
11 changes: 9 additions & 2 deletions src/esssans/beam_center_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
mask_wavelength,
to_Q,
)
from .i_of_q import merge_spectra, process_wavelength_bands
from .i_of_q import merge_spectra
from .logging import get_logger
from .normalization import iofq_denominator, normalize, solid_angle
from .normalization import (
iofq_denominator,
normalize,
process_wavelength_bands,
solid_angle,
)
from .types import (
BeamCenter,
DetectorPixelShape,
Expand All @@ -27,6 +32,7 @@
MaskedData,
NormWavelengthTerm,
QBins,
ReturnEvents,
SampleRun,
UncertaintyBroadcastMode,
WavelengthBins,
Expand Down Expand Up @@ -177,6 +183,7 @@ def _iofq_in_quadrants(
]
params = {}
params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound
params[ReturnEvents] = False
params[WavelengthBins] = wavelength_bins
params[QBins] = q_bins
params[DetectorPixelShape[SampleRun]] = pixel_shape
Expand Down
5 changes: 4 additions & 1 deletion src/esssans/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ def to_Q(
"""
Convert a data array from wavelength to Q.
"""
return CleanQ[RunType, IofQPart](data.transform_coords('Q', graph=graph))
# Keep naming of wavelength dim, subsequent steps use a (Q, wavelength) binning.
return CleanQ[RunType, IofQPart](
data.transform_coords('Q', graph=graph, rename_dims=False)
)


providers = (
Expand Down
2 changes: 1 addition & 1 deletion src/esssans/direct_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def direct_beam(pipeline: Pipeline, I0: sc.Variable, niter: int = 5) -> List[dic
# parameters, nor given by any providers, so it will be considered flat.
# TODO: Should we have a check that DirectBeam cannot be computed from the
# pipeline?
iofq = pipeline.compute(BackgroundSubtractedIofQ)
iofq = sc.values(pipeline.compute(BackgroundSubtractedIofQ))
iofq_full = iofq['band', -1]
iofq_bands = iofq['band', :-1]

Expand Down
Loading
Loading