o
    :ήc                     @   s>   d dl ZddlmZmZ dd Zdd Zdd	 ZdddZdS )    N   )_supported_float_type_to_np_modec                 C   s,   | D ]}|d dkrd|  }t |qdS )zEnsure all sizes in ``axis_sizes`` are odd.

    Parameters
    ----------
    axis_sizes : iterable of int

    Raises
    ------
    ValueError
        If any given axis size is even.
    r   r   zbWindow size for `threshold_sauvola` or `threshold_niblack` must not be even on any dimension. Got N)
ValueError)
axis_sizes	axis_sizemsg r	   >/tmp/pip-target-vg8gfxp4/lib/python/skimage/filters/_sparse.py_validate_window_size   s   r   c                 C   s8   t dd t||| jD }| | }|dkr|S || S )zGet a view into `padded` that is offset by `idx` and scaled by `val`.

    If `padded` was created by padding the original image by `kernel_shape` as
    in correlate_sparse, then the view created here will match the size of the
    original image.
    c                 S   s(   g | ]\}}}t |||d  |  qS )   )slice).0cw_sr	   r	   r
   
<listcomp>!   s    z_get_view.<locals>.<listcomp>r   )tuplezipshape)paddedkernel_shapeidxvalsl_shiftvr	   r	   r
   	_get_view   s   r   c                 C   sx   |d |d }}t |d| j krtdt| ||| }t|dd |dd D ]\}}|t| |||7 }q,|S )a  Perform correlation with a sparse kernel.

    Parameters
    ----------
    image : ndarray
        The (prepadded) image to be correlated.
    kernel_shape : tuple of int
        The shape of the sparse filter kernel.
    kernel_indices : list of coordinate tuples
        The indices of each non-zero kernel entry.
    kernel_values : list of float
        The kernel values at each location in kernel_indices.

    Returns
    -------
    out : ndarray
        The filtered image.

    Notes
    -----
    This function only returns results for the 'valid' region of the
    convolution, and thus `out` will be smaller than `image` by an amount
    equal to the kernel size along each axis.
    r   r   z*Unexpected initial index in kernel_indicesr   N)r   ndimRuntimeErrorr   copyr   )imager   kernel_indiceskernel_valuesr   r   outr	   r	   r
   _correlate_sparse)   s   "r%   reflectc           	      C   s   t |}t| j}| j|dd} |dkr| }nt|}t|j t j| dd |jD |d}t 	|}t
|| j|dd}t
t| }d|j }||vrX|g| }dg| }t||j||S )	a  Compute valid cross-correlation of `padded_array` and `kernel`.

    This function is *fast* when `kernel` is large with many zeros.

    See ``scipy.ndimage.correlate`` for a description of cross-correlation.

    Parameters
    ----------
    image : ndarray, dtype float, shape (M, N,[ ...,] P)
        The input array. If mode is 'valid', this array should already be
        padded, as a margin of the same shape as kernel will be stripped
        off.
    kernel : ndarray, dtype float shape (Q, R,[ ...,] S)
        The kernel to be correlated. Must have the same number of
        dimensions as `padded_array`. For high performance, it should
        be sparse (few nonzero entries).
    mode : string, optional
        See `scipy.ndimage.correlate` for valid modes.
        Additionally, mode 'valid' is accepted, in which case no padding is
        applied and the result is the result for the smaller image for which
        the kernel is entirely inside the original data.

    Returns
    -------
    result : array of float, shape (M, N,[ ...,] P)
        The result of cross-correlating `image` with `kernel`. If mode
        'valid' is used, the resulting shape is (M-Q+1, N-R+1,[ ...,] P-S+1).
    F)r    validc                 S   s   g | ]
}|d  |d  fqS )r   r	   )r   wr	   r	   r
   r   v   s    z$correlate_sparse.<locals>.<listcomp>)moder   g        )npasarrayr   dtypeastyper   r   r   padnonzerolistr   r   r%   )	r!   kernelr)   float_dtypepadded_imagenp_modeindicesvaluescorner_indexr	   r	   r
   correlate_sparseM   s(   






r8   )r&   )	numpyr*   _shared.utilsr   r   r   r   r%   r8   r	   r	   r	   r
   <module>   s    $