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

Fix reading of Oxford Instruments binary .ebsp files of version 4 #602

Merged
merged 3 commits into from
Feb 3, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ Removed

Fixed
-----
- Oxford Instruments .ebsp files of version 4 can now be read.
(`#602 <https://github.com/pyxem/kikuchipy/pull/602>`_)
- When loading EBSD patterns from H5OINA files, the detector tilt and binning are
available in the returned signal's ``detector`` attribute.
(`#600 <https://github.com/pyxem/kikuchipy/pull/600>`_)
Expand Down
7 changes: 5 additions & 2 deletions kikuchipy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def oxford_binary_file(tmpdir, request):
fname = tmpdir.join("dummy_oxford_file.ebsp")
f = open(fname, mode="w")

if ver != 0:
if ver > 0:
np.array(-ver, dtype=np.int64).tofile(f)

pattern_header_size = 16
Expand All @@ -366,8 +366,11 @@ def oxford_binary_file(tmpdir, request):
pattern_starts = np.arange(n_patterns, dtype=np.int64)
pattern_starts *= pattern_header_size + n_bytes + pattern_footer_size
pattern_starts += n_patterns * 8
if ver != 0:
if ver in [1, 2, 3]:
pattern_starts += 8
elif ver > 3:
np.array(0, dtype=np.uint8).tofile(f)
pattern_starts += 9

pattern_starts = np.roll(pattern_starts, shift=1)
if not all_present:
Expand Down
27 changes: 21 additions & 6 deletions kikuchipy/io/plugins/oxford_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"""Reader of uncompressed EBSD patterns from a Oxford Instruments binary
.ebsp file.

Information about the file format was provided by Oxford Instruments.
Information about the file format was generously provided by Oxford
Instruments.
"""

import logging
import os
from pathlib import Path
import struct
Expand All @@ -35,6 +37,8 @@
__all__ = ["file_reader"]


_logger = logging.getLogger(__name__)

# Plugin characteristics
# ----------------------
format_name = "Oxford binary"
Expand Down Expand Up @@ -114,6 +118,14 @@ def __init__(self, file: BinaryIO):
self.file = file # Already open file

self.version = self.get_version()
_logger.debug(f"Reading Oxford binary file of version {self.version}")

# If version > 3, read in the extra byte after file version and
# add it to the debug log
if self.version > 3: # pragma: no cover
self.file.seek(self.pattern_starts_byte_position - 1)
unknown_byte = np.fromfile(self.file, dtype=np.uint8, count=1)[0]
_logger.debug(f"Unknown byte (uint8) in file of version 4: {unknown_byte}")

# Number of patterns in the file is not known, so this is
# guessed from the file header where the file byte positions of
Expand Down Expand Up @@ -204,13 +216,16 @@ def pattern_order(self) -> np.ndarray:
@property
def pattern_starts_byte_position(self) -> int:
"""File byte position of file byte positions of patterns. For
.ebsp file version 0, this is at the first byte, while for later
versions, this is at the ninth byte, after the file version.
.ebsp file version 0, this is at the first byte, for versions
1-3 this is at the ninth byte, while for version 4 this is at
the tenth byte.
"""
if self.version != 0:
return 8
else:
if self.version == 0:
return 0
elif self.version > 3:
return 9
else:
return 8

def get_memmap(self) -> np.memmap:
"""Return a memory map of the pattern header, actual patterns,
Expand Down
1 change: 1 addition & 0 deletions kikuchipy/io/plugins/tests/test_oxford_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_not_all_patterns_present(self, oxford_binary_file):
(((2, 3), (60, 60), np.uint8, 2, False, True), 2, (2, 3)),
(((2, 3), (60, 60), np.uint16, 1, False, True), 1, (2, 3)),
(((2, 3), (60, 60), np.uint8, 0, False, True), 0, (6,)),
(((2, 3), (60, 60), np.uint8, 4, False, True), 4, (2, 3)),
],
indirect=["oxford_binary_file"],
)
Expand Down