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

FormatMRC: more robust handling of extended headers. #679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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:

Check warning on line 44 in src/dxtbx/format/FormatMRC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatMRC.py#L44

Added line #L44 was not covered by tests
# mrcfile<1.5.0 does not have indexed_extended_header
xh = mrc.extended_header

Check warning on line 46 in src/dxtbx/format/FormatMRC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatMRC.py#L46

Added line #L46 was not covered by tests

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:

Check warning on line 52 in src/dxtbx/format/FormatMRC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatMRC.py#L52

Added line #L52 was not covered by tests
# Ignore problematic extended headers
pass

Check warning on line 54 in src/dxtbx/format/FormatMRC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatMRC.py#L54

Added line #L54 was not covered by tests

@staticmethod
def _unpack_header(header):
Expand Down Expand Up @@ -73,9 +81,11 @@
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")

Check warning on line 85 in src/dxtbx/format/FormatMRC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatMRC.py#L85

Added line #L85 was not covered by tests
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
Loading