brinckmann / montepython_public

Public repository for the Monte Python Code
MIT License
93 stars 80 forks source link

KeyError:0 when using Bmodes and delensing in fake cmb likelihood #283

Closed Jimmy022 closed 1 year ago

Jimmy022 commented 2 years ago

Hi all,

I meet some errors when I set the options "core_m5.Bmodes" and "core_m5.delensing" in the likelihood of core-m5 to True. First, the software would return 'Artribute Error: 'core_m5' object has no attribute 'lmax' ' when I try to build fiducial file. I found this is because 'self.lmax+1' near montepython/likelihood_class.py", line 1460 should be changed to 'self.l_max' +1', to correspond to the definition in likelihood.

After then I meet another problem which I don't know how to solve, my command to build a fiducial file is 'python montepython/MontePython.py run -p fiducial.param -o output/test01 -N 1000 -f 0', and the error is as follows:

Traceback (most recent call last): File "/home/ustc/Works/COSPA/montepython_public/montepython/MontePython.py", line 40, in sys.exit(run()) File "/home/ustc/Works/COSPA/montepython_public/montepython/run.py", line 45, in run sampler.run(cosmo, data, command_line) File "/home/ustc/Works/COSPA/montepython_public/montepython/sampler.py", line 46, in run mcmc.chain(cosmo, data, command_line) File "/home/ustc/Works/COSPA/montepython_public/montepython/mcmc.py", line 474, in chain loglike = sampler.compute_lkl(cosmo, data) File "/home/ustc/Works/COSPA/montepython_public/montepython/sampler.py", line 775, in compute_lkl value = likelihood.loglkl(cosmo, data) File "/home/ustc/Works/COSPA/montepython_public/montepython/likelihood_class.py", line 1463, in loglkl cl[l]['bb']=cl_unlensed[l]['bb'] KeyError: 0

I printed the dict 'cl' and 'cl_unlensed', but I didn't find the problem. Please help me solve it.

Best, Jimmy

brinckmann commented 2 years ago

Hi Jimmy,

Line 1455 and 1464 should have [l] and ['bb'] inverted, i.e. cl['bb'][l]=cl_lensed['bb'][l] instead of cl[l]['bb']=cl_lensed[l]['bb'] and cl['bb'][l]=cl_unlensed['bb'][l] instead of cl[l]['bb'][l]=cl_unlensed[l]['bb']

Sorry about this bug, it and the one you already fixed (which you did correctly) will be fixed in the next version. The bugs are also mentioned in issue #267.

It probably doesn't affect you, but in addition to the Bmodes bug, there's also a bug with the OnlyTT flag when the LensingExtraction flag is used. At line 1584 the following should be added (properly indented so it's within the elif at line 1575):

if self.OnlyTT:
    Cov_obs = np.array([
        [self.Cl_fid[0, l], 0.*self.Cl_fid[self.index_tp, l]],
        [cltd_fid, cldd_fid]])
    Cov_the = np.array([
        [cl['tt'][l]+self.noise_T[l],  0.*math.sqrt(l*(l+1.))*cl['tp'][l]],
        [cltd,  cldd+self.Nldd[l]]])
else:

with the Cov_obs and Cov_the block afterwards (1585-1592) properly indented so it's inside the else. I'll probably also and a clarifying and not self.LensingExtraction on line 1595, although this is not needed from a coding perspective.

OnlyTT also wouldn't work with Bmodes. A fix similar to the one for TT + lensing would work. I might add this (although then the "OnlyTT" name doesn't really make sense), or at least add a warning.

Best, Thejs

Jimmy022 commented 2 years ago

Thank you Thejs! It works.

brinckmann commented 1 year ago

Additionally to this issue, the ell range should be changed on line 1454 from for l in range(self.lmax+1): to for l in range(self.l_min,self.l_max+1): and line 1463 from for l in range(self.lmax+1): to for l in range(self.l_min,self.l_max+1):

This will also be fixed in the next version.

Best, Thejs

dchooper commented 1 year ago

I have implemented these bugfixes, they will be included in MP v3.6.0 (coming soon!).