Skip to content

Commit

Permalink
Rename max_workers to num_workers to be consistent with dask
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpre committed Sep 2, 2023
1 parent c22e028 commit 13e7f7e
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 91 deletions.
26 changes: 0 additions & 26 deletions doc/user_guide/signal/generic_tools.rst
Expand Up @@ -345,32 +345,6 @@ data (default, ``True``) or storing it to a new signal (``False``).
(512, 512)
(724, 724)
.. _parallel-map-label:

The execution can be sped up by passing ``parallel`` keyword to the
:py:meth:`~.api.signals.BaseSignal.map` method:

.. code-block:: python
>>> import time
>>> def slow_func(data):
... time.sleep(1.)
... return data + 1
>>> s = hs.signals.Signal1D(np.arange(40).reshape((20, 2)))
>>> s
<Signal1D, title: , dimensions: (20|2)>
>>> s.map(slow_func, parallel=False)
100%|██████████████████████████████████████| 20/20 [00:20<00:00, 1.00s/it]
>>> # some operations will be done in parallel:
>>> s.map(slow_func, parallel=True)
100%|██████████████████████████████████████| 20/20 [00:02<00:00, 6.73it/s]
.. note::

HyperSpy implements *thread-based* parallelism for the :py:meth:`~.api.signals.BaseSignal.map`
method. You can control the number of threads that are created by passing an integer value
to the ``max_workers`` keyword argument. By default, it will use ``min(32, os.cpu_count())``.

.. versionadded:: 1.4
Iterating over signal using a parameter with no navigation dimension.

Expand Down
4 changes: 2 additions & 2 deletions doc/user_guide/signal2d.rst
Expand Up @@ -53,7 +53,7 @@ Sub-pixel accuracy can be achieved in two ways:
If you have a large stack of images, the image alignment is automatically done in
parallel.

You can control the number of threads used with the ``max_workers`` argument. Or by adjusting
You can control the number of threads used with the ``num_workers`` argument. Or by adjusting

Check warning on line 56 in doc/user_guide/signal2d.rst

View workflow job for this annotation

GitHub Actions / Docs (PR comments)

undefined label: 'dask-label'
the scheduler of the :ref:`dask <dask-label>` backend.

.. code-block:: python
Expand All @@ -62,7 +62,7 @@ the scheduler of the :ref:`dask <dask-label>` backend.
>>> shifts = s.estimate_shift2D()
# Align images in parallel using 4 threads
>>> s.align2D(shifts=shifts, max_workers=4)
>>> s.align2D(shifts=shifts, num_workers=4)
.. _signal2D.crop:

Expand Down
8 changes: 4 additions & 4 deletions hyperspy/_signals/complex_signal.py
Expand Up @@ -31,7 +31,7 @@
)
from hyperspy.docstrings.signal import (
SHOW_PROGRESSBAR_ARG,
MAX_WORKERS_ARG,
NUM_WORKERS_ARG,
LAZYSIGNAL_DOC,
)
from hyperspy.misc.utils import parse_quantity
Expand Down Expand Up @@ -167,7 +167,7 @@ def change_dtype(self, dtype):
'Complex data can only be converted into other complex dtypes!')

def unwrapped_phase(self, wrap_around=False, seed=None,
show_progressbar=None, max_workers=None):
show_progressbar=None, num_workers=None):
"""Return the unwrapped phase as an appropriate HyperSpy signal.
Parameters
Expand Down Expand Up @@ -202,11 +202,11 @@ def unwrapped_phase(self, wrap_around=False, seed=None,
phase = self.phase
phase.map(unwrap_phase, wrap_around=wrap_around, seed=seed,
show_progressbar=show_progressbar, ragged=False,
max_workers=max_workers)
num_workers=num_workers)
phase.metadata.General.title = f'unwrapped {phase.metadata.General.title}'
return phase # Now unwrapped!

unwrapped_phase.__doc__ %= (SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG)
unwrapped_phase.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG)

def __call__(self, axes_manager=None, power_spectrum=False,
fft_shift=False, as_numpy=None):
Expand Down
11 changes: 4 additions & 7 deletions hyperspy/_signals/eels.py
Expand Up @@ -46,8 +46,7 @@
)
from hyperspy.docstrings.signal import (
SHOW_PROGRESSBAR_ARG,
PARALLEL_ARG,
MAX_WORKERS_ARG,
NUM_WORKERS_ARG,
SIGNAL_MASK_ARG,
NAVIGATION_MASK_ARG,
LAZYSIGNAL_DOC,
Expand Down Expand Up @@ -1032,7 +1031,7 @@ def fourier_ratio_deconvolution(self, ll,

def richardson_lucy_deconvolution(self, psf, iterations=15,
show_progressbar=None,
parallel=None, max_workers=None):
num_workers=None):
"""1D Richardson-Lucy Poissonian deconvolution of
the spectrum by the given kernel.
Expand All @@ -1047,7 +1046,6 @@ def richardson_lucy_deconvolution(self, psf, iterations=15,
increasing the value will increase the noise amplification.
%s
%s
%s
Raises
------
Expand Down Expand Up @@ -1085,8 +1083,7 @@ def deconv_function(signal, kernel=None,

ds = self.map(deconv_function, kernel=psf, iterations=iterations,
psf_size=psf_size, show_progressbar=show_progressbar,
parallel=parallel, max_workers=max_workers,
ragged=False, inplace=False)
num_workers=num_workers, ragged=False, inplace=False)

ds.metadata.General.title += (
' after Richardson-Lucy deconvolution %i iterations' %
Expand All @@ -1096,7 +1093,7 @@ def deconv_function(signal, kernel=None,
'_after_R-L_deconvolution_%iiter' % iterations)
return ds

richardson_lucy_deconvolution.__doc__ %= (SHOW_PROGRESSBAR_ARG, PARALLEL_ARG, MAX_WORKERS_ARG)
richardson_lucy_deconvolution.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG)

def _are_microscope_parameters_missing(self, ignore_parameters=[]):
"""
Expand Down
50 changes: 25 additions & 25 deletions hyperspy/_signals/signal1d.py
Expand Up @@ -50,7 +50,7 @@
from hyperspy.docstrings.signal1d import CROP_PARAMETER_DOC, SPIKES_REMOVAL_TOOL_DOCSTRING
from hyperspy.docstrings.signal import (
SHOW_PROGRESSBAR_ARG,
MAX_WORKERS_ARG,
NUM_WORKERS_ARG,
SIGNAL_MASK_ARG,
NAVIGATION_MASK_ARG,
LAZYSIGNAL_DOC,
Expand Down Expand Up @@ -393,7 +393,7 @@ def shift1D(
expand=False,
fill_value=np.nan,
show_progressbar=None,
max_workers=None,
num_workers=None,
):
"""Shift the data in place over the signal axis by the amount specified
by an array.
Expand Down Expand Up @@ -498,7 +498,7 @@ def shift1D(
fill_value=fill_value,
kind=interpolation_method,
show_progressbar=show_progressbar,
max_workers=max_workers,
num_workers=num_workers,
ragged=False)

if crop and not expand:
Expand All @@ -509,15 +509,15 @@ def shift1D(
ihigh)

self.events.data_changed.trigger(obj=self)
shift1D.__doc__ %= (CROP_PARAMETER_DOC, SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG)
shift1D.__doc__ %= (CROP_PARAMETER_DOC, SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG)

def interpolate_in_between(
self,
start,
end,
delta=3,
show_progressbar=None,
max_workers=None,
num_workers=None,
**kwargs,
):
"""Replace the data in a given range by interpolation.
Expand Down Expand Up @@ -572,11 +572,11 @@ def interpolating_function(dat):
interpolating_function,
ragged=False,
show_progressbar=show_progressbar,
max_workers=max_workers,
num_workers=num_workers,
)
self.events.data_changed.trigger(obj=self)

interpolate_in_between.__doc__ %= (SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG)
interpolate_in_between.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG)

def estimate_shift1D(
self,
Expand All @@ -588,7 +588,7 @@ def estimate_shift1D(
number_of_interpolation_points=5,
mask=None,
show_progressbar=None,
max_workers=None,
num_workers=None,
):
"""Estimate the shifts in the current signal axis using
cross-correlation.
Expand Down Expand Up @@ -669,7 +669,7 @@ def estimate_shift1D(
ragged=False,
inplace=False,
show_progressbar=show_progressbar,
max_workers=max_workers,
num_workers=num_workers,
)
shift_array = shift_signal.data
if max_shift is not None:
Expand All @@ -686,7 +686,7 @@ def estimate_shift1D(
shift_array = shift_array.compute()
return shift_array

estimate_shift1D.__doc__ %= (SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG)
estimate_shift1D.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG)

def align1D(self,
start=None,
Expand Down Expand Up @@ -832,7 +832,7 @@ def smooth_savitzky_golay(
polynomial_order=None,
window_length=None,
differential_order=0,
max_workers=None,
num_workers=None,
display=True,
toolkit=None,
):
Expand Down Expand Up @@ -876,7 +876,7 @@ def smooth_savitzky_golay(
axis = self.axes_manager.signal_axes[0]
self.map(savgol_filter, window_length=window_length,
polyorder=polynomial_order, deriv=differential_order,
delta=axis.scale, ragged=False, max_workers=max_workers)
delta=axis.scale, ragged=False, num_workers=num_workers)
else:
# Interactive mode
smoother = SmoothingSavitzkyGolay(self)
Expand All @@ -887,14 +887,14 @@ def smooth_savitzky_golay(
smoother.window_length = window_length
return smoother.gui(display=display, toolkit=toolkit)

smooth_savitzky_golay.__doc__ %= (MAX_WORKERS_ARG, DISPLAY_DT, TOOLKIT_DT)
smooth_savitzky_golay.__doc__ %= (NUM_WORKERS_ARG, DISPLAY_DT, TOOLKIT_DT)

def smooth_lowess(
self,
smoothing_parameter=None,
number_of_iterations=None,
show_progressbar=None,
max_workers=None,
num_workers=None,
display=True,
toolkit=None,
):
Expand Down Expand Up @@ -937,14 +937,14 @@ def smooth_lowess(
n_iter=number_of_iterations,
show_progressbar=show_progressbar,
ragged=False,
max_workers=max_workers)
smooth_lowess.__doc__ %= (SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG, DISPLAY_DT, TOOLKIT_DT)
num_workers=num_workers)
smooth_lowess.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG, DISPLAY_DT, TOOLKIT_DT)

def smooth_tv(
self,
smoothing_parameter=None,
show_progressbar=None,
max_workers=None,
num_workers=None,
display=True,
toolkit=None,
):
Expand Down Expand Up @@ -980,9 +980,9 @@ def smooth_tv(
self.map(_tv_denoise_1d, weight=smoothing_parameter,
ragged=False,
show_progressbar=show_progressbar,
max_workers=max_workers)
num_workers=num_workers)

smooth_tv.__doc__ %= (SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG, DISPLAY_DT, TOOLKIT_DT)
smooth_tv.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG, DISPLAY_DT, TOOLKIT_DT)

def filter_butterworth(self,
cutoff_frequency_ratio=None,
Expand Down Expand Up @@ -1349,7 +1349,7 @@ def find_peaks1D_ohaver(self, xdim=None,
medfilt_radius=5,
maxpeakn=30000,
peakgroup=10,
max_workers=None):
num_workers=None):
"""Find positive peaks along a 1D Signal. It detects peaks by looking
for downward zero-crossings in the first derivative that exceed
'slope_thresh'.
Expand Down Expand Up @@ -1412,20 +1412,20 @@ def find_peaks1D_ohaver(self, xdim=None,
peakgroup=peakgroup,
subchannel=subchannel,
ragged=True,
max_workers=max_workers,
num_workers=num_workers,
inplace=False,
lazy_output=False)
return peaks.data

find_peaks1D_ohaver.__doc__ %= ( MAX_WORKERS_ARG)
find_peaks1D_ohaver.__doc__ %= ( NUM_WORKERS_ARG)

def estimate_peak_width(
self,
factor=0.5,
window=None,
return_interval=False,
show_progressbar=None,
max_workers=None,
num_workers=None,
):
"""Estimate the width of the highest intensity of peak
of the spectra at a given fraction of its maximum.
Expand Down Expand Up @@ -1499,7 +1499,7 @@ def estimating_function(spectrum,
ragged=False,
inplace=False,
show_progressbar=show_progressbar,
max_workers=max_workers,
num_workers=num_workers,
)
left, right = both.T.split()
width = right - left
Expand Down Expand Up @@ -1530,7 +1530,7 @@ def estimating_function(spectrum,
else:
return width

estimate_peak_width.__doc__ %= (SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG)
estimate_peak_width.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG)

def plot(self,
navigator="auto",
Expand Down
14 changes: 7 additions & 7 deletions hyperspy/_signals/signal2d.py
Expand Up @@ -46,7 +46,7 @@
PLOT2D_KWARGS_DOCSTRING)
from hyperspy.docstrings.signal import (
SHOW_PROGRESSBAR_ARG,
MAX_WORKERS_ARG,
NUM_WORKERS_ARG,
LAZYSIGNAL_DOC,
)
from hyperspy.ui_registry import DISPLAY_DT, TOOLKIT_DT
Expand Down Expand Up @@ -611,7 +611,7 @@ def align2D(
expand=False,
interpolation_order=1,
show_progressbar=None,
max_workers=None,
num_workers=None,
**kwargs,
):
"""Align the images in-place using :py:func:`scipy.ndimage.shift`.
Expand Down Expand Up @@ -738,7 +738,7 @@ def align2D(
shift_image,
shift=signal_shifts,
show_progressbar=show_progressbar,
max_workers=max_workers,
num_workers=num_workers,
ragged=False,
inplace=True,
fill_value=fill_value,
Expand Down Expand Up @@ -772,7 +772,7 @@ def align2D(
if return_shifts:
return shifts

align2D.__doc__ %= (SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG)
align2D.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG)

def calibrate(
self,
Expand Down Expand Up @@ -923,7 +923,7 @@ def add_ramp(self, ramp_x, ramp_y, offset=0):

def find_peaks(self, method='local_max', interactive=True,
current_index=False, show_progressbar=None,
max_workers=None, display=True, toolkit=None,
num_workers=None, display=True, toolkit=None,
get_intensity=False,
**kwargs):
"""Find peaks in a 2D signal.
Expand Down Expand Up @@ -1042,12 +1042,12 @@ def find_peaks(self, method='local_max', interactive=True,
else:
peaks = self.map(method_func, show_progressbar=show_progressbar,
inplace=False, ragged=True,
max_workers=max_workers, **kwargs)
num_workers=num_workers, **kwargs)
peaks.metadata.add_node("Peaks") # add information about the signal Axes
peaks.metadata.Peaks.signal_axes = deepcopy(self.axes_manager.signal_axes)
return peaks

find_peaks.__doc__ %= (SHOW_PROGRESSBAR_ARG, MAX_WORKERS_ARG,
find_peaks.__doc__ %= (SHOW_PROGRESSBAR_ARG, NUM_WORKERS_ARG,
DISPLAY_DT, TOOLKIT_DT)


Expand Down

0 comments on commit 13e7f7e

Please sign in to comment.