a
    ߙfb^/                     @   s   d Z ddlZddlZddlZddlmZ ddlmZm	Z	m
Z
 dZg dZG dd	 d	ZG d
d deZG dd deZdd ZdS )al  
This module contains the convolution and filter functionalities of astropy.

A few conceptual notes:
A filter kernel is mainly characterized by its response function. In the 1D
case we speak of "impulse response function", in the 2D case we call it "point
spread function". This response function is given for every kernel by an
astropy `FittableModel`, which is evaluated on a grid to obtain a filter array,
which can then be applied to binned data.

The model is centered on the array and should have an amplitude such that the array
integrates to one per default.

Currently only symmetric 2D kernels are supported.
    N)AstropyUserWarning   )discretize_modeladd_kernel_arrays_1Dadd_kernel_arrays_2Dd   )KernelKernel1DKernel2Dkernel_arithmeticsc                   @   s   e Zd ZdZdZdZdZdd Zedd Z	ed	d
 Z
edd Zedd Zedd Zd&ddZedd Zedd Zedd Zdd Zdd Zdd Zd d! Zd"d# Zd'd$d%ZdS )(r   zq
    Convolution kernel base class.

    Parameters
    ----------
    array : ndarray
        Kernel array.
    FTNc                 C   s   t || _d S )N)npZ
asanyarray_array)selfarray r   7lib/python3.9/site-packages/astropy/convolution/core.py__init__,   s    zKernel.__init__c                 C   s   | j S )z:
        Deviation from the normalization to one.
        )Z_truncationr   r   r   r   
truncation/   s    zKernel.truncationc                 C   s   | j S )z
        Indicates if kernel is bool.

        If the kernel is bool the multiplication in the convolution could
        be omitted, to increase the performance.
        )_is_boolr   r   r   r   is_bool6   s    zKernel.is_boolc                 C   s   | j S )z(
        Kernel response model.
        )_modelr   r   r   r   model@   s    zKernel.modelc                 C   s   | j jS )z#
        Kernel dimension.
        )r   ndimr   r   r   r   	dimensionG   s    zKernel.dimensionc                 C   s   dd | j jD S )z-
        Index of the kernel center.
        c                 S   s   g | ]}|d  qS )   r   ).0Z	axes_sizer   r   r   
<listcomp>S       z!Kernel.center.<locals>.<listcomp>r   shaper   r   r   r   centerN   s    zKernel.centerintegralc                 C   sh   |dkr| j  }n|dkr(| j  }ntd|dkrFtdt nt| j || j  | j  | _	dS )ag  
        Normalize the filter kernel.

        Parameters
        ----------
        mode : {'integral', 'peak'}
            One of the following modes:
                * 'integral' (default)
                    Kernel is normalized such that its integral = 1.
                * 'peak'
                    Kernel is normalized such that its peak = 1.
        r"   Zpeakz*invalid mode, must be 'integral' or 'peak'r   z8The kernel cannot be normalized because it sums to zero.N)
r   summax
ValueErrorwarningswarnr   r   ZdivideZ_kernel_sum)r   modeZnormalizationr   r   r   	normalizeU   s    zKernel.normalizec                 C   s   | j jS )z,
        Shape of the kernel array.
        r   r   r   r   r   r    s   s    zKernel.shapec                 C   s   | j S )a  
        Indicates if the filter kernel is separable.

        A 2D filter is separable, when its filter array can be written as the
        outer product of two 1D arrays.

        If a filter kernel is separable, higher dimension convolutions will be
        performed by applying the 1D filter array consecutively on every dimension.
        This is significantly faster, than using a filter array with the same
        dimension.
        )
_separabler   r   r   r   	separablez   s    zKernel.separablec                 C   s   | j S )z&
        Filter kernel array.
        r   r   r   r   r   r      s    zKernel.arrayc                 C   s   t | |dS )z)
        Add two filter kernels.
        addr   r   kernelr   r   r   __add__   s    zKernel.__add__c                 C   s   t | |dS )z.
        Subtract two filter kernels.
        subr.   r/   r   r   r   __sub__   s    zKernel.__sub__c                 C   s   t | |dS zF
        Multiply kernel with number or convolve two kernels.
        mulr.   r   valuer   r   r   __mul__   s    zKernel.__mul__c                 C   s   t | |dS r4   r.   r6   r   r   r   __rmul__   s    zKernel.__rmul__c                 C   s   | j S )z5
        Array representation of the kernel.
        r,   r   r   r   r   	__array__   s    zKernel.__array__c                 C   s   t |d tjkrtS |S dS )z?
        Wrapper for multiplication with numpy arrays.
        r   N)typer   ZufuncNotImplemented)r   r   contextr   r   r   __array_wrap__   s    zKernel.__array_wrap__)r"   )N)__name__
__module____qualname____doc__r*   r   r   r   propertyr   r   r   r   r!   r)   r    r+   r   r1   r3   r8   r9   r:   r>   r   r   r   r   r      s8   

	






r   c                       s"   e Zd ZdZd fdd	Z  ZS )r	   u4  
    Base class for 1D filter kernels.

    Parameters
    ----------
    model : `~astropy.modeling.FittableModel`
        Model to be evaluated.
    x_size : int or None, optional
        Size of the kernel array. Default = ⌊8*width+1⌋.
        Only used if ``array`` is None.
    array : ndarray or None, optional
        Kernel array.
    width : number
        Width of the filter kernel.
    mode : str, optional
        One of the following discretization modes:
            * 'center' (default)
                Discretize model by taking the value
                at the center of the bin.
            * 'linear_interp'
                Discretize model by linearly interpolating
                between the values at the corners of the bin.
            * 'oversample'
                Discretize model by taking the average
                on an oversampled grid.
            * 'integrate'
                Discretize model by integrating the
                model over the bin.
    factor : number, optional
        Factor of oversampling. Default factor = 10.
    Nc                    s   | j r|d urtd|d u r&| j}n|t|kr:td|d dkrjt| d d t|d d f}n&t|d  d t|d d d f}t| j |fi |}n|d u rtdt | d S )N-Array argument not allowed for kernel models.x_size should be an integerr   r         ?r   #Must specify either array or model.r   	TypeErrorZ_default_sizeintr   superr   )r   r   x_sizer   kwargsx_range	__class__r   r   r      s    $&zKernel1D.__init__)NNNr?   r@   rA   rB   r   __classcell__r   r   rO   r   r	      s    r	   c                       s"   e Zd ZdZd fdd	Z  ZS )r
   u  
    Base class for 2D filter kernels.

    Parameters
    ----------
    model : `~astropy.modeling.FittableModel`
        Model to be evaluated.
    x_size : int, optional
        Size in x direction of the kernel array. Default = ⌊8*width + 1⌋.
        Only used if ``array`` is None.
    y_size : int, optional
        Size in y direction of the kernel array. Default = ⌊8*width + 1⌋.
        Only used if ``array`` is None,
    array : ndarray or None, optional
        Kernel array. Default is None.
    mode : str, optional
        One of the following discretization modes:
            * 'center' (default)
                Discretize model by taking the value
                at the center of the bin.
            * 'linear_interp'
                Discretize model by performing a bilinear interpolation
                between the values at the corners of the bin.
            * 'oversample'
                Discretize model by taking the average
                on an oversampled grid.
            * 'integrate'
                Discretize model by integrating the
                model over the bin.
    width : number
        Width of the filter kernel.
    factor : number, optional
        Factor of oversampling. Default factor = 10.
    Nc                    sD  | j r"|d urtd|d u r(| j}n|t|kr<td|d u rJ|}n|t|kr^td|d dkrt| d d t|d d f}n&t|d  d t|d d d f}|d dkrt| d d t|d d f}n&t|d  d t|d d d f}t| j ||fi |}n|d u r4tdt | d S )	NrD   rE   zy_size should be an integerr   r   rF   r   rG   rH   )r   r   rL   Zy_sizer   rM   rN   Zy_rangerO   r   r   r     s*    $&$&
zKernel2D.__init__)NNNNrQ   r   r   rO   r   r
      s   #r
   c                 C   sR  t | trzt |trz|dkr*t| j|j}|dkrBt| j|j }|dkrRtdt|d}| jof|j|_| jpt|j|_nt | trt |tr|dkrt| j|j}|dkrt| j|j }|dkrtdt|d}| jo|j|_| jp|j|_nZt | tst | trFt	
|rF|dkr<t| }| j|9  _ntdntd|S )a  
    Add, subtract or multiply two kernels.

    Parameters
    ----------
    kernel : `astropy.convolution.Kernel`
        Kernel instance.
    value : `astropy.convolution.Kernel`, float, or int
        Value to operate with.
    operation : {'add', 'sub', 'mul'}
        One of the following operations:
            * 'add'
                Add two kernels
            * 'sub'
                Subtract two kernels
            * 'mul'
                Multiply kernel with number or convolve two kernels.
    r-   r2   r5   zYKernel operation not supported. Maybe you want to use convolve(kernel1, kernel2) instead.)r   zKernel operation not supported.)
isinstancer	   r   r   	Exceptionr*   r   r
   r   r   Zisscalarcopyr   )r0   r7   Z	operationZ	new_arrayZ
new_kernelr   r   r   r   A  s:    




r   )rB   r&   rU   Znumpyr   Zastropy.utils.exceptionsr   Zutilsr   r   r   ZMAX_NORMALIZATION__all__r   r	   r
   r   r   r   r   r   <module>   s    >K