Skip to content

Commit

Permalink
Merge branch 'RELEASE_next_patch' into RELEASE_next_minor
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Sep 12, 2020
2 parents ebe06c0 + 43a9b4a commit 912fc57
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
33 changes: 27 additions & 6 deletions hyperspy/_signals/lazy.py
Expand Up @@ -29,6 +29,8 @@


from hyperspy.signal import BaseSignal
from hyperspy.defaults_parser import preferences
from hyperspy.docstrings.signal import SHOW_PROGRESSBAR_ARG
from hyperspy.exceptions import VisibleDeprecationWarning
from hyperspy.external.progressbar import progressbar
from hyperspy.misc.array_tools import _requires_linear_rebin
Expand Down Expand Up @@ -85,28 +87,47 @@ class LazySignal(BaseSignal):
"""
_lazy = True

def compute(self, progressbar=True, close_file=False):
def compute(self, close_file=False, show_progressbar=None, **kwargs):
"""Attempt to store the full signal in memory.
close_file: bool
Parameters
----------
close_file : bool, default False
If True, attemp to close the file associated with the dask
array data if any. Note that closing the file will make all other
associated lazy signals inoperative.
%s
Returns
-------
None
"""
if progressbar:
cm = ProgressBar
else:
cm = dummy_context_manager
if "progressbar" in kwargs:
warnings.warn(
"The `progressbar` keyword is deprecated and will be removed "
"in HyperSpy 2.0. Use `show_progressbar` instead.",
VisibleDeprecationWarning,
)
show_progressbar = kwargs["progressbar"]

if show_progressbar is None:
show_progressbar = preferences.General.show_progressbar

cm = ProgressBar if show_progressbar else dummy_context_manager

with cm():
da = self.data
data = da.compute()
if close_file:
self.close_file()
self.data = data

self._lazy = False
self._assign_subclass()

compute.__doc__ %= SHOW_PROGRESSBAR_ARG

def close_file(self):
"""Closes the associated data file if any.
Expand Down
12 changes: 12 additions & 0 deletions hyperspy/tests/signal/test_lazy.py
Expand Up @@ -24,6 +24,7 @@
import hyperspy.api as hs
from hyperspy import _lazy_signals
from hyperspy._signals.lazy import _reshuffle_mixed_blocks, to_array
from hyperspy.exceptions import VisibleDeprecationWarning


def _signal():
Expand Down Expand Up @@ -137,3 +138,14 @@ def test_ma_lazify():
assert np.isnan(l.data[1].compute())
ss = hs.stack([s, s])
assert np.isnan(ss.data[:, 1]).all()


def test_warning():
sig = _signal()

with pytest.warns(VisibleDeprecationWarning, match="progressbar"):
sig.compute(progressbar=False)

assert sig._lazy == False
thing = to_array(sig, chunks=None)
assert isinstance(thing, np.ndarray)

0 comments on commit 912fc57

Please sign in to comment.