Skip to content

Commit

Permalink
utils tested
Browse files Browse the repository at this point in the history
  • Loading branch information
damonge committed Dec 31, 2023
1 parent 61a84ba commit 27c2dd8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
5 changes: 5 additions & 0 deletions pymaster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
from pymaster import nmtlib as lib # noqa
import numpy as np # noqa
from pymaster.utils import ( # noqa
nmt_params,
set_sht_calculator,
set_n_iter_default,
set_tol_pinv_default,
get_default_params,
NmtMapInfo,
NmtAlmInfo,
mask_apodization,
Expand Down
2 changes: 1 addition & 1 deletion pymaster/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, mask, maps, spin=None, templates=None, beam=None,
if n_iter is None:
n_iter = ut.nmt_params.n_iter_default
if tol_pinv is None:
tol_pinv = ut.nmt_params.tol_pinv
tol_pinv = ut.nmt_params.tol_pinv_default

# 0. Preliminary initializations
# These first attributes are compulsory for all fields
Expand Down
11 changes: 11 additions & 0 deletions pymaster/tests/test_field_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ def test_field_alloc():


def test_field_error():
# SHTs using healpy for CAR maps
f0 = nmt.NmtField(FT.msk, [FT.mps[0]], wcs=FT.wcs)
mp = f0.get_maps()
alm = f0.get_alms()
nmt.set_sht_calculator('healpy')
with pytest.raises(ValueError):
nmt.utils.map2alm(mp, 0, f0.minfo, f0.ainfo, n_iter=0)
with pytest.raises(ValueError):
nmt.utils.alm2map(alm, 0, f0.minfo, f0.ainfo)
nmt.set_sht_calculator('ducc')

with pytest.raises(ValueError): # Not passing WCS
nmt.NmtField(FT.msk, [FT.mps[0]], beam=FT.beam)
with pytest.raises(ValueError): # Passing 1D maps
Expand Down
11 changes: 10 additions & 1 deletion pymaster/tests/test_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ def test_spin1():
np.fabs(tl))*1E-5).all()


def mastest(wtemp, wpure, do_teb=False):
def mastest(wtemp, wpure, do_teb=False, use_healpy=False):
if use_healpy:
nmt.set_sht_calculator('healpy')
prefix = "test/benchmarks/bm"
if wtemp:
prefix += "_yc"
Expand Down Expand Up @@ -386,6 +388,8 @@ def mastest(wtemp, wpure, do_teb=False):
assert ((np.fabs(cl-tl) <=
np.fmin(np.fabs(cl),
np.fabs(tl))*1E-5).all())
if use_healpy:
nmt.set_sht_calculator('ducc')


@pytest.mark.parametrize("wtemp,wpure,do_teb",
Expand All @@ -399,6 +403,11 @@ def test_workspace_master_teb_np(wtemp, wpure, do_teb):
mastest(wtemp, wpure, do_teb=do_teb)


def test_workspace_master_healpy():
mastest(False, False, do_teb=False, use_healpy=True)
assert nmt.get_default_params()['sht_calculator'] == 'ducc'


def test_workspace_shorten():
w = nmt.NmtWorkspace()
w.read_from("test/benchmarks/bm_yc_yp_w02.fits") # OK read
Expand Down
47 changes: 47 additions & 0 deletions pymaster/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest
import pymaster as nmt


def test_params_set_get():
# SHT calculator
bak = nmt.get_default_params()['sht_calculator']
nmt.set_sht_calculator('healpy')
assert nmt.get_default_params()['sht_calculator'] == 'healpy'
nmt.set_sht_calculator(bak)

# n_iter
bak = nmt.get_default_params()['n_iter_default']
nmt.set_n_iter_default(5)
assert nmt.get_default_params()['n_iter_default'] == 5
nmt.set_n_iter_default(bak)

# n_iter_mask
bak = nmt.get_default_params()['n_iter_mask_default']
nmt.set_n_iter_default(5, mask=True)
assert nmt.get_default_params()['n_iter_mask_default'] == 5
nmt.set_n_iter_default(bak, mask=True)

# tol_pinv
bak = nmt.get_default_params()['tol_pinv_default']
nmt.set_tol_pinv_default(1E-3)
assert nmt.get_default_params()['tol_pinv_default'] == 1E-3
nmt.set_tol_pinv_default(bak)

# Wrong SHT calculator
with pytest.raises(KeyError):
nmt.set_sht_calculator('healpyy')

# Fake us not having ducc
nmt.utils.HAVE_DUCC = False
with pytest.raises(ValueError):
nmt.set_sht_calculator('ducc')
nmt.utils.HAVE_DUCC = True
nmt.set_sht_calculator('ducc')

# Negative n_iter
with pytest.raises(ValueError):
nmt.set_n_iter_default(-1)

# Wrong tolerance
with pytest.raises(ValueError):
nmt.set_tol_pinv_default(2)
16 changes: 8 additions & 8 deletions pymaster/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self):
self.sht_calculator = 'ducc' if HAVE_DUCC else 'healpy'
self.n_iter_default = 3
self.n_iter_mask_default = 3
self.tol_pinv = 1E-10
self.tol_pinv_default = 1E-10


nmt_params = NmtParams()
Expand All @@ -71,20 +71,20 @@ def set_n_iter_default(n_iter, mask=False):
def set_tol_pinv_default(tol_pinv, mask=False):
if (tol_pinv > 1) or (tol_pinv < 0):
raise ValueError('tol_pinv must be between 0 and 1.')
nmt_params.tol_pinv = tol_pinv
nmt_params.tol_pinv_default = tol_pinv


def get_default_params():
return {k: getattr(nmt_params, k)
for k in ['sht_calculator',
'n_iter_default',
'n_iter_mask_default',
'tol_pinv']}
'tol_pinv_default']}


class _SHTInfo(object):
def __init__(self, nring, theta, phi0, nphi, weight,
is_CAR=False, nx_short=-1, nx_full=-1):
is_CAR=False, nx_short=-1, nx_full=-1, nside=-1):
self.nring = nring
self.theta = theta
self.phi0 = phi0
Expand All @@ -97,6 +97,7 @@ def __init__(self, nring, theta, phi0, nphi, weight,
off = np.concatenate([[0], off[:-1]])
self.offsets = off.astype(np.uint64, copy=False)
self.weight = weight
self.nside = nside

@classmethod
def from_rectpix(cls, n_theta, theta_min, d_theta,
Expand Down Expand Up @@ -152,7 +153,7 @@ def from_nside(cls, nside):
theta[south] = np.pi-theta[south]
weight = 4*np.pi/npix
return cls(nring=nring, theta=theta, phi0=phi0,
nphi=nphi, weight=weight)
nphi=nphi, weight=weight, nside=nside)

def pad_map(self, maps):
if not self.is_CAR:
Expand Down Expand Up @@ -666,8 +667,7 @@ def _alm2map_healpy(alm, spin, sht_info, alm_info):
raise ValueError("Can't use healpy for CAR maps")
kwargs = {'lmax': alm_info.lmax, 'mmax': alm_info.mmax}
if spin == 0:
map = [hp.alm2map(alm, mside=sht_info.nside, verbose=False,
**kwargs)]
map = [hp.alm2map(alm[0], nside=sht_info.nside, **kwargs)]
else:
map = hp.alm2map_spin(alm, sht_info.nside, spin, **kwargs)
return np.array(map)
Expand All @@ -678,7 +678,7 @@ def _map2alm_healpy(map, spin, sht_info, alm_info):
raise ValueError("Can't use healpy for CAR maps")
kwargs = {'lmax': alm_info.lmax, 'mmax': alm_info.mmax}
if spin == 0:
alm = [hp.map2alm(map, **kwargs)]
alm = [hp.map2alm(map[0], iter=0, **kwargs)]
else:
alm = hp.map2alm_spin(map, spin, **kwargs)
return np.array(alm)
Expand Down

0 comments on commit 27c2dd8

Please sign in to comment.