Skip to content

Commit

Permalink
FormatMRC: more robust handling of extended headers. (#679)
Browse files Browse the repository at this point in the history
For mrcfile>=1.5.0 use the indexed_extended_header.

Attempt to process the extended header only if its length is equal
to the number of images.

Ignore extended header values if the pixel size is set to zero.
  • Loading branch information
dagewa authored and graeme-winter committed Feb 5, 2024
1 parent b192cf2 commit 8362f0d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions newsfragments/679.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``FormatMRC``: better handling of extended headers: padding is supported in mrcfile>=1.5.0 (https://github.com/ccpem/mrcfile/issues/50), and extended headers are ignored if they contain junk values.
18 changes: 14 additions & 4 deletions src/dxtbx/format/FormatMRC.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ def _start(self):

with mrcfile.open(self._image_file, header_only=True) as mrc:
h = mrc.header
xh = mrc.extended_header
try:
xh = mrc.indexed_extended_header
except AttributeError:
# mrcfile<1.5.0 does not have indexed_extended_header
xh = mrc.extended_header

self._header_dictionary = self._unpack_header(h)
if len(xh) > 0:
self._extend_header(xh)
if len(xh) == int(h["nz"]):
try:
self._extend_header(xh)
except ValueError:
# Ignore problematic extended headers
pass

@staticmethod
def _unpack_header(header):
Expand Down Expand Up @@ -73,9 +81,11 @@ def _extend_header(self, xh):
self._header_dictionary["tilt_axis"] = xh["Tilt axis angle"][0]
self._header_dictionary["pixelSpacing"] = xh["Pixel size X"][0]
assert self._header_dictionary["pixelSpacing"] == xh["Pixel size Y"][0]
if self._header_dictionary["pixelSpacing"] == 0:
raise ValueError("Incorrect extended header")
self._header_dictionary["acceleratingVoltage"] = xh["HT"][0]
self._header_dictionary["camera"] = xh["Camera name"][0]
self._header_dictionary["binning"] = xh["Binning Width"][0]
self._header_dictionary["binning"] = max(xh["Binning Width"][0], 1)
self._header_dictionary["noiseReduction"] = xh["Ceta noise reduction"][0]
if b"Ceta" in self._header_dictionary["camera"]:
# Does this ever differ from the Binning Width from the header?
Expand Down

0 comments on commit 8362f0d

Please sign in to comment.