/
dataset.jl
227 lines (190 loc) · 7.97 KB
/
dataset.jl
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Stores variables needed to construct the posterior
@kwdef mutable struct DataSet{F}
d :: F # data
Cϕ # ϕ covariance
Cf # unlensed field covariance
Cf̃ = nothing # lensed field covariance (not always needed)
Cn # noise covariance
Cn̂ = Cn # approximate noise covariance, diagonal in same basis as Cf
M = 1 # user mask
M̂ = M # approximate user mask, diagonal in same basis as Cf
B = 1 # beam and instrumental transfer functions
B̂ = B # approximate beam and instrumental transfer functions, diagonal in same basis as Cf
D = 1 # mixing matrix for mixed parametrization
G = 1 # reparametrization for ϕ
P = 1 # pixelization operator (if estimating field on higher res than data)
L = alloc_cache(LenseFlow(similar(diag(Cϕ))),d) # a CachedLenseFlow which will be reused for memory
end
function subblock(ds::DataSet, block)
DataSet(map(collect(pairs(fields(ds)))) do (k,v)
@match (k,v) begin
((:Cϕ || :G || :L), v) => v
(_, L::Union{Nothing,FuncOp,Real}) => L
(_, L) => getindex(L,block)
end
end...)
end
function (ds::DataSet)(;θ...)
DataSet(map(fieldvalues(ds)) do v
(v isa ParamDependentOp) ? v(;θ...) : v
end...)
end
function check_hat_operators(ds::DataSet)
@unpack B̂, M̂, Cn̂, Cf = ds()
@assert(all([(L isa Scalar) || (L isa typeof(Cf)) || (Cf isa FlatIEBCov && L isa DiagOp{<:FlatIEBFourier}) for L in [B̂,M̂,Cn̂]]),
"B̂, M̂, Cn̂ should be scalars or the same type as Cf")
end
adapt_structure(to, ds::DataSet) = DataSet(adapt(to, fieldvalues(ds))...)
@doc doc"""
resimulate(ds::DataSet; f=..., ϕ=...)
Resimulate the data in a given dataset, potentially at a fixed f and/or ϕ (both
are resimulated if not provided)
"""
function resimulate(ds::DataSet; f=simulate(ds.Cf), ϕ=simulate(ds.Cϕ), n=simulate(ds.Cn), f̃=ds.L(ϕ)*f, return_truths=false)
@unpack M,P,B = ds
@set! ds.d = M*P*B*f̃ + n
return_truths ? @namedtuple(ds,f,ϕ,n,f̃) : ds
end
@doc doc"""
load_sim_dataset
Create a `DataSet` object with some simulated data. E.g.
```julia
@unpack f,ϕ,ds = load_sim_dataset(;
θpix = 2,
Nside = 128,
pol = :I,
T = Float32
);
```
"""
function load_sim_dataset(;
# basic configuration
θpix,
θpix_data = θpix,
Nside,
pol,
T = Float32,
storage = Array,
# noise parameters, or set Cℓn or even Cn directly
μKarcminT = 3,
ℓknee = 100,
αknee = 3,
Cℓn = nothing,
Cn = nothing,
# beam parameters, or set B directly
beamFWHM = 0,
B = nothing,
# mask parameters, or set M directly
pixel_mask_kwargs = nothing,
bandpass_mask = LowPass(3000),
M = nothing, M̂ = nothing,
# theory
Cℓ = nothing,
fiducial_θ = NamedTuple(),
rfid = nothing,
seed = nothing,
D = nothing,
G = nothing,
Nϕ_fac = 2,
ϕ=nothing, f=nothing, f̃=nothing, Bf̃=nothing, n=nothing, d=nothing, # can override any of these simulated fields
L = LenseFlow,
∂mode = fourier∂
)
# the biggest ℓ on the 2D fourier grid
ℓmax = round(Int,ceil(√2*fieldinfo(Flat(θpix=θpix,Nside=Nside)).nyq)+1)
# CMB Cℓs
if rfid != nothing
@warn "`rfid` will be removed in a future version. Use `fiducial_θ=(r=...,)` instead."
fiducial_θ = merge(fiducial_θ,(r=rfid,))
end
Aϕ₀ = get(fiducial_θ, :Aϕ, 1)
fiducial_θ = Base.structdiff(fiducial_θ, NamedTuple{(:Aϕ,)}) # remove Aϕ key if present
if Cℓ==nothing
Cℓ = camb(;fiducial_θ..., ℓmax=ℓmax)
else
if !isempty(fiducial_θ)
error("Can't pass both `Cℓ` and `fiducial_θ` parameters which affect `Cℓ`, choose one or the other.")
elseif maximum(Cℓ.total.TT.ℓ) < ℓmax
error("ℓmax of `Cℓ` argument should be higher than $ℓmax for this configuration.")
end
end
r₀ = Cℓ.params.r
# noise Cℓs (these are non-debeamed, hence beamFWHM=0 below; the beam comes in via the B operator)
if (Cℓn == nothing)
Cℓn = noiseCℓs(μKarcminT=μKarcminT, beamFWHM=0, ℓknee=ℓknee, αknee=αknee, ℓmax=ℓmax)
end
# some things which depend on whether we chose :I, :P, or :IP
pol = Symbol(pol)
S,ks,F,F̂,nF = @match pol begin
:I => (S0, (:TT,), FlatMap, FlatFourier, 1)
:P => (S2, (:EE,:BB), FlatQUMap, FlatEBFourier, 2)
:IP => (S02, (:TT,:EE,:BB,:TE), FlatIQUMap, FlatIEBFourier, 3)
_ => throw(ArgumentError("`pol` should be one of :I, :P, or :IP"))
end
# pixelization
Pix = Flat(Nside=Nside, θpix=θpix, ∂mode=∂mode)
if (θpix_data == θpix)
Pix_data = Pix
P = Identity
else
Pix_data = Flat(Nside=Nside÷(θpix_data÷θpix), θpix=θpix_data, ∂mode=∂mode)
P = FuncOp(
op = f -> ud_grade(f, θpix_data, deconv_pixwin=false, anti_aliasing=false),
opᴴ = f -> ud_grade(f, θpix, deconv_pixwin=false, anti_aliasing=false)
)
end
# covariances
Cϕ₀ = adapt(storage, Cℓ_to_Cov(Pix, T, S0, (Cℓ.total.ϕϕ)))
Cfs = adapt(storage, Cℓ_to_Cov(Pix, T, S, (Cℓ.unlensed_scalar[k] for k in ks)...))
Cft = adapt(storage, Cℓ_to_Cov(Pix, T, S, (Cℓ.tensor[k] for k in ks)...))
Cf̃ = adapt(storage, Cℓ_to_Cov(Pix, T, S, (Cℓ.total[k] for k in ks)...))
Cn̂ = adapt(storage, Cℓ_to_Cov(Pix_data, T, S, (Cℓn[k] for k in ks)...))
if (Cn == nothing); Cn = Cn̂; end
Cf = ParamDependentOp((mem; r=r₀, _...)->(mem .= Cfs + T(r/r₀)*Cft), similar(Cfs))
Cϕ = ParamDependentOp((mem; Aϕ=Aϕ₀, _...)->(mem .= T(Aϕ) .* Cϕ₀), similar(Cϕ₀))
# data mask
if (M == nothing)
M̂ = M = adapt(storage, Cℓ_to_Cov(Pix_data, T, S, ((k==:TE ? 0 : 1) * bandpass_mask.diag.Wℓ for k in ks)...; units=1))
if (pixel_mask_kwargs != nothing)
M = M * adapt(storage, Diagonal(F{Pix_data}(repeated(T.(make_mask(Nside÷(θpix_data÷θpix),θpix_data; pixel_mask_kwargs...).Ix),nF)...)))
end
end
if diag(M̂) isa BandPass
M̂ = Diagonal(M̂ * one(diag(Cf)))
end
# beam
if (B == nothing)
B̂ = B = adapt(storage, Cℓ_to_Cov(Pix, T, S, ((k==:TE ? 0 : 1) * sqrt(beamCℓs(beamFWHM=beamFWHM)) for k=ks)..., units=1))
end
# D mixing matrix
if (D == nothing)
σ²len = T(deg2rad(5/60)^2)
D = ParamDependentOp(
function (mem;r=r₀,_...)
Cfr = Cf(mem,r=r)
mem .= sqrt(Diagonal(diag(Cfr) .+ σ²len .+ 2*diag(Cn̂)) * pinv(Cfr))
end,
similar(Cf())
)
end
# simulate data
seed_for_storage!(storage, seed)
if (ϕ == nothing); ϕ = simulate(Cϕ); end
if (f == nothing); f = simulate(Cf); end
if (n == nothing); n = simulate(Cn); end
Lϕ = cache(L(ϕ),f)
if (f̃ == nothing); f̃ = Lϕ*f; end
if (Bf̃ == nothing); Bf̃ = B*f̃; end
if (d == nothing); d = M*P*Bf̃ + n; end
# put everything in DataSet
ds = DataSet(;@namedtuple(d, Cn, Cn̂, Cf, Cf̃, Cϕ, M, M̂, B, B̂, D, P, L=Lϕ)...)
# with the DataSet created, we can now more conveniently call the quadratic
# estimate to compute Nϕ if needed for the G mixing matrix
if (G == nothing)
Nϕ = quadratic_estimate(ds,(pol in (:P,:IP) ? :EB : :TT)).Nϕ / Nϕ_fac
G₀ = @. nan2zero(sqrt(1 + 2/($Cϕ()/Nϕ)))
G = ParamDependentOp((;Aϕ=Aϕ₀,_...)->(@. nan2zero(sqrt(1 + 2/(($(Cϕ(Aϕ=Aϕ))/Nϕ)))/G₀)))
end
@set! ds.G = G
return adapt(storage, @namedtuple(f, f̃, ϕ, n, ds, ds₀=ds(), T, P=Pix, Cℓ, L))
end