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

Enable apply_delens=True in CMBLensed #214

Open
wants to merge 2 commits 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` preventing use of `apply_delens=True`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify it is a shape error,
add link to this pull request


3.4.0 (2023-12-11)
==================
Expand Down
197 changes: 197 additions & 0 deletions change_minimal_213_ONLY.ipynb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please post the notebook to gist.github.com and link it in the pull request.
Remove it from the pull request.

Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# For clarity, this addresses Issue 213 only"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pysm3\n",
"import pysm3.units as u\n",
"from pysm3.models.cmb import CMBMap\n",
"from pysm3.models.cmb import CMBLensed as CMBLensedOriginal"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Issue 213"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When running CMBLensed with `apply_delens=True`, there is a ValueError."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"operands could not be broadcast together with shapes (2199,) (2,) \n"
]
}
],
"source": [
"try:\n",
" will_crash = CMBLensedOriginal(\n",
" nside=128,\n",
" cmb_spectra='pysm_2/camb_lenspotentialCls.dat',\n",
" # max_nside=None,\n",
" # cmb_seed=0,\n",
" apply_delens=True,\n",
" delensing_ells='pysm_2/delens_ells.txt',\n",
" # map_dist=None,\n",
" )\n",
"except ValueError as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Resolution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"At line (43), adding `unpack=True`, resolves this:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Revised, minimal changes to the __init__ method are all that is needed\n",
"\n",
"class CMBLensedRevised(CMBMap):\n",
" # intherit from CMBMap so we get the `get_emission` method\n",
" def __init__(\n",
" self,\n",
" nside,\n",
" cmb_spectra,\n",
" max_nside=None,\n",
" cmb_seed=None,\n",
" apply_delens=False,\n",
" delensing_ells=None,\n",
" map_dist=None,\n",
" ):\n",
" \"\"\"Lensed CMB\n",
"\n",
" Takes an input unlensed CMB and lensing spectrum from CAMB and uses\n",
" Taylens to apply lensing, it optionally simulates delensing by\n",
" suppressing the lensing power at specific scales with the user\n",
" provided `delensing_ells`.\n",
"\n",
" Parameters\n",
" ----------\n",
"\n",
" cmb_spectra : path\n",
" Input text file from CAMB, spectra unlensed\n",
" cmb_seed : int\n",
" Numpy random seed for synfast, set to None for a random seed\n",
" apply_delens : bool\n",
" If true, simulate delensing with taylens\n",
" delensing_ells : path\n",
" Space delimited file with ells in the first columns and suppression\n",
" factor (1 for no suppression) in the second column\n",
" \"\"\"\n",
" try:\n",
" super().__init__(nside=nside, max_nside=max_nside, map_dist=map_dist)\n",
" except ValueError:\n",
" pass # suppress exception about not providing any input map\n",
" self.cmb_spectra = self.read_txt(cmb_spectra, unpack=True)\n",
" self.cmb_seed = cmb_seed\n",
" self.apply_delens = apply_delens\n",
" self.delensing_ells = (\n",
" None if delensing_ells is None else self.read_txt(delensing_ells, \n",
" unpack=True)\n",
" )\n",
"\n",
" self.map = u.Quantity(self.run_taylens(), unit=u.uK_CMB, copy=False)\n",
"\n",
"\n",
" def run_taylens(self):\n",
" pass\n",
"\n",
"CMBLensedRevised.run_taylens = CMBLensedOriginal.run_taylens"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successful run.\n"
]
}
],
"source": [
"try:\n",
" will_run = CMBLensedRevised(\n",
" nside=128,\n",
" cmb_spectra='pysm_2/camb_lenspotentialCls.dat',\n",
" # max_nside=None,\n",
" # cmb_seed=0,\n",
" apply_delens=True,\n",
" delensing_ells='pysm_2/delens_ells.txt',\n",
" # map_dist=None,\n",
" )\n",
" print(\"Successful run.\")\n",
"except ValueError as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because the `c1` delensing ells are a no-op (it is a column of 1's), no change is seen when applying delensing_ells. I do not know enough about the method to evaluate success or failure. When addressing Issue 212, it needs to account for this as well."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "cmb-ml2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.21"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
3 changes: 2 additions & 1 deletion src/pysm3/models/cmb.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ def __init__(
self.cmb_seed = cmb_seed
self.apply_delens = apply_delens
self.delensing_ells = (
None if delensing_ells is None else self.read_txt(delensing_ells)
None if delensing_ells is None else self.read_txt(delensing_ells,
unpack=True)
)
self.map = u.Quantity(self.run_taylens(), unit=u.uK_CMB, copy=False)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_cmb.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,28 @@ def test_cmb_lensed(model_tag, freq):
assert_quantity_allclose(
expected_output, model.get_emission(freq * u.GHz), rtol=1e-5
)


def test_cmb_lensed_apply_delens():
# Addressing issue #213; CMBLensed model was raising a ValueError when
# apply_delens=True

try:
model = pysm3.models.CMBLensed(
nside=64,
cmb_spectra='pysm_2/camb_lenspotentialCls.dat',
apply_delens=False,
delensing_ells='pysm_2/delens_ells.txt'
)
except ValueError:
pytest.fail(f"Unexpected ValueError without apply_delens=True")

try:
model = pysm3.models.CMBLensed(
nside=64,
cmb_spectra='pysm_2/camb_lenspotentialCls.dat',
apply_delens=True,
delensing_ells='pysm_2/delens_ells.txt'
)
except ValueError:
pytest.fail(f"ValueError raised when applying delensing")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not use a try except block.
split the test in 2 tests:
test_cmb_lensed_no_delens
test_cmb_lensed_delens