o
    پi	                     @   s*   d Z ddlZddlmZ G dd dZdS )z Interpolation helpers for timm layers

RegularGridInterpolator from https://github.com/sbarratt/torch_interpolations
Copyright Shane Barratt, Apache 2.0 license
    N)productc                   @   s    e Zd ZdZdd Zdd ZdS )RegularGridInterpolatorz Interpolate data defined on a rectilinear grid with even or uneven spacing.
    Produces similar results to scipy RegularGridInterpolator or interp2d
    in 'linear' mode.

    Taken from https://github.com/sbarratt/torch_interpolations
    c                 C   s   || _ || _t| j tst| j tsJ t| jtjsJ t| jj| _t	| j | _
t	| j| j
ks4J t| j D ]\}}t|tjsEJ |jd | jj| ksRJ q9d S )Nr   )pointsvalues
isinstancetuplelisttorchTensorshapemslenn	enumerate)selfr   r   ip r   K/home/ubuntu/.local/lib/python3.10/site-packages/timm/layers/interpolate.py__init__   s   z RegularGridInterpolator.__init__c                 C   s  | j d usJ | jd usJ t|t| j ksJ |d jd }|D ]}|jd |ks-J q"g }g }g }t| j |D ]b\}}t||}|jd d |||jd k< |d d|jd d }	|||	  }
|| | }d|
|
dk < d||dk < |
dk|dk@ }d |
|< ||< ||	|f ||
|f ||
|  q:d}t	ddg| j
dD ]'}dd t||D }dd t||D }|| j| tjt|dd	 7 }qtjt|dd	}|| S )
Nr      g        g      ?)repeatc                 S   s   g | ]\}}|| qS r   r   ).0onoffidxr   r   r   
<listcomp>?   s    z4RegularGridInterpolator.__call__.<locals>.<listcomp>c                 S   s   g | ]
\}}|d |  qS )r   r   )r   r   distr   r   r   r   @   s    )dim)r   r   r   r   zipr	   	bucketizeclampappendr   r   prodstack)r   points_to_interpKxidxsdistsoverallsr   	idx_rightidx_left	dist_left
dist_right	both_zero	numeratorindexeras_sbs_sdenominatorr   r   r   __call__"   s>   
z RegularGridInterpolator.__call__N)__name__
__module____qualname____doc__r   r4   r   r   r   r   r   
   s    r   )r8   r	   	itertoolsr   r   r   r   r   r   <module>   s    