Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-212: metadata for bias computation #209

Merged
merged 26 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions heracles/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,14 @@ async def __call__(
del vis

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

# set metadata of array
update_metadata(pos, catalog, nbar=nbar, bias=bias)
update_metadata(
pos, catalog, nbar=nbar, musq=musq, dens=dens, fsky=fsky, bias=bias
)

# return the position map
return pos
Expand Down Expand Up @@ -338,7 +342,7 @@ async def __call__(

# total weighted variance from online algorithm
ngal = 0
wmean, var = 0.0, 0.0
wmean, w2mean, var = 0.0, 0.0, 0.0

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

ngal += page.size
wmean += (w - wmean).sum() / ngal
w2mean += (w**2 - w2mean).sum() / ngal
var += (v**2 - var).sum() / ngal

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

# compute bias from variance (per object)
bias = 4 * np.pi * fsky**2 * (var / wmean**2) / ngal
musq = var / w2mean
deff = w2mean / wmean**2
dens = ngal / (4 * np.pi * fsky) / deff
bias = fsky * musq / dens

# set metadata of array
update_metadata(val, catalog, wbar=wbar, bias=bias)
update_metadata(
val, catalog, wbar=wbar, musq=musq, dens=dens, fsky=fsky, bias=bias
)

# return the value map
return val
Expand Down Expand Up @@ -409,7 +419,7 @@ async def __call__(

# total weighted variance from online algorithm
ngal = 0
wmean, var = 0.0, 0.0
wmean, w2mean, var = 0.0, 0.0, 0.0

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

ngal += page.size
wmean += (w - wmean).sum() / ngal
w2mean += (w**2 - w2mean).sum() / ngal
var += (re**2 + im**2 - var).sum() / ngal

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

# bias from measured variance, for E/B decomposition
bias = 2 * np.pi * fsky**2 * (var / wmean**2) / ngal
musq = var / w2mean
deff = w2mean / wmean**2
dens = ngal / (4 * np.pi * fsky) / deff
bias = (1 / 2) * fsky * musq / dens

# set metadata of array
update_metadata(val, catalog, wbar=wbar, bias=bias)
update_metadata(
val, catalog, wbar=wbar, musq=musq, dens=dens, fsky=fsky, bias=bias
)

# return the shear map
return val
Expand Down Expand Up @@ -541,10 +557,15 @@ async def __call__(
wht /= wbar

# bias from weights
bias = 4 * np.pi * fsky**2 * (w2mean / wmean**2) / ngal
musq = 1.0
deff = w2mean / wmean**2
dens = ngal / (4 * np.pi * fsky) / deff
bias = fsky * musq / dens

# set metadata of array
update_metadata(wht, catalog, wbar=wbar, bias=bias)
update_metadata(
wht, catalog, wbar=wbar, musq=musq, dens=dens, fsky=fsky, bias=bias
)

# return the weight map
return wht
Expand Down
42 changes: 42 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def test_positions(mapper, catalog, vmap):
"nside": mapper.nside,
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"musq": 1.0,
"dens": pytest.approx(npix / np.pi),
"fsky": 1.0,
"bias": pytest.approx(bias / nbar**2),
}
np.testing.assert_array_equal(m, 0)
Expand All @@ -223,6 +226,9 @@ def test_positions(mapper, catalog, vmap):
"nside": mapper.nside,
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"musq": 1.0,
"dens": pytest.approx(npix / np.pi),
"fsky": 1.0,
"bias": pytest.approx(bias / nbar**2),
}
np.testing.assert_array_equal(m, 1.0)
Expand All @@ -246,6 +252,9 @@ def test_positions(mapper, catalog, vmap):
"nside": mapper.nside,
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"musq": 1.0,
"dens": pytest.approx(npix / (np.pi * catalog.fsky)),
"fsky": catalog.fsky,
"bias": pytest.approx(bias / nbar**2),
}

Expand All @@ -264,6 +273,9 @@ def test_positions(mapper, catalog, vmap):
"nside": mapper.nside,
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"musq": 1.0,
"dens": pytest.approx(npix / (np.pi * catalog.fsky)),
"fsky": catalog.fsky,
"bias": pytest.approx(bias / nbar**2),
}

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

w = next(iter(catalog))["w"]
v = next(iter(catalog))["g1"]
v1 = w.sum()
v2 = ((w * v) ** 2).sum()
v3 = (w**2).sum()
w = w.reshape(w.size // 4, 4).sum(axis=-1)
wbar = w.mean()
wmean = v1 / (4.0 * npix)
w2mean = v3 / (4.0 * npix)
var = v2 / (4.0 * npix)
musq = var / w2mean
deff = w2mean / wmean**2
dens = npix / np.pi / deff
bias = (4 * np.pi / npix / npix) * v2

assert m.shape == (npix,)
Expand All @@ -302,6 +322,9 @@ def test_scalar_field(mapper, catalog):
"nside": mapper.nside,
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"musq": pytest.approx(musq),
"dens": pytest.approx(dens),
"fsky": 1.0,
"bias": pytest.approx(bias / wbar**2),
}
np.testing.assert_array_almost_equal(m, 0)
Expand All @@ -318,9 +341,17 @@ def test_complex_field(mapper, catalog):
w = next(iter(catalog))["w"]
re = next(iter(catalog))["g1"]
im = next(iter(catalog))["g2"]
v1 = w.sum()
v2 = ((w * re) ** 2 + (w * im) ** 2).sum()
v3 = (w**2).sum()
w = w.reshape(w.size // 4, 4).sum(axis=-1)
wbar = w.mean()
wmean = v1 / (4.0 * npix)
w2mean = v3 / (4.0 * npix)
var = v2 / (4.0 * npix)
musq = var / w2mean
deff = w2mean / wmean**2
dens = npix / np.pi / deff
bias = (4 * np.pi / npix / npix) * v2 / 2

assert m.shape == (2, npix)
Expand All @@ -333,6 +364,9 @@ def test_complex_field(mapper, catalog):
"nside": mapper.nside,
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"fsky": 1.0,
"dens": pytest.approx(dens),
"musq": pytest.approx(musq),
"bias": pytest.approx(bias / wbar**2),
}
np.testing.assert_array_almost_equal(m, 0)
Expand All @@ -351,6 +385,11 @@ def test_weights(mapper, catalog):
w = w.reshape(w.size // 4, 4).sum(axis=-1)
wbar = w.mean()
bias = (4 * np.pi / npix / npix) * v2
v1 = w.sum()
wmean = v1 / (4.0 * npix)
w2mean = v2 / (4.0 * npix)
deff = w2mean / wmean**2
dens = npix / np.pi / deff

assert m.shape == (12 * mapper.nside**2,)
assert m.dtype.metadata == {
Expand All @@ -362,6 +401,9 @@ def test_weights(mapper, catalog):
"nside": mapper.nside,
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"musq": 1.0,
"dens": pytest.approx(dens),
"fsky": 1.0,
"bias": pytest.approx(bias / wbar**2),
}
np.testing.assert_array_almost_equal(m, w / wbar)
Expand Down