Lensing a flat-sky map
using CMBLensing, PythonPlot CondaPkg Found dependencies: /home/cosmo/CMBLensing/docs/CondaPkg.toml CondaPkg Found dependencies: /home/cosmo/.julia/packages/CondaPkg/Pc7ZA/CondaPkg.toml CondaPkg Found dependencies: /home/cosmo/.julia/packages/PythonCall/avYrV/CondaPkg.toml CondaPkg Found dev dependencies: /home/cosmo/CMBLensing/CondaPkg.toml CondaPkg Found dependencies: /home/cosmo/.julia/packages/PythonPlot/96RVm/CondaPkg.toml CondaPkg Initialising pixi │ /home/cosmo/.julia/artifacts/8429d049f6c01106c62971e9540e146dc068df25/bin/pixi │ init │ --format pixi └ /home/cosmo/CMBLensing/docs/.CondaPkgCreated /home/cosmo/CMBLensing/docs/.CondaPkg/pixi.toml CondaPkg Wrote /home/cosmo/CMBLensing/docs/.CondaPkg/pixi.toml │ [dependencies] │ pip = ">=22.0.0" │ openssl = ">=3, <3.6" │ libstdcxx = ">=3.4,<14.0" │ libstdcxx-ng = ">=3.4,<14.0" │ matplotlib = ">=1" │ │ [dependencies.python] │ channel = "conda-forge" │ build = "*cp*" │ version = ">=3.8,<4, >=3.8,<3.13" │ │ [workspace] │ name = ".CondaPkg" │ platforms = ["linux-64"] │ channels = ["conda-forge"] │ channel-priority = "strict" │ description = "automatically generated by CondaPkg.jl" │ │ [pypi-dependencies] └ camb = "*" CondaPkg Installing packages │ /home/cosmo/.julia/artifacts/8429d049f6c01106c62971e9540e146dc068df25/bin/pixi │ install └ --manifest-path /home/cosmo/CMBLensing/docs/.CondaPkg/pixi.tomlThe default environment has been installed.First we load a simulated unlensed field, $f$, and lensing potential, $\phi$,
(;ds, 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̃);
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 view(::Matrix{Float32}, :, :) with eltype Float32:
42.4917 44.3693 52.3324 … 5.49145 25.8024 37.6444
61.7412 80.0716 96.2205 18.8675 30.8133 45.0763
68.8343 94.4283 116.629 34.9179 42.0598 49.9406
80.5291 102.17 122.191 58.6123 66.5426 69.4232
115.639 126.896 137.736 87.9665 103.862 109.601
164.959 164.971 162.959 … 121.265 144.23 159.545
206.646 203.857 187.759 142.035 168.649 193.364
224.086 225.423 206.196 153.945 178.023 203.87
218.488 219.416 203.986 178.555 192.131 205.231
194.147 194.942 183.197 206.758 201.859 193.823
156.976 158.291 146.869 … 211.901 191.177 165.808
119.192 112.742 98.0594 191.156 167.348 135.63
89.0905 73.5382 57.4828 151.193 136.161 110.413
⋮ ⋱ ⋮
36.2827 8.56095 -23.5153 23.1352 28.6581 39.1263
19.0126 -5.40166 -24.0705 … 9.72169 17.6327 25.1676
-9.44453 -17.1365 -15.8627 -24.7351 -18.6058 -9.86043
-30.7132 -21.2565 -4.37116 -60.8258 -53.5433 -40.6618
-46.9334 -29.2556 -4.80714 -75.4059 -66.8536 -58.8336
-54.7159 -37.4071 -14.6319 -58.7881 -57.0675 -61.5433
-45.9201 -34.0569 -18.7733 … -27.1752 -30.77 -45.7683
-28.8925 -23.1219 -15.9577 -6.77632 -12.7547 -27.0997
-21.6542 -18.28 -13.7379 -9.24981 -10.7236 -19.8542
-21.4905 -19.4173 -13.0666 -16.6091 -10.982 -16.1361
-10.4007 -12.9173 -6.23349 -17.2824 -1.80104 -2.63631
14.3754 8.11817 13.4931 … -8.23016 13.6776 20.4779If 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{SubArray{Float32, 2, Matrix{Float32}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}}:
42.491726
61.741222
68.83433
80.52907
115.63884
164.95853
206.64626
224.0864
218.48813
194.14687
156.9762
119.19162
89.09053
⋮
39.126286
25.167568
-9.860431
-40.66182
-58.833633
-61.543335
-45.768276
-27.09967
-19.85424
-16.136148
-2.6363068
20.47795For 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]);
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): likePowerLens, 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"]);
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 precompute!!(LenseFlow(ϕ),f)BenchmarkTools.Trial: 669 samples with 1 evaluation per sample.
Range (min … max): 5.853 ms … 18.155 ms ┊ GC (min … max): 0.00% … 19.15%
Time (median): 7.290 ms ┊ GC (median): 9.81%
Time (mean ± σ): 7.481 ms ± 1.024 ms ┊ GC (mean ± σ): 10.80% ± 5.74%
█▁ ▄▂ ▅▅
█▅▂▂▁▁▁▃▇██▅▅▇██▇▆████▆▄▄▆██▅▄▅▇▃▂▃▃▅▄▅▄▄▃▂▂▁▃▃▄▄▄▃▃▁▂▁▁▁▃ ▄
5.85 ms Histogram: frequency by time 10.2 ms <
Memory estimate: 77.98 MiB, allocs estimate: 888.Once cached, it's faster and less memory intensive to repeatedly apply the operator:
@benchmark Lϕ * f setup=(Lϕ=precompute!!(LenseFlow(ϕ),f))BenchmarkTools.Trial: 242 samples with 1 evaluation per sample.
Range (min … max): 10.867 ms … 17.732 ms ┊ GC (min … max): 0.00% … 0.00%
Time (median): 11.487 ms ┊ GC (median): 0.00%
Time (mean ± σ): 12.109 ms ± 1.563 ms ┊ GC (mean ± σ): 0.00% ± 0.00%
▁█▆▁ ▁▄
▄████▇█▄▄▂▃▅██▄▁▂▂▂▁▃▁▁▃▁▂▂▁▁▁▁▂▃▂▁▁▁▁▁▂▁▁▁▂▁▂▁▂▂▂▁▁▃▃▂▃▁▄▃ ▃
10.9 ms Histogram: frequency by time 17 ms <
Memory estimate: 2.02 MiB, allocs estimate: 338.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.
For repeated CUDA likelihood and gradient evaluations, opt in to graph replay with LenseFlow(ϕ, 7; gpu_graphs=true). Retain the result of precompute!!(LenseFlow(ϕ, 7; gpu_graphs=true), f) between calls, or pass L=LenseFlow(7; gpu_graphs=true) to load_sim. The ordinary RK4 path remains the default. Graph replay uses the same integration steps and arithmetic.
Graph replay supports dense Float32 and Float64 Lambert scalar, QU, and IQU fields, including batches. Other layouts and CPU fields use ordinary RK4. Each integration direction runs ordinary RK4 on first use and captures on reuse, so temporary promoted workspaces avoid capture costs. Warm both the likelihood and its gradient before timing with CUDA.@sync. Graphs retain device workspaces and use more memory in exchange for fewer host launches. Results and gradients own their output storage and can be retained across later calls.
Use a separate cached operator for each concurrent task or chain. A sequential change of CUDA task or stream synchronizes the previous work and rebuilds the graphs. Adaptation and checkpoint loading also start with an empty graph cache; device pointers and executable graphs are never saved in checkpoints.