Beispiel #1
0
    def align_with_array_1D(self,
                            shift_array,
                            axis=-1,
                            interpolation_method='linear'):
        """Shift each one dimensional object by the amount specify by a 
        given array

        Parameters
        ----------
        shift_map : numpy array
            The shift is specify in the units of the selected axis
        interpolation_method : str or int
            Specifies the kind of interpolation as a string ('linear',
            'nearest', 'zero', 'slinear', 'quadratic, 'cubic') or as an 
            integer specifying the order of the spline interpolator to 
            use.
            
        """

        axis = self.axes_manager._get_positive_index(axis)
        coord = self.axes_manager.axes[axis]
        offset = coord.offset
        _axis = coord.axis.copy()
        maxval = np.cumprod(shift_array.shape)[-1] - 1
        pbar = progressbar.progressbar(maxval = maxval)
        i = 0
        for dat, shift in zip(self.iterate_axis(axis),
                              utils.iterate_axis(shift_array, axis)):
                si = sp.interpolate.interp1d(_axis ,dat,
                                             bounds_error=False,
                                             fill_value=0.,
                                             kind=interpolation_method)
                coord.offset = offset - shift[0]
                dat[:] = si(coord.axis)
                pbar.update(i)
                i += 1
        coord.offset = offset

        # Cropping time
        mini, maxi = shift_array.min(), shift_array.max()
        if mini < 0:
            self.crop_in_units(axis, None, coord.axis[-1] + mini +
             coord.scale)
        if maxi > 0:
            self.crop_in_units(axis, offset + maxi)
Beispiel #2
0
    def align_with_array_1D(self,
                            shift_array,
                            axis=-1,
                            interpolation_method='linear'):
        """Shift each one dimensional object by the amount specify by a given
        array

        Parameters
        ----------
        shift_map : numpy array
            The shift is specify in the units of the selected axis
        interpolation_method : str or int
            Specifies the kind of interpolation as a string ('linear',
            'nearest', 'zero', 'slinear', 'quadratic, 'cubic') or as an integer
            specifying the order of the spline interpolator to use.
        """

        axis = self._get_positive_axis_index_index(axis)
        coord = self.axes_manager.axes[axis]
        offset = coord.offset
        _axis = coord.axis.copy()
        maxval = np.cumprod(shift_array.shape)[-1] - 1
        pbar = progressbar.progressbar(maxval=maxval)
        i = 0
        for dat, shift in zip(self.iterate_axis(axis),
                              utils.iterate_axis(shift_array, axis)):
            si = sp.interpolate.interp1d(_axis,
                                         dat,
                                         bounds_error=False,
                                         fill_value=0.,
                                         kind=interpolation_method)
            coord.offset = offset - shift[0]
            dat[:] = si(coord.axis)
            pbar.update(i)
            i += 1
        coord.offset = offset

        # Cropping time
        mini, maxi = shift_array.min(), shift_array.max()
        if mini < 0:
            self.crop_in_units(axis, offset - mini)
        if maxi > 0:
            self.crop_in_units(axis, None, _axis[-1] - maxi)
Beispiel #3
0
    def estimate_shift_in_index_1D(self,
                                   irange=(None, None),
                                   axis=-1,
                                   reference_indexes=None,
                                   max_shift=None,
                                   interpolate=True,
                                   number_of_interpolation_points=5):
        """Estimate the shifts in a given axis using cross-correlation

        This method can only estimate the shift by comparing unidimensional
        features that should not change the position in the given axis. To
        decrease the memory usage, the time of computation and the accuracy of
        the results it is convenient to select the feature of interest setting
        the irange keyword.

        By default interpolation is used to obtain subpixel precision.

        Parameters
        ----------
        axis : int
            The axis in which the analysis will be performed.
        irange : tuple of ints (i1, i2)
             Define the range of the feature of interest. If i1 or i2 are None,
             the range will not be limited in that side.
        reference_indexes : tuple of ints or None
            Defines the coordinates of the spectrum that will be used as a
            reference. If None the spectrum of 0 coordinates will be used.
        max_shift : int

        interpolate : bool

        number_of_interpolation_points : int
            Number of interpolation points. Warning: making this number too big
            can saturate the memory

        Return
        ------
        An array with the result of the estimation
        """

        ip = number_of_interpolation_points + 1
        axis = self.axes_manager.axes[axis]
        if reference_indexes is None:
            reference_indexes = [
                0,
            ] * (len(self.axes_manager.axes) - 1)
        else:
            reference_indexes = list(reference_indexes)
        reference_indexes.insert(axis.index_in_array, slice(None))
        i1, i2 = irange
        array_shape = [axis.size for axis in self.axes_manager.axes]
        array_shape[axis.index_in_array] = 1
        shift_array = np.zeros(array_shape)
        ref = self.data[reference_indexes][i1:i2]
        if interpolate is True:
            ref = utils.interpolate_1D(ip, ref)
        for dat, shift in zip(
                self.iterate_axis(axis.index_in_array),
                utils.iterate_axis(shift_array, axis.index_in_array)):
            dat = dat[i1:i2]
            if interpolate is True:
                dat = utils.interpolate_1D(ip, dat)
            shift[:] = np.argmax(np.correlate(ref, dat, 'full')) - len(ref) + 1

        if max_shift is not None:
            if interpolate is True:
                max_shift *= ip
            shift_array.clip(a_min=-max_shift, a_max=max_shift)
        if interpolate is True:
            shift_array /= ip
        shift_array *= axis.scale
        return shift_array
Beispiel #4
0
    def estimate_shift_in_index_1D(self,
                                   irange=(None,None),
                                   axis=-1,
                                   reference_indices=None,
                                   max_shift=None,
                                   interpolate=True,
                                   number_of_interpolation_points=5):
        """Estimate the shifts in a given axis using cross-correlation

        This method can only estimate the shift by comparing 
        unidimensional features that should not change the position in 
        the given axis. To decrease the memory usage, the time of 
        computation and the accuracy of the results it is convenient to 
        select the feature of interest setting
        the irange keyword.

        By default interpolation is used to obtain subpixel precision.

        Parameters
        ----------
        axis : int
            The axis in which the analysis will be performed.
        irange : tuple of ints (i1, i2)
             Define the range of the feature of interest. If i1 or i2 
             are None,
             the range will not be limited in that side.
        reference_indices : tuple of ints or None
            Defines the coordinates of the spectrum that will be used 
            as a
            reference. If None the spectrum of 0 coordinates will be 
            used.
        max_shift : int

        interpolate : bool

        number_of_interpolation_points : int
            Number of interpolation points. Warning: making this number 
            too big
            can saturate the memory

        Return
        ------
        An array with the result of the estimation.
        
        """

        ip = number_of_interpolation_points + 1
        axis = self.axes_manager.axes[axis]
        if reference_indices is None:
            reference_indices = [0,] * (len(self.axes_manager.axes) - 1)
        else:
            reference_indices = list(reference_indices)
        reference_indices.insert(axis.index_in_array, slice(None))
        i1, i2 = irange
        array_shape = [axis.size for axis in self.axes_manager.axes]
        array_shape[axis.index_in_array] = 1
        shift_array = np.zeros(array_shape)
        ref = self.data[reference_indices][i1:i2]
        if interpolate is True:
            ref = utils.interpolate_1D(ip, ref)
        maxval = self.axes_manager.navigation_size
        pbar = progressbar.progressbar(maxval=maxval)
        i = 0
        for dat, shift in zip(self.iterate_axis(axis.index_in_array),
                              utils.iterate_axis(shift_array,
                                                 axis.index_in_array)):
            dat = dat[i1:i2]
            if interpolate is True:
                dat = utils.interpolate_1D(ip, dat)
            shift[:] = np.argmax(
                np.correlate(ref, dat,'full')) - len(ref) + 1
            i+=1
            pbar.update(i)
        pbar.finish()

        if max_shift is not None:
            if interpolate is True:
                max_shift *= ip
            shift_array.clip(a_min = -max_shift, a_max = max_shift)
        if interpolate is True:
            shift_array /= ip
        shift_array *= axis.scale
        return shift_array