From 09e8eb88c9555191502b98dde89e60663c9a0c37 Mon Sep 17 00:00:00 2001 From: Aaron Brewster Date: Fri, 13 Sep 2024 14:24:26 -0700 Subject: [PATCH 1/9] NXmx: read data_scale_factor 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 --- newsfragments/XXX.feature | 1 + src/dxtbx/format/FormatNXmx.py | 4 ++-- src/dxtbx/nexus/__init__.py | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 newsfragments/XXX.feature diff --git a/newsfragments/XXX.feature b/newsfragments/XXX.feature new file mode 100644 index 000000000..3b3f6e056 --- /dev/null +++ b/newsfragments/XXX.feature @@ -0,0 +1 @@ +Add support for reading the detector gain for nexus files diff --git a/src/dxtbx/format/FormatNXmx.py b/src/dxtbx/format/FormatNXmx.py index 55b5879a5..9f169460c 100644 --- a/src/dxtbx/format/FormatNXmx.py +++ b/src/dxtbx/format/FormatNXmx.py @@ -68,10 +68,11 @@ 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 @@ -86,7 +87,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: diff --git a/src/dxtbx/nexus/__init__.py b/src/dxtbx/nexus/__init__.py index e506e3aca..27761f100 100644 --- a/src/dxtbx/nexus/__init__.py +++ b/src/dxtbx/nexus/__init__.py @@ -245,6 +245,7 @@ def get_dxtbx_scan( def get_dxtbx_detector( nxdetector: nxmx.NXdetector, wavelength: float, + nxdata: nxmx.NXdata, ) -> dxtbx.model.Detector: """Generate a dxtbx detector model from an NXdetector and NXbeam. @@ -461,6 +462,9 @@ def equipment_component_key(dependency): p.set_mu(mu) p.set_px_mm_strategy(px_mm) + if nxdata.data_scale_factor and not nxdata.data_scale_factor.shape: + p.set_gain(1/nxdata.data_scale_factor) + return detector From 05ee079432c2375bdbbbf900ff144a2739199efa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:25:36 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/dxtbx/format/FormatNXmx.py | 4 +++- src/dxtbx/nexus/__init__.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/dxtbx/format/FormatNXmx.py b/src/dxtbx/format/FormatNXmx.py index 9f169460c..c57e0de4f 100644 --- a/src/dxtbx/format/FormatNXmx.py +++ b/src/dxtbx/format/FormatNXmx.py @@ -72,7 +72,9 @@ def _start(self): 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, nxdata) + 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 diff --git a/src/dxtbx/nexus/__init__.py b/src/dxtbx/nexus/__init__.py index 27761f100..169e25463 100644 --- a/src/dxtbx/nexus/__init__.py +++ b/src/dxtbx/nexus/__init__.py @@ -463,7 +463,7 @@ def equipment_component_key(dependency): p.set_px_mm_strategy(px_mm) if nxdata.data_scale_factor and not nxdata.data_scale_factor.shape: - p.set_gain(1/nxdata.data_scale_factor) + p.set_gain(1 / nxdata.data_scale_factor) return detector From d4f999683d5958f334abf2cd6c9a7f90ea127fb5 Mon Sep 17 00:00:00 2001 From: DiamondLightSource-build-server Date: Fri, 13 Sep 2024 21:25:47 +0000 Subject: [PATCH 3/9] Rename newsfragments/XXX.feature to newsfragments/756.feature --- newsfragments/{XXX.feature => 756.feature} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename newsfragments/{XXX.feature => 756.feature} (100%) diff --git a/newsfragments/XXX.feature b/newsfragments/756.feature similarity index 100% rename from newsfragments/XXX.feature rename to newsfragments/756.feature From d952a1f277eafea424ee6269c4179e881b8c4b61 Mon Sep 17 00:00:00 2001 From: Aaron Brewster Date: Thu, 19 Sep 2024 11:37:45 -0700 Subject: [PATCH 4/9] Add test --- tests/nexus/test_mpccd_nexus.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/nexus/test_mpccd_nexus.py diff --git a/tests/nexus/test_mpccd_nexus.py b/tests/nexus/test_mpccd_nexus.py new file mode 100644 index 000000000..16d4a4286 --- /dev/null +++ b/tests/nexus/test_mpccd_nexus.py @@ -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 + Includs 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 From 7eb9e3357acfbf2ce8c9054e2bba62c1c76ffba5 Mon Sep 17 00:00:00 2001 From: Nicholas Devenish Date: Wed, 2 Oct 2024 07:44:19 +0100 Subject: [PATCH 5/9] Bump, now nxmx conda-forge From 91d7b5064d3d10c037871fc65f940bc11645effd Mon Sep 17 00:00:00 2001 From: Nicholas Devenish Date: Wed, 2 Oct 2024 09:52:49 +0100 Subject: [PATCH 6/9] Explicitly require nxmx >=0.0.4 now --- dependencies.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dependencies.yaml b/dependencies.yaml index ba2e95410..647fbd407 100644 --- a/dependencies.yaml +++ b/dependencies.yaml @@ -73,7 +73,7 @@ run: - mrcfile - natsort - {{ pin_compatible('numpy') }} # [not bootstrap] - - nxmx + - nxmx >=0.0.4 - orderedset - pint - pycbf # [prebuilt_cctbx] From 228e200b523391a1ec4c235666c001018d02b655 Mon Sep 17 00:00:00 2001 From: Nicholas Devenish Date: Tue, 15 Oct 2024 09:44:47 +0100 Subject: [PATCH 7/9] Update test_mpccd_nexus.py --- tests/nexus/test_mpccd_nexus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/nexus/test_mpccd_nexus.py b/tests/nexus/test_mpccd_nexus.py index 16d4a4286..433924b4b 100644 --- a/tests/nexus/test_mpccd_nexus.py +++ b/tests/nexus/test_mpccd_nexus.py @@ -6,7 +6,7 @@ def test_mpccd_nexus_gain(dials_data): """ Tests SACLA MPCCD image from CXI.DB 221 - Includs parameter data_scale_factor, which accounts for a gain of 10 + Includes parameter data_scale_factor, which accounts for a gain of 10 """ try: From b3b500a61213034f16c5a99a72973a5493b829ea Mon Sep 17 00:00:00 2001 From: Aaron Brewster Date: Tue, 29 Oct 2024 16:58:23 -0700 Subject: [PATCH 8/9] As scale factor is optional, allow NXdata to be optional Should fix the tests --- src/dxtbx/nexus/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dxtbx/nexus/__init__.py b/src/dxtbx/nexus/__init__.py index 169e25463..a960d0541 100644 --- a/src/dxtbx/nexus/__init__.py +++ b/src/dxtbx/nexus/__init__.py @@ -245,7 +245,7 @@ def get_dxtbx_scan( def get_dxtbx_detector( nxdetector: nxmx.NXdetector, wavelength: float, - nxdata: nxmx.NXdata, + nxdata: nxmx.NXdata = None, ) -> dxtbx.model.Detector: """Generate a dxtbx detector model from an NXdetector and NXbeam. @@ -462,7 +462,7 @@ def equipment_component_key(dependency): p.set_mu(mu) p.set_px_mm_strategy(px_mm) - if nxdata.data_scale_factor and not nxdata.data_scale_factor.shape: + if nxdata and nxdata.data_scale_factor and not nxdata.data_scale_factor.shape: p.set_gain(1 / nxdata.data_scale_factor) return detector From fea74534cacb9a5ec903581542a3262c66027f0c Mon Sep 17 00:00:00 2001 From: Aaron Brewster Date: Wed, 30 Oct 2024 16:35:14 -0700 Subject: [PATCH 9/9] Code review --- src/dxtbx/nexus/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dxtbx/nexus/__init__.py b/src/dxtbx/nexus/__init__.py index a960d0541..2e3938e79 100644 --- a/src/dxtbx/nexus/__init__.py +++ b/src/dxtbx/nexus/__init__.py @@ -245,7 +245,7 @@ def get_dxtbx_scan( def get_dxtbx_detector( nxdetector: nxmx.NXdetector, wavelength: float, - nxdata: nxmx.NXdata = None, + nxdata: nxmx.NXdata | None = None, ) -> dxtbx.model.Detector: """Generate a dxtbx detector model from an NXdetector and NXbeam.