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

Allow static background in metadata to be a Dask array #413

Merged
merged 2 commits into from
Aug 19, 2021
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 doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Changed

Fixed
-----
- Allow static background in EBSD metadata to be a Dask array.
(`#413 <https://github.com/pyxem/kikuchipy/pull/413>`_)
- Set newest supported version of Sphinx to 4.0.2 so that nbsphinx works.
(`#403 <https://github.com/pyxem/kikuchipy/pull/403>`_)

Expand Down
28 changes: 15 additions & 13 deletions kikuchipy/signals/ebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,29 +516,31 @@ def remove_static_background(
dtype_out = self.data.dtype.type

# Get background pattern
if not isinstance(static_bg, (np.ndarray, da.Array)):
if static_bg is None:
try:
md = self.metadata
ebsd_node = metadata_nodes("ebsd")
static_bg = da.from_array(
md.get_item(ebsd_node + ".static_background"), chunks="auto"
)
except AttributeError:
raise OSError(
"The static background is not a numpy or dask array or could not be"
" read from signal metadata."
static_bg_in_metadata = md.get_item(ebsd_node + ".static_background")
if isinstance(static_bg_in_metadata, (da.Array, np.ndarray)):
static_bg = da.asarray(static_bg_in_metadata, chunks="auto")
else:
raise ValueError
except (AttributeError, ValueError):
raise ValueError(
"`static_bg` is not a valid NumPy or Dask array or could not be "
"read from signal metadata"
)
if dtype_out != static_bg.dtype:
raise ValueError(
f"The static background dtype_out {static_bg.dtype} is not the same as "
f"pattern dtype_out {dtype_out}."
f"Static background dtype_out {static_bg.dtype} is not the same as "
f"pattern dtype_out {dtype_out}"
)
pat_shape = self.axes_manager.signal_shape[::-1]
bg_shape = static_bg.shape
if bg_shape != pat_shape:
raise OSError(
f"The pattern {pat_shape} and static background {bg_shape} shapes are "
"not identical."
raise ValueError(
f"Signal {pat_shape} and static background {bg_shape} shapes are not "
"identical"
)
dtype = np.float32
static_bg = static_bg.astype(dtype)
Expand Down
12 changes: 9 additions & 3 deletions kikuchipy/signals/tests/test_ebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ def test_remove_static_background(
(
np.ones((3, 3), dtype=np.int8),
ValueError,
"The static background dtype_out",
"Static background dtype_out",
),
(None, OSError, "The static background is not a numpy or dask"),
(np.ones((3, 2), dtype=np.uint8), OSError, "The pattern"),
(None, ValueError, "`static_bg` is not a valid NumPy or Dask array"),
(np.ones((3, 2), dtype=np.uint8), ValueError, "Signal"),
],
)
def test_incorrect_static_background_pattern(
Expand All @@ -265,6 +265,12 @@ def test_lazy_remove_static_background(self, dummy_signal, dummy_background):
dummy_signal.remove_static_background(static_bg=dummy_background)
assert isinstance(dummy_signal.data, da.Array)

ebsd_node = kp.signals.util.metadata_nodes("ebsd")
dummy_signal.metadata.set_item(
ebsd_node + ".static_background", da.from_array(dummy_background)
)
dummy_signal.remove_static_background()

def test_remove_static_background_scalebg(self, dummy_signal, dummy_background):
dummy_signal2 = dummy_signal.deepcopy()

Expand Down