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

fix: CMBLensed handling monopole and dipole #215

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Updated `pixell` from 0.17.3 to 0.26.0 https://github.com/galsci/pysm/pull/183
- Initial implementation of a point source catalog component emission https://github.com/galsci/pysm/pull/187
- Switch the build system to Hatch https://github.com/galsci/pysm/pull/189
- Fix bug in `CMBLensed` to read spectra that include monopole and dipole

3.4.0 (2023-12-11)
==================
Expand Down
410 changes: 410 additions & 0 deletions change_minimal_212_ONLY.ipynb

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/pysm3/models/cmb.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ def __init__(
self.delensing_ells = (
None if delensing_ells is None else self.read_txt(delensing_ells)
)

# Remove monopole and dipole, if present in cmb_spectra
if self.cmb_spectra[0][0] == 0:
self.cmb_spectra = self.cmb_spectra[:, 2:]
# Remove monopole and dipole, if present in delensing_ells
if self.apply_delens and self.delensing_ells[0][0] == 0:
self.delensing_ells = self.delensing_ells[:, 2:]

self.map = u.Quantity(self.run_taylens(), unit=u.uK_CMB, copy=False)

def run_taylens(self):
Expand Down
64 changes: 64 additions & 0 deletions tests/test_cmb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest
from astropy.tests.helper import assert_quantity_allclose
from unittest.mock import patch

import pysm3
import pysm3.units as u
Expand Down Expand Up @@ -72,3 +73,66 @@ def test_cmb_lensed(model_tag, freq):
assert_quantity_allclose(
expected_output, model.get_emission(freq * u.GHz), rtol=1e-5
)


@pytest.mark.parametrize("freq", [100])
def test_cmb_lensed_l01(freq):

model_number = 5
expected_output = pysm3.read_map(
f"pysm_2_test_data/check{model_number}cmb_{freq}p0_64.fits",
64,
unit="uK_RJ",
field=(0, 1, 2),
)

# Use a temporary model to get access to the read_txt method
temp_model = pysm3.Model(nside=64)
c1_spectra = temp_model.read_txt('pysm_2/camb_lenspotentialCls.dat',
unpack=True)
c1_delens_l = temp_model.read_txt('pysm_2/delens_ells.txt',
unpack=True)

# This portion should always pass
with patch.object(target=pysm3.models.cmb.CMBLensed,
attribute="read_txt",
side_effect=[c1_spectra, c1_delens_l]):
# The PySM test was done with a different seed than the one
# baked into the preset models
c1 = pysm3.models.cmb.CMBLensed(
nside=64,
cmb_spectra='pysm_2/camb_lenspotentialCls.dat',
cmb_seed=1234
)
model = pysm3.Sky(nside=64, component_objects=[c1])

try:
assert_quantity_allclose(
expected_output,
model.get_emission(freq * u.GHz),
rtol=1e-5
)
except AssertionError:
pytest.fail(f"Unexpected AssertionError. Superfail.")

# This portion should pass only with the fix

# Add rows of 0s for l=0,1
c1_spectra_l01 = np.zeros((c1_spectra.shape[0], c1_spectra.shape[1] + 2))
c1_spectra_l01[:, 2:] = c1_spectra

with patch.object(target=pysm3.models.cmb.CMBLensed,
attribute="read_txt",
side_effect=[c1_spectra_l01, c1_delens_l]):
c1 = pysm3.models.cmb.CMBLensed(
nside=64,
cmb_spectra='pysm_2/camb_lenspotentialCls.dat',
cmb_seed=1234
)
model = pysm3.Sky(nside=64, component_objects=[c1])

assert_quantity_allclose(
expected_output,
model.get_emission(freq * u.GHz),
rtol=1e-5
)