File size: 12,067 Bytes
9af7384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inverse Distance Weighting (IDW) Interpolation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us suppose we have a data that shows the variation of one quantity of interest across space.\n",
    "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. <br><br>\n",
    "We would like to perform an interpolation using these data points such that a few things are satisifed.\n",
    "1. The interpolation is exact - the value at the known data points is the same as the estimated value, and \n",
    "2. We would want far away points from a given source data point to receive less importance than nearby points.\n",
    "3. Wikipedia has an excellent article on IDW. I am linking it [here](https://en.wikipedia.org/wiki/Inverse_distance_weighting)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are using the following approximation for coordinate_type being latlong_small<br>\n",
    "$| \\vec{r_2}− \\vec{r_1}| ≈ \\text{R }\\times \\sqrt[]{(Lat_2 - Lat_1)^{2} + (Long_2 - Long_1)^{2}}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "df = pd.read_csv('../../testdata/30-03-18.csv')\n",
    "data = np.array(df[['longitude','latitude','value']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_grid(X,y,res):\n",
    "    y_min = y.min()-0.2\n",
    "    y_max = y.max()+0.2\n",
    "    x_min = X.min()-0.2\n",
    "    x_max = X.max()+0.2\n",
    "    x_arr = np.linspace(x_min,x_max,res)\n",
    "    y_arr = np.linspace(y_min,y_max,res)\n",
    "    xx,yy = np.meshgrid(x_arr,y_arr)  \n",
    "    return xx,yy\n",
    "\n",
    "def idw(dataset, exponent = 2,  resolution='standard', coordinate_type='euclidean',verbose='False'):\n",
    "    \"\"\"\n",
    "        Here X is the set of spatial locations - Usually assumed to be Lat-Long\n",
    "        To be extended to higher dimenstions y - estimated value , exponenet - how\n",
    "        much weight to assign to far off locations to be estimated for each data point, \n",
    "        extent - interpolate over a grid - what is xmax xmin ymax ymin\n",
    "    \"\"\"\n",
    "    if coordinate_type == 'latlong_small':\n",
    "        \"\"\"\n",
    "            Assume that the Earth is a Sphere, and use polar coordinates\n",
    "            $| \\vec{r_2}− \\vec{r_1}| ≈ \\text{R }\\times \\sqrt[]{(Lat_2 - Lat_1)^{2} + (Long_2 - Long_1)^{2}}$\n",
    "        \"\"\"\n",
    "        return \"To be done later\"\n",
    "    if coordinate_type == 'latlong_large':\n",
    "        \"\"\"\n",
    "            Code to be written after understanding all the projections.\n",
    "        \"\"\"\n",
    "        return \"To be done later\"\n",
    "    if coordinate_type==\"euclidean\":\n",
    "        \n",
    "#         print(dataset)\n",
    "        X = dataset[:,0]\n",
    "        y = dataset[:,1]\n",
    "        if resolution=='high':\n",
    "            xx,yy = make_grid(X,y,1000)\n",
    "            \n",
    "        if resolution=='low':\n",
    "            xx,yy = make_grid(X,y,10)\n",
    "            \n",
    "        if resolution=='standard':\n",
    "            xx,yy = make_grid(X,y,100)\n",
    "        \n",
    "        new = []\n",
    "        new_arr = dataset\n",
    "        for points in new_arr:\n",
    "            mindist = np.inf\n",
    "            val = 0\n",
    "            for j in range(len(yy)):\n",
    "                temp = yy[j][0]\n",
    "                for i in range(len(xx[0])):\n",
    "                    dist = np.linalg.norm(np.array([xx[0][i],temp]) - points[:2])\n",
    "                    if dist<mindist:\n",
    "                        mindist = dist\n",
    "                        val = (i,j)\n",
    "            new.append((points,val))\n",
    "        print(new)\n",
    "        new_grid = np.zeros((len(xx),len(yy)))\n",
    "        for i in range(len(new)):\n",
    "            x = new[i][1][0]\n",
    "            y = new[i][1][1]\n",
    "            new_grid[x][y] = new[i][0][2]\n",
    "            print(new[i])\n",
    "        x_nz,y_nz = np.nonzero(new_grid)\n",
    "        list_nz = []\n",
    "        for i in range(len(x_nz)):\n",
    "            list_nz.append((x_nz[i],y_nz[i]))\n",
    "        \n",
    "        final = np.copy(new_grid)\n",
    "        \n",
    "        for i in range(len(xx[0])):\n",
    "            for j in range(len(yy)):\n",
    "                normalise = 0\n",
    "                if (i,j) in list_nz:\n",
    "                    continue\n",
    "                else:\n",
    "                    \"\"\"\n",
    "                    Could potentially have a divide by zero error here\n",
    "                    Use a try except clause\n",
    "                    \"\"\"\n",
    "                    for elem in range(len(x_nz)):\n",
    "                        source = np.array([x_nz[elem],y_nz[elem]])\n",
    "                        target = np.array([xx[0][i],yy[j][0]])\n",
    "                        dist = (np.abs(xx[0][source[0]] - target[0])**exponent + np.abs(yy[source[1]][0] - target[1])**exponent)**(1/exponent)\n",
    "                        final[i][j]+=new_grid[x_nz[elem],y_nz[elem]]/dist\n",
    "                        normalise+=1/(dist)\n",
    "                final[i][j]/=normalise\n",
    "    \n",
    "    return final\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(array([ 77.234291,  28.581197, 194.      ]), (60, 39)), (array([ 77.245721,  28.739434, 267.      ]), (62, 60)), (array([ 77.101961,  28.822931, 273.      ]), (42, 72)), (array([ 76.991463,  28.620806, 129.      ]), (27, 44)), (array([ 77.0325413,  28.60909  , 176.       ]), (33, 42)), (array([ 77.072196,  28.570859, 172.      ]), (38, 37)), (array([ 77.1670103,  28.5646102, 168.       ]), (51, 36)), (array([ 77.1180053,  28.5627763, 105.       ]), (45, 36)), (array([ 77.272404,  28.530782, 203.      ]), (66, 32)), (array([ 77.26075 ,  28.563827, 192.      ]), (64, 36)), (array([77.0996943, 28.610304 , 95.       ]), (42, 43)), (array([ 77.2273074,  28.5918245, 148.       ]), (59, 40)), (array([ 77.09211 ,  28.732219, 203.      ]), (41, 59)), (array([ 77.317084,  28.668672, 221.      ]), (72, 51)), (array([ 77.1585447,  28.6573814, 141.       ]), (50, 49)), (array([ 77.2011573,  28.6802747, 192.       ]), (56, 52)), (array([ 77.237372,  28.612561, 203.      ]), (61, 43)), (array([ 77.305651,  28.632707, 152.      ]), (70, 46)), (array([ 77.1473105,  28.6514781, 185.       ]), (49, 48)), (array([ 77.16482 ,  28.699254, 290.      ]), (51, 55)), (array([ 77.170221,  28.728722, 273.      ]), (52, 59)), (array([ 77.2005604,  28.6372688, 173.       ]), (56, 46)), (array([ 77.2011573,  28.7256504, 269.       ]), (56, 58)), (array([ 77.136777,  28.669119, 160.      ]), (47, 51)), (array([77.267246, 28.49968 , 78.      ]), (65, 27)), (array([ 77.2494387,  28.6316945, 211.       ]), (62, 45)), (array([ 77.2735737,  28.5512005, 252.       ]), (66, 34)), (array([ 77.2159377,  28.5504249, 133.       ]), (58, 34)), (array([77.1112615, 28.7500499, 77.       ]), (44, 62)), (array([77.22445, 28.63576, 96.     ]), (59, 46))]\n",
      "(array([ 77.234291,  28.581197, 194.      ]), (60, 39))\n",
      "(array([ 77.245721,  28.739434, 267.      ]), (62, 60))\n",
      "(array([ 77.101961,  28.822931, 273.      ]), (42, 72))\n",
      "(array([ 76.991463,  28.620806, 129.      ]), (27, 44))\n",
      "(array([ 77.0325413,  28.60909  , 176.       ]), (33, 42))\n",
      "(array([ 77.072196,  28.570859, 172.      ]), (38, 37))\n",
      "(array([ 77.1670103,  28.5646102, 168.       ]), (51, 36))\n",
      "(array([ 77.1180053,  28.5627763, 105.       ]), (45, 36))\n",
      "(array([ 77.272404,  28.530782, 203.      ]), (66, 32))\n",
      "(array([ 77.26075 ,  28.563827, 192.      ]), (64, 36))\n",
      "(array([77.0996943, 28.610304 , 95.       ]), (42, 43))\n",
      "(array([ 77.2273074,  28.5918245, 148.       ]), (59, 40))\n",
      "(array([ 77.09211 ,  28.732219, 203.      ]), (41, 59))\n",
      "(array([ 77.317084,  28.668672, 221.      ]), (72, 51))\n",
      "(array([ 77.1585447,  28.6573814, 141.       ]), (50, 49))\n",
      "(array([ 77.2011573,  28.6802747, 192.       ]), (56, 52))\n",
      "(array([ 77.237372,  28.612561, 203.      ]), (61, 43))\n",
      "(array([ 77.305651,  28.632707, 152.      ]), (70, 46))\n",
      "(array([ 77.1473105,  28.6514781, 185.       ]), (49, 48))\n",
      "(array([ 77.16482 ,  28.699254, 290.      ]), (51, 55))\n",
      "(array([ 77.170221,  28.728722, 273.      ]), (52, 59))\n",
      "(array([ 77.2005604,  28.6372688, 173.       ]), (56, 46))\n",
      "(array([ 77.2011573,  28.7256504, 269.       ]), (56, 58))\n",
      "(array([ 77.136777,  28.669119, 160.      ]), (47, 51))\n",
      "(array([77.267246, 28.49968 , 78.      ]), (65, 27))\n",
      "(array([ 77.2494387,  28.6316945, 211.       ]), (62, 45))\n",
      "(array([ 77.2735737,  28.5512005, 252.       ]), (66, 34))\n",
      "(array([ 77.2159377,  28.5504249, 133.       ]), (58, 34))\n",
      "(array([77.1112615, 28.7500499, 77.       ]), (44, 62))\n",
      "(array([77.22445, 28.63576, 96.     ]), (59, 46))\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(100, 100)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idw(data).shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp = data[10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([10, 10, 10]), array([0, 1, 2]))"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(data==temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "result  = np.nonzero(data==temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(result[0])[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "listOfCoordinates= list(zip(result[0], result[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(10, 0), (10, 1), (10, 2)]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "listOfCoordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}