Lensing a flat-sky map

using CMBLensing, PyPlot

First we load a simulated unlensed field, $f$, and lensing potential, $\phi$,

@unpack f,ϕ = load_sim(
    θpix  = 2,       # size of the pixels in arcmin
    Nside = 256,     # number of pixels per side in the map
    T     = Float32, # Float32 or Float64 (former is ~twice as fast)
    pol   = :I       # :I for Intensity, :P for polarization, or :IP for both
);

We can lense the map with LenseFlow,

f̃ = LenseFlow(ϕ) * f;

And flip between lensed and unlensed maps,

animate([f,f̃], fps=1)

The difference between lensed and unlensed,

plot(f-f̃);

png

Loading your own data

CMBLensing flat-sky Field objects like f or ϕ are just thin wrappers around arrays. You can get the underlying data arrays for $I(\mathbf{x})$, $Q(\mathbf{x})$, and $U(\mathbf{x})$ with f[:Ix], f[:Qx], and f[:Ux] respectively, or the Fourier coefficients, $I(\mathbf{l})$, $Q(\mathbf{l})$, and $U(\mathbf{l})$ with f[:Il], f[:Ql], and f[:Ul],

mapdata = f[:Ix]
256×256 Matrix{Float32}:
 -137.506    -135.863   -139.938   …  -106.844    -133.718    -141.185
 -162.008    -158.634   -144.207      -123.028    -143.036    -154.226
 -189.142    -187.814   -164.6        -142.948    -153.377    -168.338
 -211.424    -217.855   -198.548      -149.419    -158.325    -180.981
 -224.029    -235.437   -222.779      -142.501    -158.073    -189.343
 -219.66     -231.823   -223.83    …  -131.083    -152.625    -187.048
 -201.404    -211.731   -209.517      -112.112    -136.707    -171.097
 -174.616    -184.069   -186.961       -88.5285   -114.238    -147.41
 -151.912    -161.456   -168.767       -77.7296   -100.553    -129.225
 -135.45     -149.133   -162.283       -83.0712    -97.4815   -117.354
 -111.505    -131.8     -151.59    …   -72.3644    -78.0888    -91.5045
  -71.4332    -91.9005  -112.627       -39.0312    -42.3267    -53.0172
  -25.0111    -33.2699   -47.6303      -11.1724    -16.3141    -21.0449
    ⋮                              ⋱                             ⋮
    1.03793   -12.5495   -22.2395       36.7157     29.6271     16.7166
   -1.38325   -22.1895   -44.635   …    35.4538     24.0184     13.3151
  -22.3489    -46.8946   -74.2558       18.9734      4.20395    -7.19412
  -46.6956    -72.3081   -99.3655       -1.05698   -16.4943    -29.972
  -63.3776    -88.1583  -111.619       -19.6289    -32.5491    -45.6992
  -76.3535    -98.3398  -117.57        -34.7811    -46.9238    -60.0989
  -86.3077   -102.84    -117.762   …   -47.4724    -62.8156    -73.1193
  -97.6943   -108.582   -120.257       -56.7525    -78.6816    -88.3761
 -109.339    -116.727   -127.606       -68.5834    -92.6184   -102.989
 -116.557    -122.424   -134.891       -81.1899   -104.531    -112.901
 -117.852    -121.164   -136.314       -88.2501   -112.248    -120.145
 -123.779    -123.406   -136.939   …   -96.0058   -123.577    -130.149

If you have your own map data in an array you'd like to load into a CMBLensing Field object, you can construct it as follows:

FlatMap(mapdata, θpix=3)
65536-element 256×256-pixel 3.0′-resolution LambertMap{Array{Float32, 2}}:
 -137.50613
 -162.00809
 -189.14218
 -211.42392
 -224.02896
 -219.66003
 -201.40434
 -174.61607
 -151.91231
 -135.45027
 -111.50457
  -71.43317
  -25.011139
    ⋮
   16.716599
   13.315134
   -7.1941237
  -29.971952
  -45.699158
  -60.098892
  -73.119255
  -88.37607
 -102.9887
 -112.90086
 -120.14466
 -130.14949

For more info on Field objects, see Field Basics.

Inverse lensing

You can inverse lense a map with the \ operator (which does A \ b ≡ inv(A) * b):

LenseFlow(ϕ) \ f;

Note that this is true inverse lensing, rather than lensing by the negative deflection (which is often called "anti-lensing"). This means that lensing then inverse lensing a map should get us back the original map. Lets check that this is the case:

Ns = [7 10 20]
plot([f - (LenseFlow(ϕ,N) \ (LenseFlow(ϕ,N) * f)) for N in Ns],
    title=["ODE steps = $N" for N in Ns]);

png

A cool feature of LenseFlow is that inverse lensing is trivially done by running the LenseFlow ODE in reverse. Note that as we crank up the number of ODE steps above, we recover the original map to higher and higher precision.

Other lensing algorithms

We can also lense via:

  • PowerLens: the standard Taylor series expansion to any order:
\[ f(x+\nabla x) \approx f(x) + (\nabla f)(\nabla \phi) + \frac{1}{2} (\nabla \nabla f) (\nabla \phi)^2 + ... \]
  • TayLens (Næss&Louis 2013): like PowerLens, but first a nearest-pixel permute step, then a Taylor expansion around the now-smaller residual displacement
plot([(PowerLens(ϕ,2)*f - f̃) (Taylens(ϕ,2)*f - f̃)], 
    title=["PowerLens - LenseFlow" "TayLens - LenseFlow"]);

png

Benchmarking

LenseFlow is highly optimized code since it appears on the inner-most loop of our analysis algorithms. To benchmark LenseFlow, note that there is first a precomputation step, which caches some data in preparation for applying it to a field of a given type. This was done automatically when evaluating LenseFlow(ϕ) * f but we can benchmark it separately since in many cases this only needs to be done once for a given $\phi$, e.g. when Wiener filtering at fixed $\phi$,

using BenchmarkTools
@benchmark cache(LenseFlow(ϕ),f)
BenchmarkTools.Trial: 92 samples with 1 evaluation.
 Range (min … max):  50.139 ms … 63.742 ms  ┊ GC (min … max): 8.33% … 5.86%
 Time  (median):     54.378 ms              ┊ GC (median):    7.53%
 Time  (mean ± σ):   54.899 ms ±  2.860 ms  ┊ GC (mean ± σ):  8.65% ± 2.57%

         ▁ ▁▆ ▁▆ ▁ ▁ ▆ ▁  ▁    █  ▁             ▁              
  ▄▁▁▇▄▄▄█▇██▇██▇█▄█▇█▄█▄▇█▄▄▇▄█▇▄█▄▇▁▄▄▇▄▁▄▄▁▁▁█▄▄▁▁▁▁▄▁▇▁▁▄ ▁
  50.1 ms         Histogram: frequency by time          62 ms <

 Memory estimate: 92.37 MiB, allocs estimate: 1653.

Once cached, it's faster and less memory intensive to repeatedly apply the operator:

@benchmark Lϕ * f setup=(Lϕ=cache(LenseFlow(ϕ),f))
BenchmarkTools.Trial: 51 samples with 1 evaluation.
 Range (min … max):  42.197 ms … 52.053 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     44.032 ms              ┊ GC (median):    0.00%
 Time  (mean ± σ):   45.220 ms ±  2.829 ms  ┊ GC (mean ± σ):  2.52% ± 3.83%

  ▁▁▁▁▁█▄▁▁▁▁▁▁  ▁ ▁            ▁     ▁             ▁          
  █████████████▁▆█▁█▆▁▆▁▆▁▁▁▁▁▁▁█▁▆▁▁▁█▁▁▁▆▁▁▆▆▁▁▁▁▆█▆▁▁▁▁▁▁▆ ▁
  42.2 ms         Histogram: frequency by time        51.8 ms <

 Memory estimate: 16.14 MiB, allocs estimate: 441.

Note that this documentation is generated on limited-performance cloud servers. Actual benchmarks are likely much faster locally or on a cluster, and yet (much) faster on GPU.