File size: 7,240 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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pykrige import OrdinaryKriging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "ok = OrdinaryKriging(data[:,0],data[:,1],data[:,2])\n",
    "ok.ex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b = ok.execute('grid',x[0],y[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pykrige import OrdinaryKriging\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "def ordinary_kriging(dataset, resolution='standard', coordinate_type='euclidean',verbose='False',method='grid', isvariance = False):\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",
    "        ok = OrdinaryKriging(dataset[:,0],dataset[:,1],dataset[:,2])\n",
    "        X = dataset[:,0]\n",
    "        y = dataset[:,1]\n",
    "        \n",
    "        if resolution=='high':\n",
    "            xx,yy = make_grid(X,y,1000)\n",
    "            \n",
    "        elif resolution=='low':\n",
    "            xx,yy = make_grid(X,y,10)\n",
    "            \n",
    "        elif resolution=='standard':\n",
    "            xx,yy = make_grid(X,y,100)\n",
    "            \n",
    "        else:\n",
    "            print('Value Error - Resolution can only be one of \\nhigh, low or standard')\n",
    "        \n",
    "        values, variances = ok.execute(method, xx[0], yy[:,0])\n",
    "        \n",
    "    if isvariance:\n",
    "        return values, variances\n",
    "    else:\n",
    "        del variances\n",
    "        return np.array(values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[129.94984945, 129.7682324 , 129.58820662, ..., 159.34079485,\n",
       "        159.99175016, 160.63241067],\n",
       "       [130.22090025, 130.03615966, 129.8529146 , ..., 159.9575165 ,\n",
       "        160.61228126, 161.25625641],\n",
       "       [130.50105231, 130.31324536, 130.12683652, ..., 160.59265384,\n",
       "        161.25084023, 161.8977369 ],\n",
       "       ...,\n",
       "       [207.22133238, 207.82739139, 208.44615116, ..., 248.64646661,\n",
       "        248.3790241 , 248.11033441],\n",
       "       [207.92838926, 208.53490708, 209.15376273, ..., 248.91678379,\n",
       "        248.65601627, 248.39371596],\n",
       "       [208.61942088, 209.22595474, 209.84445913, ..., 249.17442481,\n",
       "        248.9203453 , 248.66446245]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ordinary_kriging(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* What does ok('points') really do?\n",
    "* Specifically test when points aren't really passed - they are let's say the point of an array\n",
    "* Returns the diagonal matrix of all these coordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([129.94984945, 130.03615966, 130.12683652, 130.22219703,\n",
       "       130.32258826, 130.42839089, 130.54002324, 130.65794596,\n",
       "       130.7826674 , 130.91474976, 131.05481629, 131.20355964,\n",
       "       131.36175158, 131.53025441, 131.71003442, 131.90217771,\n",
       "       132.107909  , 132.32861401, 132.56586607, 132.82145795,\n",
       "       133.0974399 , 133.39616477, 133.72034153, 134.07309736,\n",
       "       134.45804822, 134.87937482, 135.34189663, 135.85112772,\n",
       "       136.41328222, 137.03517039, 137.72388496, 138.48612122,\n",
       "       139.326921  , 140.24763047, 141.24300526, 142.29757046,\n",
       "       143.37881815, 144.38425962, 144.49187978, 143.1202101 ,\n",
       "       141.66667134, 140.45686022, 139.66795657, 142.48270308,\n",
       "       147.03665055, 151.8487008 , 156.90272514, 162.25791164,\n",
       "       168.04938768, 173.63870768, 180.93567147, 190.3440156 ,\n",
       "       199.86834472, 208.48375248, 215.75635742, 222.1915652 ,\n",
       "       228.08641413, 233.15249702, 236.89713686, 239.83524192,\n",
       "       242.45744315, 244.57483343, 245.52139699, 245.88236757,\n",
       "       246.12295211, 246.3306567 , 246.52369882, 246.70598807,\n",
       "       246.87792737, 247.03919426, 247.18952217, 247.3288843 ,\n",
       "       247.45749059, 247.57573348, 247.68412862, 247.78326467,\n",
       "       247.87376505, 247.95626051, 248.03137024, 248.09968963,\n",
       "       248.16178271, 248.21817801, 248.26936683, 248.31580309,\n",
       "       248.35790422, 248.39605277, 248.43059841, 248.46186013,\n",
       "       248.49012851, 248.51566797, 248.53871897, 248.55950011,\n",
       "       248.57821004, 248.59502931, 248.61012204, 248.62363741,\n",
       "       248.63571111, 248.64646661, 248.65601627, 248.66446245])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ordinary_kriging(data,method='points')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
    "x, y = make_grid(data[:,0],data[:,1],100)"
   ]
  }
 ],
 "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
}