
    j	                     0    d Z ddlZddlmZ  G d d      Zy)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y)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                 d   || _         || _        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 ]L  \  }}t        |t
        j                        sJ |j                  d   | j                  j                  |   k(  rLJ  y )Nr   )pointsvalues
isinstancetuplelisttorchTensorshapemslenn	enumerate)selfr   r   ips        /home/vela/workspace/domain/tn-silver/web/tn-site/.runtime/depth-eval/venv/lib/python3.12/site-packages/timm/layers/interpolate.py__init__z RegularGridInterpolator.__init__   s    $++u-DKK1NNN$++u||444t{{(()T[[!477|tvv%%%dkk*DAqa...771:!2!21!5555 +    c                 z   | j                   J | j                  J t        |      t        | j                         k(  sJ |d   j                  d   }|D ]  }|j                  d   |k(  rJ  g }g }g }t	        | j                   |      D ]  \  }}t        j                  ||      }|j                  d   dz
  |||j                  d   k\  <   |dz
  j                  d|j                  d   dz
        }	|||	   z
  }
||   |z
  }d|
|
dk  <   d||dk  <   |
dk(  |dk(  z  }dx|
|<   ||<   |j                  |	|f       |j                  |
|f       |j                  |
|z           d}t        ddg| j                        D ]  }t	        ||      D cg c]
  \  }}||    }}}t	        ||      D cg c]  \  }}|d|z
      }}}|| j                  |   t        j                  t        j                  |      d      z  z  } t        j                  t        j                  |      d      }||z  S c c}}w c c}}w )Nr      g        g      ?)repeat)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onoffidxas_sdistbs_sdenominators                        r   __call__z RegularGridInterpolator.__call__"   sP   {{&&&{{&&&#$DKK(8888Q%%a(!A771:?"? " %56DAq1-I12aIi1771:-.!A,,Q
Q?HAhKI9)J')Ii!m$)+JzA~&"aJ!O<I;==Ii :i#8KK9-.LL)Z01OOI
23 7 	1vdff5G14Wd1CD1C:5#CJ1CDD7:7E7JK7JtDUO7JDKT*

5;;t,!45 5I 6
 jjX!6A>;&& EKs   H1.H7N)__name__
__module____qualname____doc__r   r6    r   r   r   r   
   s    6 "'r   r   )r:   r   	itertoolsr   r   r;   r   r   <module>r=      s   
  :' :'r   