Wiener_Filter.ipynb 19.1 KB
 Philipp Arras committed Feb 01, 2018 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 { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# A NIFTy demonstration" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## IFT: Big Picture\n", "IFT starting point:\n", "\n", "$$d = Rs+n$$\n", "\n",  Martin Reinecke committed Feb 04, 2018 27  "Typically, $s$ is a continuous field, $d$ a discrete data vector. Particularly, $R$ is not invertible.\n",  Philipp Arras committed Feb 01, 2018 28 29 30 31 32 33  "\n", "IFT aims at **inverting** the above uninvertible problem in the **best possible way** using Bayesian statistics.\n", "\n", "\n", "## NIFTy\n", "\n",  Martin Reinecke committed Feb 04, 2018 34  "NIFTy (Numerical Information Field Theory) is a Python framework in which IFT problems can be tackled easily.\n",  Philipp Arras committed Feb 01, 2018 35 36 37 38 39  "\n", "Main Interfaces:\n", "\n", "- **Spaces**: Cartesian, 2-Spheres (Healpix, Gauss-Legendre) and their respective harmonic spaces.\n", "- **Fields**: Defined on spaces.\n",  Martin Reinecke committed Feb 04, 2018 40  "- **Operators**: Acting on fields."  Philipp Arras committed Feb 01, 2018 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  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Wiener Filter: Formulae\n", "\n", "### Assumptions\n", "\n", "- $d=Rs+n$, $R$ linear operator.\n", "- $\\mathcal P (s) = \\mathcal G (s,S)$, $\\mathcal P (n) = \\mathcal G (n,N)$ where $S, N$ are positive definite matrices.\n", "\n", "### Posterior\n", "The Posterior is given by:\n", "\n", "$$\\mathcal P (s|d) \\propto P(s,d) = \\mathcal G(d-Rs,N) \\,\\mathcal G(s,S) \\propto \\mathcal G (m,D)$$\n", "\n", "where\n", "\\begin{align}\n", "m &= Dj \\\\\n", "D^{-1}&= (S^{-1} +R^\\dagger N^{-1} R )\\\\\n", "j &= R^\\dagger N^{-1} d\n", "\\end{align}\n", "\n", "Let us implement this in NIFTy!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Wiener Filter: Example\n", "\n",  Martin Reinecke committed Feb 06, 2018 83 84  "- We assume statistical homogeneity and isotropy. Therefore the signal covariance $S$ is diagonal in harmonic space, and is described by a one-dimensional power spectrum, assumed here as $$P(k) = P_0\\,\\left(1+\\left(\\frac{k}{k_0}\\right)^2\\right)^{-\\gamma /2},$$\n", "with $P_0 = 0.2, k_0 = 5, \\gamma = 4$.\n",  Martin Reinecke committed Feb 04, 2018 85  "- $N = 0.2 \\cdot \\mathbb{1}$.\n",  Martin Reinecke committed Feb 04, 2018 86 87  "- Number of data points $N_{pix} = 512$.\n", "- reconstruction in harmonic space.\n",  Philipp Arras committed Feb 01, 2018 88  "- Response operator:\n",  Martin Reinecke committed Feb 04, 2018 89  "$$R = FFT_{\\text{harmonic} \\rightarrow \\text{position}}$$\n"  Philipp Arras committed Feb 01, 2018 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104  ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "N_pixels = 512 # Number of pixels\n", "\n", "def pow_spec(k):\n",  Martin Reinecke committed Feb 04, 2018 105 106  " P0, k0, gamma = [.2, 5, 4]\n", " return P0 / ((1. + (k/k0)**2)**(gamma / 2))"  Philipp Arras committed Feb 01, 2018 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  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Wiener Filter: Implementation" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "### Import Modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "import numpy as np\n",  Martin Reinecke committed Feb 04, 2018 142  "np.random.seed(40)\n",  Martin Reinecke committed Feb 04, 2018 143 144 145  "import nifty4 as ift\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline"  Philipp Arras committed Feb 01, 2018 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Implement Propagator" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [  Martin Reinecke committed Feb 05, 2018 169  "def Curvature(R, N, Sh):\n",  Martin Reinecke committed Feb 04, 2018 170  " IC = ift.GradientNormController(iteration_limit=50000,\n",  Martin Reinecke committed Feb 04, 2018 171 172  " tol_abs_gradnorm=0.1)\n", " inverter = ift.ConjugateGradient(controller=IC)\n",  Martin Reinecke committed Feb 05, 2018 173 174  " # WienerFilterCurvature is (R.adjoint*N.inverse*R + Sh.inverse) plus some handy\n", " # helper methods.\n",  Martin Reinecke committed Feb 06, 2018 175  " return ift.library.WienerFilterCurvature(R,N,Sh,inverter)"  Philipp Arras committed Feb 01, 2018 176 177 178 179 180 181 182 183 184 185 186 187 188  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "### Conjugate Gradient Preconditioning\n", "\n", "- $D$ is defined via:\n",  Martin Reinecke committed Feb 04, 2018 189  "$$D^{-1} = \\mathcal S_h^{-1} + R^\\dagger N^{-1} R.$$\n",  Philipp Arras committed Feb 01, 2018 190 191  "In the end, we want to apply $D$ to $j$, i.e. we need the inverse action of $D^{-1}$. This is done numerically (algorithm: *Conjugate Gradient*). \n", "\n",  Martin Reinecke committed Feb 04, 2018 192  ""  Philipp Arras committed Feb 01, 2018 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Generate Mock data\n", "\n", "- Generate a field $s$ and $n$ with given covariances.\n", "- Calculate $d$." ] }, { "cell_type": "code", "execution_count": null,  Martin Reinecke committed Feb 05, 2018 226  "metadata": {},  Philipp Arras committed Feb 01, 2018 227 228  "outputs": [], "source": [  Martin Reinecke committed Feb 04, 2018 229 230 231 232  "s_space = ift.RGSpace(N_pixels)\n", "h_space = s_space.get_default_codomain()\n", "HT = ift.HarmonicTransformOperator(h_space, target=s_space)\n", "p_space = ift.PowerSpace(h_space)\n",  Philipp Arras committed Feb 01, 2018 233 234  "\n", "# Operators\n",  Martin Reinecke committed Feb 04, 2018 235 236  "Sh = ift.create_power_operator(h_space, power_spectrum=pow_spec)\n", "R = HT #*ift.create_harmonic_smoothing_operator((h_space,), 0, 0.02)\n",  Philipp Arras committed Feb 01, 2018 237 238  "\n", "# Fields and data\n",  Martin Reinecke committed Feb 04, 2018 239 240  "sh = ift.power_synthesize(ift.PS_field(p_space, pow_spec),real_signal=True)\n", "noiseless_data=R(sh)\n",  Martin Reinecke committed Feb 04, 2018 241  "noise_amplitude = np.sqrt(0.2)\n",  Martin Reinecke committed Feb 04, 2018 242 243 244  "N = ift.ScalingOperator(noise_amplitude**2, s_space)\n", "\n", "n = ift.Field.from_random(domain=s_space, random_type='normal',\n",  Martin Reinecke committed Feb 04, 2018 245  " std=noise_amplitude, mean=0)\n",  Martin Reinecke committed Feb 04, 2018 246 247  "d = noiseless_data + n\n", "j = R.adjoint_times(N.inverse_times(d))\n",  Martin Reinecke committed Feb 05, 2018 248 249  "curv = Curvature(R=R, N=N, Sh=Sh)\n", "D = curv.inverse"  Philipp Arras committed Feb 01, 2018 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  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Run Wiener Filter" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "m = D(j)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Create Power Spectra of Signal and Reconstruction" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [  Martin Reinecke committed Feb 04, 2018 297 298  "s_power = ift.power_analyze(sh)\n", "m_power = ift.power_analyze(m)\n",  Martin Reinecke committed Feb 18, 2018 299 300  "s_power_data = s_power.to_global_data()\n", "m_power_data = m_power.to_global_data()\n",  Philipp Arras committed Feb 01, 2018 301 302  "\n", "# Get signal data and reconstruction data\n",  Martin Reinecke committed Feb 18, 2018 303 304  "s_data = HT(sh).to_global_data()\n", "m_data = HT(m).to_global_data()\n",  Philipp Arras committed Feb 01, 2018 305  "\n",  Martin Reinecke committed Feb 18, 2018 306  "d_data = d.to_global_data()"  Philipp Arras committed Feb 01, 2018 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Signal Reconstruction" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [  Martin Reinecke committed Feb 04, 2018 330  "plt.figure(figsize=(15,10))\n",  Martin Reinecke committed Feb 06, 2018 331 332 333  "plt.plot(s_data, 'r', label=\"Signal\", linewidth=3)\n", "plt.plot(d_data, 'k.', label=\"Data\")\n", "plt.plot(m_data, 'k', label=\"Reconstruction\",linewidth=3)\n",  Philipp Arras committed Feb 01, 2018 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348  "plt.title(\"Reconstruction\")\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [  Martin Reinecke committed Feb 04, 2018 349  "plt.figure(figsize=(15,10))\n",  Martin Reinecke committed Feb 06, 2018 350 351 352  "plt.plot(s_data - s_data, 'r', label=\"Signal\", linewidth=3)\n", "plt.plot(d_data - s_data, 'k.', label=\"Data\")\n", "plt.plot(m_data - s_data, 'k', label=\"Reconstruction\",linewidth=3)\n",  Martin Reinecke committed Feb 04, 2018 353  "plt.axhspan(-noise_amplitude,noise_amplitude, facecolor='0.9', alpha=.5)\n",  Philipp Arras committed Feb 01, 2018 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  "plt.title(\"Residuals\")\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Power Spectrum" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [  Martin Reinecke committed Feb 04, 2018 380  "plt.figure(figsize=(15,10))\n",  Philipp Arras committed Feb 01, 2018 381 382 383 384 385  "plt.loglog()\n", "plt.xlim(1, int(N_pixels/2))\n", "ymin = min(m_power_data)\n", "plt.ylim(ymin, 1)\n", "xs = np.arange(1,int(N_pixels/2),.1)\n",  Martin Reinecke committed Feb 06, 2018 386 387 388  "plt.plot(xs, pow_spec(xs), label=\"True Power Spectrum\", color='k',alpha=0.5)\n", "plt.plot(s_power_data, 'r', label=\"Signal\")\n", "plt.plot(m_power_data, 'k', label=\"Reconstruction\")\n",  Martin Reinecke committed Feb 04, 2018 389 390  "plt.axhline(noise_amplitude**2 / N_pixels, color=\"k\", linestyle='--', label=\"Noise level\", alpha=.5)\n", "plt.axhspan(noise_amplitude**2 / N_pixels, ymin, facecolor='0.9', alpha=.5)\n",  Philipp Arras committed Feb 01, 2018 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  "plt.title(\"Power Spectrum\")\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Wiener Filter on Incomplete Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "# Operators\n",  Martin Reinecke committed Feb 04, 2018 418 419  "Sh = ift.create_power_operator(h_space, power_spectrum=pow_spec)\n", "N = ift.ScalingOperator(noise_amplitude**2,s_space)\n",  Philipp Arras committed Feb 01, 2018 420 421 422  "# R is defined below\n", "\n", "# Fields\n",  Martin Reinecke committed Feb 04, 2018 423 424 425 426  "sh = ift.power_synthesize(ift.PS_field(p_space,pow_spec),real_signal=True)\n", "s = HT(sh)\n", "n = ift.Field.from_random(domain=s_space, random_type='normal',\n", " std=noise_amplitude, mean=0)"  Philipp Arras committed Feb 01, 2018 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "### Partially Lose Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "l = int(N_pixels * 0.2)\n",  Martin Reinecke committed Feb 04, 2018 451  "h = int(N_pixels * 0.2 * 2)\n",  Philipp Arras committed Feb 01, 2018 452  "\n",  Martin Reinecke committed Feb 18, 2018 453 454 455  "mask = np.full(s_space.shape, 1.)\n", "mask[l:h] = 0\n", "mask = ift.Field.from_global_data(s_space, mask)\n",  Philipp Arras committed Feb 01, 2018 456  "\n",  Martin Reinecke committed Feb 04, 2018 457  "R = ift.DiagonalOperator(mask)*HT\n",  Martin Reinecke committed Feb 18, 2018 458 459 460  "n = n.to_global_data()\n", "n[l:h] = 0\n", "n = ift.Field.from_global_data(s_space, n)\n",  Philipp Arras committed Feb 01, 2018 461  "\n",  Martin Reinecke committed Feb 04, 2018 462  "d = R(sh) + n"  Philipp Arras committed Feb 01, 2018 463 464 465 466 467 468 469 470 471 472 473 474  ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [  Martin Reinecke committed Feb 05, 2018 475 476  "curv = Curvature(R=R, N=N, Sh=Sh)\n", "D = curv.inverse\n",  Philipp Arras committed Feb 01, 2018 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495  "j = R.adjoint_times(N.inverse_times(d))\n", "m = D(j)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Compute Uncertainty\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {  Martin Reinecke committed Feb 04, 2018 496  "scrolled": true  Philipp Arras committed Feb 01, 2018 497 498 499  }, "outputs": [], "source": [  Martin Reinecke committed Feb 18, 2018 500  "m_mean, m_var = ift.probe_with_posterior_samples(curv, HT, 200)"  Philipp Arras committed Feb 01, 2018 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "### Get data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "# Get signal data and reconstruction data\n",  Martin Reinecke committed Feb 18, 2018 525 526 527  "s_data = s.to_global_data()\n", "m_data = HT(m).to_global_data()\n", "m_var_data = m_var.to_global_data()\n",  Martin Reinecke committed Feb 06, 2018 528  "uncertainty = np.sqrt(m_var_data)\n",  Martin Reinecke committed Feb 18, 2018 529  "d_data = d.to_global_data()\n",  Philipp Arras committed Feb 01, 2018 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545  "\n", "# Set lost data to NaN for proper plotting\n", "d_data[d_data == 0] = np.nan" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "fig = plt.figure(figsize=(15,10))\n",  Martin Reinecke committed Feb 06, 2018 546 547 548 549 550  "plt.axvspan(l, h, facecolor='0.8',alpha=0.5)\n", "plt.fill_between(range(N_pixels), m_data - uncertainty, m_data + uncertainty, facecolor='0.5', alpha=0.5)\n", "plt.plot(s_data, 'r', label=\"Signal\", alpha=1, linewidth=3)\n", "plt.plot(d_data, 'k.', label=\"Data\")\n", "plt.plot(m_data, 'k', label=\"Reconstruction\", linewidth=3)\n",  Philipp Arras committed Feb 01, 2018 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568  "plt.title(\"Reconstruction of incomplete data\")\n", "plt.legend()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 2d Example" ] }, { "cell_type": "code", "execution_count": null,  Martin Reinecke committed Feb 05, 2018 569  "metadata": {},  Philipp Arras committed Feb 01, 2018 570 571 572  "outputs": [], "source": [ "N_pixels = 256 # Number of pixels\n",  Martin Reinecke committed Feb 06, 2018 573  "sigma2 = 2. # Noise variance\n",  Philipp Arras committed Feb 01, 2018 574 575  "\n", "def pow_spec(k):\n",  Martin Reinecke committed Feb 04, 2018 576  " P0, k0, gamma = [.2, 2, 4]\n",  Martin Reinecke committed Feb 06, 2018 577  " return P0 * (1. + (k/k0)**2)**(-gamma/2)\n",  Philipp Arras committed Feb 01, 2018 578  "\n",  Martin Reinecke committed Feb 04, 2018 579  "s_space = ift.RGSpace([N_pixels, N_pixels])"  Philipp Arras committed Feb 01, 2018 580 581 582 583 584 585 586 587 588 589 590 591  ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [  Martin Reinecke committed Feb 04, 2018 592  "h_space = s_space.get_default_codomain()\n",  Martin Reinecke committed Feb 04, 2018 593  "HT = ift.HarmonicTransformOperator(h_space,s_space)\n",  Martin Reinecke committed Feb 04, 2018 594  "p_space = ift.PowerSpace(h_space)\n",  Philipp Arras committed Feb 01, 2018 595 596  "\n", "# Operators\n",  Martin Reinecke committed Feb 04, 2018 597 598  "Sh = ift.create_power_operator(h_space, power_spectrum=pow_spec)\n", "N = ift.ScalingOperator(sigma2,s_space)\n",  Philipp Arras committed Feb 01, 2018 599 600  "\n", "# Fields and data\n",  Martin Reinecke committed Feb 04, 2018 601 602  "sh = ift.power_synthesize(ift.PS_field(p_space,pow_spec),real_signal=True)\n", "n = ift.Field.from_random(domain=s_space, random_type='normal',\n",  Philipp Arras committed Feb 01, 2018 603 604 605 606  " std=np.sqrt(sigma2), mean=0)\n", "\n", "# Lose some data\n", "\n",  Martin Reinecke committed Feb 04, 2018 607 608  "l = int(N_pixels * 0.33)\n", "h = int(N_pixels * 0.33 * 2)\n",  Philipp Arras committed Feb 01, 2018 609  "\n",  Martin Reinecke committed Feb 18, 2018 610 611 612  "mask = np.full(s_space.shape, 1.)\n", "mask[l:h,l:h] = 0.\n", "mask = ift.Field.from_global_data(s_space, mask)\n",  Philipp Arras committed Feb 01, 2018 613  "\n",  Martin Reinecke committed Feb 04, 2018 614  "R = ift.DiagonalOperator(mask)*HT\n",  Martin Reinecke committed Feb 18, 2018 615 616 617  "n = n.to_global_data()\n", "n[l:h, l:h] = 0\n", "n = ift.Field.from_global_data(s_space, n)\n",  Martin Reinecke committed Feb 05, 2018 618 619  "curv = Curvature(R=R, N=N, Sh=Sh)\n", "D = curv.inverse\n",  Philipp Arras committed Feb 01, 2018 620  "\n",  Martin Reinecke committed Feb 04, 2018 621  "d = R(sh) + n\n",  Philipp Arras committed Feb 01, 2018 622 623 624 625 626 627  "j = R.adjoint_times(N.inverse_times(d))\n", "\n", "# Run Wiener filter\n", "m = D(j)\n", "\n", "# Uncertainty\n",  Martin Reinecke committed Feb 18, 2018 628  "m_mean, m_var = ift.probe_with_posterior_samples(curv, HT, 20)\n",  Philipp Arras committed Feb 01, 2018 629 630  "\n", "# Get data\n",  Martin Reinecke committed Feb 04, 2018 631 632  "s_power = ift.power_analyze(sh)\n", "m_power = ift.power_analyze(m)\n",  Martin Reinecke committed Feb 18, 2018 633 634 635 636 637 638  "s_power_data = s_power.to_global_data()\n", "m_power_data = m_power.to_global_data()\n", "s_data = HT(sh).to_global_data()\n", "m_data = HT(m).to_global_data()\n", "m_var_data = m_var.to_global_data()\n", "d_data = d.to_global_data()\n",  Philipp Arras committed Feb 01, 2018 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685  "\n", "uncertainty = np.sqrt(np.abs(m_var_data))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "cm = ['magma', 'inferno', 'plasma', 'viridis'][1]\n", "\n", "mi = np.min(s_data)\n", "ma = np.max(s_data)\n", "\n", "fig, axes = plt.subplots(1, 2, figsize=(15, 7))\n", "\n", "data = [s_data, d_data]\n", "caption = [\"Signal\", \"Data\"]\n", "\n", "for ax in axes.flat:\n", " im = ax.imshow(data.pop(0), interpolation='nearest', cmap=cm, vmin=mi,\n", " vmax=ma)\n", " ax.set_title(caption.pop(0))\n", "\n", "fig.subplots_adjust(right=0.8)\n", "cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])\n", "fig.colorbar(im, cax=cbar_ax)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "mi = np.min(s_data)\n", "ma = np.max(s_data)\n", "\n",  Martin Reinecke committed Feb 06, 2018 686  "fig, axes = plt.subplots(3, 2, figsize=(15, 22.5))\n",  Martin Reinecke committed Feb 18, 2018 687 688  "samp1 = HT(curv.draw_sample()+m).to_global_data()\n", "samp2 = HT(curv.draw_sample()+m).to_global_data()\n",  Philipp Arras committed Feb 01, 2018 689  "\n",  Martin Reinecke committed Feb 06, 2018 690 691  "data = [s_data, m_data, samp1, samp2, s_data - m_data, uncertainty]\n", "caption = [\"Signal\", \"Reconstruction\", \"Sample 1\", \"Sample 2\", \"Residuals\", \"Uncertainty Map\"]\n",  Philipp Arras committed Feb 01, 2018 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725  "\n", "for ax in axes.flat:\n", " im = ax.imshow(data.pop(0), interpolation='nearest', cmap=cm, vmin=mi, vmax=ma)\n", " ax.set_title(caption.pop(0))\n", "\n", "fig.subplots_adjust(right=0.8)\n", "cbar_ax = fig.add_axes([.85, 0.15, 0.05, 0.7])\n", "fig.colorbar(im, cax=cbar_ax)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Is the uncertainty map reliable?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "precise = (np.abs(s_data-m_data) < uncertainty )\n", "print(\"Error within uncertainty map bounds: \" + str(np.sum(precise) * 100 / N_pixels**2) + \"%\")\n", "\n",  Martin Reinecke committed Feb 04, 2018 726  "plt.figure(figsize=(15,10))\n",  Philipp Arras committed Feb 01, 2018 727  "plt.imshow(precise.astype(float), cmap=\"brg\")\n",  Martin Reinecke committed Feb 04, 2018 728  "plt.colorbar()"  Philipp Arras committed Feb 01, 2018 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743  ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Start Coding\n", "## NIFTy Repository + Installation guide\n", "\n", "https://gitlab.mpcdf.mpg.de/ift/NIFTy\n", "\n",  Martin Reinecke committed Feb 04, 2018 744  "NIFTy v4 **more or less stable!**"  Philipp Arras committed Feb 01, 2018 745 746 747 748 749 750  ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": {  Martin Reinecke committed Feb 05, 2018 751  "display_name": "Python 2",  Philipp Arras committed Feb 01, 2018 752  "language": "python",  Martin Reinecke committed Feb 05, 2018 753  "name": "python2"  Philipp Arras committed Feb 01, 2018 754 755 756 757  }, "language_info": { "codemirror_mode": { "name": "ipython",  Martin Reinecke committed Feb 05, 2018 758  "version": 2  Philipp Arras committed Feb 01, 2018 759 760 761 762 763  }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python",  Martin Reinecke committed Feb 05, 2018 764 765  "pygments_lexer": "ipython2", "version": "2.7.12"  Philipp Arras committed Feb 01, 2018 766 767 768 769 770  } }, "nbformat": 4, "nbformat_minor": 2 }