def reconstruct_object(flags, value): """ Reconstructs the value (if necessary) after having saved it in a dictionary """ if not isinstance(flags, list): flags = parse_flag_string(flags) if 'sig' in flags: if isinstance(value, dict): from hyperspy.signal import BaseSignal value = BaseSignal(**value) value._assign_subclass() return value if 'fn' in flags: ifdill, thing = value if ifdill is None: return thing if ifdill in [True, 'True', b'True']: return dill.loads(thing) # should not be reached raise ValueError("The object format is not recognized") if isinstance(value, Array): value = value.compute() return value
def find_peaks(self, method='local_max', interactive=True, current_index=False, show_progressbar=None, parallel=None, max_workers=None, display=True, toolkit=None, **kwargs): """Find peaks in a 2D signal. Function to locate the positive peaks in an image using various, user specified, methods. Returns a structured array containing the peak positions. Parameters ---------- method : str Select peak finding algorithm to implement. Available methods are: * 'local_max' - simple local maximum search using the :py:func:`skimage.feature.peak_local_max` function * 'max' - simple local maximum search using the :py:func:`~hyperspy.utils.peakfinders2D.find_peaks_max`. * 'minmax' - finds peaks by comparing maximum filter results with minimum filter, calculates centers of mass. See the :py:func:`~hyperspy.utils.peakfinders2D.find_peaks_minmax` function. * 'zaefferer' - based on gradient thresholding and refinement by local region of interest optimisation. See the :py:func:`~hyperspy.utils.peakfinders2D.find_peaks_zaefferer` function. * 'stat' - based on statistical refinement and difference with respect to mean intensity. See the :py:func:`~hyperspy.utils.peakfinders2D.find_peaks_stat` function. * 'laplacian_of_gaussian' - a blob finder using the laplacian of Gaussian matrices approach. See the :py:func:`~hyperspy.utils.peakfinders2D.find_peaks_log` function. * 'difference_of_gaussian' - a blob finder using the difference of Gaussian matrices approach. See the :py:func:`~hyperspy.utils.peakfinders2D.find_peaks_log` function. * 'template_matching' - A cross correlation peakfinder. This method requires providing a template with the ``template`` parameter, which is used as reference pattern to perform the template matching to the signal. It uses the :py:func:`skimage.feature.match_template` function and the peaks position are obtained by using `minmax` method on the template matching result. interactive : bool If True, the method parameter can be adjusted interactively. If False, the results will be returned. current_index : bool if True, the computation will be performed for the current index. %s %s %s %s %s **kwargs : dict Keywords parameters associated with above methods, see the documentation of each method for more details. Notes ----- As a convenience, the 'local_max' method accepts the 'distance' and 'threshold' argument, which will be map to the 'min_distance' and 'threshold_abs' of the :py:func:`skimage.feature.peak_local_max` function. Returns ------- peaks : :py:class:`~hyperspy.signal.BaseSignal` or numpy.ndarray if current_index=True Array of shape `_navigation_shape_in_array` in which each cell contains an array with dimensions (npeaks, 2) that contains the `x, y` pixel coordinates of peaks found in each image sorted first along `y` and then along `x`. """ method_dict = { 'local_max': find_local_max, 'max': find_peaks_max, 'minmax': find_peaks_minmax, 'zaefferer': find_peaks_zaefferer, 'stat': find_peaks_stat, 'laplacian_of_gaussian': find_peaks_log, 'difference_of_gaussian': find_peaks_dog, 'template_matching': find_peaks_xc, } # As a convenience, we map 'distance' to 'min_distance' and # 'threshold' to 'threshold_abs' when using the 'local_max' method to # match with the arguments of skimage.feature.peak_local_max. if method == 'local_max': if 'distance' in kwargs.keys(): kwargs['min_distance'] = kwargs.pop('distance') if 'threshold' in kwargs.keys(): kwargs['threshold_abs'] = kwargs.pop('threshold') if method in method_dict.keys(): method_func = method_dict[method] else: raise NotImplementedError(f"The method `{method}` is not " "implemented. See documentation for " "available implementations.") if interactive: # Create a peaks signal with the same navigation shape as a # placeholder for the output axes_dict = self.axes_manager._get_axes_dicts( self.axes_manager.navigation_axes) peaks = BaseSignal(np.empty(self.axes_manager.navigation_shape), axes=axes_dict) pf2D = PeaksFinder2D(self, method=method, peaks=peaks, **kwargs) pf2D.gui(display=display, toolkit=toolkit) elif current_index: peaks = method_func(self.__call__(), **kwargs) else: peaks = self.map(method_func, show_progressbar=show_progressbar, parallel=parallel, inplace=False, ragged=True, max_workers=max_workers, **kwargs) if peaks._lazy: peaks.compute() return peaks