Skip to content

Commit

Permalink
NXmx: read data_scale_factor (#756)
Browse files Browse the repository at this point in the history
New parameter as of nexusformat/definitions#1343. This is not full support of the parameter or of data_offset, but it's one of the use cases needed, namely reading a single gain value

Co-authored-by: Nicholas Devenish <[email protected]>
  • Loading branch information
phyy-nx and ndevenish authored Oct 30, 2024
1 parent f30e353 commit 834cb48
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ run:
- mrcfile
- natsort
- {{ pin_compatible('numpy') }} # [not bootstrap]
- nxmx
- nxmx >=0.0.4
- orderedset
- pint
- pycbf # [prebuilt_cctbx]
Expand Down
1 change: 1 addition & 0 deletions newsfragments/756.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for reading the detector gain for nexus files
6 changes: 4 additions & 2 deletions src/dxtbx/format/FormatNXmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ def _start(self):
nxinstrument = nxentry.instruments[0]
nxdetector = nxinstrument.detectors[0]
nxbeam = nxinstrument.beams[0]
nxdata = nxmx_obj.entries[0].data[0]
self._goniometer_model = dxtbx.nexus.get_dxtbx_goniometer(nxsample)
self._beam_factory = dxtbx.nexus.CachedWavelengthBeamFactory(nxbeam)
wavelength = self._beam_factory.make_beam(index=0).get_wavelength()
self._detector_model = dxtbx.nexus.get_dxtbx_detector(nxdetector, wavelength)
self._detector_model = dxtbx.nexus.get_dxtbx_detector(
nxdetector, wavelength, nxdata
)

# if the detector is between the sample and the source, and perpendicular
# to the beam, then invert the distance vector, as this is probably wrong
Expand All @@ -86,7 +89,6 @@ def _start(self):
if self._scan_model:
self._num_images = len(self._scan_model)
else:
nxdata = nxmx_obj.entries[0].data[0]
if nxdata.signal:
data = nxdata[nxdata.signal]
else:
Expand Down
4 changes: 4 additions & 0 deletions src/dxtbx/nexus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def get_dxtbx_scan(
def get_dxtbx_detector(
nxdetector: nxmx.NXdetector,
wavelength: float,
nxdata: nxmx.NXdata | None = None,
) -> dxtbx.model.Detector:
"""Generate a dxtbx detector model from an NXdetector and NXbeam.
Expand Down Expand Up @@ -461,6 +462,9 @@ def equipment_component_key(dependency):
p.set_mu(mu)
p.set_px_mm_strategy(px_mm)

if nxdata and nxdata.data_scale_factor and not nxdata.data_scale_factor.shape:
p.set_gain(1 / nxdata.data_scale_factor)

return detector


Expand Down
24 changes: 24 additions & 0 deletions tests/nexus/test_mpccd_nexus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

import dxtbx


def test_mpccd_nexus_gain(dials_data):
"""
Tests SACLA MPCCD image from CXI.DB 221
Includes parameter data_scale_factor, which accounts for a gain of 10
"""

try:
h5path = (
dials_data("image_examples", pathlib=True)
/ "SACLA-MPCCD-run197287-0-nexus.h5"
)
except Exception as e:
print(type(e), str(e))
raise
img = dxtbx.load(h5path)

d = img.get_detector()

assert d[0].get_gain() == 10

0 comments on commit 834cb48

Please sign in to comment.