Skip to content

Commit

Permalink
testing done
Browse files Browse the repository at this point in the history
  • Loading branch information
damonge committed Oct 6, 2024
1 parent 6b48f30 commit 2a2b984
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
5 changes: 5 additions & 0 deletions pymaster/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ def compute_coupling_coefficients(self, fla1, fla2,
if flb2 is None:
flb2 = fla2

if np.any([fla1.anisotropic_mask, fla2.anisotropic_mask,
flb1.anisotropic_mask, flb2.anisotropic_mask]):
raise NotImplementedError("Covariance matrix estimation not "
"implemented for anisotropic weights.")

if (not (fla1.is_compatible(fla2) and
fla1.is_compatible(flb1) and
fla1.is_compatible(flb2))):
Expand Down
17 changes: 13 additions & 4 deletions pymaster/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ class NmtField(object):
deprojection and purification, but you don't care about
deprojection bias. This will significantly reduce the memory taken
up by the resulting object.
mask_22 (`array`): Array containing the mask corresponding to the
``22`` component of an anisotropic weight matrix. If not ``None``
the ``mask`` argument will be interpreted as the ``11`` component
of the same matrix. The off-diagonal component of the weight
matrix must then be passed as ``mask_12`` (below). If ``mask_22``
and ``mask_12`` are ``None``, isotropic weighting is assumed,
described by ``mask``.
mask_12 (`array`): Array containing the off-diagonal component of the
anisotropic weight matrix. See ``mask_22`` above.
"""
def __init__(self, mask, maps, *, spin=None, templates=None, beam=None,
purify_e=False, purify_b=False, n_iter=None, n_iter_mask=None,
Expand Down Expand Up @@ -233,11 +242,11 @@ def __init__(self, mask, maps, *, spin=None, templates=None, beam=None,
raise ValueError("Anisotropic masks can't be used with "
"scalar fields.")
if pure_any:
raise ValueError("Purification not implemented for "
"anisotropic masks")
raise NotImplementedError("Purification not implemented for "
"anisotropic masks")
if templates is not None:
raise ValueError("Contaminant deprojection not supported "
"for anisotropic masks.")
raise NotImplementedError("Contaminant deprojection not "
"supported for anisotropic masks.")

# 3. Check templates
if isinstance(templates, (list, tuple, np.ndarray)):
Expand Down
60 changes: 60 additions & 0 deletions pymaster/tests/test_master_anisotropic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
import numpy as np
import healpy as hp
import pymaster as nmt
import pytest


def test_anisotropic_weighting_smoke():
nside = 64
npix = hp.nside2npix(nside)
m = np.ones(npix)

f = nmt.NmtField(m, [m, m],
mask_12=0.5*m,
mask_22=2.0*m)
msk_a = f.get_anisotropic_mask()

assert np.allclose(msk_a[1], 0.5*m)
assert np.allclose(msk_a[0], -0.5*m)


def test_anisotropic_weighting_errors():
nside = 64
npix = hp.nside2npix(nside)
m = np.ones(npix)

# Field-level errors
# Must pass full weight matrix
with pytest.raises(ValueError):
nmt.NmtField(m, [m, m], mask_12=m)

# Non-positive-definite weight matrix
with pytest.raises(ValueError):
nmt.NmtField(m, [m, m], mask_22=0*m, mask_12=m)

# No anisotropic matrix for scalar fields
with pytest.raises(ValueError):
nmt.NmtField(m, None, spin=0, mask_22=m, mask_12=m*0)

# No anisotropic matrix for scalar fields (second check)
with pytest.raises(ValueError):
nmt.NmtField(m, [m], mask_22=m, mask_12=m*0)

# No purification with anisotropic weights
with pytest.raises(NotImplementedError):
nmt.NmtField(m, [m, m], mask_12=0*m, mask_22=m,
purify_b=True)

# No contaminant deprojection with anisotropic weights
with pytest.raises(NotImplementedError):
nmt.NmtField(m, [m, m], mask_12=0*m, mask_22=m,
templates=[[m, m]])

# Workspace-level errors
f = nmt.NmtField(m, [m, m],
mask_12=0.5*m,
mask_22=2.0*m)
b = nmt.NmtBin.from_nside_linear(nside, nlb=4)
with pytest.raises(NotImplementedError):
nmt.NmtWorkspace.from_fields(f, f, b, l_toeplitz=3)

# Covariance-level errors
with pytest.raises(NotImplementedError):
nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f)


def test_anisotropic_weighting():
Expand Down
7 changes: 1 addition & 6 deletions pymaster/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@ def test_moore_penrose_pinv():
# Pick up largest eigval
e = e[:, w > 0.1].squeeze()
# Should only have two non-zero eigvals
print(w)
assert np.sum(np.fabs(w) < 1E-15) == 2
# Check e is parallel to v
print(np.dot(e, v))
print(np.dot(e, e))
assert np.isclose(np.dot(e, v),
assert np.isclose(np.fabs(np.dot(e, v)),
np.sqrt(np.dot(e, e)))

# For invertible matrix, we just get
Expand All @@ -76,8 +73,6 @@ def test_moore_penrose_pinv():
m = np.diag(w)
im = nmt.utils.moore_penrose_pinvh(m, None)
imb = np.diag(1/w)
print(im)
print(imb)
assert np.allclose(im, imb)


Expand Down
4 changes: 2 additions & 2 deletions pymaster/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ def compute_coupling_matrix(self, fl1, fl2, bins, is_teb=False,

anisotropic_mask_any = fl1.anisotropic_mask or fl2.anisotropic_mask
if anisotropic_mask_any and (l_toeplitz >= 0):
raise ValueError("Toeplitz approximation not implemented for "
"anisotropic masks.")
raise NotImplementedError("Toeplitz approximation not "
"implemented for anisotropic masks.")
ut._toeplitz_sanity(l_toeplitz, l_exact, dl_band,
bins.bin.ell_max, fl1, fl2)

Expand Down

0 comments on commit 2a2b984

Please sign in to comment.