Wiener_Filter.ipynb 19.8 KB
Newer Older
Philipp Arras's avatar
Philipp Arras committed
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's avatar
tweaks    
Martin Reinecke committed
27
    "Typically, $s$ is a continuous field, $d$ a discrete data vector. Particularly, $R$ is not invertible.\n",
Philipp Arras's avatar
Philipp Arras committed
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's avatar
tweaks    
Martin Reinecke committed
34
    "NIFTy (Numerical Information Field Theory) is a Python framework in which IFT problems can be tackled easily.\n",
Philipp Arras's avatar
Philipp Arras committed
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's avatar
tweaks    
Martin Reinecke committed
40
    "- **Operators**: Acting on fields."
Philipp Arras's avatar
Philipp Arras committed
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's avatar
tweaks    
Martin Reinecke committed
83
    "- One-dimensional signal with power spectrum: $$P(k) = P_0\\,\\left(1+\\left(\\frac{k}{k_0}\\right)^2\\right)^{-\\gamma /2},$$\n",
Philipp Arras's avatar
Philipp Arras committed
84
    "with $P_0 = 0.2, k_0 = 5, \\gamma = 4$. Recall: $P(k)$ defines an isotropic and homogeneous $S$.\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
85
    "- $N = 0.2 \\cdot \\mathbb{1}$.\n",
Martin Reinecke's avatar
Martin Reinecke committed
86
87
    "- Number of data points $N_{pix} = 512$.\n",
    "- reconstruction in harmonic space.\n",
Philipp Arras's avatar
Philipp Arras committed
88
    "- Response operator:\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
89
    "$$R = FFT_{\\text{harmonic} \\rightarrow \\text{position}}$$\n"
Philipp Arras's avatar
Philipp Arras committed
90
91
92
93
94
95
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
96
    "collapsed": true,
Philipp Arras's avatar
Philipp Arras committed
97
98
99
100
101
102
103
104
105
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "N_pixels = 512     # Number of pixels\n",
    "\n",
    "def pow_spec(k):\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
106
107
    "    P0, k0, gamma = [.2, 5, 4]\n",
    "    return P0 / ((1. + (k/k0)**2)**(gamma / 2))"
Philipp Arras's avatar
Philipp Arras committed
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
   ]
  },
  {
   "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's avatar
tweaks    
Martin Reinecke committed
143
    "np.random.seed(40)\n",
144
145
146
    "import nifty4 as ift\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
Philipp Arras's avatar
Philipp Arras committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Implement Propagator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
164
    "collapsed": true,
Philipp Arras's avatar
Philipp Arras committed
165
166
167
168
169
170
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
171
    "def Curvature(R, N, Sh):\n",
Martin Reinecke's avatar
Martin Reinecke committed
172
    "    IC = ift.GradientNormController(iteration_limit=50000,\n",
173
174
    "                                    tol_abs_gradnorm=0.1)\n",
    "    inverter = ift.ConjugateGradient(controller=IC)\n",
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
175
176
177
    "    # WienerFilterCurvature is (R.adjoint*N.inverse*R + Sh.inverse) plus some handy\n",
    "    # helper methods.\n",
    "    return ift.library.WienerFilterCurvature(R,N,Sh,inverter)\n"
Philipp Arras's avatar
Philipp Arras committed
178
179
180
181
182
183
184
185
186
187
188
189
190
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "### Conjugate Gradient Preconditioning\n",
    "\n",
    "- $D$ is defined via:\n",
Martin Reinecke's avatar
Martin Reinecke committed
191
    "$$D^{-1} = \\mathcal S_h^{-1} + R^\\dagger N^{-1} R.$$\n",
Philipp Arras's avatar
Philipp Arras committed
192
193
    "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's avatar
Martin Reinecke committed
194
    "<!--\n",
Philipp Arras's avatar
Philipp Arras committed
195
196
197
198
199
200
    "- One can define the *condition number* of a non-singular and normal matrix $A$:\n",
    "$$\\kappa (A) := \\frac{|\\lambda_{\\text{max}}|}{|\\lambda_{\\text{min}}|},$$\n",
    "where $\\lambda_{\\text{max}}$ and $\\lambda_{\\text{min}}$ are the largest and smallest eigenvalue of $A$, respectively.\n",
    "\n",
    "- The larger $\\kappa$ the slower Conjugate Gradient.\n",
    "\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
201
    "- By default, conjugate gradient solves: $D^{-1} m = j$ for $m$, where $D^{-1}$ can be badly conditioned. If one knows a non-singular matrix $T$ for which $TD^{-1}$ is better conditioned, one can solve the equivalent problem:\n",
Philipp Arras's avatar
Philipp Arras committed
202
203
204
205
206
    "$$\\tilde A m = \\tilde j,$$\n",
    "where $\\tilde A = T D^{-1}$ and $\\tilde j = Tj$.\n",
    "\n",
    "- In our case $S^{-1}$ is responsible for the bad conditioning of $D$ depending on the chosen power spectrum. Thus, we choose\n",
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
207
208
    "$$T = \\mathcal F^\\dagger S_h^{-1} \\mathcal F.$$\n",
    "-->"
Philipp Arras's avatar
Philipp Arras committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
   ]
  },
  {
   "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,
228
229
230
   "metadata": {
    "collapsed": true
   },
Philipp Arras's avatar
Philipp Arras committed
231
232
   "outputs": [],
   "source": [
233
234
235
236
    "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's avatar
Philipp Arras committed
237
238
    "\n",
    "# Operators\n",
239
240
    "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's avatar
Philipp Arras committed
241
242
    "\n",
    "# Fields and data\n",
243
244
    "sh = ift.power_synthesize(ift.PS_field(p_space, pow_spec),real_signal=True)\n",
    "noiseless_data=R(sh)\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
245
    "noise_amplitude = np.sqrt(0.2)\n",
246
247
248
    "N = ift.ScalingOperator(noise_amplitude**2, s_space)\n",
    "\n",
    "n = ift.Field.from_random(domain=s_space, random_type='normal',\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
249
    "                          std=noise_amplitude, mean=0)\n",
250
251
    "d = noiseless_data + n\n",
    "j = R.adjoint_times(N.inverse_times(d))\n",
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
252
253
    "curv = Curvature(R=R, N=N, Sh=Sh)\n",
    "D = curv.inverse"
Philipp Arras's avatar
Philipp Arras committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Run Wiener Filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
271
    "collapsed": true,
Philipp Arras's avatar
Philipp Arras committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    "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": {
296
    "collapsed": true,
Philipp Arras's avatar
Philipp Arras committed
297
298
299
300
301
302
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
303
304
305
306
    "s_power = ift.power_analyze(sh)\n",
    "m_power = ift.power_analyze(m)\n",
    "s_power_data = s_power.val.real\n",
    "m_power_data = m_power.val.real\n",
Philipp Arras's avatar
Philipp Arras committed
307
308
    "\n",
    "# Get signal data and reconstruction data\n",
309
310
    "s_data = HT(sh).val.real\n",
    "m_data = HT(m).val.real\n",
Philipp Arras's avatar
Philipp Arras committed
311
    "\n",
312
    "d_data = d.val.real"
Philipp Arras's avatar
Philipp Arras committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
   ]
  },
  {
   "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's avatar
tweaks    
Martin Reinecke committed
336
    "plt.figure(figsize=(15,10))\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
337
    "plt.plot(s_data, 'g', label=\"Signal\")\n",
Philipp Arras's avatar
Philipp Arras committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
    "plt.plot(d_data, 'k+', label=\"Data\")\n",
    "plt.plot(m_data, 'r', label=\"Reconstruction\")\n",
    "plt.title(\"Reconstruction\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
355
    "plt.figure(figsize=(15,10))\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
356
    "plt.plot(s_data - s_data, 'g', label=\"Signal\")\n",
Philipp Arras's avatar
Philipp Arras committed
357
358
    "plt.plot(d_data - s_data, 'k+', label=\"Data\")\n",
    "plt.plot(m_data - s_data, 'r', label=\"Reconstruction\")\n",
359
    "plt.axhspan(-noise_amplitude,noise_amplitude, facecolor='0.9', alpha=.5)\n",
Philipp Arras's avatar
Philipp Arras committed
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
    "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's avatar
tweaks    
Martin Reinecke committed
386
    "plt.figure(figsize=(15,10))\n",
Philipp Arras's avatar
Philipp Arras committed
387
388
389
390
391
392
    "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",
    "plt.plot(xs, pow_spec(xs), label=\"True Power Spectrum\", linewidth=.7, color='k')\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
393
    "plt.plot(s_power_data, 'g', label=\"Signal\")\n",
Philipp Arras's avatar
Philipp Arras committed
394
    "plt.plot(m_power_data, 'r', label=\"Reconstruction\")\n",
395
396
    "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's avatar
Philipp Arras committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    "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": {
417
    "collapsed": true,
Philipp Arras's avatar
Philipp Arras committed
418
419
420
421
422
423
424
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Operators\n",
425
426
    "Sh = ift.create_power_operator(h_space, power_spectrum=pow_spec)\n",
    "N = ift.ScalingOperator(noise_amplitude**2,s_space)\n",
Philipp Arras's avatar
Philipp Arras committed
427
428
429
    "# R is defined below\n",
    "\n",
    "# Fields\n",
430
431
432
433
    "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's avatar
Philipp Arras committed
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": {
451
    "collapsed": true,
Philipp Arras's avatar
Philipp Arras committed
452
453
454
455
456
457
458
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "l = int(N_pixels * 0.2)\n",
459
    "h = int(N_pixels * 0.2 * 2)\n",
Philipp Arras's avatar
Philipp Arras committed
460
    "\n",
461
    "mask = ift.Field(s_space, val=1)\n",
Philipp Arras's avatar
Philipp Arras committed
462
463
    "mask.val[ l : h] = 0\n",
    "\n",
464
    "R = ift.DiagonalOperator(mask)*HT\n",
Philipp Arras's avatar
Philipp Arras committed
465
466
    "n.val[l:h] = 0\n",
    "\n",
467
    "d = R(sh) + n"
Philipp Arras's avatar
Philipp Arras committed
468
469
470
471
472
473
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
474
    "collapsed": true,
Philipp Arras's avatar
Philipp Arras committed
475
476
477
478
479
480
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
481
482
    "curv = Curvature(R=R, N=N, Sh=Sh)\n",
    "D = curv.inverse\n",
Philipp Arras's avatar
Philipp Arras committed
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
    "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": {
502
    "scrolled": true
Philipp Arras's avatar
Philipp Arras committed
503
504
505
   },
   "outputs": [],
   "source": [
506
507
    "sc = ift.probing.utils.StatCalculator()\n",
    "for i in range(200):\n",
508
    "    print(i)\n",
509
510
511
    "    sc.add(HT(curv.generate_posterior_sample()))\n",
    "\n",
    "m_var = sc.var"
Philipp Arras's avatar
Philipp Arras committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "### Get data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
529
    "collapsed": true,
Philipp Arras's avatar
Philipp Arras committed
530
531
532
533
534
535
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
536
537
538
539
    "s_power = ift.power_analyze(sh)\n",
    "m_power = ift.power_analyze(m)\n",
    "s_power_data = s_power.val.real\n",
    "m_power_data = m_power.val.real\n",
Philipp Arras's avatar
Philipp Arras committed
540
541
    "\n",
    "# Get signal data and reconstruction data\n",
542
543
544
    "s_data = s.val.real\n",
    "m_data = HT(m).val.real\n",
    "m_var_data = m_var.val.real\n",
Philipp Arras's avatar
Philipp Arras committed
545
    "uncertainty = np.sqrt(np.abs(m_var_data))\n",
546
    "d_data = d.val.real\n",
Philipp Arras's avatar
Philipp Arras committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
    "\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": [
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
562
563
    "plt.figure(figsize=(15,10))\n",
    "plt.plot(s_data, 'g', label=\"Signal\", linewidth=1)\n",
Philipp Arras's avatar
Philipp Arras committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
    "plt.plot(d_data, 'k+', label=\"Data\", alpha=1)\n",
    "plt.axvspan(l, h, facecolor='0.8', alpha=.5)\n",
    "plt.title(\"Incomplete Data\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15,10))\n",
581
    "plt.plot(s_data, 'g', label=\"Signal\", alpha=1, linewidth=4)\n",
Philipp Arras's avatar
Philipp Arras committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
    "plt.plot(d_data, 'k+', label=\"Data\", alpha=.5)\n",
    "plt.plot(m_data, 'r', label=\"Reconstruction\")\n",
    "plt.axvspan(l, h, facecolor='0.8', alpha=.5)\n",
    "plt.fill_between(range(N_pixels), m_data - uncertainty, m_data + uncertainty, facecolor='0')\n",
    "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,
604
605
606
   "metadata": {
    "collapsed": true
   },
Philipp Arras's avatar
Philipp Arras committed
607
608
609
   "outputs": [],
   "source": [
    "N_pixels = 256      # Number of pixels\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
610
    "sigma2 = 2.        # Noise variance\n",
Philipp Arras's avatar
Philipp Arras committed
611
612
613
    "\n",
    "\n",
    "def pow_spec(k):\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
614
    "    P0, k0, gamma = [.2, 2, 4]\n",
Philipp Arras's avatar
Philipp Arras committed
615
616
617
    "    return P0 * (1. + (k/k0)**2)**(- gamma / 2)\n",
    "\n",
    "\n",
618
    "s_space = ift.RGSpace([N_pixels, N_pixels])"
Philipp Arras's avatar
Philipp Arras committed
619
620
621
622
623
624
625
626
627
628
629
630
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
631
    "h_space = s_space.get_default_codomain()\n",
Martin Reinecke's avatar
Martin Reinecke committed
632
    "HT = ift.HarmonicTransformOperator(h_space,s_space)\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
633
    "p_space = ift.PowerSpace(h_space)\n",
Philipp Arras's avatar
Philipp Arras committed
634
635
    "\n",
    "# Operators\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
636
637
    "Sh = ift.create_power_operator(h_space, power_spectrum=pow_spec)\n",
    "N = ift.ScalingOperator(sigma2,s_space)\n",
Philipp Arras's avatar
Philipp Arras committed
638
639
    "\n",
    "# Fields and data\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
640
641
    "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's avatar
Philipp Arras committed
642
643
644
645
    "                      std=np.sqrt(sigma2), mean=0)\n",
    "\n",
    "# Lose some data\n",
    "\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
646
647
    "l = int(N_pixels * 0.33)\n",
    "h = int(N_pixels * 0.33 * 2)\n",
Philipp Arras's avatar
Philipp Arras committed
648
    "\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
649
    "mask = ift.Field(s_space, val=1)\n",
Philipp Arras's avatar
Philipp Arras committed
650
651
    "mask.val[l:h,l:h] = 0\n",
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
652
    "R = ift.DiagonalOperator(mask)*HT\n",
Philipp Arras's avatar
Philipp Arras committed
653
    "n.val[l:h, l:h] = 0\n",
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
654
655
    "curv = Curvature(R=R, N=N, Sh=Sh)\n",
    "D = curv.inverse\n",
Philipp Arras's avatar
Philipp Arras committed
656
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
657
    "d = R(sh) + n\n",
Philipp Arras's avatar
Philipp Arras committed
658
659
660
661
662
663
    "j = R.adjoint_times(N.inverse_times(d))\n",
    "\n",
    "# Run Wiener filter\n",
    "m = D(j)\n",
    "\n",
    "# Uncertainty\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
664
665
    "sc = ift.probing.utils.StatCalculator()\n",
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
666
667
    "IC = ift.GradientNormController(iteration_limit=50000,\n",
    "                                tol_abs_gradnorm=0.1)\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
668
    "inverter = ift.ConjugateGradient(controller=IC)\n",
Martin Reinecke's avatar
Martin Reinecke committed
669
    "curv = ift.library.wiener_filter_curvature.WienerFilterCurvature(R,N,Sh,inverter)\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
670
671
    "\n",
    "for i in range(20):\n",
672
    "    print(i)\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
673
674
675
    "    sc.add(HT(curv.generate_posterior_sample()))\n",
    "\n",
    "m_var = sc.var\n",
Philipp Arras's avatar
Philipp Arras committed
676
677
    "\n",
    "# Get data\n",
Martin Reinecke's avatar
Martin Reinecke committed
678
679
680
681
682
683
684
685
    "s_power = ift.power_analyze(sh)\n",
    "m_power = ift.power_analyze(m)\n",
    "s_power_data = s_power.val.real\n",
    "m_power_data = m_power.val.real\n",
    "s_data = HT(sh).val.real\n",
    "m_data = HT(m).val.real\n",
    "m_var_data = m_var.val.real\n",
    "d_data = d.val.real\n",
Philipp Arras's avatar
Philipp Arras committed
686
687
688
689
690
691
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
    "\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",
    "fig, axes = plt.subplots(2, 2, figsize=(15, 15))\n",
    "\n",
    "data = [s_data, m_data, s_data - m_data, uncertainty]\n",
    "caption = [\"Signal\", \"Reconstruction\", \"Residuals\", \"Uncertainty Map\"]\n",
    "\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's avatar
tweaks    
Martin Reinecke committed
771
    "plt.figure(figsize=(15,10))\n",
Philipp Arras's avatar
Philipp Arras committed
772
    "plt.imshow(precise.astype(float), cmap=\"brg\")\n",
Martin Reinecke's avatar
Martin Reinecke committed
773
    "plt.colorbar()"
Philipp Arras's avatar
Philipp Arras committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
   ]
  },
  {
   "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's avatar
tweaks    
Martin Reinecke committed
789
    "NIFTy v4 **more or less stable!**"
Philipp Arras's avatar
Philipp Arras committed
790
791
792
793
794
795
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
796
   "display_name": "Python 3",
Philipp Arras's avatar
Philipp Arras committed
797
   "language": "python",
798
   "name": "python3"
Philipp Arras's avatar
Philipp Arras committed
799
800
801
802
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
803
    "version": 3
Philipp Arras's avatar
Philipp Arras committed
804
805
806
807
808
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
809
810
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
Philipp Arras's avatar
Philipp Arras committed
811
812
813
814
815
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}