Пример #1
0
def filter_convolve(data, filters, filter_rot=False, method='scipy'):
    r"""Filter convolve

    This method convolves the input image with the wavelet filters.

    Parameters
    ----------
    data : numpy.ndarray
        Input data, 2D array
    filters : numpy.ndarray
        Wavelet filters, 3D array
    filter_rot : bool, optional
        Option to rotate wavelet filters (default is `False`)
    method : {'astropy', 'scipy'}, optional
        Convolution method (default is 'scipy')

    Returns
    -------
    numpy.ndarray
        Convolved data

    Examples
    --------
    >>> from modopt.signal.wavelet import filter_convolve
    >>> x = np.arange(9).reshape(3, 3).astype(float)
    >>> y = np.arange(36).reshape(4, 3, 3).astype(float)
    >>> filter_convolve(x, y)
    array([[[  174.,   165.,   174.],
            [   93.,    84.,    93.],
            [  174.,   165.,   174.]],
    <BLANKLINE>
           [[  498.,   489.,   498.],
            [  417.,   408.,   417.],
            [  498.,   489.,   498.]],
    <BLANKLINE>
           [[  822.,   813.,   822.],
            [  741.,   732.,   741.],
            [  822.,   813.,   822.]],
    <BLANKLINE>
           [[ 1146.,  1137.,  1146.],
            [ 1065.,  1056.,  1065.],
            [ 1146.,  1137.,  1146.]]])

    >>> filter_convolve(y, y, filter_rot=True)
    array([[ 14550.,  14586.,  14550.],
           [ 14874.,  14910.,  14874.],
           [ 14550.,  14586.,  14550.]])

    """

    if filter_rot:
        return np.sum([
            convolve(coef, f, method=method)
            for coef, f in zip(data, rotate_stack(filters))
        ],
                      axis=0)

    else:
        return np.array([convolve(data, f, method=method) for f in filters])
Пример #2
0
def convolve_stack(data, kernel, rot_kernel=False, method='scipy'):
    r"""Convolve stack of data with stack of kernels

    This method convolves the input data with a given kernel using FFT and
    is the default convolution used for all routines

    Parameters
    ----------
    data : np.ndarray
        Input data array, normally a 2D image
    kernel : np.ndarray
        Input kernel array, normally a 2D kernel
    rot_kernel : bool
        Option to rotate kernels by 180 degrees
    method : str {'astropy', 'scipy'}, optional
        Convolution method (default is 'scipy')

    Returns
    -------
    np.ndarray convolved data

    Examples
    --------
    >>> from math.convolve import convolve
    >>> import numpy as np
    >>> a = np.arange(18).reshape(2, 3, 3)
    >>> b = a + 10
    >>> convolve_stack(a, b)
    array([[[  534.,   525.,   534.],
            [  453.,   444.,   453.],
            [  534.,   525.,   534.]],
    <BLANKLINE>
           [[ 2721.,  2712.,  2721.],
            [ 2640.,  2631.,  2640.],
            [ 2721.,  2712.,  2721.]]])

    >>> convolve_stack(a, b, rot_kernel=True)
    array([[[  474.,   483.,   474.],
        [  555.,   564.,   555.],
        [  474.,   483.,   474.]],
    <BLANKLINE>
       [[ 2661.,  2670.,  2661.],
        [ 2742.,  2751.,  2742.],
        [ 2661.,  2670.,  2661.]]])

    See Also
    --------
    convolve : The convolution function called by convolve_stack

    """

    if rot_kernel:
        kernel = rotate_stack(kernel)

    return np.array([
        convolve(data_i, kernel_i, method=method)
        for data_i, kernel_i in zip(data, kernel)
    ])
Пример #3
0
 def test_rotate_stack(self):
     """Test rotate_stack."""
     npt.assert_array_equal(
         np_adjust.rotate_stack(self.data2),
         np.array([
             [[8, 7, 6], [5, 4, 3], [2, 1, 0]],
             [[17, 16, 15], [14, 13, 12], [11, 10, 9]],
         ]),
         err_msg='Incorrect stack rotation',
     )
Пример #4
0
def psf_convolve(data, psf, psf_rot=False, psf_type='fixed', method='scipy'):
    """Convolve data with PSF

    This method convolves an image with a PSF

    Parameters
    ----------
    data : np.ndarray
        Input data array, normally an array of 2D images
    psf : np.ndarray
        Input PSF array, normally either a single 2D PSF or an array of 2D
        PSFs
    psf_rot: bool
        Option to rotate PSF by 180 degrees
    psf_type : str {'fixed', 'obj_var'}, optional
        PSF type (default is 'fixed')
    method : str {'astropy', 'scipy'}, optional
        Convolution method (default is 'astropy')

        'fixed':
            The PSF is fixed, i.e. it is the same for each image

        'obj_var':
            The PSF is object variant, i.e. it is different for each image

    Returns
    -------
    np.ndarray convolved data

    Raises
    ------
    ValueError
        If `psf_type` is not 'fixed' or 'obj_var'

    """

    if psf_type not in ('fixed', 'obj_var'):
        raise ValueError('Invalid PSF type. Options are "fixed" or "obj_var"')

    if psf_rot and psf_type == 'fixed':
        psf = rotate(psf)

    elif psf_rot:
        psf = rotate_stack(psf)

    if psf_type == 'fixed':
        return np.array([convolve(data_i, psf, method=method) for data_i in
                        data])

    elif psf_type == 'obj_var':

        return convolve_stack(data, psf, method=method)
Пример #5
0
def psf_convolve(data, psf, psf_rot=False, psf_type='fixed', method='scipy'):
    """Convolve data with PSF

    This method convolves an image with a PSF

    Parameters
    ----------
    data : np.ndarray
        Input data array, normally an array of 2D images
    psf : np.ndarray
        Input PSF array, normally either a single 2D PSF or an array of 2D
        PSFs
    psf_rot: bool
        Option to rotate PSF by 180 degrees
    psf_type : str {'fixed', 'obj_var'}, optional
        PSF type (default is 'fixed')
    method : str {'astropy', 'scipy'}, optional
        Convolution method (default is 'astropy')

        'fixed':
            The PSF is fixed, i.e. it is the same for each image

        'obj_var':
            The PSF is object variant, i.e. it is different for each image

    Returns
    -------
    np.ndarray convolved data

    Raises
    ------
    ValueError
        If `psf_type` is not 'fixed' or 'obj_var'

    """

    if psf_type not in ('fixed', 'obj_var'):
        raise ValueError('Invalid PSF type. Options are "fixed" or "obj_var"')

    if psf_rot and psf_type == 'fixed':
        psf = rotate(psf)

    elif psf_rot:
        psf = rotate_stack(psf)

    if psf_type == 'fixed':
        return np.array(
            [convolve(data_i, psf, method=method) for data_i in data])

    elif psf_type == 'obj_var':

        return convolve_stack(data, psf, method=method)