getting_started_0.ipynb 17.6 KB
Newer Older
Philipp Arras's avatar
Philipp Arras committed
1
2
3
4
5
6
7
8
9
10
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
11
    "# Code example: Wiener filter"
Philipp Arras's avatar
Philipp Arras committed
12
13
14
15
16
17
18
19
20
21
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
22
    "## Introduction\n",
Philipp Arras's avatar
Philipp Arras committed
23
24
25
26
    "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
    "\n",
    "IFT aims at **inverting** the above uninvertible problem in the **best possible way** using Bayesian statistics.\n",
    "\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
31
    "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
32
33
34
35
36
    "\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
37
    "- **Operators**: Acting on fields."
Philipp Arras's avatar
Philipp Arras committed
38
39
40
41
42
43
44
45
46
47
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
48
    "## Wiener filter on one-dimensional fields\n",
Philipp Arras's avatar
Philipp Arras committed
49
50
51
52
53
54
55
56
57
    "\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",
Philipp Arras's avatar
Philipp Arras committed
58
    "$$\\mathcal P (s|d) \\propto P(s,d) = \\mathcal G(d-Rs,N) \\,\\mathcal G(s,S) \\propto \\mathcal G (s-m,D) $$\n",
Philipp Arras's avatar
Philipp Arras committed
59
60
    "\n",
    "where\n",
Philipp Arras's avatar
Philipp Arras committed
61
62
63
    "$$m = Dj$$\n",
    "with\n",
    "$$D = (S^{-1} +R^\\dagger N^{-1} R)^{-1} , \\quad j = R^\\dagger N^{-1} d.$$\n",
Philipp Arras's avatar
Philipp Arras committed
64
65
66
67
68
69
70
71
72
73
74
75
    "\n",
    "Let us implement this in NIFTy!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
76
    "### In NIFTy\n",
Philipp Arras's avatar
Philipp Arras committed
77
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
78
79
    "- 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's avatar
tweaks    
Martin Reinecke committed
80
    "- $N = 0.2 \\cdot \\mathbb{1}$.\n",
Martin Reinecke's avatar
Martin Reinecke committed
81
82
    "- Number of data points $N_{pix} = 512$.\n",
    "- reconstruction in harmonic space.\n",
Philipp Arras's avatar
Philipp Arras committed
83
    "- Response operator:\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
84
    "$$R = FFT_{\\text{harmonic} \\rightarrow \\text{position}}$$\n"
Philipp Arras's avatar
Philipp Arras committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
   ]
  },
  {
   "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's avatar
tweaks    
Martin Reinecke committed
100
101
    "    P0, k0, gamma = [.2, 5, 4]\n",
    "    return P0 / ((1. + (k/k0)**2)**(gamma / 2))"
Philipp Arras's avatar
Philipp Arras committed
102
103
104
105
106
107
108
109
110
111
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
112
    "### Implementation"
Philipp Arras's avatar
Philipp Arras committed
113
114
115
116
117
118
119
120
121
122
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
123
    "#### Import Modules"
Philipp Arras's avatar
Philipp Arras committed
124
125
126
127
128
129
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
Martin Reinecke's avatar
Martin Reinecke committed
130
    "scrolled": true,
Philipp Arras's avatar
Philipp Arras committed
131
132
133
134
135
136
137
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
Philipp Arras's avatar
Philipp Arras committed
138
    "import nifty8 as ift\n",
139
    "import matplotlib.pyplot as plt\n",
Philipp Arras's avatar
Philipp Arras committed
140
    "plt.style.use(\"seaborn-notebook\")"
Philipp Arras's avatar
Philipp Arras committed
141
142
143
144
145
146
147
148
149
150
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
151
    "#### Implement Propagator"
Philipp Arras's avatar
Philipp Arras committed
152
153
154
155
156
157
158
159
160
161
162
163
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
164
    "def Curvature(R, N, Sh):\n",
Martin Reinecke's avatar
Martin Reinecke committed
165
    "    IC = ift.GradientNormController(iteration_limit=50000,\n",
166
    "                                    tol_abs_gradnorm=0.1)\n",
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
167
168
    "    # WienerFilterCurvature is (R.adjoint*N.inverse*R + Sh.inverse) plus some handy\n",
    "    # helper methods.\n",
169
    "    return ift.WienerFilterCurvature(R,N,Sh,iteration_controller=IC,iteration_controller_sampling=IC)"
Philipp Arras's avatar
Philipp Arras committed
170
171
172
173
174
175
176
177
178
179
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
180
    "#### Conjugate Gradient Preconditioning\n",
Philipp Arras's avatar
Philipp Arras committed
181
182
    "\n",
    "- $D$ is defined via:\n",
Martin Reinecke's avatar
Martin Reinecke committed
183
    "$$D^{-1} = \\mathcal S_h^{-1} + R^\\dagger N^{-1} R.$$\n",
Philipp Arras's avatar
Philipp Arras committed
184
185
    "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
186
    "<!--\n",
Philipp Arras's avatar
Philipp Arras committed
187
188
189
190
191
192
    "- 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
193
    "- 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
194
195
196
197
198
    "$$\\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
199
200
    "$$T = \\mathcal F^\\dagger S_h^{-1} \\mathcal F.$$\n",
    "-->"
Philipp Arras's avatar
Philipp Arras committed
201
202
203
204
205
206
207
208
209
210
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
211
    "#### Generate Mock data\n",
Philipp Arras's avatar
Philipp Arras committed
212
213
214
215
216
217
218
219
    "\n",
    "- Generate a field $s$ and $n$ with given covariances.\n",
    "- Calculate $d$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Martin Reinecke's avatar
Martin Reinecke committed
220
221
222
   "metadata": {
    "scrolled": true
   },
Philipp Arras's avatar
Philipp Arras committed
223
224
   "outputs": [],
   "source": [
225
226
227
    "s_space = ift.RGSpace(N_pixels)\n",
    "h_space = s_space.get_default_codomain()\n",
    "HT = ift.HarmonicTransformOperator(h_space, target=s_space)\n",
Philipp Arras's avatar
Philipp Arras committed
228
229
    "\n",
    "# Operators\n",
230
    "Sh = ift.create_power_operator(h_space, power_spectrum=pow_spec)\n",
Philipp Arras's avatar
Philipp Arras committed
231
    "R = HT # @ ift.create_harmonic_smoothing_operator((h_space,), 0, 0.02)\n",
Philipp Arras's avatar
Philipp Arras committed
232
233
    "\n",
    "# Fields and data\n",
Philipp Arras's avatar
Philipp Arras committed
234
    "sh = Sh.draw_sample_with_dtype(dtype=np.float64)\n",
235
    "noiseless_data=R(sh)\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
236
    "noise_amplitude = np.sqrt(0.2)\n",
237
    "N = ift.ScalingOperator(s_space, noise_amplitude**2)\n",
238
239
    "\n",
    "n = ift.Field.from_random(domain=s_space, random_type='normal',\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
240
    "                          std=noise_amplitude, mean=0)\n",
241
242
    "d = noiseless_data + n\n",
    "j = R.adjoint_times(N.inverse_times(d))\n",
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
243
244
    "curv = Curvature(R=R, N=N, Sh=Sh)\n",
    "D = curv.inverse"
Philipp Arras's avatar
Philipp Arras committed
245
246
247
248
249
250
251
252
253
254
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
255
    "#### Run Wiener Filter"
Philipp Arras's avatar
Philipp Arras committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "m = D(j)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
279
    "#### Results"
Philipp Arras's avatar
Philipp Arras committed
280
281
282
283
284
285
286
287
288
289
290
291
292
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "# Get signal data and reconstruction data\n",
Martin Reinecke's avatar
Martin Reinecke committed
293
294
295
    "s_data = HT(sh).val\n",
    "m_data = HT(m).val\n",
    "d_data = d.val\n",
Philipp Arras's avatar
Philipp Arras committed
296
    "\n",
297
    "plt.plot(s_data, 'r', label=\"Signal\", linewidth=2)\n",
Martin Reinecke's avatar
Martin Reinecke committed
298
    "plt.plot(d_data, 'k.', label=\"Data\")\n",
299
    "plt.plot(m_data, 'k', label=\"Reconstruction\",linewidth=2)\n",
Philipp Arras's avatar
Philipp Arras committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
    "plt.title(\"Reconstruction\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
315
    "plt.plot(s_data - s_data, 'r', label=\"Signal\", linewidth=2)\n",
Martin Reinecke's avatar
Martin Reinecke committed
316
    "plt.plot(d_data - s_data, 'k.', label=\"Data\")\n",
317
    "plt.plot(m_data - s_data, 'k', label=\"Reconstruction\",linewidth=2)\n",
318
    "plt.axhspan(-noise_amplitude,noise_amplitude, facecolor='0.9', alpha=.5)\n",
Philipp Arras's avatar
Philipp Arras committed
319
320
321
322
323
324
325
326
327
328
329
330
331
    "plt.title(\"Residuals\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
332
    "#### Power Spectrum"
Philipp Arras's avatar
Philipp Arras committed
333
334
335
336
337
338
339
340
341
342
343
344
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
Martin Reinecke committed
345
346
    "s_power_data = ift.power_analyze(sh).val\n",
    "m_power_data = ift.power_analyze(m).val\n",
Philipp Arras's avatar
Philipp Arras committed
347
348
349
350
351
    "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's avatar
Martin Reinecke committed
352
353
354
    "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",
355
356
    "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
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
    "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",
384
    "Sh = ift.create_power_operator(h_space, power_spectrum=pow_spec)\n",
385
    "N = ift.ScalingOperator(s_space, noise_amplitude**2)\n",
Philipp Arras's avatar
Philipp Arras committed
386
387
388
    "# R is defined below\n",
    "\n",
    "# Fields\n",
Philipp Arras's avatar
Philipp Arras committed
389
    "sh = Sh.draw_sample_with_dtype(dtype=np.float64)\n",
390
391
392
    "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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
   ]
  },
  {
   "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",
417
    "h = int(N_pixels * 0.2 * 2)\n",
Philipp Arras's avatar
Philipp Arras committed
418
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
419
420
    "mask = np.full(s_space.shape, 1.)\n",
    "mask[l:h] = 0\n",
Martin Reinecke's avatar
Martin Reinecke committed
421
    "mask = ift.Field.from_raw(s_space, mask)\n",
Philipp Arras's avatar
Philipp Arras committed
422
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
423
    "R = ift.DiagonalOperator(mask)(HT)\n",
Martin Reinecke's avatar
Martin Reinecke committed
424
    "n = n.val_rw()\n",
Martin Reinecke's avatar
Martin Reinecke committed
425
    "n[l:h] = 0\n",
Martin Reinecke's avatar
Martin Reinecke committed
426
    "n = ift.Field.from_raw(s_space, n)\n",
Philipp Arras's avatar
Philipp Arras committed
427
    "\n",
428
    "d = R(sh) + n"
Philipp Arras's avatar
Philipp Arras committed
429
430
431
432
433
434
435
436
437
438
439
440
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
441
442
    "curv = Curvature(R=R, N=N, Sh=Sh)\n",
    "D = curv.inverse\n",
Philipp Arras's avatar
Philipp Arras committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    "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": {
462
    "scrolled": true
Philipp Arras's avatar
Philipp Arras committed
463
464
465
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
Martin Reinecke committed
466
    "m_mean, m_var = ift.probe_with_posterior_samples(curv, HT, 200, np.float64)"
Philipp Arras's avatar
Philipp Arras committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
   ]
  },
  {
   "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's avatar
Martin Reinecke committed
491
492
493
    "s_data = s.val\n",
    "m_data = HT(m).val\n",
    "m_var_data = m_var.val\n",
Martin Reinecke's avatar
Martin Reinecke committed
494
    "uncertainty = np.sqrt(m_var_data)\n",
Martin Reinecke's avatar
Martin Reinecke committed
495
    "d_data = d.val_rw()\n",
Philipp Arras's avatar
Philipp Arras committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
    "\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
Martin Reinecke committed
511
512
    "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",
513
    "plt.plot(s_data, 'r', label=\"Signal\", alpha=1, linewidth=2)\n",
Martin Reinecke's avatar
Martin Reinecke committed
514
    "plt.plot(d_data, 'k.', label=\"Data\")\n",
515
    "plt.plot(m_data, 'k', label=\"Reconstruction\", linewidth=2)\n",
Philipp Arras's avatar
Philipp Arras committed
516
517
518
519
520
521
522
523
524
525
526
527
    "plt.title(\"Reconstruction of incomplete data\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
Philipp Arras's avatar
Philipp Arras committed
528
    "## Wiener filter on two-dimensional field"
Philipp Arras's avatar
Philipp Arras committed
529
530
531
532
533
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Martin Reinecke's avatar
Martin Reinecke committed
534
   "metadata": {},
Philipp Arras's avatar
Philipp Arras committed
535
536
537
   "outputs": [],
   "source": [
    "N_pixels = 256      # Number of pixels\n",
Martin Reinecke's avatar
Martin Reinecke committed
538
    "sigma2 = 2.         # Noise variance\n",
Philipp Arras's avatar
Philipp Arras committed
539
540
    "\n",
    "def pow_spec(k):\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
541
    "    P0, k0, gamma = [.2, 2, 4]\n",
Martin Reinecke's avatar
Martin Reinecke committed
542
    "    return P0 * (1. + (k/k0)**2)**(-gamma/2)\n",
Philipp Arras's avatar
Philipp Arras committed
543
    "\n",
544
    "s_space = ift.RGSpace([N_pixels, N_pixels])"
Philipp Arras's avatar
Philipp Arras committed
545
546
547
548
549
550
551
552
553
554
555
556
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
557
    "h_space = s_space.get_default_codomain()\n",
Martin Reinecke's avatar
Martin Reinecke committed
558
    "HT = ift.HarmonicTransformOperator(h_space,s_space)\n",
Philipp Arras's avatar
Philipp Arras committed
559
560
    "\n",
    "# Operators\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
561
    "Sh = ift.create_power_operator(h_space, power_spectrum=pow_spec)\n",
562
    "N = ift.ScalingOperator(s_space, sigma2)\n",
Philipp Arras's avatar
Philipp Arras committed
563
564
    "\n",
    "# Fields and data\n",
Philipp Arras's avatar
Philipp Arras committed
565
    "sh = Sh.draw_sample_with_dtype(dtype=np.float64)\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
566
    "n = ift.Field.from_random(domain=s_space, random_type='normal',\n",
Philipp Arras's avatar
Philipp Arras committed
567
568
569
570
    "                      std=np.sqrt(sigma2), mean=0)\n",
    "\n",
    "# Lose some data\n",
    "\n",
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
571
572
    "l = int(N_pixels * 0.33)\n",
    "h = int(N_pixels * 0.33 * 2)\n",
Philipp Arras's avatar
Philipp Arras committed
573
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
574
575
    "mask = np.full(s_space.shape, 1.)\n",
    "mask[l:h,l:h] = 0.\n",
Martin Reinecke's avatar
Martin Reinecke committed
576
    "mask = ift.Field.from_raw(s_space, mask)\n",
Philipp Arras's avatar
Philipp Arras committed
577
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
578
    "R = ift.DiagonalOperator(mask)(HT)\n",
Martin Reinecke's avatar
Martin Reinecke committed
579
    "n = n.val_rw()\n",
Martin Reinecke's avatar
Martin Reinecke committed
580
    "n[l:h, l:h] = 0\n",
Martin Reinecke's avatar
Martin Reinecke committed
581
    "n = ift.Field.from_raw(s_space, n)\n",
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
582
583
    "curv = Curvature(R=R, N=N, Sh=Sh)\n",
    "D = curv.inverse\n",
Philipp Arras's avatar
Philipp Arras committed
584
    "\n",
Martin Reinecke's avatar
Martin Reinecke committed
585
    "d = R(sh) + n\n",
Philipp Arras's avatar
Philipp Arras committed
586
587
588
589
590
591
    "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
Martin Reinecke committed
592
    "m_mean, m_var = ift.probe_with_posterior_samples(curv, HT, 20, np.float64)\n",
Philipp Arras's avatar
Philipp Arras committed
593
594
    "\n",
    "# Get data\n",
Martin Reinecke's avatar
Martin Reinecke committed
595
596
597
598
    "s_data = HT(sh).val\n",
    "m_data = HT(m).val\n",
    "m_var_data = m_var.val\n",
    "d_data = d.val\n",
Philipp Arras's avatar
Philipp Arras committed
599
600
601
602
603
604
605
606
607
608
609
610
611
    "uncertainty = np.sqrt(np.abs(m_var_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
612
    "cmap = ['magma', 'inferno', 'plasma', 'viridis'][1]\n",
Philipp Arras's avatar
Philipp Arras committed
613
614
615
616
    "\n",
    "mi = np.min(s_data)\n",
    "ma = np.max(s_data)\n",
    "\n",
Philipp Arras's avatar
Philipp Arras committed
617
    "fig, axes = plt.subplots(1, 2)\n",
Philipp Arras's avatar
Philipp Arras committed
618
619
620
621
622
    "\n",
    "data = [s_data, d_data]\n",
    "caption = [\"Signal\", \"Data\"]\n",
    "\n",
    "for ax in axes.flat:\n",
623
    "    im = ax.imshow(data.pop(0), interpolation='nearest', cmap=cmap, vmin=mi,\n",
Philipp Arras's avatar
Philipp Arras committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    "                   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",
Philipp Arras's avatar
Philipp Arras committed
645
    "fig, axes = plt.subplots(3, 2, figsize=(10, 15))\n",
Philipp Arras's avatar
Philipp Arras committed
646
    "sample = HT(curv.draw_sample(from_inverse=True)+m).val\n",
Martin Reinecke's avatar
Martin Reinecke committed
647
    "post_mean = (m_mean + HT(m)).val\n",
Philipp Arras's avatar
Philipp Arras committed
648
    "\n",
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
649
650
    "data = [s_data, m_data, post_mean, sample, s_data - m_data, uncertainty]\n",
    "caption = [\"Signal\", \"Reconstruction\", \"Posterior mean\", \"Sample\", \"Residuals\", \"Uncertainty Map\"]\n",
Philipp Arras's avatar
Philipp Arras committed
651
652
    "\n",
    "for ax in axes.flat:\n",
653
    "    im = ax.imshow(data.pop(0), interpolation='nearest', cmap=cmap, vmin=mi, vmax=ma)\n",
Philipp Arras's avatar
Philipp Arras committed
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
    "    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": [
Martin Reinecke's avatar
Martin Reinecke committed
682
    "precise = (np.abs(s_data-m_data) < uncertainty)\n",
Philipp Arras's avatar
Philipp Arras committed
683
684
685
    "print(\"Error within uncertainty map bounds: \" + str(np.sum(precise) * 100 / N_pixels**2) + \"%\")\n",
    "\n",
    "plt.imshow(precise.astype(float), cmap=\"brg\")\n",
Martin Reinecke's avatar
Martin Reinecke committed
686
    "plt.colorbar()"
Philipp Arras's avatar
Philipp Arras committed
687
688
689
690
691
692
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
693
   "display_name": "Python 3",
Philipp Arras's avatar
Philipp Arras committed
694
   "language": "python",
695
   "name": "python3"
Philipp Arras's avatar
Philipp Arras committed
696
697
698
699
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
700
    "version": 3
Philipp Arras's avatar
Philipp Arras committed
701
702
703
704
705
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
706
   "pygments_lexer": "ipython3",
707
   "version": "3.9.2"
Philipp Arras's avatar
Philipp Arras committed
708
709
710
711
712
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}