Skip to content

Commit 9fda271

Browse files
authored
gh-212: metadata for bias computation (#209)
1 parent 779246d commit 9fda271

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

heracles/fields.py

+31-10
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,14 @@ async def __call__(
305305
del vis
306306

307307
# compute bias of positions, including weight variance
308-
bias = ngal / (4 * np.pi) * mapper.area**2 * (w2mean / nbar**2)
308+
musq = 1.0
309+
dens = (nbar / mapper.area) ** 2 / (ngal / (4 * np.pi * fsky)) / w2mean
310+
bias = fsky * musq / dens
309311

310312
# set metadata of array
311-
update_metadata(pos, catalog, nbar=nbar, bias=bias)
313+
update_metadata(
314+
pos, catalog, nbar=nbar, musq=musq, dens=dens, fsky=fsky, bias=bias
315+
)
312316

313317
# return the position map
314318
return pos
@@ -338,7 +342,7 @@ async def __call__(
338342

339343
# total weighted variance from online algorithm
340344
ngal = 0
341-
wmean, var = 0.0, 0.0
345+
wmean, w2mean, var = 0.0, 0.0, 0.0
342346

343347
# go through pages in catalogue and map values
344348
async for page in aiter_pages(catalog, progress):
@@ -354,6 +358,7 @@ async def __call__(
354358

355359
ngal += page.size
356360
wmean += (w - wmean).sum() / ngal
361+
w2mean += (w**2 - w2mean).sum() / ngal
357362
var += (v**2 - var).sum() / ngal
358363

359364
del lon, lat, v, w
@@ -371,10 +376,15 @@ async def __call__(
371376
val /= wbar
372377

373378
# compute bias from variance (per object)
374-
bias = 4 * np.pi * fsky**2 * (var / wmean**2) / ngal
379+
musq = var / w2mean
380+
deff = w2mean / wmean**2
381+
dens = ngal / (4 * np.pi * fsky) / deff
382+
bias = fsky * musq / dens
375383

376384
# set metadata of array
377-
update_metadata(val, catalog, wbar=wbar, bias=bias)
385+
update_metadata(
386+
val, catalog, wbar=wbar, musq=musq, dens=dens, fsky=fsky, bias=bias
387+
)
378388

379389
# return the value map
380390
return val
@@ -409,7 +419,7 @@ async def __call__(
409419

410420
# total weighted variance from online algorithm
411421
ngal = 0
412-
wmean, var = 0.0, 0.0
422+
wmean, w2mean, var = 0.0, 0.0, 0.0
413423

414424
# go through pages in catalogue and get the shear values,
415425
async for page in aiter_pages(catalog, progress):
@@ -425,6 +435,7 @@ async def __call__(
425435

426436
ngal += page.size
427437
wmean += (w - wmean).sum() / ngal
438+
w2mean += (w**2 - w2mean).sum() / ngal
428439
var += (re**2 + im**2 - var).sum() / ngal
429440

430441
del lon, lat, re, im, w
@@ -441,10 +452,15 @@ async def __call__(
441452
val /= wbar
442453

443454
# bias from measured variance, for E/B decomposition
444-
bias = 2 * np.pi * fsky**2 * (var / wmean**2) / ngal
455+
musq = var / w2mean
456+
deff = w2mean / wmean**2
457+
dens = ngal / (4 * np.pi * fsky) / deff
458+
bias = (1 / 2) * fsky * musq / dens
445459

446460
# set metadata of array
447-
update_metadata(val, catalog, wbar=wbar, bias=bias)
461+
update_metadata(
462+
val, catalog, wbar=wbar, musq=musq, dens=dens, fsky=fsky, bias=bias
463+
)
448464

449465
# return the shear map
450466
return val
@@ -541,10 +557,15 @@ async def __call__(
541557
wht /= wbar
542558

543559
# bias from weights
544-
bias = 4 * np.pi * fsky**2 * (w2mean / wmean**2) / ngal
560+
musq = 1.0
561+
deff = w2mean / wmean**2
562+
dens = ngal / (4 * np.pi * fsky) / deff
563+
bias = fsky * musq / dens
545564

546565
# set metadata of array
547-
update_metadata(wht, catalog, wbar=wbar, bias=bias)
566+
update_metadata(
567+
wht, catalog, wbar=wbar, musq=musq, dens=dens, fsky=fsky, bias=bias
568+
)
548569

549570
# return the weight map
550571
return wht

tests/test_fields.py

+42
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ def test_positions(mapper, catalog, vmap):
204204
"nside": mapper.nside,
205205
"lmax": mapper.lmax,
206206
"deconv": mapper.deconvolve,
207+
"musq": 1.0,
208+
"dens": pytest.approx(npix / np.pi),
209+
"fsky": 1.0,
207210
"bias": pytest.approx(bias / nbar**2),
208211
}
209212
np.testing.assert_array_equal(m, 0)
@@ -223,6 +226,9 @@ def test_positions(mapper, catalog, vmap):
223226
"nside": mapper.nside,
224227
"lmax": mapper.lmax,
225228
"deconv": mapper.deconvolve,
229+
"musq": 1.0,
230+
"dens": pytest.approx(npix / np.pi),
231+
"fsky": 1.0,
226232
"bias": pytest.approx(bias / nbar**2),
227233
}
228234
np.testing.assert_array_equal(m, 1.0)
@@ -246,6 +252,9 @@ def test_positions(mapper, catalog, vmap):
246252
"nside": mapper.nside,
247253
"lmax": mapper.lmax,
248254
"deconv": mapper.deconvolve,
255+
"musq": 1.0,
256+
"dens": pytest.approx(npix / (np.pi * catalog.fsky)),
257+
"fsky": catalog.fsky,
249258
"bias": pytest.approx(bias / nbar**2),
250259
}
251260

@@ -264,6 +273,9 @@ def test_positions(mapper, catalog, vmap):
264273
"nside": mapper.nside,
265274
"lmax": mapper.lmax,
266275
"deconv": mapper.deconvolve,
276+
"musq": 1.0,
277+
"dens": pytest.approx(npix / (np.pi * catalog.fsky)),
278+
"fsky": catalog.fsky,
267279
"bias": pytest.approx(bias / nbar**2),
268280
}
269281

@@ -287,9 +299,17 @@ def test_scalar_field(mapper, catalog):
287299

288300
w = next(iter(catalog))["w"]
289301
v = next(iter(catalog))["g1"]
302+
v1 = w.sum()
290303
v2 = ((w * v) ** 2).sum()
304+
v3 = (w**2).sum()
291305
w = w.reshape(w.size // 4, 4).sum(axis=-1)
292306
wbar = w.mean()
307+
wmean = v1 / (4.0 * npix)
308+
w2mean = v3 / (4.0 * npix)
309+
var = v2 / (4.0 * npix)
310+
musq = var / w2mean
311+
deff = w2mean / wmean**2
312+
dens = npix / np.pi / deff
293313
bias = (4 * np.pi / npix / npix) * v2
294314

295315
assert m.shape == (npix,)
@@ -302,6 +322,9 @@ def test_scalar_field(mapper, catalog):
302322
"nside": mapper.nside,
303323
"lmax": mapper.lmax,
304324
"deconv": mapper.deconvolve,
325+
"musq": pytest.approx(musq),
326+
"dens": pytest.approx(dens),
327+
"fsky": 1.0,
305328
"bias": pytest.approx(bias / wbar**2),
306329
}
307330
np.testing.assert_array_almost_equal(m, 0)
@@ -318,9 +341,17 @@ def test_complex_field(mapper, catalog):
318341
w = next(iter(catalog))["w"]
319342
re = next(iter(catalog))["g1"]
320343
im = next(iter(catalog))["g2"]
344+
v1 = w.sum()
321345
v2 = ((w * re) ** 2 + (w * im) ** 2).sum()
346+
v3 = (w**2).sum()
322347
w = w.reshape(w.size // 4, 4).sum(axis=-1)
323348
wbar = w.mean()
349+
wmean = v1 / (4.0 * npix)
350+
w2mean = v3 / (4.0 * npix)
351+
var = v2 / (4.0 * npix)
352+
musq = var / w2mean
353+
deff = w2mean / wmean**2
354+
dens = npix / np.pi / deff
324355
bias = (4 * np.pi / npix / npix) * v2 / 2
325356

326357
assert m.shape == (2, npix)
@@ -333,6 +364,9 @@ def test_complex_field(mapper, catalog):
333364
"nside": mapper.nside,
334365
"lmax": mapper.lmax,
335366
"deconv": mapper.deconvolve,
367+
"fsky": 1.0,
368+
"dens": pytest.approx(dens),
369+
"musq": pytest.approx(musq),
336370
"bias": pytest.approx(bias / wbar**2),
337371
}
338372
np.testing.assert_array_almost_equal(m, 0)
@@ -351,6 +385,11 @@ def test_weights(mapper, catalog):
351385
w = w.reshape(w.size // 4, 4).sum(axis=-1)
352386
wbar = w.mean()
353387
bias = (4 * np.pi / npix / npix) * v2
388+
v1 = w.sum()
389+
wmean = v1 / (4.0 * npix)
390+
w2mean = v2 / (4.0 * npix)
391+
deff = w2mean / wmean**2
392+
dens = npix / np.pi / deff
354393

355394
assert m.shape == (12 * mapper.nside**2,)
356395
assert m.dtype.metadata == {
@@ -362,6 +401,9 @@ def test_weights(mapper, catalog):
362401
"nside": mapper.nside,
363402
"lmax": mapper.lmax,
364403
"deconv": mapper.deconvolve,
404+
"musq": 1.0,
405+
"dens": pytest.approx(dens),
406+
"fsky": 1.0,
365407
"bias": pytest.approx(bias / wbar**2),
366408
}
367409
np.testing.assert_array_almost_equal(m, w / wbar)

0 commit comments

Comments
 (0)