
    Ng	                     8    d Z ddlZddlmZ  G 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                       e Zd ZdZd Z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                 .   || _         || _        t          | j         t                    st          | j         t                    sJ t          | j        t
          j                  sJ t	          | j        j                  | _        t          | j                   | _
        t          | j                  | j
        k    sJ t          | j                   D ]D\  }}t          |t
          j                  sJ |j        d         | j        j        |         k    sJ Ed S )Nr   )pointsvalues
isinstancetuplelisttorchTensorshapemslenn	enumerate)selfr   r   ips        S/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/layers/interpolate.py__init__z RegularGridInterpolator.__init__   s    $+u--NDK1N1NNNN$+u|44444t{())T[!!47||tv%%%%dk** 	6 	6DAqa.....71:!21!555555	6 	6    c                 @   | j         J | j        J t          |          t          | j                   k    sJ |d         j        d         }|D ]}|j        d         |k    sJ g }g }g }t	          | j         |          D ]\  }}t          j        ||          }|j        d         dz
  |||j        d         k    <   |dz
                      d|j        d         dz
            }	|||	         z
  }
||         |z
  }d|
|
dk     <   d||dk     <   |
dk    |dk    z  }dx|
|<   ||<   |                    |	|f           |                    |
|f           |                    |
|z              d}t          ddg| j
                  D ]o}d t	          ||          D             }d t	          ||          D             }|| j        |         t          j        t          j        |          d          z  z  }pt          j        t          j        |          d          }||z  S )	Nr      g        g      ?)repeatc                 $    g | ]\  }}||         S  r   ).0onoffidxs      r   
<listcomp>z4RegularGridInterpolator.__call__.<locals>.<listcomp>?   s     DDD:5#CJDDDr   c                 *    g | ]\  }}|d |z
           S )r   r   )r   r   dists      r   r!   z4RegularGridInterpolator.__call__.<locals>.<listcomp>@   s$    KKKtDUOKKKr   )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denominators                     r   __call__z RegularGridInterpolator.__call__"   s_   {&&&{&&&#$$DK(8(88888Q%a(! 	# 	#A71:?????%566 	4 	4DAq1--I12aIi171:-.!A,,Q
Q??HAhKI9)J')Ii!m$)+JzA~&"aJ!O<I;==Ii :i#8KK9-...LL)Z0111OOI
23333	1vdf555 	5 	5GDDWd1C1CDDDDKKs7E7J7JKKKDT*
5;t,,!4445 5IIjX!6!6A>>>;&&r   N)__name__
__module____qualname____doc__r   r;   r   r   r   r   r   
   s<         6 6 6 "' "' "' "' "'r   r   )r?   r   	itertoolsr   r   r   r   r   <module>rA      s^    
       :' :' :' :' :' :' :' :' :' :'r   