Gen_Data.ipynb 16.1 KB
 Maximilian Schanner committed May 06, 2020 1 2 3 4 ``````{ "cells": [ { "cell_type": "code", `````` Stefan Mauerberger committed May 07, 2020 5 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 28 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 29 30 31 32 `````` "metadata": {}, "outputs": [], "source": [ "# seed for reproducability\n", `````` Maximilian Schanner committed May 06, 2020 33 34 `````` "seed = 161\n", "np.random.seed(seed)" `````` Maximilian Schanner committed May 06, 2020 35 36 37 38 39 40 41 42 43 44 45 46 47 `````` ] }, { "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", `````` Stefan Mauerberger committed May 07, 2020 48 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 49 50 51 52 53 `````` "metadata": {}, "outputs": [], "source": [ "# some basic parameters\n", "# the name of the output file\n", `````` Maximilian Schanner committed May 06, 2020 54 `````` "out = '../dat/synth_data_clean_complete.csv'\n", `````` Maximilian Schanner committed May 06, 2020 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", `````` Maximilian Schanner committed May 06, 2020 72 `````` " + f\"dinc={dinc:.2f}, dint={dint:d}, kappa={kappa:.1f}, seed={seed:d}\\n\"" `````` Maximilian Schanner committed May 06, 2020 73 74 75 76 77 78 79 80 81 82 83 `````` ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sampling from the Fisher-von Mises distribution" ] }, { "cell_type": "code", `````` Stefan Mauerberger committed May 07, 2020 84 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 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 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 114 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 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 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 174 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 175 `````` "metadata": {}, `````` Stefan Mauerberger committed May 07, 2020 176 `````` "outputs": [], `````` Maximilian Schanner committed May 06, 2020 177 `````` "source": [ `````` Maximilian Schanner committed May 06, 2020 178 `````` "SF = sample_Fisher(1000, mu=(-1, 0, 0), kappa=kappa);\n", `````` Maximilian Schanner committed May 06, 2020 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 197 `````` "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 directly download the reported coefficients as a reference model." `````` Maximilian Schanner committed May 06, 2020 198 199 200 201 `````` ] }, { "cell_type": "code", `````` Stefan Mauerberger committed May 07, 2020 202 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 203 204 205 `````` "metadata": {}, "outputs": [], "source": [ `````` Stefan Mauerberger committed May 07, 2020 206 `````` "IGRF = pd.read_csv('https://www.ngdc.noaa.gov/IAGA/vmod/coeffs/igrf13coeffs.txt', header=0, delim_whitespace=True, skiprows=3)\n", `````` Maximilian Schanner committed May 06, 2020 207 208 209 210 211 212 213 214 215 216 217 218 219 220 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 221 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 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 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 269 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 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 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 305 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 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 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 332 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 348 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 364 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 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 `````` "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", `````` Stefan Mauerberger committed May 07, 2020 419 `````` "execution_count": null, `````` Maximilian Schanner committed May 06, 2020 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 `````` "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", `````` Maximilian Schanner committed May 06, 2020 447 `````` "[1] P. Alken, E. Thebault, C. Beggan, H. Amit, J. Aubert, J. Baerenzung, T.N. Bondar, \n", `````` Maximilian Schanner committed May 06, 2020 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 `````` " 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", `````` Stefan Mauerberger committed May 07, 2020 479 `````` "version": "3.6.9" `````` Maximilian Schanner committed May 06, 2020 480 481 482 483 484 `````` } }, "nbformat": 4, "nbformat_minor": 2 }``````