<jupyter_start><jupyter_text>Inverse Distance Weighting (IDW) Interpolation Let us suppose we have a data that shows the variation of one quantity of interest across space.This could be equivalently viewed as { ($\vec{x_1}, y_1)$,$(\vec{x_2}, y_2)$,$(\vec{x_3}, y_3)$, ...}, where the $\vec{x_i}$'s represent the coordinates of the points where we have data and the $y_i$'s are the actual data at those points. We would like to perform an interpolation using these data points such that a few things are satisifed.1. The interpolation is exact - the value at the known data points is the same as the estimated value, and 2. We would want far away points from a given source data point to receive less importance than nearby points.3. Wikipedia has an excellent article on IDW. I am linking it [here](https://en.wikipedia.org/wiki/Inverse_distance_weighting). We are using the following approximation for coordinate_type being latlong_small$| \vec{r_2}− \vec{r_1}| ≈ \text{R }\times \sqrt[]{(Lat_2 - Lat_1)^{2} + (Long_2 - Long_1)^{2}}$<jupyter_code>import numpy as np
import pandas as pd
df = pd.read_csv('../../testdata/30-03-18.csv')
data = np.array(df[['longitude','latitude','value']])
def make_grid(X,y,res):
    y_min = y.min()-0.2
    y_max = y.max()+0.2
    x_min = X.min()-0.2
    x_max = X.max()+0.2
    x_arr = np.linspace(x_min,x_max,res)
    y_arr = np.linspace(y_min,y_max,res)
    xx,yy = np.meshgrid(x_arr,y_arr)  
    return xx,yy

def idw(dataset, exponent = 2,  resolution='standard', coordinate_type='euclidean',verbose='False'):
    """
        Here X is the set of spatial locations - Usually assumed to be Lat-Long
        To be extended to higher dimenstions y - estimated value , exponenet - how
        much weight to assign to far off locations to be estimated for each data point, 
        extent - interpolate over a grid - what is xmax xmin ymax ymin
    """
    if coordinate_type == 'latlong_small':
        """
            Assume that the Earth is a Sphere, and use polar coordinates
            $| \vec{r_2}− \vec{r_1}| ≈ \text{R }\times \sqrt[]{(Lat_2 - Lat_1)^{2} + (Long_2 - Long_1)^{2}}$
        """
        return "To be done later"
    if coordinate_type == 'latlong_large':
        """
            Code to be written after understanding all the projections.
        """
        return "To be done later"
    if coordinate_type=="euclidean":
        
#         print(dataset)
        X = dataset[:,0]
        y = dataset[:,1]
        if resolution=='high':
            xx,yy = make_grid(X,y,1000)
            
        if resolution=='low':
            xx,yy = make_grid(X,y,10)
            
        if resolution=='standard':
            xx,yy = make_grid(X,y,100)
        
        new = []
        new_arr = dataset
        for points in new_arr:
            mindist = np.inf
            val = 0
            for j in range(len(yy)):
                temp = yy[j][0]
                for i in range(len(xx[0])):
                    dist = np.linalg.norm(np.array([xx[0][i],temp]) - points[:2])
                    if dist<mindist:
                        mindist = dist
                        val = (i,j)
            new.append((points,val))
        print(new)
        new_grid = np.zeros((len(xx),len(yy)))
        for i in range(len(new)):
            x = new[i][1][0]
            y = new[i][1][1]
            new_grid[x][y] = new[i][0][2]
            print(new[i])
        x_nz,y_nz = np.nonzero(new_grid)
        list_nz = []
        for i in range(len(x_nz)):
            list_nz.append((x_nz[i],y_nz[i]))
        
        final = np.copy(new_grid)
        
        for i in range(len(xx[0])):
            for j in range(len(yy)):
                normalise = 0
                if (i,j) in list_nz:
                    continue
                else:
                    """
                    Could potentially have a divide by zero error here
                    Use a try except clause
                    """
                    for elem in range(len(x_nz)):
                        source = np.array([x_nz[elem],y_nz[elem]])
                        target = np.array([xx[0][i],yy[j][0]])
                        dist = (np.abs(xx[0][source[0]] - target[0])**exponent + np.abs(yy[source[1]][0] - target[1])**exponent)**(1/exponent)
                        final[i][j]+=new_grid[x_nz[elem],y_nz[elem]]/dist
                        normalise+=1/(dist)
                final[i][j]/=normalise
    
    return final

idw(data).shape

temp = data[10]
np.where(data==temp)
result  = np.nonzero(data==temp)
np.unique(result[0])[0]
listOfCoordinates= list(zip(result[0], result[1]))
listOfCoordinates<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1319622964867181, 'fraction_numerical': 0.01713796058269066, 'mean_word_length': 2.049640757674722, 'pattern_counts': {'":': 1, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '454112', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1468, 'n_words': 406, 'path': 'polire/interpolate/idw/tests/IDW Initial.ipynb', 'repo_name': 'patel-zeel/ALdeploy', 'size': '4668'}