Gen_Data.ipynb 33.5 KB
Newer Older
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "import sys\n",
    "import os\n",
    "# relative import\n",
    "sys.path.append(os.path.abspath('') + '/../')\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "from scipy.stats import uniform, gamma\n",
    "\n",
    "import pyfield\n",
    "from corbass.utils import load, nez2dif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# seed for reproducability\n",
33
34
    "seed = 161\n",
    "np.random.seed(seed)"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate synthetic data for tests\n",
    "\n",
    "This notebook serves the purpose of generating synthetic data for testing the `CORBASS` algorithm. We therefore generate data from a given set of Gauss coefficients and add synthetic errors from the Fisher-von Mises and the gamma distribution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# some basic parameters\n",
    "# the name of the output file\n",
54
    "out = '../dat/synth_data_clean_complete.csv'\n",
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    "# switch for using locations and incompleteness structure from the example data file\n",
    "real_locs = False\n",
    "# the number of records to be generated, is only used if real_locs is False\n",
    "n_points = 412\n",
    "# the fraction of incomplete records, works as a switch if real_locs is False\n",
    "r_inc = 0.\n",
    "# switch for corrupting the data by noise\n",
    "noise = False\n",
    "# the error levels to be stored\n",
    "ddec = 4.5\n",
    "dinc = 4.5\n",
    "dint = 8250\n",
    "# the average concentration parameter from GEOMAGIA for the interval [750, today] is 650\n",
    "kappa = 650\n",
    "\n",
    "header = f\"# This file was produced using the notebook Gen_Data.ipynb with the following parameters:\\n\" \\\n",
    "       + f\"# real_locs={real_locs}, n_points={n_points:d}, r_inc={r_inc:.2f}, noise={noise}, ddec={ddec:.2f}, \" \\\n",
72
    "       + f\"dinc={dinc:.2f}, dint={dint:d}, kappa={kappa:.1f}, seed={seed:d}\\n\""
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
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sampling from the Fisher-von Mises distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The sampling process involves some rotations, thus we first define some convenience functions\n",
    "def angles(vec):\n",
    "    return np.arctan2(vec[1], vec[0]), \\\n",
    "        np.pi/2 - np.arctan2(vec[2], np.sqrt(vec[0]**2 + vec[1]**2))\n",
    "\n",
    "\n",
    "def rot_z(ang):\n",
    "    return np.array([[np.cos(ang), np.sin(ang), 0],\n",
    "                     [-np.sin(ang), np.cos(ang), 0],\n",
    "                     [0, 0, 1]])\n",
    "\n",
    "\n",
    "def rot_y(ang):\n",
    "    return np.array([[np.cos(ang), 0, np.sin(ang)],\n",
    "                     [0, 1, 0],\n",
    "                     [-np.sin(ang), 0,  np.cos(ang)]])\n",
    "\n",
    "\n",
    "def rotator(vec):\n",
    "    vec = np.asarray(vec)\n",
    "    p, t = angles(vec)\n",
    "    return np.dot(rot_y(t).T, rot_z(p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This cell contains the sampling procedure\n",
    "def sample_Fisher(n, mu=(0, 0, 1), kappa=20):\n",
    "    \"\"\" Generate samples from the Fisher distribution\n",
    "    \n",
    "    Parameters:\n",
    "    -----------\n",
    "    n : int\n",
    "        The number of samples to be generated\n",
    "    mu : array-like of length 3, optional\n",
    "        A vector pointing towards the center of the distribution. Its length is ignored.\n",
    "    kappa : float, optional\n",
    "        The concentration parameter.\n",
    "    \n",
    "    Returns:\n",
    "    --------\n",
    "        numpy array of shape (3, n) containing the sampled vectors\n",
    "    \n",
    "    Reference:\n",
    "    ----------\n",
    "    [1]: W. Jakob, \"Numerically stable sampling of the von Mises \n",
    "         Fisher distribution on S^2 (and other tricks)\",\n",
    "         http://www.mitsuba-renderer.org/~wenzel/files/vmf.pdf,\n",
    "         2015\n",
    "    \"\"\"\n",
    "    if kappa <= 0:\n",
    "        raise ValueError(f\"The concentration parameter has to be positive, but kappa={kappa} was given.\\n\"\n",
    "                         f\"For kappa=0 use a uniform sampler on the sphere.\")\n",
    "    trafo_mat = rotator(mu)\n",
    "    \n",
    "    # sample from the uniform circle, V in [1]\n",
    "    angles = uniform.rvs(scale=2*np.pi, size=n)\n",
    "    vs = np.array([np.cos(angles),\n",
    "                   np.sin(angles)])\n",
    "    # sample W in [1] via inverse cdf sampling\n",
    "    def inv_cdf(x):\n",
    "        return 1 + np.log(x + (1-x)*np.exp(-2*kappa))/kappa\n",
    "    \n",
    "    unis = uniform.rvs(size=n)\n",
    "    ws = inv_cdf(unis)\n",
    "    ret = np.sqrt(1-ws**2)*vs\n",
    "    res = np.einsum(\"i...,ij->j...\", np.array([ret[0], ret[1], ws]), trafo_mat)\n",
    "    if n == 1:\n",
    "        return res.flatten()\n",
    "    else:\n",
    "        return res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We draw some samples and plot the result, to eye-check whether the procedure works:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
179
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAlsAAAE9CAYAA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IPDlDrdJZE3tidx/AjznnPu3nW5PtzCzkaVV82ZWBN7FJuQBrUZsM7MvALfRWt11EviIc+7lzrbq2mdmR4A8MNV+6TGt8twcZvarwB8CI8As8JRz7v7OturaZWYPAP8X4AOfcc79iw43qSuY2eeBdwDDwDjwSefcn3S0UV3AzO4FHgV+SutzC+CfOOce6Vyrrn1m9ibgz2j9d8AD/sI59/sbPq/CloiIiEh2NIwoIiIikiGFLREREZEMKWyJiIiIZEhhS0RERCRDClsiIiIiGVLYEhEREcmQwpaIXPPMLDGzp8zsGTP7sZn9j2b2qv++mdk7zGzOzDZUi8jM/jsz+6NVXr/PzJ41sw1VmxaR7qKwJSLdoOacu9M5dwfwbuAB1t5u61Hn3AMXv2hm/kYb4Zx7tH1tEZFlClsi0lWcc+eBg8BH21uarKnd0/U3ZvafaFXixsy+ZGZPtHvJDq449sNm9qKZ/S1wT5a/g4h0l+tqI2oRuT445461hxFvoLVFzKXcBbzBOXe8/fx3nHPT7X3RHm9v5ZUD/hnwNmAO+BvgR9m0XkS6jcKWiHSrS/ZqrfDDFUEL4O+3950E2AncAowB33bOTQCY2cPArZvWUhHpagpbItJ1zGwfkADn13F4dcXPvQN4F/B259yimX0bKLTf1kayIvKaaM6WiHQVMxsBPgX8kXPuSgNSPzDTDlqvA+5uv/4D4B1mNmRmIfAbm9diEel26tkSkW5QNLOngBCIgc8B//Y1nOdrwEfM7CfAC8BjAM65s2b2T4HvA2eBJwEfwMzeDxxwzn1io7+EiHQnu/L/4ycicm1qDxP+nnPufRleYw/wX5xzb8jqGiJybdEwoohcT5rAGzZa1HQtZnYf8BVgMovzi8i1ST1bIiIiIhlSz5aIiIhIhhS2RERERDKksCUiIiKSIYUtERERkQwpbImIiIhk6P8HQHkS8F5HMBIAAAAASUVORK5CYII=\n",
180
181
182
183
184
185
186
187
188
189
190
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
191
    "SF = sample_Fisher(1000, mu=(-1, 0, 0), kappa=kappa);\n",
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    "fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n",
    "\n",
    "D = np.arctan2(SF[1], SF[0])\n",
    "I = np.arctan2(SF[2], np.sqrt(SF[0]**2 + SF[1]**2))\n",
    "\n",
    "ax.set_aspect(1)\n",
    "ax.set_xlim((-np.pi, np.pi));\n",
    "ax.set_xlabel('D [rad.]')\n",
    "ax.set_ylim((-np.pi/2., np.pi/2.));\n",
    "ax.set_ylabel('I [rad.]')\n",
    "ax.scatter(D, I, alpha=0.4);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate synthetic data\n",
210
    "We first need a set of coefficients for the field. At the time of writing this, IGRF-13 [1] had just been released and we take the reported coefficients as a reference model. You can get it [here](https://www.ngdc.noaa.gov/IAGA/vmod/coeffs/igrf13coeffs.txt). Place the file in the `/dat/` folder."
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "IGRF = pd.read_csv('../dat/igrf13coeffs.txt', header=0, delim_whitespace=True, skiprows=3)\n",
    "coeffs = IGRF[['2020.0']].to_numpy().flatten()\n",
    "# retrieve the maximal degree using pyfield and the index of the last entry in coeffs\n",
    "l_max = pyfield.i2lm_l(len(coeffs)-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we generate random points as points of observation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ran_sph(npoints, r=1., ndim=3, nocluster=True):\n",
    "    \"\"\" Sample random points on the ndim-sphere.\n",
    "\n",
    "    Parameters:\n",
    "    -----------\n",
    "    npoints : int\n",
    "        The number of points to sample\n",
    "    r : float, optional\n",
    "        The radius of the sphere to be sampled on\n",
    "    ndim : int, optional\n",
    "        The dimension of the sphere\n",
    "    nocluster : bool, optional\n",
    "        Whether toexclude points sampled outside of the ball, i.e. sample \n",
    "        uniformly distributed\n",
    "\n",
    "    Returns:\n",
    "    --------\n",
    "        Array of shape (ndim, npoints) including the sampled points.\n",
    "\n",
    "    References:\n",
    "    -----------\n",
    "    [1] http://mathworld.wolfram.com/SpherePointPicking.html\n",
    "    [2] http://mathworld.wolfram.com/HyperspherePointPicking.html\n",
    "    \"\"\"\n",
    "\n",
    "    vec = np.random.randn(ndim, npoints)\n",
    "    if nocluster:\n",
    "        n = np.linalg.norm(vec, axis=0)\n",
    "        vec = vec[:, n <= 1.]\n",
    "        while vec.shape[1] < npoints:\n",
    "            ad = np.random.randn(ndim, npoints)\n",
    "            n = np.linalg.norm(ad, axis=0)\n",
    "            ad = ad[:, n <= 1.]\n",
    "            vec = np.concatenate((vec, ad), axis=1)\n",
    "        if vec.shape[1] > npoints:\n",
    "            vec = vec[:, 0:npoints]\n",
    "    vec /= np.linalg.norm(vec, axis=0)\n",
    "    if npoints == 1:\n",
    "        vec = vec.flatten()\n",
    "    return vec*r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "if real_locs:\n",
    "    # load reference data\n",
    "    pars = load('../examples/Example_Parfile.py')\n",
    "    # ungroup the data\n",
    "    ref_data = pars.data.filter(lambda x: True)\n",
    "    ref_data.reset_index(inplace=True)\n",
    "    n_points = len(ref_data)\n",
    "\n",
    "    x_obs = np.zeros((3, n_points), order='F')\n",
    "    x_obs[0] = ref_data['co-lat']\n",
    "    x_obs[1] = ref_data['lon']\n",
    "    x_obs[2] = ref_data['rad']\n",
    "else:\n",
    "    x_obs = ran_sph(n_points, r=pyfield.REARTH)\n",
    "    # transform x_obs from cartesian coordinates to co-lat, lon, rad\n",
    "    pyfield.mapLoc(fromSys=pyfield.SYS_GEO,\n",
    "                   fromForm=pyfield.COOR_CAR,\n",
    "                   toSys=pyfield.SYS_GEO,\n",
    "                   toForm=pyfield.COOR_CLR,\n",
    "                   t=0,\n",
    "                   x=np.asfortranarray(x_obs))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we use the `pyfield` library to get the basis functions and generate a field from the coefficients."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "dspharm = np.empty((len(coeffs), 3*n_points), order='F')\n",
    "pyfield.dspharm(src=pyfield.SOURCE_INTERNAL,\n",
    "                gSys=pyfield.SYS_GEO,\n",
    "                atSys=pyfield.SYS_GEO,\n",
    "                atForm=pyfield.COOR_CLR,\n",
    "                bSys=pyfield.SYS_GEO,\n",
    "                bForm=pyfield.FIELD_NED,\n",
    "                lmax=l_max,\n",
    "                R=pyfield.REARTH,\n",
    "                t=0.,\n",
    "                at=x_obs,\n",
    "                B=dspharm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The fields $N, E, Z$ components are then easily calculated using a dot product with the coefficients."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "field_obs = np.dot(coeffs, dspharm).reshape(-1, 3).T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using a utility function from `CORBASS`, we can transform these to $D,I,F$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "decs, incs, ints = nez2dif(*field_obs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we set up error levels for the components and add some synthetic noise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "if noise:\n",
    "    mus = np.array([np.cos(np.deg2rad(incs))*np.cos(np.deg2rad(decs)),\n",
    "                    np.cos(np.deg2rad(incs))*np.sin(np.deg2rad(decs)),\n",
    "                    np.sin(np.deg2rad(incs))])\n",
    "\n",
    "    # the angular components retrieve an error from the Fisher-vonMises distribution\n",
    "    # the intensities retrieve an error from the gamma distribution\n",
    "    for it, mu, d, i, f in zip(np.arange(n_points), mus.T, decs, incs, ints):\n",
    "        samp = sample_Fisher(1, mu=mu, kappa=kappa)\n",
    "        decs[it] = np.rad2deg(np.arctan2(samp[1], samp[0]))\n",
    "        incs[it] = np.rad2deg(np.arctan2(samp[2], np.sqrt(samp[0]**2 + samp[1]**2)))\n",
    "        b = ints[it]/dint**2\n",
    "        a = ints[it] * b\n",
    "        ints[it] = gamma.rvs(a=a, scale=1./b)\n",
    "\n",
    "# mimic some incompleteness in the data\n",
    "if r_inc != 0.:\n",
    "    if real_locs:\n",
    "        # incompleteness of reference data\n",
    "        decs[ref_data.query('not (D==D)').index] = np.nan\n",
    "        incs[ref_data.query('not (I==I)').index] = np.nan\n",
    "        ints[ref_data.query('not (F==F)').index] = np.nan\n",
    "    else:\n",
    "        if r_inc != 0.:\n",
    "            # generate indices of missing points\n",
    "            ind = np.arange(n_points)\n",
    "            np.random.shuffle(ind)\n",
    "            mnum = np.int(r_inc*n_points)\n",
    "            mind = ind[0:mnum]\n",
    "            # generate a boolean array of triples of True and False for missing\n",
    "            # values at each point\n",
    "            mdist = np.zeros((3, n_points), dtype=bool)\n",
    "            bools = [True, True, False, False]\n",
    "            for j in mind:\n",
    "                np.random.shuffle(bools)\n",
    "                mdist[:, j] = bools[0:3]\n",
    "            # set the missing values to nan\n",
    "            decs[mdist[0]] = np.nan\n",
    "            incs[mdist[1]] = np.nan\n",
    "            ints[mdist[2]] = np.nan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally we build a pandas `DataFrame` from the synthetic data and store it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "synth_data = pd.DataFrame({'co-lat': x_obs[0], \n",
    "                           'lat': 90-x_obs[0],\n",
    "                           'lon': x_obs[1], \n",
    "                           'rad': x_obs[2],\n",
    "                           't': 2020,\n",
    "                           'dt': 0,\n",
    "                           'D': decs, \n",
    "                           'I': incs, \n",
    "                           'F': ints,\n",
    "                           'dD': ddec,\n",
    "                           'dI': dinc,\n",
    "                           'dF': dint})\n",
    "\n",
    "out_frame = synth_data.to_csv()\n",
    "outfile = open(out, \"w\")\n",
    "outfile.write(header + out_frame)\n",
    "outfile.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
460
    "[1] P. Alken, E. Thebault, C. Beggan, H. Amit, J. Aubert, J. Baerenzung, T.N. Bondar,  \n",
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    "      W. Brown, S. Cali, A. Chambodut, A. Chulliat, G. Cox, C. C. Finlay, A. Fournier,  \n",
    "      N. Gillet, A. Grayver, M. Hammer, M. Holschneider, L. Huder, G. Hulot, T. Jager,  \n",
    "      C. Kloss, M. Korte, W. Kuang, A. Kuvshinov, B. Langlais, J.-M. Leger, V. Lesur,  \n",
    "      P. W. Livermore, F. J. Lowes, S. Macmillan, W. Magnes, M. Mandea, S. Marsal,  \n",
    "      J. Matzka, M. C. Metman, T. Minami, A. Morschhauser, J. E. Mound, M. Nair,  \n",
    "      S. Nakano, N. Olsen, F. J. Pavon-Carrasco, V. G. Petrov, G. Ropp, M. Rother,  \n",
    "      T. J. Sabaka, S. Sanchez, D. Saturnino, N. Schnepf, X. Shen, C. Stolle,  \n",
    "      A. Tangborn, L. Tner-Clausen, H. Toh, J. M. Torta, J. Varner, P. Vigneron,  \n",
    "      F. Vervelidou, I. Wardinski, J. Wicht, A. Woods, Y. Yang, Z. Zeren and B. Zhou,  \n",
    "      \"International Geomagnetic Reference Field: the thirteenth generation\",  \n",
    "      submitted to Earth, Planets and Space, see also:  \n",
    "      https://www.ngdc.noaa.gov/IAGA/vmod/igrf.html"
   ]
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}