Skip to content

Commit

Permalink
working all
Browse files Browse the repository at this point in the history
  • Loading branch information
JaimeRZP committed Nov 27, 2024
1 parent 573d557 commit 144afac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
23 changes: 10 additions & 13 deletions heracles/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,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 @@ -358,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 @@ -375,11 +376,9 @@ async def __call__(
val /= wbar

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

# set metadata of array
Expand Down Expand Up @@ -420,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 @@ -436,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 @@ -452,13 +452,10 @@ async def __call__(
val /= wbar

# bias from measured variance, for E/B decomposition
musq = var / wmean**2
dens = ngal / (4 * np.pi * fsky)
# variance = var / w2mean
# deff = w2mean / wmean**2
# neff = ngal / (2 * np.pi * fsky) / deff
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, musq=musq, dens=dens, fsky=fsky, bias=bias
Expand Down
34 changes: 22 additions & 12 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,19 @@ def test_scalar_field(mapper, catalog):

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

assert m.shape == (npix,)
assert m.dtype.metadata == {
Expand All @@ -319,9 +324,9 @@ def test_scalar_field(mapper, catalog):
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"musq": pytest.approx(musq),
"dens": pytest.approx(npix / np.pi),
"dens": pytest.approx(dens),
"fsky": 1.0,
"bias": pytest.approx(bias / wbar**2),
"bias": pytest.approx(bias),
}
np.testing.assert_array_almost_equal(m, 0)

Expand All @@ -337,14 +342,19 @@ def test_complex_field(mapper, catalog):
w = next(iter(catalog))["w"]
re = next(iter(catalog))["g1"]
im = next(iter(catalog))["g2"]
v2 = ((w * re) ** 2 + (w * im) ** 2).sum()
v1 = w.sum()
v2 = (w ** 2).sum()
v3 = ((w * re) ** 2 + (w * im) ** 2).sum()
w = w.reshape(w.size // 4, 4).sum(axis=-1)
wbar = w.mean()
v1 = w.sum()
bias = (4 * np.pi / npix / npix) * v2 / 2
wmean = v1 / (4.0 * npix)
w2mean = v2 / (4.0 * npix)
musq = w2mean / wmean**2
var = v3 / (4.0 * npix)
musq = var / w2mean
deff = w2mean / wmean**2
dens = npix / np.pi / deff
bias = (4 * np.pi / npix / npix) * v3 / 2
bias = bias / wbar**2

assert m.shape == (2, npix)
assert m.dtype.metadata == {
Expand All @@ -357,9 +367,9 @@ def test_complex_field(mapper, catalog):
"lmax": mapper.lmax,
"deconv": mapper.deconvolve,
"fsky": 1.0,
"dens": npix / np.pi,
"dens": pytest.approx(dens),
"musq": pytest.approx(musq),
"bias": pytest.approx(bias / wbar**2),
"bias": pytest.approx(bias),
}
np.testing.assert_array_almost_equal(m, 0)

Expand Down

0 comments on commit 144afac

Please sign in to comment.