33import 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+
8240def 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
0 commit comments