Skip to content

Commit 9b13808

Browse files
authored
Fix a few bugs and investigate issue with C compilers in mac (#238)
* fixed two easy bugs * catalog2alm parallel * better CAR tests
1 parent 6ec307f commit 9b13808

File tree

8 files changed

+270
-183
lines changed

8 files changed

+270
-183
lines changed

pymaster/tests/test_master_car.py

Lines changed: 51 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,54 @@
33
import pymaster as nmt
44

55

6-
class WorkspaceTesterCAR(object):
6+
class CARTester(object):
77
def __init__(self):
88
from astropy.io import fits
99
from astropy.wcs import WCS
1010

11-
# Read mask
1211
hdul = fits.open("test/benchmarks/msk_car.fits")
1312
self.msk = hdul[0].data
14-
# Set up coordinates
1513
self.wcs = WCS(hdul[0].header)
16-
self.nx, self.ny = self.wcs.pixel_shape
1714
hdul.close()
18-
# Read maps
15+
1916
hdul = fits.open("test/benchmarks/mps_car.fits")
20-
self.mps = hdul[0].data
17+
self.mp = hdul[0].data
2118
hdul.close()
2219

23-
self.minfo = nmt.NmtMapInfo(self.wcs, (self.ny, self.nx))
24-
self.lmax = self.minfo.get_lmax()
25-
self.nlb = 50
26-
self.npix = self.minfo.npix
27-
self.b = nmt.NmtBin.from_lmax_linear(self.lmax, self.nlb)
28-
(self.l, self.cltt, self.clte,
29-
self.clee, self.clbb, self.cltb,
30-
self.cleb) = np.loadtxt("test/benchmarks/pspy_cls.txt", unpack=True)
31-
self.f0 = nmt.NmtField(self.msk, [self.mps[0]], wcs=self.wcs, n_iter=0)
32-
self.f0_half = nmt.NmtField(self.msk[:self.ny//2, :self.nx//2],
33-
[self.mps[0][:self.ny//2, :self.nx//2]],
34-
wcs=self.wcs, n_iter=0)
35-
self.b_half = nmt.NmtBin.from_lmax_linear(self.lmax//2, self.nlb)
36-
self.b_doub = nmt.NmtBin.from_lmax_linear(self.lmax*2, self.nlb)
37-
self.n_good = np.zeros([1, (self.lmax+1)])
38-
self.n_bad = np.zeros([2, (self.lmax+1)])
39-
self.n_half = np.zeros([1, (self.lmax//2+1)])
40-
41-
42-
WT = WorkspaceTesterCAR()
43-
44-
45-
@pytest.mark.skipif(True, reason='slow')
46-
def test_workspace_car_master():
47-
f0 = nmt.NmtField(WT.msk, [WT.mps[0]],
48-
wcs=WT.wcs, n_iter=0)
49-
f2 = nmt.NmtField(WT.msk, [WT.mps[1], WT.mps[2]],
50-
wcs=WT.wcs, n_iter=0)
51-
f = [f0, f2]
52-
53-
for ip1 in range(2):
54-
for ip2 in range(ip1, 2):
55-
if ip1 == ip2 == 0:
56-
cl_bm = np.array([WT.cltt])
57-
elif ip1 == ip2 == 1:
58-
cl_bm = np.array([WT.clee, WT.cleb,
59-
WT.cleb, WT.clbb])
60-
else:
61-
cl_bm = np.array([WT.clte, WT.cltb])
62-
w = nmt.NmtWorkspace.from_fields(f[ip1], f[ip2], WT.b)
63-
cl = w.decouple_cell(nmt.compute_coupled_cell(f[ip1], f[ip2]))
64-
assert np.amax(np.fabs(cl-cl_bm)) <= 1E-10
65-
66-
# TEB
67-
w = nmt.NmtWorkspace.from_fields(f[0], f[1], WT.b,
68-
is_teb=True)
69-
c00 = nmt.compute_coupled_cell(f[0], f[0])
70-
c02 = nmt.compute_coupled_cell(f[0], f[1])
71-
c22 = nmt.compute_coupled_cell(f[1], f[1])
72-
cl = w.decouple_cell(np.array([c00[0], c02[0], c02[1],
73-
c22[0], c22[1], c22[2],
74-
c22[3]]))
75-
cl_bm = np.array([WT.cltt, WT.clte, WT.cltb,
76-
WT.clee, WT.cleb, WT.cleb,
77-
WT.clbb])
78-
assert np.amax(np.fabs(cl-cl_bm)) <= 1E-10
79-
80-
81-
@pytest.mark.skipif(False, reason='slow')
20+
self.leff, self.cl_bm = np.loadtxt(
21+
"test/benchmarks/bm_car.txt", unpack=True)
22+
23+
self.lmax = 100
24+
self.f0 = nmt.NmtField(self.msk, [self.mp],
25+
wcs=self.wcs, lmax=self.lmax)
26+
self.b = nmt.NmtBin.from_lmax_linear(self.lmax, nlb=5)
27+
28+
29+
WT = CARTester()
30+
31+
32+
def test_cl_car():
33+
w = nmt.NmtWorkspace.from_fields(WT.f0, WT.f0, WT.b)
34+
cl = w.decouple_cell(nmt.compute_coupled_cell(WT.f0,
35+
WT.f0))[0]
36+
37+
assert np.amax(np.fabs(cl-WT.cl_bm)) <= 1E-10
38+
39+
8240
def test_workspace_car_methods():
8341
w = nmt.NmtWorkspace.from_fields(WT.f0, WT.f0,
8442
WT.b) # OK init
8543
# Incompatible bandpowers
8644
with pytest.raises(ValueError):
87-
w.compute_coupling_matrix(WT.f0, WT.f0,
88-
WT.b_doub)
45+
bd = nmt.NmtBin.from_lmax_linear(2*WT.lmax, nlb=5)
46+
w.compute_coupling_matrix(WT.f0, WT.f0, bd)
47+
8948
# Incompatible resolutions
90-
assert not WT.f0.is_compatible(WT.f0_half)
49+
nx, ny = WT.wcs.pixel_shape
50+
fhalf = nmt.NmtField(WT.msk[:ny//2, :nx//2],
51+
[WT.mp[:ny//2, :nx//2]],
52+
wcs=WT.wcs, lmax=WT.lmax)
53+
assert not WT.f0.is_compatible(fhalf)
9154

9255
# Wrong fields for TEB
9356
with pytest.raises(RuntimeError):
@@ -97,32 +60,36 @@ def test_workspace_car_methods():
9760
w.compute_coupling_matrix(WT.f0, WT.f0, WT.b)
9861

9962
# Test couple_cell
100-
c = w.couple_cell(WT.n_good)
63+
n_good = np.ones([1, WT.lmax+1])
64+
n_bad = np.ones([2, WT.lmax+1])
65+
n_half = np.ones([1, WT.lmax//2+1])
66+
c = w.couple_cell(n_good)
10167
assert c.shape == (1, w.wsp.lmax+1)
10268
with pytest.raises(ValueError):
103-
w.couple_cell(WT.n_bad)
69+
w.couple_cell(n_bad)
10470
with pytest.raises(ValueError):
105-
w.couple_cell(WT.n_half)
71+
w.couple_cell(n_half)
10672

10773
# Test decouple_cell
108-
c = w.decouple_cell(WT.n_good)
74+
c = w.decouple_cell(n_good)
10975
assert c.shape == (1, WT.b.bin.n_bands)
11076
with pytest.raises(ValueError):
111-
w.decouple_cell(WT.n_bad)
77+
w.couple_cell(n_bad)
11278
with pytest.raises(ValueError):
113-
w.decouple_cell(WT.n_half)
114-
c = w.decouple_cell(WT.n_good, cl_bias=WT.n_good,
115-
cl_noise=WT.n_good)
79+
w.couple_cell(n_half)
80+
c = w.decouple_cell(n_good, cl_bias=n_good,
81+
cl_noise=n_good)
11682
assert c.shape == (1, WT.b.bin.n_bands)
11783
with pytest.raises(ValueError):
118-
w.decouple_cell(WT.n_good, cl_bias=WT.n_good,
119-
cl_noise=WT.n_bad)
84+
w.decouple_cell(n_good, cl_bias=n_good,
85+
cl_noise=n_bad)
12086
with pytest.raises(ValueError):
121-
w.decouple_cell(WT.n_good, cl_bias=WT.n_good,
122-
cl_noise=WT.n_half)
87+
w.decouple_cell(n_good, cl_bias=n_good,
88+
cl_noise=n_half)
12389
with pytest.raises(ValueError):
124-
w.decouple_cell(WT.n_good, cl_bias=WT.n_bad,
125-
cl_noise=WT.n_good)
90+
w.decouple_cell(n_good, cl_bias=n_bad,
91+
cl_noise=n_good)
12692
with pytest.raises(ValueError):
127-
w.decouple_cell(WT.n_good, cl_bias=WT.n_half,
128-
cl_noise=WT.n_good)
93+
w.decouple_cell(n_good, cl_bias=n_half,
94+
cl_noise=n_good)
95+
return

pymaster/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,11 +849,12 @@ def _map2alm_healpy(map, spin, sht_info, alm_info):
849849
'healpy': _alm2map_healpy}
850850

851851

852-
def _alm2catalog_ducc0(alms, positions, spin, lmax):
852+
def _alm2catalog_ducc0(alms, positions, spin, lmax, nthreads=0):
853853
alms = np.atleast_2d(alms)
854854
values = ducc0.sht.synthesis_general(alm=alms, spin=spin,
855855
lmax=lmax, loc=positions.T,
856-
epsilon=1E-5)
856+
epsilon=1E-5,
857+
nthreads=nthreads)
857858
return values
858859

859860

pyproject.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "pymaster"
10-
version = "2.5"
10+
version = "2.5.1"
1111
authors = [
1212
{name="David Alonso", email="[email protected]"}
1313
]
1414
description = "Library for pseudo-Cl computation"
1515
readme = {file = "README.md", content-type = "text/markdown"}
1616
license = {file = "LICENSE"}
17-
requires-python = ">=3.8"
17+
requires-python = ">=3.10"
1818
dependencies = [
1919
"healpy",
2020
"ducc0"
2121
]
2222
classifiers=[
2323
'License :: OSI Approved :: BSD License',
24-
'Programming Language :: Python :: 2',
25-
'Programming Language :: Python :: 2.7',
2624
'Programming Language :: Python :: 3',
27-
'Programming Language :: Python :: 3.6',
2825
'Operating System :: Unix',
2926
'Operating System :: MacOS'
3027
]

test/benchmarks/bm_car.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
4.000000000000000000e+00 5.510475822947054347e-02
2+
9.000000000000000000e+00 4.207912493588251979e-02
3+
1.400000000000000000e+01 4.075652624298726351e-02
4+
1.900000000000000000e+01 3.532924063446543639e-02
5+
2.400000000000000000e+01 2.892971272739440838e-02
6+
2.900000000000000000e+01 2.816949340835114488e-02
7+
3.400000000000000000e+01 2.007970877841726634e-02
8+
3.900000000000000000e+01 1.992746104802381457e-02
9+
4.400000000000000000e+01 1.846897729604191768e-02
10+
4.900000000000000000e+01 1.750155971048967737e-02
11+
5.400000000000000000e+01 1.551095715555601134e-02
12+
5.900000000000000000e+01 1.640164463469220499e-02
13+
6.400000000000000000e+01 1.279488746149882658e-02
14+
6.900000000000000000e+01 1.284001677258483388e-02
15+
7.400000000000000000e+01 1.268205528084791721e-02
16+
7.900000000000000000e+01 1.205514296421096414e-02
17+
8.400000000000000000e+01 1.143024725294552839e-02
18+
8.900000000000000000e+01 1.033587911480781697e-02
19+
9.400000000000000000e+01 9.231566612771795188e-03

test/benchmarks/generate_benchmarks.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ def getmaskapoana_car(w,aps,fsk=0.1,dec0=-50,ra0=0.) :
9292
#write_flat_map("msk_car_small.fits",mask,wcs,"mask")
9393

9494

95+
96+
from pixell import enmap, utils, enplot, curvedsky
97+
98+
shape, wcs = enmap.fullsky_geometry(res=np.deg2rad(0.5))
99+
lmax = 100
100+
ls = np.arange(lmax+1)
101+
cl_in = 1/(ls+10)
102+
103+
mp = curvedsky.rand_map(shape, wcs, ps=cl_in)
104+
b = nmt.NmtBin.from_lmax_linear(lmax, nlb=5)
105+
cl1 = b.bin_cell(hp.alm2cl(curvedsky.map2alm(mp, lmax=lmax)))
106+
107+
mp.write("mps_car_b.fits")
108+
109+
msk = enmap.ones(shape, wcs)
110+
msk.write("msk_car_b.fits")
111+
112+
np.savetxt("bm_car.txt",
113+
np.transpose([b.get_effective_ells(), cl1]))
114+
115+
116+
117+
118+
95119
##########################
96120
# Flat-sky stuff
97121
wcs,msk=read_flat_map("msk_flat.fits")

test/benchmarks/mps_car.fits

-12.8 MB
Binary file not shown.

test/benchmarks/msk_car.fits

-2.96 MB
Binary file not shown.

0 commit comments

Comments
 (0)