55from sklearn .base import BaseEstimator , RegressorMixin
66from scipy .special import erf
77import matplotlib .pyplot as plt
8- from numpy import linspace , meshgrid , array
8+ from numpy import linspace , meshgrid , array , append
99import pylab
1010from numpy import ravel
1111from spotpython .utils .aggregate import aggregate_mean_var
12+ import logging
13+
14+ logger = logging .getLogger (__name__ )
15+ # configure the handler and formatter as needed
16+ py_handler = logging .FileHandler (f"{ __name__ } .log" , mode = "w" )
17+ py_formatter = logging .Formatter ("%(name)s %(asctime)s %(levelname)s %(message)s" )
18+ # add formatter to the handler
19+ py_handler .setFormatter (py_formatter )
20+ # add handler to the logger
21+ logger .addHandler (py_handler )
1222
1323
1424class Kriging (BaseEstimator , RegressorMixin ):
@@ -99,11 +109,18 @@ def __init__(
99109 if self .model_fun_evals is None :
100110 self .model_fun_evals = 100
101111
112+ # Logging information
113+ self .log = {}
114+ self .log ["negLnLike" ] = []
115+ self .log ["theta" ] = []
116+ self .log ["p" ] = []
117+ self .log ["Lambda" ] = []
118+
102119 self .logtheta_lambda_ = None
103120 self .U_ = None
104121 self .X_ = None
105122 self .y_ = None
106- self .NegLnLike_ = None
123+ self .negLnLike = None
107124 self .Psi_ = None
108125 if method not in ["interpolation" , "regression" , "reinterpolation" ]:
109126 raise ValueError ("method must be one of 'interpolation', 'regression', or 'reinterpolation']" )
@@ -127,7 +144,43 @@ def get_model_params(self) -> Dict[str, float]:
127144 Returns:
128145 dict: Parameter names not included in get_params() mapped to their values.
129146 """
130- return {"log_theta_lambda" : self .logtheta_lambda_ , "U" : self .U_ , "X" : self .X_ , "y" : self .y_ , "NegLnLike" : self .NegLnLike_ }
147+ return {"log_theta_lambda" : self .logtheta_lambda_ , "U" : self .U_ , "X" : self .X_ , "y" : self .y_ , "negLnLike" : self .negLnLike }
148+
149+ def _update_log (self ) -> None :
150+ """
151+ If spot_writer is not None, this method writes the current values of
152+ negLnLike, theta, p (if optim_p is True),
153+ and Lambda (if method is not "interpolation") to the spot_writer object.
154+
155+ Args:
156+ self (object): The Kriging object.
157+
158+ Returns:
159+ None
160+
161+ """
162+ self .log ["negLnLike" ] = append (self .log ["negLnLike" ], self .negLnLike )
163+ self .log ["theta" ] = append (self .log ["theta" ], self .theta )
164+ if self .optim_p :
165+ self .log ["p" ] = append (self .log ["p" ], self .p )
166+ if (self .method == "regression" ) or (self .method == "reinterpolation" ):
167+ self .log ["Lambda" ] = append (self .log ["Lambda" ], self .Lambda )
168+ # get the length of the log
169+ self .log_length = len (self .log ["negLnLike" ])
170+ if self .spot_writer is not None :
171+ negLnLike = self .negLnLike .copy ()
172+ self .spot_writer .add_scalar ("spot_negLnLike" , negLnLike , self .counter + self .log_length )
173+ # add the self.n_theta theta values to the writer with one key "theta",
174+ # i.e, the same key for all theta values
175+ theta = self .theta .copy ()
176+ self .spot_writer .add_scalars ("spot_theta" , {f"theta_{ i } " : theta [i ] for i in range (self .n_theta )}, self .counter + self .log_length )
177+ if (self .method == "regression" ) or (self .method == "reinterpolation" ):
178+ Lambda = self .Lambda .copy ()
179+ self .spot_writer .add_scalar ("spot_Lambda" , Lambda , self .counter + self .log_length )
180+ if self .optim_p :
181+ p = self .p .copy ()
182+ self .spot_writer .add_scalars ("spot_p" , {f"p_{ i } " : p [i ] for i in range (self .n_p )}, self .counter + self .log_length )
183+ self .spot_writer .flush ()
131184
132185 def fit (self , X : np .ndarray , y : np .ndarray , bounds : Optional [List [Tuple [float , float ]]] = None ) -> "Kriging" :
133186 """
@@ -190,7 +243,10 @@ def fit(self, X: np.ndarray, y: np.ndarray, bounds: Optional[List[Tuple[float, f
190243 self .p = 2
191244
192245 # Once logtheta_lambda is found, compute the final correlation matrix
193- self .NegLnLike_ , self .Psi_ , self .U_ = self .likelihood (self .logtheta_lambda_ )
246+ self .negLnLike , self .Psi_ , self .U_ = self .likelihood (self .logtheta_lambda_ )
247+
248+ # Update log with the current values
249+ self ._update_log ()
194250 return self
195251
196252 def predict (self , X : np .ndarray , return_std = False , return_val : str = "y" ) -> np .ndarray :
@@ -273,8 +329,8 @@ def likelihood(self, x: np.ndarray) -> Tuple[float, np.ndarray, np.ndarray]:
273329
274330 Returns:
275331 (float, np.ndarray, np.ndarray):
276- (NegLnLike , Psi, U) where:
277- - NegLnLike (float): The negative concentrated log-likelihood.
332+ (negLnLike , Psi, U) where:
333+ - negLnLike (float): The negative concentrated log-likelihood.
278334 - Psi (np.ndarray): The correlation matrix.
279335 - U (np.ndarray): The Cholesky factor (or None if ill-conditioned).
280336 """
@@ -329,8 +385,8 @@ def likelihood(self, x: np.ndarray) -> Tuple[float, np.ndarray, np.ndarray]:
329385 tresid = np .linalg .solve (U .T , tresid )
330386 SigmaSqr = (resid @ tresid ) / n
331387
332- NegLnLike = (n / 2.0 ) * np .log (SigmaSqr ) + 0.5 * LnDetPsi
333- return NegLnLike , Psi , U
388+ negLnLike = (n / 2.0 ) * np .log (SigmaSqr ) + 0.5 * LnDetPsi
389+ return negLnLike , Psi , U
334390
335391 def _pred (self , x : np .ndarray ) -> float :
336392 """
0 commit comments