Skip to content

Commit

Permalink
Merge pull request #2695 from ericpre/map_arguments
Browse files Browse the repository at this point in the history
Follow up #2617
  • Loading branch information
francisco-dlp committed Apr 8, 2021
2 parents 499824b + 1d6a320 commit 9d4b920
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
12 changes: 12 additions & 0 deletions hyperspy/_signals/lazy.py
Expand Up @@ -574,6 +574,9 @@ def _map_iterate(self,
# unpacking keyword arguments
if iterating_kwargs is None:
iterating_kwargs = {}
elif isinstance(iterating_kwargs, (tuple, list)):
iterating_kwargs = dict((k, v) for k, v in iterating_kwargs)

nav_indexes = self.axes_manager.navigation_indices_in_array
if ragged and inplace:
raise ValueError("Ragged and inplace are not compatible with a lazy signal")
Expand All @@ -588,7 +591,15 @@ def _map_iterate(self,
nav_chunks = old_sig._get_navigation_chunk_size()
args = ()
arg_keys = ()

for key in iterating_kwargs:
if not isinstance(iterating_kwargs[key], BaseSignal):
iterating_kwargs[key] = BaseSignal(iterating_kwargs[key].T).T
warnings.warn(
"Passing arrays as keyword arguments can be ambigous. "
"This is deprecated and will be removed in HyperSpy 2.0. "
"Pass signal instances instead.",
VisibleDeprecationWarning)
if iterating_kwargs[key]._lazy:
if iterating_kwargs[key]._get_navigation_chunk_size() != nav_chunks:
iterating_kwargs[key].rechunk(nav_chunks=nav_chunks)
Expand All @@ -604,6 +615,7 @@ def _map_iterate(self,
else:
args += (iterating_kwargs[key].data, )
arg_keys += (key,)

if autodetermine: #trying to guess the output d-type and size from one signal
testing_kwargs = {}
for key in iterating_kwargs:
Expand Down
1 change: 1 addition & 0 deletions hyperspy/misc/utils.py
Expand Up @@ -1158,6 +1158,7 @@ def func(*args):

return func, iterators


def process_function_blockwise(data,
*args,
function,
Expand Down
7 changes: 6 additions & 1 deletion hyperspy/signal.py
Expand Up @@ -4607,7 +4607,9 @@ def _map_iterate(
A tuple with structure (('key1', value1), ('key2', value2), ..)
where the key-value pairs will be passed as kwargs for the
function to be mapped, and the values will be iterated together
with the signal navigation.
with the signal navigation. The value needs to be a signal
instance because passing array can be ambigous and will be removed
in HyperSpy 2.0.
%s
%s
%s
Expand Down Expand Up @@ -4668,6 +4670,9 @@ def _map_iterate(
if parallel is None:
parallel = preferences.General.parallel

if isinstance(iterating_kwargs, (tuple, list)):
iterating_kwargs = dict((k, v) for k, v in iterating_kwargs)

size = max(1, self.axes_manager.navigation_size)
func, iterators = create_map_objects(function, size, iterating_kwargs, **kwargs)
iterators = (self._iterate_signal(),) + iterators
Expand Down
21 changes: 16 additions & 5 deletions hyperspy/tests/signals/test_map_method.py
Expand Up @@ -110,7 +110,7 @@ def test_different_shapes(self, parallel):
if s._lazy:
# inplace not compatible with ragged and lazy
with pytest.raises(ValueError):
s.map(rotate, angle=angles.T, reshape=True, inplace=True,
s.map(rotate, angle=angles.T, reshape=True, inplace=True,
ragged=True)
s = s.map(rotate, angle=angles.T, reshape=True, inplace=False,
ragged=True)
Expand Down Expand Up @@ -155,6 +155,7 @@ def test_ragged_navigation_shape(self, ragged):
assert out.axes_manager.navigation_shape == s.axes_manager.navigation_shape
assert out.data.shape[:2] == s.axes_manager.navigation_shape[::-1]


@lazifyTestClass(ragged=False)
class TestSignal1D:

Expand Down Expand Up @@ -212,6 +213,7 @@ def test_ragged(self, ragged):
else:
np.testing.assert_allclose(s.data, out.data)


@lazifyTestClass(ragged=False)
class TestSignal0D:

Expand Down Expand Up @@ -325,6 +327,7 @@ def test_func(d, i):
assert not 'b' in ax_names
assert 0 == sl.axes_manager.navigation_dimension


class TestLazyMap:
def setup_method(self, method):
dask_array = da.zeros((10, 11, 12, 13), chunks=(3, 3, 3, 3))
Expand All @@ -347,6 +350,16 @@ def test_map_nav_size_error(self):
with pytest.raises(ValueError):
self.s.map(function=f, b=s_iter, inplace=False)

def test_map_iterate_array(self):
s = self.s
iter_array, _ = np.meshgrid(range(11), range(10))
f = lambda a, b: a + b
iterating_kwargs = {'b':iter_array.T}
s_out = s._map_iterate(function=f, iterating_kwargs=iterating_kwargs,
inplace=False)
np.testing.assert_array_equal(s_out.mean(axis=(2, 3)).data, iter_array)


@pytest.mark.parametrize('ragged', [True, False, None])
def test_singleton(ragged):
sig = hs.signals.Signal2D(np.empty((3, 2)))
Expand All @@ -362,8 +375,8 @@ def test_singleton(ragged):
assert isinstance(_s, hs.signals.BaseSignal)
assert not isinstance(_s, hs.signals.Signal1D)


def test_lazy_singleton():
from hyperspy._signals.lazy import LazySignal
sig = hs.signals.Signal2D(np.empty((3, 2)))
sig = sig.as_lazy()
sig.axes_manager[0].name = 'x'
Expand All @@ -381,8 +394,8 @@ def test_lazy_singleton():
assert not isinstance(_s, hs.signals.Signal1D)
#assert isinstance(_s, LazySignal)


def test_lazy_singleton_ragged():
from hyperspy._signals.lazy import LazySignal
sig = hs.signals.Signal2D(np.empty((3, 2)))
sig = sig.as_lazy()
sig.axes_manager[0].name = 'x'
Expand All @@ -407,5 +420,3 @@ def test_map_ufunc(caplog):
assert np.log(s) == s.map(np.log)
np.testing.assert_allclose(s.data, np.log(data))
assert "can direcly operate on hyperspy signals" in caplog.records[0].message


0 comments on commit 9d4b920

Please sign in to comment.