a
    tDf                     @  s  d dl mZ d dlZd dlZd dlmZ d dlZzd dlZd dl	ZW n e
yZ   dZY n0 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ d	Zd
Zdd Zdd Zdd Zdd Zdd ZeeeeeedZd'ddZeej j Z eej j!Z!eej j"Z"eej j#Z#eej j$Z$eej j%Z%eej j&Z&eej j'Z'eej j(Z(eej j)Z)eej j*Z*eej j+Z+eej j,Z,eej j-Z-dd Z.eej d(ddZ/eej d)ddZ0d*d!d"Z1eej d+d#d$Z2eej d,d%d&Z3dS )-    )annotationsN)Sequence)asarray)concatenate)arange)NUMPY_GE_200)derived_fromskip_doctestzDask array only supports taking an FFT along an axis that 
has a single chunk. An FFT operation was tried on axis %s 
which has chunks %s. To change the array's chunks use dask.Array.rechunk.z
    Wrapping of %s

    The axis along which the FFT is applied must have only one chunk. To change
    the array's chunking use dask.Array.rechunk.

    The %s docstring follows below:

    c                 C  s<   |du r| j S t| j }t|D ]\}}|| f||< q |S )z*For computing the output chunks of [i]fft*N)chunkslist	enumerateasaxesr
   iaxis r   [/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/dask/array/fft.py_fft_out_chunks'   s    
r   c                   sd   |du r fdd|D }t |}|d d d |d< t  j}t|D ]\}}|| f||< qH|S )z(For computing the output chunks of rfft*Nc                   s   g | ]} j | d  qS r   r
   .0r   r   r   r   
<listcomp>4       z$_rfft_out_chunks.<locals>.<listcomp>      r   r
   r   r   r   r   r   _rfft_out_chunks1   s    
r!   c                   s\   |du r. fdd|D }d|d d  |d< t  j}t|D ]\}}|| f||< q@|S )z)For computing the output chunks of irfft*Nc                   s   g | ]} j | d  qS r   r   r   r   r   r   r   @   r   z%_irfft_out_chunks.<locals>.<listcomp>r   r   r   r    r   r   r   r   _irfft_out_chunks=   s    
r"   c                 C  sX   t |dksJ |d }|d u r8d| j| d d  g}|d }t| j}|f||< |S Nr   r   r   lenr
   r   )r   r   r   r   nr
   r   r   r   _hfft_out_chunksH   s    

r'   c                 C  s   t |dksJ |d }|d u r2| j| d g}nt |dksBJ |d }t| j}|d dkrn|d d }n|d d }|f||< |S r#   r$   )r   r   r   r   r&   r
   mr   r   r   _ihfft_out_chunksW   s    

r)   )fftifftrfftirffthfftihfftc                   s   t dur2t jju rtdnt jju r2tddu r@jztd W n typ   td Y n0 dfdd	}	dr| d fd	d	}t
}j}|jd
 | }jdurtd|f  |_| jj7  _t|j|_||_|S )a|  Wrap 1D, 2D, and ND real and complex FFT functions

    Takes a function that behaves like ``numpy.fft`` functions and
    a specified kind to match it to that are named after the functions
    in the ``numpy.fft`` API.

    Supported kinds include:

        * fft
        * fft2
        * fftn
        * ifft
        * ifft2
        * ifftn
        * rfft
        * rfft2
        * rfftn
        * irfft
        * irfft2
        * irfftn
        * hfft
        * ihfft

    Examples
    --------
    >>> import dask.array.fft as dff
    >>> parallel_fft = dff.fft_wrap(np.fft.fft)
    >>> parallel_ifft = dff.fft_wrap(np.fft.ifft)
    Nz+SciPy's `rfft` doesn't match the NumPy API.z,SciPy's `irfft` doesn't match the NumPy API.Z2nzGiven unknown `kind` %s.c           
        sx  t | } |d u rjdr d}qdrd|d u rBtt| j}qhtrRtdt ttt	|}qd}nt	t
|t	|k rtd }|d u rtj| jd | jd}z||d	j}W n ty   |j}Y n0 |D ],}t	| j| d
krtt|| j| f q܈| ||}||f}dr^|d u r8d n|d }|d u rNd n|d }	|	|f}| jg|R ||dS )N2)r   r&   z^DeprecationWarning: `axes` should not be `None` if `s` is not `None` (Deprecated in NumPy 2.0))r   zDuplicate axes not allowed.)   )dtype)r   r   r*   r   r3   r
   )r   endswithtuplerangendimr   warningswarnDeprecationWarningr%   set
ValueErrornpZonesr3   	TypeErrorr
   chunk_error
map_blocks)
r   r   r   Z_dtypesampleZ	each_axisr
   argsr   r&   )r3   fft_funckindout_chunk_fnr   r   func   sB    

zfft_wrap.<locals>.funcr*   c                   s0   d }|d ur|f}d }|d ur$|f} | ||S )Nr   )r   r&   r   r   r   )_funcr   r   rG      s    .r   )NN)NN)scipyZfftpackr,   r=   r-   __name___out_chunk_fnsrstripKeyErrorr5   inspect	getmodule__doc__fft_preambler	   )rD   rE   r3   rG   Zfunc_mod	func_nameZfunc_fullnamer   )rH   r3   rD   rE   rF   r   fft_wrapv   s0    
-


rT   c                 C  s4   |   }|| |d d k  |8  < |||  }|S )Nr   r   )copy)r   r&   drr   r   r   _fftfreq_block   s    rX         ?autoc                 C  s0   t | } t|}t| t|d}|jtt| |dS )Nr4   )r3   r&   rV   )intfloat_arangerA   rX   r&   rV   r
   rW   r   r   r   fftfreq   s    r_   c                 C  s6   t | } t|}t| d d t|d}|| |  }|S )Nr   r   r4   )r[   r\   r]   r^   r   r   r   rfftfreq  s
    r`   Fc           	      C  s   |d u rt t| j}nt|ts(|f}| }|D ]}|j| }|t|du  d }|jtd g }td |||< t|}|jtd g }t|d ||< t|}t	|| || g|d}t
| j| dkr0||| j| i}q0|S )NFr   )r   r   )r   r7   r8   
isinstancer   shaper[   slicer6   _concatenater%   r
   Zrechunk)	xr   inverseyr   r&   Zn_2lrW   r   r   r   _fftshift_helper  s$    

ri   c                 C  s   t | |ddS )NFr   rf   ri   re   r   r   r   r   fftshift.  s    rm   c                 C  s   t | |ddS )NTrj   rk   rl   r   r   r   	ifftshift3  s    rn   )NN)rY   rZ   )rY   rZ   )NF)N)N)4
__future__r   rO   r9   collections.abcr   numpyr>   rJ   Zscipy.fftpackImportErrorZdask.array.corer   r   rd   Zdask.array.creationr   r]   Zdask.array.numpy_compatr   Z
dask.utilsr   r	   r@   rR   r   r!   r"   r'   r)   rL   rT   r*   Zfft2Zfftnr+   Zifft2Zifftnr,   Zrfft2Zrfftnr-   Zirfft2Zirfftnr.   r/   rX   r_   r`   ri   rm   rn   r   r   r   r   <module>   sl   



q	

