From 7ab9f8ca70c50fac9dab8d341e60c8890b1c81cf Mon Sep 17 00:00:00 2001 From: Nicholas Devenish Date: Thu, 22 Aug 2024 09:23:03 +0100 Subject: [PATCH] FullCBFWriter: Fix bug causing panel shuffling (#752) --- newsfragments/752.bugfix | 1 + src/dxtbx/format/cbf_writer.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 newsfragments/752.bugfix diff --git a/newsfragments/752.bugfix b/newsfragments/752.bugfix new file mode 100644 index 000000000..f01107525 --- /dev/null +++ b/newsfragments/752.bugfix @@ -0,0 +1 @@ +Stop ``dxtbx.image_average`` shuffling panel positions for segmented detectors. diff --git a/src/dxtbx/format/cbf_writer.py b/src/dxtbx/format/cbf_writer.py index 595538ecd..62d72125f 100644 --- a/src/dxtbx/format/cbf_writer.py +++ b/src/dxtbx/format/cbf_writer.py @@ -215,6 +215,7 @@ def level_string(key): detector_axes_names = [] # save these for later panelkeys = [] panelnames = [] + panelindices = [] def recursive_setup_basis_dict(key, parent_name="", panel_id=0): # Set up CBF axis names, including equipment components and depends_on chains @@ -230,6 +231,8 @@ def recursive_setup_basis_dict(key, parent_name="", panel_id=0): panelname = "PANEL_%d" % panel_id panelkeys.append(key) panelnames.append(panelname) + dxtbx_panel_index = list(detector).index(node) + panelindices.append(dxtbx_panel_index) panel_id += 1 if len(key) == 1: @@ -253,6 +256,11 @@ def recursive_setup_basis_dict(key, parent_name="", panel_id=0): return panel_id recursive_setup_basis_dict((0,)) + # The order `recursive_setup_basis_dict` visits panels does not necessarily match + # the order returned by dxtbx's `get_detector` and `get_raw_data`. + # See https://github.com/cctbx/dxtbx/issues/745. + self.panelindices = panelindices # needed in add_data_to_cbf + sorted_panels = [detector[i] for i in panelindices] if index is None: cbf_root = self.imageset.paths()[0] @@ -327,7 +335,7 @@ def recursive_setup_basis_dict(key, parent_name="", panel_id=0): # defined as [min-trusted-value, max-trusted-value]. The CBF definition # of 'overload' is in fact saturation - i.e. the max-trusted-value, while # the undefined_value is below the min-trusted-value. - trusted_ranges = [panel.get_trusted_range() for panel in detector] + trusted_ranges = [panel.get_trusted_range() for panel in sorted_panels] try: add_frame_specific_cbf_tables( cbf, @@ -336,7 +344,7 @@ def recursive_setup_basis_dict(key, parent_name="", panel_id=0): trusted_ranges, diffrn_id, False, - gain=[panel.get_gain() for panel in detector], + gain=[panel.get_gain() for panel in sorted_panels], flux=beam.get_flux(), ) except TypeError: @@ -348,7 +356,7 @@ def recursive_setup_basis_dict(key, parent_name="", panel_id=0): trusted_ranges, diffrn_id, False, - gain=[panel.get_gain() for panel in detector], + gain=[panel.get_gain() for panel in sorted_panels], ) """Data items in the AXIS category record the information required @@ -596,6 +604,7 @@ def add_data_to_cbf(self, cbf, index=None, data=None, verbose=False): data = self.imageset.get_raw_data(index) if not isinstance(data, tuple): data = (data,) + data = tuple([data[i] for i in self.panelindices]) array_names = [] cbf.find_category(b"diffrn_data_frame")