Exemple #1
0
    def plot_interactive_virtual_image(self, roi, **kwargs):
        """Plots an interactive virtual image formed with a specified and
        adjustable roi.

        Parameters
        ----------
        roi : :obj:`hyperspy.roi.BaseInteractiveROI`
            Any interactive ROI detailed in HyperSpy.
        **kwargs:
            Keyword arguments to be passed to `Diffraction2D.plot`

        Examples
        --------
        .. code-block:: python

            import hyperspy.api as hs
            roi = hs.roi.CircleROI(0, 0, 0.2)
            data.plot_interactive_virtual_image(roi)

        """
        self.plot(**kwargs)
        roi.add_widget(self, axes=self.axes_manager.signal_axes)
        # Add the ROI to the appropriate signal axes.
        dark_field = roi.interactive(self, navigation_signal='same')
        dark_field_placeholder = \
            BaseSignal(np.zeros(self.axes_manager.navigation_shape[::-1]))
        # Create an output signal for the virtual dark-field calculation.
        dark_field_sum = interactive(
            # Create an interactive signal
            dark_field.sum,
            # Formed from the sum of the pixels in the dark-field signal
            event=dark_field.axes_manager.events.any_axis_changed,
            # That updates whenever the widget is moved
            axis=dark_field.axes_manager.signal_axes,
            out=dark_field_placeholder,
            # And outputs into the prepared placeholder.
        )
        dark_field_sum.axes_manager.update_axes_attributes_from(
            self.axes_manager.navigation_axes,
            ['scale', 'offset', 'units', 'name'])
        dark_field_sum.metadata.General.title = "Virtual Dark Field"
        # Set the parameters
        dark_field_sum.plot()  # Plot the result
Exemple #2
0
def test_iterate_markers():
    from skimage.feature import peak_local_max
    import scipy.misc
    ims = BaseSignal(scipy.misc.face()).as_signal2D([1, 2])
    index = np.array([peak_local_max(im.data, min_distance=100,
                                     num_peaks=4) for im in ims])
    # Add multiple markers
    for i in range(4):
        xs = index[:, i, 1]
        ys = index[:, i, 0]
        m = markers.point(x=xs, y=ys, color='red')
        ims.add_marker(m, plot_marker=True, permanent=True)
        m = markers.text(x=10 + xs, y=10 + ys, text=str(i), color='k')
        ims.add_marker(m, plot_marker=True, permanent=True)
    xs = index[:, :, 1]
    ys = index[:, :, 0]
    x1 = np.min(xs, 1)
    y1 = np.min(ys, 1)
    x2 = np.max(xs, 1)
    y2 = np.max(ys, 1)
    m = markers.rectangle(x1, y1, x2, y2, color='green')
    ims.add_marker(m, plot_marker=True, permanent=True)
    m = markers.arrow(x1, y1, x2, y2, arrowstyle='<->',edgecolor='red')
    ims.add_marker(m, plot_marker=True, permanent=True)
    m = markers.ellipse((x1+x2)/2, (y1+y2)/2, x2-x1, y2-y1,
                      edgecolor='yellow')
    ims.add_marker(m, plot_marker=True, permanent=True)

    for im in ims:
        m_original = ims.metadata.Markers
        m_iterated = im.metadata.Markers
        for key in m_original.keys():
            mo = m_original[key]
            mi = m_iterated[key]
            assert mo.__class__.__name__ == mi.__class__.__name__
            assert mo.name == mi.name
            assert mo.get_data_position('x1') == mi.get_data_position('x1')
            assert mo.get_data_position('y1') == mi.get_data_position('y1')
            assert mo.get_data_position('text') == mi.get_data_position('text')
            for propkey in mo.marker_properties:
                assert mo.marker_properties[propkey] == \
                    mi.marker_properties[propkey]
Exemple #3
0
    def extract_intensities(self, radius: int = 1):
        """Basic intensity integration routine, takes the maximum value at the
        given vector positions with the number of pixels given by `radius`.

        Parameters
        ----------
        radius: int,
            Number of pixels within which to find the largest maximum

        Returns
        -------
        intensities : :obj:`hyperspy.signals.BaseSignal`
            List of extracted intensities
        """
        intensities = self.dp.map(
            _get_intensities, vectors=self.vector_pixels, radius=radius, inplace=False
        )

        intensities = BaseSignal(intensities)
        intensities.axes_manager.set_signal_dimension(0)

        return intensities
Exemple #4
0
    def get_magnitudes(self, *args, **kwargs):
        """Calculate the magnitude of diffraction vectors.

        Returns
        -------
        magnitudes : BaseSignal
            A signal with navigation dimensions as the original diffraction
            vectors containging an array of gvector magnitudes at each
            navigation position.

        """
        #If ragged the signal axes will not be defined
        if len(self.axes_manager.signal_axes) == 0:
            magnitudes = self.map(calculate_norms_ragged,
                                  inplace=False,
                                  *args,
                                  **kwargs)
        #Otherwise easier to calculate.
        else:
            magnitudes = BaseSignal(calculate_norms(self))
            magnitudes.axes_manager.set_signal_dimension(0)

        return magnitudes
Exemple #5
0
    def __call__(self, signal, out=None, axes=None, order=0):
        """Slice the signal according to the ROI, and return it.

        Arguments
        ---------
        signal : Signal
            The signal to slice with the ROI.
        out : Signal, default = None
            If the 'out' argument is supplied, the sliced output will be put
            into this instead of returning a Signal. See Signal.__getitem__()
            for more details on 'out'.
        axes : specification of axes to use, default = None
            The axes argument specifies which axes the ROI will be applied on.
            The items in the collection can be either of the following:
                * a tuple of:
                    - DataAxis. These will not be checked with
                      signal.axes_manager.
                    - anything that will index signal.axes_manager
                * For any other value, it will check whether the navigation
                  space can fit the right number of axis, and use that if it
                  fits. If not, it will try the signal space.
        order : The spline interpolation order to use when extracting the line
            profile. 0 means nearest-neighbor interpolation, and is both the
            default and the fastest.
        """
        if axes is None and signal in self.signal_map:
            axes = self.signal_map[signal][1]
        else:
            axes = self._parse_axes(axes, signal.axes_manager)
        profile = Line2DROI.profile_line(signal.data, (self.x1, self.y1),
                                         (self.x2, self.y2),
                                         axes=axes,
                                         linewidth=self.linewidth,
                                         order=order)
        length = np.linalg.norm(np.diff(np.array(
            ((self.x1, self.y1), (self.x2, self.y2))),
                                        axis=0),
                                axis=1)[0]
        if out is None:
            axm = signal.axes_manager.deepcopy()
            i0 = min(axes[0].index_in_array, axes[1].index_in_array)
            axm.remove([ax.index_in_array + 3j for ax in axes])
            axis = DataAxis(profile.shape[i0],
                            scale=length / profile.shape[i0],
                            units=axes[0].units,
                            navigate=axes[0].navigate)
            axis.axes_manager = axm
            axm._axes.insert(i0, axis)
            from hyperspy.signals import BaseSignal
            roi = BaseSignal(
                profile,
                axes=axm._get_axes_dicts(),
                metadata=signal.metadata.deepcopy().as_dictionary(),
                original_metadata=signal.original_metadata.deepcopy(
                ).as_dictionary())
            return roi
        else:
            out.data = profile
            i0 = min(axes[0].index_in_array, axes[1].index_in_array)
            ax = out.axes_manager._axes[i0]
            size = len(profile)
            scale = length / len(profile)
            axchange = size != ax.size or scale != ax.scale
            if axchange:
                ax.size = len(profile)
                ax.scale = length / len(profile)
            out.events.data_changed.trigger(out)
 def test_low_high_value(self):
     data = arange(11)
     s = BaseSignal(data)
     axes = s.axes_manager[0]
     assert axes.low_value == data[0]
     assert axes.high_value == data[-1]
Exemple #7
0
    def reconstruct_phase(self,
                          reference=None,
                          sb_size=None,
                          sb_smoothness=None,
                          sb_unit=None,
                          sb='lower',
                          sb_position=None,
                          output_shape=None,
                          plotting=False,
                          show_progressbar=False,
                          store_parameters=True,
                          parallel=None):
        """Reconstruct electron holograms. Operates on multidimensional
        hyperspy signals. There are several usage schemes:
         1. Reconstruct 1d or Nd hologram without reference
         2. Reconstruct 1d or Nd hologram using single reference hologram
         3. Reconstruct Nd hologram using Nd reference hologram (applies each
         reference to each hologram in Nd stack)

         The reconstruction parameters (sb_position, sb_size, sb_smoothness)
         have to be 1d or to have same dimensionality as the hologram.

        Parameters
        ----------
        reference : ndarray, :class:`~hyperspy.signals.Signal2D, None
            Vacuum reference hologram.
        sb_size : float, ndarray, :class:`~hyperspy.signals.BaseSignal, None
            Sideband radius of the aperture in corresponding unit (see
            'sb_unit'). If None, the radius of the aperture is set to 1/3 of
            the distance between sideband and center band.
        sb_smoothness : float, ndarray, :class:`~hyperspy.signals.BaseSignal, None
            Smoothness of the aperture in the same unit as sb_size.
        sb_unit : str, None
            Unit of the two sideband parameters 'sb_size' and 'sb_smoothness'.
            Default: None - Sideband size given in pixels
            'nm': Size and smoothness of the aperture are given in 1/nm.
            'mrad': Size and smoothness of the aperture are given in mrad.
        sb : str, None
            Select which sideband is selected. 'upper' or 'lower'.
        sb_position : tuple, :class:`~hyperspy.signals.Signal1D, None
            The sideband position (y, x), referred to the non-shifted FFT. If
            None, sideband is determined automatically from FFT.
        output_shape: tuple, None
            Choose a new output shape. Default is the shape of the input
            hologram. The output shape should not be larger than the input
            shape.
        plotting : boolean
            Shows details of the reconstruction (i.e. SB selection).
        show_progressbar : boolean
            Shows progressbar while iterating over different slices of the
            signal (passes the parameter to map method).
        parallel : bool
            Run the reconstruction in parallel
        store_parameters : boolean
            Store reconstruction parameters in metadata

        Returns
        -------
        wave : :class:`~hyperspy.signals.WaveImage
            Reconstructed electron wave. By default object wave is devided by
            reference wave

        Examples
        --------
        >>> import hyperspy.api as hs
        >>> s = hs.datasets.example_signals.object_hologram()
        >>> sb_position = s.estimate_sideband_position()
        >>> sb_size = s.estimate_sideband_size(sb_position)
        >>> sb_size.data
        >>> wave = s.reconstruct_phase(sb_position=sb_position, sb_size=sb_size)

        """

        # TODO: Use defaults for choosing sideband, smoothness, relative filter
        # size and output shape if not provided
        # TODO: Plot FFT with marked SB and SB filter if plotting is enabled

        # Parsing reference:
        if not isinstance(reference, HologramImage):
            if isinstance(reference, Signal2D):
                if (not reference.axes_manager.navigation_shape
                        == self.axes_manager.navigation_shape
                        and reference.axes_manager.navigation_size):

                    raise ValueError('The navigation dimensions of object and'
                                     'reference holograms do not match')

                _logger.warning('The reference image signal type is not '
                                'HologramImage. It will be converted to '
                                'HologramImage automatically.')
                reference.set_signal_type('hologram')
            elif reference is not None:
                reference = HologramImage(reference)
                if isinstance(reference.data, daArray):
                    reference = reference.as_lazy()

        # Testing match of navigation axes of reference and self
        # (exception: reference nav_dim=1):
        if (reference and not reference.axes_manager.navigation_shape
                == self.axes_manager.navigation_shape
                and reference.axes_manager.navigation_size):

            raise ValueError('The navigation dimensions of object and '
                             'reference holograms do not match')

        if reference and not reference.axes_manager.signal_shape == self.axes_manager.signal_shape:

            raise ValueError('The signal dimensions of object and reference'
                             ' holograms do not match')

        # Parsing sideband position:
        if sb_position is None:
            _logger.warning('Sideband position is not specified. The sideband '
                            'will be found automatically which may cause '
                            'wrong results.')
            if reference is None:
                sb_position = self.estimate_sideband_position(
                    sb=sb, parallel=parallel)
            else:
                sb_position = reference.estimate_sideband_position(
                    sb=sb, parallel=parallel)

        else:
            if isinstance(sb_position, BaseSignal) and \
               not sb_position._signal_dimension == 1:
                raise ValueError('sb_position dimension has to be 1')

            if not isinstance(sb_position, Signal1D):
                sb_position = Signal1D(sb_position)
                if isinstance(sb_position.data, daArray):
                    sb_position = sb_position.as_lazy()

            if not sb_position.axes_manager.signal_size == 2:
                raise ValueError('sb_position should to have signal size of 2')

        if sb_position.axes_manager.navigation_size != self.axes_manager.navigation_size:
            if sb_position.axes_manager.navigation_size:
                raise ValueError('Sideband position dimensions do not match'
                                 ' neither reference nor hologram dimensions.')
            # sb_position navdim=0, therefore map function should not iterate:
            else:
                sb_position_temp = sb_position.data
        else:
            sb_position_temp = sb_position.deepcopy()

        ## Parsing sideband size

        # Default value is 1/2 distance between sideband and central band
        if sb_size is None:
            if reference is None:
                sb_size = self.estimate_sideband_size(sb_position,
                                                      parallel=parallel)
            else:
                sb_size = reference.estimate_sideband_size(sb_position,
                                                           parallel=parallel)
        else:
            if not isinstance(sb_size, BaseSignal):
                if isinstance(sb_size,
                              (np.ndarray, daArray)) and sb_size.size > 1:
                    # transpose if np.array of multiple instances
                    sb_size = BaseSignal(sb_size).T
                else:
                    sb_size = BaseSignal(sb_size)
                if isinstance(sb_size.data, daArray):
                    sb_size = sb_size.as_lazy()

        if sb_size.axes_manager.navigation_size != self.axes_manager.navigation_size:
            if sb_size.axes_manager.navigation_size:
                raise ValueError('Sideband size dimensions do not match '
                                 'neither reference nor hologram dimensions.')
            # sb_position navdim=0, therefore map function should not iterate:
            else:
                sb_size_temp = np.float64(sb_size.data)
        else:
            sb_size_temp = sb_size.deepcopy()

        # Standard edge smoothness of sideband aperture 5% of sb_size
        if sb_smoothness is None:
            sb_smoothness = sb_size * 0.05
        else:
            if not isinstance(sb_smoothness, BaseSignal):
                if isinstance(
                        sb_smoothness,
                    (np.ndarray, daArray)) and sb_smoothness.size > 1:
                    sb_smoothness = BaseSignal(sb_smoothness).T
                else:
                    sb_smoothness = BaseSignal(sb_smoothness)
                if isinstance(sb_smoothness.data, daArray):
                    sb_smoothness = sb_smoothness.as_lazy()

        if sb_smoothness.axes_manager.navigation_size != self.axes_manager.navigation_size:
            if sb_smoothness.axes_manager.navigation_size:
                raise ValueError('Sideband smoothness dimensions do not match'
                                 ' neither reference nor hologram '
                                 'dimensions.')
            # sb_position navdim=0, therefore map function should not iterate it:
            else:
                sb_smoothness_temp = np.float64(sb_smoothness.data)
        else:
            sb_smoothness_temp = sb_smoothness.deepcopy()

        # Convert sideband size from 1/nm or mrad to pixels
        if sb_unit == 'nm':
            f_sampling = np.divide(
                1,
                [a * b for a, b in \
                 zip(self.axes_manager.signal_shape,
                     (self.axes_manager.signal_axes[0].scale,
                      self.axes_manager.signal_axes[1].scale))]
            )
            sb_size_temp = sb_size_temp / np.mean(f_sampling)
            sb_smoothness_temp = sb_smoothness_temp / np.mean(f_sampling)
        elif sb_unit == 'mrad':
            f_sampling = np.divide(
                1,
                [a * b for a, b in \
                 zip(self.axes_manager.signal_shape,
                     (self.axes_manager.signal_axes[0].scale,
                      self.axes_manager.signal_axes[1].scale))]
            )
            try:
                ht = self.metadata.Acquisition_instrument.TEM.beam_energy
            except:
                raise AttributeError("Please define the beam energy."
                                     "You can do this e.g. by using the "
                                     "set_microscope_parameters method")

            momentum = 2 * constants.m_e * constants.elementary_charge * ht * \
                    1000 * (1 + constants.elementary_charge * ht * \
                            1000 / (2 * constants.m_e * constants.c ** 2))
            wavelength = constants.h / np.sqrt(momentum) * 1e9  # in nm
            sb_size_temp = sb_size_temp / (1000 * wavelength *
                                           np.mean(f_sampling))
            sb_smoothness_temp = sb_smoothness_temp / (1000 * wavelength *
                                                       np.mean(f_sampling))

        # Find output shape:
        if output_shape is None:
            ##  Future improvement will give a possibility to choose
            # if sb_size.axes_manager.navigation_size > 0:
            #     output_shape = (np.int(sb_size.inav[0].data*2), np.int(sb_size.inav[0].data*2))
            # else:
            #     output_shape = (np.int(sb_size.data*2), np.int(sb_size.data*2))
            output_shape = self.axes_manager.signal_shape
            output_shape = output_shape[::-1]

        # Logging the reconstruction parameters if appropriate:
        _logger.info('Sideband position in pixels: {}'.format(sb_position))
        _logger.info('Sideband aperture radius in pixels: {}'.format(sb_size))
        _logger.info(
            'Sideband aperture smoothness in pixels: {}'.format(sb_smoothness))

        # Reconstructing object electron wave:

        # Checking if reference is a single image, which requires sideband
        # parameters as a nparray to avoid iteration trough those:
        wave_object = self.map(
            reconstruct,
            holo_sampling=(self.axes_manager.signal_axes[0].scale,
                           self.axes_manager.signal_axes[1].scale),
            sb_size=sb_size_temp,
            sb_position=sb_position_temp,
            sb_smoothness=sb_smoothness_temp,
            output_shape=output_shape,
            plotting=plotting,
            show_progressbar=show_progressbar,
            inplace=False,
            parallel=parallel,
            ragged=False)

        # Reconstructing reference wave and applying it (division):
        if reference is None:
            wave_reference = 1
        # case when reference is 1d
        elif reference.axes_manager.navigation_size != self.axes_manager.navigation_size:

            # Prepare parameters for reconstruction of the reference wave:

            if reference.axes_manager.navigation_size == 0 and \
               sb_position.axes_manager.navigation_size > 0:
                # 1d reference, but parameters are multidimensional
                sb_position_ref = _first_nav_pixel_data(sb_position_temp)
            else:
                sb_position_ref = sb_position_temp

            if reference.axes_manager.navigation_size == 0 and \
               sb_size.axes_manager.navigation_size > 0:
                # 1d reference, but parameters are multidimensional
                sb_size_ref = _first_nav_pixel_data(sb_size_temp)
            else:
                sb_size_ref = sb_size_temp

            if reference.axes_manager.navigation_size == 0 and \
               sb_smoothness.axes_manager.navigation_size > 0:
                # 1d reference, but parameters are multidimensional
                sb_smoothness_ref = np.float64(
                    _first_nav_pixel_data(sb_smoothness_temp))
            else:
                sb_smoothness_ref = sb_smoothness_temp
            #

            wave_reference = reference.map(
                reconstruct,
                holo_sampling=(self.axes_manager.signal_axes[0].scale,
                               self.axes_manager.signal_axes[1].scale),
                sb_size=sb_size_ref,
                sb_position=sb_position_ref,
                sb_smoothness=sb_smoothness_ref,
                output_shape=output_shape,
                plotting=plotting,
                show_progressbar=show_progressbar,
                inplace=False,
                parallel=parallel,
                ragged=False)

        else:
            wave_reference = reference.map(
                reconstruct,
                holo_sampling=(self.axes_manager.signal_axes[0].scale,
                               self.axes_manager.signal_axes[1].scale),
                sb_size=sb_size_temp,
                sb_position=sb_position_temp,
                sb_smoothness=sb_smoothness_temp,
                output_shape=output_shape,
                plotting=plotting,
                show_progressbar=show_progressbar,
                inplace=False,
                parallel=parallel,
                ragged=False)

        wave_image = wave_object / wave_reference

        # New signal is a complex
        wave_image.set_signal_type('complex_signal2d')

        wave_image.axes_manager.signal_axes[0].scale = \
                self.axes_manager.signal_axes[0].scale * \
                self.axes_manager.signal_shape[0] / output_shape[1]
        wave_image.axes_manager.signal_axes[1].scale = \
                self.axes_manager.signal_axes[1].scale * \
                self.axes_manager.signal_shape[1] / output_shape[0]

        # Reconstruction parameters are stored in holo_reconstruction_parameters:

        if store_parameters:
            rec_param_dict = OrderedDict([('sb_position', sb_position_temp),
                                          ('sb_size', sb_size_temp),
                                          ('sb_units', sb_unit),
                                          ('sb_smoothness', sb_smoothness_temp)
                                          ])
            wave_image.metadata.Signal.add_node('Holography')
            wave_image.metadata.Signal.Holography.add_node(
                'Reconstruction_parameters')
            wave_image.metadata.Signal.Holography.Reconstruction_parameters.add_dictionary(
                rec_param_dict)
            _logger.info('Reconstruction parameters stored in metadata')

        return wave_image
Exemple #8
0
def test_apodization(lazy, window_type, inplace):
    SIZE_NAV0 = 2
    SIZE_NAV1 = 3
    SIZE_NAV2 = 4
    SIZE_SIG0 = 50
    SIZE_SIG1 = 60
    SIZE_SIG2 = 70

    ax_dict0 = {'size': SIZE_NAV0, 'navigate': True}
    ax_dict1 = {'size': SIZE_SIG0, 'navigate': False}
    ax_dict2 = {'size': SIZE_SIG1, 'navigate': False}
    ax_dict3 = {'size': SIZE_SIG2, 'navigate': False}

    # 1. Test apodization for signal 1D, 2D, 3D:
    data = np.random.rand(SIZE_NAV0 * SIZE_NAV1 * SIZE_SIG0 *
                          SIZE_NAV2).reshape(
                              (SIZE_NAV0, SIZE_NAV1, SIZE_NAV2, SIZE_SIG0))
    data2 = np.random.rand(SIZE_NAV0 * SIZE_NAV1 * SIZE_SIG0 *
                           SIZE_SIG1).reshape(
                               (SIZE_NAV0, SIZE_NAV1, SIZE_SIG0, SIZE_SIG1))
    data3 = np.random.rand(SIZE_NAV0 * SIZE_SIG2 * SIZE_SIG0 *
                           SIZE_SIG1).reshape(
                               (SIZE_NAV0, SIZE_SIG0, SIZE_SIG1, SIZE_SIG2))
    signal1d = Signal1D(data)
    signal2d = Signal2D(data2)
    signal3d = BaseSignal(data3, axes=[ax_dict0, ax_dict1, ax_dict2, ax_dict3])
    if lazy:
        signal1d = signal1d.as_lazy()
        signal2d = signal2d.as_lazy()
        signal3d = signal3d.as_lazy()
    if window_type == 'hann':
        window = np.hanning(SIZE_SIG0)
        window1 = np.hanning(SIZE_SIG1)
        window2 = np.hanning(SIZE_SIG2)
        window2d = np.outer(window, window1)
        window3d = outer_nd(window, window1, window2)

        if inplace:
            signal1d_a = signal1d.deepcopy()
            signal1d_a.apply_apodization(window=window_type, inplace=inplace)
        else:
            signal1d_a = signal1d.apply_apodization(window=window_type)
        data_a = data * window[np.newaxis, np.newaxis, np.newaxis, :]

        if inplace:
            signal2d_a = signal2d.deepcopy()
            signal2d_a.apply_apodization(window=window_type, inplace=inplace)
        else:
            signal2d_a = signal2d.apply_apodization(window=window_type,
                                                    inplace=inplace)
        data2_a = data2 * window2d[np.newaxis, np.newaxis, :, :]

        if inplace:
            signal3d_a = signal3d.deepcopy()
            signal3d_a.apply_apodization(window=window_type, inplace=inplace)
        else:
            signal3d_a = signal3d.apply_apodization(window=window_type,
                                                    inplace=inplace)
        data3_a = data3 * window3d[np.newaxis, :, :, :]

        assert np.allclose(signal1d_a.data, data_a)
        assert np.allclose(signal2d_a.data, data2_a)
        assert np.allclose(signal3d_a.data, data3_a)

        for hann_order in 9 * (np.random.rand(5)) + 1:
            window = hann_window_nth_order(SIZE_SIG0, order=int(hann_order))
            signal1d_a = signal1d.apply_apodization(window=window_type,
                                                    hann_order=int(hann_order))
            data_a = data * window[np.newaxis, np.newaxis, np.newaxis, :]
            assert np.allclose(signal1d_a.data, data_a)
    elif window_type == 'hamming':
        window = np.hamming(SIZE_SIG0)
        signal1d_a = signal1d.apply_apodization(window=window_type)
        data_a = data * window[np.newaxis, np.newaxis, np.newaxis, :]
        assert np.allclose(signal1d_a.data, data_a)
    elif window_type == 'tukey':
        for tukey_alpha in np.random.rand(5):
            window = tukey(SIZE_SIG0, alpha=tukey_alpha)
            signal1d_a = signal1d.apply_apodization(window=window_type,
                                                    tukey_alpha=tukey_alpha)
            data_a = data * window[np.newaxis, np.newaxis, np.newaxis, :]
            assert np.allclose(signal1d_a.data, data_a)

    # 2. Test raises:
    with pytest.raises(ValueError):
        signal1d.apply_apodization(window='hamm')
 def setup_method(self, method):
     self.s = BaseSignal(np.random.random((2, 3, 4)))  # (|4, 3, 2)
 def setup_method(self, method):
     self.s = BaseSignal(np.arange(2))
Exemple #11
0
def test_stack_warning():
    with pytest.warns(VisibleDeprecationWarning, match="deprecated"):
        _ = utils.stack([BaseSignal([1]), BaseSignal([2])], mmap=True)
def test_nul_signal():
    s = BaseSignal(np.random.random())
    with pytest.raises(AttributeError):
        s.T.fft()
    with pytest.raises(AttributeError):
        s.T.ifft()
Exemple #13
0
 def setup_method(self, method):
     self.s = BaseSignal([0.1, 0.2, 0.3])
Exemple #14
0
 def setUp(self):
     self.s = BaseSignal(np.arange(2))
Exemple #15
0
 def setUp(self):
     self.s = BaseSignal(np.random.random((2, 3, 4)))  # (|4, 3, 2)
Exemple #16
0
    def _check_adapt_map_input(self, ins, varname=''):
        '''
        Check and adapt an input parameter that will work with the map function
        for this signal. An adapted signal is returned in case it works. If it
        does not work, a meaningful ValueError is returned instead.

        Parameters
        ----------
        ins : {number, ndarray, hs.signals.BaseSignal}
         One of the following:
         -  Single number.
         -  Numpy array with the same number of elements as this signal
         navigation shape.
         -  HyperSpy signal with same navigation shape + null signal dimension,
         or same signal shape as navigation shape + null navigation dimension,
         or same len as this signal, or containing just a single value.

        Returns
        -------
        aus : hs.signals.BaseSignal
         If a single value was provided, this is a signal with navigation shape
         equal to 1. Any other compatible case is transformed into a signal with
         same navigation shape as this signal, and null navigation dimension. In
         case no possible transformation is possible, a ValueError type results.
        '''
        aus = None
        navs = self.axes_manager.navigation_shape

        if isinstance(ins, Number):
            aus = BaseSignal(ins).T
            return aus

        elif isinstance(ins, np.ndarray):
            if ins.shape == navs:
                aus = BaseSignal(ins.T).T
            elif ins.shape == navs[::-1]:
                aus = BaseSignal(ins).T
            elif len(ins) == len(self):
                aus = BaseSignal(ins.reshape(navs[::-1])).T
            else:
                aus = ValueError('Input array not recognized, ')
            return aus

        elif isinstance(ins, BaseSignal):
            if ins.axes_manager.navigation_shape == navs and (
               ins.axes_manager.signal_dimension == 0):
                aus = ins
            elif ins.axes_manager.signal_shape == navs and (
                 ins.axes_manager.navigation_dimension == 0):
                aus = ins.T
            elif ins.data.shape == (1,):
                aus = BaseSignal(ins.data).T
            elif len(ins) == len(self) and (
                 ins.axes_manager.signal_dimension == 0):
                aus = BaseSignal(ins.data.reshape(navs[::-1])).T
            else:
                aus = ValueError('Input signal not recognized, ')
            return aus

        if aus is None:
            return aus
        else:
            aus = ValueError('Input '+type(ins)+' not recognized, ')
            return aus