-
Notifications
You must be signed in to change notification settings - Fork 24
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not use a try except block. |
There was a problem hiding this comment.
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