alessiospuriomancini / cosmopower

Machine Learning - accelerated Bayesian inference
https://alessiospuriomancini.github.io/cosmopower
GNU General Public License v3.0
59 stars 26 forks source link

KeyError: 'obch2 is not a file in the archive' #7

Closed Eminencetoosure closed 2 years ago

Eminencetoosure commented 2 years ago

import numpy as np import pyDOE as pyDOE

number of parameters and samples

n_params = 7 n_samples = 400000

parameter ranges

obh2 = np.linspace(0.019, 0.026, n_samples) omch2 = np.linspace(0.051, 0.255, n_samples) h0 = np.linspace(0.64, 0.82, n_samples) n_s = np.linspace(0.84, 1.1, n_samples) s_8_input = np.linspace(0.1, 1.3, n_samples) logt_agn = np.linspace(7.6, 8.0, n_samples) A = np.linspace(-6.0, 6.0, n_samples)

LHS grid

AllParams = np.vstack([obh2, omch2, h0, n_s, s_8_input, logt_agn, A]) lhd = pyDOE.lhs(n_params, samples=n_samples, criterion=None) idx = (lhd * n_samples).astype(int)

AllCombinations = np.zeros((n_samples, n_params)) for i in range(n_params): AllCombinations[:, i] = AllParams[i][idx[:, i]]

saving

params = {'obh2': AllCombinations[:, 0], 'omch2': AllCombinations[:, 1], 'h0': AllCombinations[:, 2], 'n_s': AllCombinations[:, 3], 's_8_input': AllCombinations[:, 4], 'logt_agn': AllCombinations[:, 5], 'A': AllCombinations[:, 6] }

np.savez('your_LHS_parameter_file.npz', **params)

/Users/apple/base/lib/python3.9/site-packages/numpy/lib/npyio.py:232: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison if key in self._files: /Users/apple/base/lib/python3.9/site-packages/numpy/lib/npyio.py:234: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison elif key in self.files:

KeyError Traceback (most recent call last) Input In [18], in <cell line: 4>() 1 np.savez_compressed('your_LHS_parameter_file.npz', obh2=obh2, omch2=omch2, h0=h0, n_s=n_s, s_8_input=s_8_input, 2 logt_agn=logt_agn, A=A) 3 b = np.load('your_LHS_parameter_file.npz') ----> 4 print(b[A])

File ~/base/lib/python3.9/site-packages/numpy/lib/npyio.py:249, in NpzFile.getitem(self, key) 247 return self.zip.read(key) 248 else: --> 249 raise KeyError("%s is not a file in the archive" % key)

KeyError: '[-6. -5.99997 -5.99994 ... 5.99994 5.99997 6. ] is not a file in the archive'

dpiras commented 2 years ago

Hi @Eminencetoosure, thank you for your interest in cosmopower!

Could you please help us understand the issue that you found? I can see that the problem arises when you load your saved dictionary and call a particular key; could you please confirm this is the case?

Eminencetoosure commented 2 years ago

yes, I was trying to call my dictionary to check the associate value. Hence I got the exception err.

Please can I understand why and I will like you to provide me with the solution to this?

Warm regards Victor.

On Sun, Jul 10, 2022 at 12:10 AM Davide Piras @.***> wrote:

Hi @Eminencetoosure https://github.com/Eminencetoosure, thank you for your interest in cosmopower!

Could you please help us understand the issue that you found? I can see that the problem arises when you load your saved dictionary and call a particular key; could you please confirm this is the case?

— Reply to this email directly, view it on GitHub https://github.com/alessiospuriomancini/cosmopower/issues/7#issuecomment-1179619395, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5L3WLNCJ4YUBK3AGSLXRDVTIBFNANCNFSM53EBPYTA . You are receiving this because you were mentioned.Message ID: @.***>

dpiras commented 2 years ago

Thank you for confirming! We think that there is a simple typo in your code. When you call a dictionary through a key, you need to write

print(b['A'])

namely with the single quote around the key A. Does this fix the problem for you?

Eminencetoosure commented 2 years ago

Thank you for sharing that. but can you please tell me why im getting same error message in the message attached to this email?

On Sun, Jul 10, 2022 at 12:15 AM Victor Olatunji @.***> wrote:

yes, I was trying to call my dictionary to check the associate value. Hence I got the exception err.

Please can I understand why and I will like you to provide me with the solution to this?

Warm regards Victor.

On Sun, Jul 10, 2022 at 12:10 AM Davide Piras @.***> wrote:

Hi @Eminencetoosure https://github.com/Eminencetoosure, thank you for your interest in cosmopower!

Could you please help us understand the issue that you found? I can see that the problem arises when you load your saved dictionary and call a particular key; could you please confirm this is the case?

— Reply to this email directly, view it on GitHub https://github.com/alessiospuriomancini/cosmopower/issues/7#issuecomment-1179619395, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5L3WLNCJ4YUBK3AGSLXRDVTIBFNANCNFSM53EBPYTA . You are receiving this because you were mentioned.Message ID: @.***>

Eminencetoosure commented 2 years ago

import numpy as np import classy import sys

krange1 = np.logspace(np.log10(1e-5), np.log10(1e-4), num=20, endpoint=False) krange2 = np.logspace(np.log10(1e-4), np.log10(1e-3), num=40, endpoint=False) krange3 = np.logspace(np.log10(1e-3), np.log10(1e-2), num=60, endpoint=False) krange4 = np.logspace(np.log10(1e-2), np.log10(1e-1), num=80, endpoint=False) krange5 = np.logspace(np.log10(1e-1), np.log10(1), num=100, endpoint=False) krange6 = np.logspace(np.log10(1), np.log10(10), num=120, endpoint=False)

k = np.concatenate((krange1, krange2, krange3, krange4, krange5, krange6)) num_k = len(k) # 420 k-modes np.savetxt('k_modes.txt', k)

cosmo = classy.Class()

params_lhs = np.load('your_LHS_parameter_file.npz')

def spectra_generation(i): print('parameter set ', i)

# Define the cosmology parameter (what is not specified will be set to CLASS default parameters)
params = {'output': 'tCl mPk',
         'non linear':'hmcode',
         'z_max_pk': 5, 
         'P_k_max_1/Mpc': 10.,
         'nonlinear_min_k_max': 100.,
         'N_ncdm' : 0,
         'N_eff' : 3.046,
         'obch2': params_lhs['obch2'][i],
         'omch2': params_lhs['omch2'][i],
         'h0': params_lhs['h0'][i], 
         'n_s': params_lhs['n_s'][i],
         's_8_input': params_lhs['s_8_input'][i],
         'logt_agn': params_lhs['logt_agn'][i],
         'A': params_lhs["A"][i], 
         }

# Set the parameters to the cosmological code
cosmo.set(params)

try:
    cosmo.compute()
    z = params_lhs['z'][i]

    # non linear power spectrum 
    Pnonlin = np.array([cosmo.pk(ki, z) for ki in k])

    # linear power spectrum
    Plin = np.array([cosmo.pk_lin(ki, z) for ki in k])
    cosmo_array = np.hstack(([params_lhs[k][i] for k in params_lhs], Plin))
    f=open('./linear.dat','ab')
    np.savetxt(f, [cosmo_array])
    f.close()

    # non linear boost
    ratio = Pnonlin/Plin
    cosmo_array = np.hstack(([params_lhs[k][i] for k in params_lhs], ratio))
    f=open('./boost.dat','ab')
    np.savetxt(f, [cosmo_array])
    f.close()

# parameter set rejected by Class
except classy.CosmoComputationError as failure_message:
    print(str(failure_message)+'\n')
    cosmo.struct_cleanup()
    cosmo.empty()

# something wrong in Class init
except classy.CosmoSevereError as critical_message:
    print("Something went wrong when calling CLASS" + str(critical_message))
    cosmo.struct_cleanup()
    cosmo.empty()
return

loop over parameter sets

for i in range(len(params_lhs['obch2'])): spectra_generation(i)

KeyError Traceback (most recent call last) Input In [11], in <cell line: 80>() 77 return 79 # loop over parameter sets ---> 80 for i in range(len(params_lhs['obch2'])): 81 spectra_generation(i)

File ~/base/lib/python3.9/site-packages/numpy/lib/npyio.py:249, in NpzFile.getitem(self, key) 247 return self.zip.read(key) 248 else: --> 249 raise KeyError("%s is not a file in the archive" % key)

KeyError: 'obch2 is not a file in the archive'

dpiras commented 2 years ago

This is a different bit of code, right? If so, I believe the problem is simply that obch2 is not a key of params_lhs. Possible keys that you probably meant are obh2 or omch2.

You can print the keys of a dictionary (in this case params_lhs) by typing: print(params_lhs.keys())

Hope this helps?

Eminencetoosure commented 2 years ago

Finally, it work tho.. it was typo error ..

Thank you so much for the support. David.

Eminencetoosure commented 2 years ago

import numpy as np import classy import sys

krange1 = np.logspace(np.log10(1e-5), np.log10(1e-4), num=20, endpoint=False) krange2 = np.logspace(np.log10(1e-4), np.log10(1e-3), num=40, endpoint=False) krange3 = np.logspace(np.log10(1e-3), np.log10(1e-2), num=60, endpoint=False) krange4 = np.logspace(np.log10(1e-2), np.log10(1e-1), num=80, endpoint=False) krange5 = np.logspace(np.log10(1e-1), np.log10(1), num=100, endpoint=False) krange6 = np.logspace(np.log10(1), np.log10(10), num=120, endpoint=False)

k = np.concatenate((krange1, krange2, krange3, krange4, krange5, krange6)) num_k = len(k) # 420 k-modes np.savetxt('k_modes.txt', k)

cosmo = classy.Class()

params_lhs = np.load('your_LHS_parameter_file.npz')

def spectra_generation(i): print('parameter set ', i)

# Define the cosmology parameter (what is not specified will be set to CLASS default parameters)
params = {'output': 'tCl mPk',
         'non linear':'hmcode',
         'z_max_pk': 5, 
         'P_k_max_1/Mpc': 10.,
         'nonlinear_min_k_max': 100.,
         'N_ncdm' : 0,
         'N_eff' : 3.046,
         'obch2': params_lhs['obch2'][i],
         'omch2': params_lhs['omch2'][i],
         'h0': params_lhs['h0'][i], 
         'n_s': params_lhs['n_s'][i],
         's_8_input': params_lhs['s_8_input'][i],
         'logt_agn': params_lhs['logt_agn'][i],
         'A': params_lhs["A"][i], 
         }

# Set the parameters to the cosmological code
cosmo.set(params)

try:
    cosmo.compute()
    z = params_lhs['z'][i]

    # non linear power spectrum 
    Pnonlin = np.array([cosmo.pk(ki, z) for ki in k])

    # linear power spectrum
    Plin = np.array([cosmo.pk_lin(ki, z) for ki in k])
    cosmo_array = np.hstack(([params_lhs[k][i] for k in params_lhs], Plin))
    f=open('/Users/apple/Downloads/linear.dat','ab',)
    np.savetxt(f, [cosmo_array])
    f.close() 

    # non linear boost
    ratio = Pnonlin/Plin
    cosmo_array = np.hstack(([params_lhs[k][i] for k in params_lhs], ratio))
    f=open('/Users/apple/Downloads/boost.dat','ab')
    np.savetxt(f, [cosmo_array]) 
    f.close()

# parameter set rejected by Class
except classy.CosmoComputationError as failure_message:
    print(str(failure_message)+'\n')
    cosmo.struct_cleanup()
    cosmo.empty()

# something wrong in Class init
except classy.CosmoSevereError as critical_message:
    print("Something went wrong when calling CLASS" + str(critical_message))
    cosmo.struct_cleanup()
    cosmo.empty()
return

loop over parameter sets

for i in range(len(params_lhs['obch2'])):

spectra_generation(i)

spectra_generation(i)

parameter set 68195 Something went wrong when calling CLASS

Error in Class: Class did not read input parameter(s): obch2, omch2, h0, s_8_input, logt_agn, A

Please my function has failed to write linear. dat and boost.dat file in my local disk. if you can help me look through why the function has failed to create the linear and boost files

Thank you so much David

dpiras commented 2 years ago

Hi Victor, it looks like you are using the wrong keys to call CLASS; for instance, h0 should be just h. Could you please let us know which code did you base your snippet on? Did you have a chance to look at this generation script?

Eminencetoosure commented 2 years ago

Hello David, thank you for reaching out to me and I must say you are very kind. As regard, the question h0 is the class from my cosmology parameter, not h as I have declared it h0 from the start of my project.

Well I will work on correction you made and i will get back to you.

dpiras commented 2 years ago

Just let us know if we can help you further, Victor! To clarify, I meant that you should write e.g.:

'h': params_lhs['h0'][i],

since CLASS requires the parameter h to be specified.

Eminencetoosure commented 2 years ago

try: cosmo.compute() z = params_lhs['z'][i]

can i please know what "z" represent as none of my npz data file posses dictionary key "z" .

dpiras commented 2 years ago

z indicates redshift; you can find more at this script.