17
17
from cobaya .install import download_file , download_github_release , pip_install
18
18
from cobaya .likelihood import Likelihood
19
19
from cobaya .log import LoggedError , get_logger
20
- from cobaya .tools import VersionCheckError , are_different_params_lists , create_banner
20
+ from cobaya .tools import VersionCheckError , are_different_params_lists
21
21
22
22
pla_url_prefix = r"https://pla.esac.esa.int/pla-sl/data-action?COSMOLOGY.COSMOLOGY_OID="
23
23
@@ -42,7 +42,7 @@ def initialize(self):
42
42
self .packages_path
43
43
)
44
44
# Load clipy instead of clik
45
- clik = load_clipy (
45
+ clipy = load_clipy (
46
46
path = self .path ,
47
47
install_path = install_path ,
48
48
logger = self .log ,
@@ -58,6 +58,8 @@ def initialize(self):
58
58
self .log ,
59
59
f"{ excpt } . To install a new version, { msg_to_install } with `--upgrade`." ,
60
60
) from excpt
61
+ # Disable JAX to avoid dependency issues
62
+ os .environ ["CLIPY_NOJAX" ] = "1"
61
63
# Loading the likelihood data
62
64
data_path = get_data_path (self .__class__ .get_qualified_class_name ())
63
65
if not os .path .isabs (self .clik_file ):
@@ -67,51 +69,46 @@ def initialize(self):
67
69
os .path .join (self .path or self .packages_path , "data" , data_path ),
68
70
)
69
71
self .clik_file = os .path .join (self .path_data , self .clik_file )
70
- # clipy handles both lensing and non-lensing likelihoods with single constructor
71
72
try :
72
- # Disable JAX to avoid dependency issues
73
- os . environ [ "CLIPY_NOJAX" ] = "1"
74
- self .clik = clik .clik (self .clik_file )
75
- except Exception as excpt :
73
+ self . commands = None
74
+
75
+ self .clik_likelihood = clipy .clik (self .clik_file , ** ( self . commands or {}) )
76
+ except clipy . clik_emul_error as excpt :
76
77
# Is it that the file was not found?
77
78
if not os .path .exists (self .clik_file ):
78
79
raise ComponentNotInstalledError (
79
80
self .log ,
80
- "The .clik file was not found where specified in the "
81
- "'clik_file' field of the settings of this likelihood. "
82
- "Maybe the 'path' given is not correct? The full path where "
83
- " the .clik file was searched for is '%s'" ,
84
- self .clik_file ,
81
+ "The .clik file was not found where specified in the 'clik_file' "
82
+ "field of the settings of this likelihood. Install this likelihood "
83
+ f"with 'cobaya-install { self . get_qualified_class_name () } '. If this "
84
+ "error persists, maybe the 'path' given is not correct? The full path"
85
+ " where the .clik file was searched for is '{ self.clik_file}'" ,
85
86
) from excpt
86
87
# Else: unknown clipy error
87
- self .log .error (
88
- "An unexpected error occurred in clipy (possibly related to "
89
- "multiple simultaneous initialization, or simultaneous "
90
- "initialization of incompatible likelihoods; e.g. polarised "
91
- "vs non-polarised 'lite' likelihoods. See error info below:"
92
- )
93
- raise
94
- self .l_maxs = self .clik .get_lmax ()
95
- # calculate requirements here so class can also be separately instantiated
96
- requested_cls = ["tt" , "ee" , "bb" , "te" , "tb" , "eb" ]
97
- # clipy automatically handles lensing detection, but we need to check the lmax values
98
- has_cl = [lmax != - 1 for lmax in self .l_maxs ]
99
- # Check if this is a lensing likelihood by examining the structure
100
- if len (self .l_maxs ) > 6 and self .l_maxs [0 ] != - 1 :
101
- # First element is pp for lensing likelihoods
102
- self .lensing = True
103
- requested_cls = ["pp" ] + requested_cls
104
- else :
105
- self .lensing = False
106
- # For non-lensing, use get_has_cl if available
107
- if hasattr (self .clik , "get_has_cl" ):
108
- has_cl = self .clik .get_has_cl ()
109
- self .requested_cls = [cl for cl , i in zip (requested_cls , has_cl ) if int (i )]
110
- self .l_maxs_cls = [lmax for lmax , i in zip (self .l_maxs , has_cl ) if int (i )]
111
- self .expected_params = list (self .clik .extra_parameter_names )
88
+ raise LoggedError (
89
+ self .log ,
90
+ f"An unexpected error occurred in clipy: { excpt } " ,
91
+ ) from excpt
92
+ except Exception as excpt : # unknown clippy error
93
+ raise LoggedError (
94
+ self .log ,
95
+ f"An unmanaged error occurred in clipy: { excpt } . Please report it as a "
96
+ "GitHub issue in the Cobaya repo." ,
97
+ ) from excpt
98
+ lmaxs = self .clik_likelihood .lmax
99
+ cls_sorted = ["tt" , "ee" , "bb" , "te" , "tb" , "eb" ]
100
+ if len (lmaxs ) > 6 : # lensing likelihood!
101
+ cls_sorted = ["pp" ] + cls_sorted
102
+ self .requested_cls_lmax = {
103
+ cl : lmax for cl , lmax in zip (cls_sorted , lmaxs ) if lmax != - 1
104
+ }
105
+ self .expected_params = list (self .clik_likelihood .extra_parameter_names )
112
106
# Placeholder for vector passed to clipy
113
- length = len (self .l_maxs ) if self .lensing else len (has_cl )
114
- self .vector = np .zeros (np .sum (self .l_maxs ) + length + len (self .expected_params ))
107
+ self .vector = np .zeros (
108
+ sum (list (self .requested_cls_lmax .values ()))
109
+ + len (self .requested_cls_lmax ) # account for ell=0
110
+ + len (self .expected_params )
111
+ )
115
112
116
113
def initialize_with_params (self ):
117
114
# Check that the parameters are the right ones
@@ -129,45 +126,35 @@ def initialize_with_params(self):
129
126
130
127
def get_requirements (self ):
131
128
# State requisites to the theory code
132
- return {"Cl" : dict ( zip ( self .requested_cls , self . l_maxs_cls )) }
129
+ return {"Cl" : self .requested_cls_lmax }
133
130
134
131
def logp (self , ** params_values ):
135
- # get Cl's from the theory code
132
+ # Get Cl's from the theory code
136
133
cl = self .provider .get_Cl (units = "FIRASmuK2" )
137
134
return self .log_likelihood (cl , ** params_values )
138
135
139
136
def log_likelihood (self , cl , ** params_values ):
140
- # fill with Cl's
137
+ # Fill with Cl's
141
138
self .vector [: - len (self .expected_params )] = np .concatenate (
142
139
[
143
- (
144
- cl [spectrum ][: 1 + lmax ]
145
- if spectrum not in ["tb" , "eb" ]
146
- else np .zeros (1 + lmax )
147
- )
148
- for spectrum , lmax in zip (self .requested_cls , self .l_maxs_cls )
140
+ cl [spec ][: 1 + lmax ] if spec not in ["tb" , "eb" ] else np .zeros (1 + lmax )
141
+ for spec , lmax in self .requested_cls_lmax .items ()
149
142
]
150
143
)
151
144
# check for nan's: may produce issues in clipy
152
145
# dot product is apparently the fastest way in threading-enabled numpy
153
146
if np .isnan (np .dot (self .vector , self .vector )):
154
147
return - np .inf
155
- # fill with likelihood parameters
148
+ # Fill with likelihood parameters
156
149
self .vector [- len (self .expected_params ) :] = [
157
150
params_values [p ] for p in self .expected_params
158
151
]
159
- # clipy returns a scalar, not an array like clik
160
- loglike = self .clik (self .vector )
161
- # Convert to Python float
162
- loglike = float (loglike )
152
+ loglike = self .clik_likelihood (self .vector )
163
153
# "zero" of clipy, and sometimes nan's returned
164
- if np . allclose ( loglike , - 1e30 ) or np .isnan (loglike ):
154
+ if loglike <= - 1e30 or np .isnan (loglike ):
165
155
loglike = - np .inf
166
156
return loglike
167
157
168
- def close (self ):
169
- del self .clik # Clean up clipy object
170
-
171
158
@classmethod
172
159
def get_code_path (cls , path ):
173
160
return os .path .realpath (os .path .join (path , "code" , common_path ))
0 commit comments