diff --git a/src/mitim_modules/maestro/utils/PORTALSbeat.py b/src/mitim_modules/maestro/utils/PORTALSbeat.py index 57b14aeb..1a95ec91 100644 --- a/src/mitim_modules/maestro/utils/PORTALSbeat.py +++ b/src/mitim_modules/maestro/utils/PORTALSbeat.py @@ -253,7 +253,7 @@ def _inform(self, use_previous_residual = True, use_previous_surrogate_data = Tr if use_previous_surrogate_data and ('portals_surrogate_data_file' in self.maestro_instance.parameters_trans_beat): if 'surrogateOptions' not in self.optimization_options: self.optimization_options['surrogateOptions'] = {} - self.optimization_options['surrogateOptions']["extrapointsFile"] = self.maestro_instance.parameters_trans_beat['portals_surrogate_data_file'] + self.optimization_options['surrogateOptions']["add_data_from_file"] = self.maestro_instance.parameters_trans_beat['portals_surrogate_data_file'] self.folder_starting_point = self.maestro_instance.parameters_trans_beat['portals_last_run_folder'] diff --git a/src/mitim_tools/opt_tools/BOTORCHtools.py b/src/mitim_tools/opt_tools/BOTORCHtools.py index a013eb22..0243de4c 100644 --- a/src/mitim_tools/opt_tools/BOTORCHtools.py +++ b/src/mitim_tools/opt_tools/BOTORCHtools.py @@ -197,7 +197,7 @@ def __init__( self.input_transform = input_transform self.to(train_X) - def posterior_full( + def posterior( self, X, output_indices=None, @@ -249,303 +249,11 @@ def _Xs_and_transforms(self, X): Xs = (X,) * len(self.transforms) return zip(Xs, self.transforms) -class SingleTaskGP_MITIM2(botorch.models.gp_regression.SingleTaskGP): - def __init__( - self, - train_X, - train_Y, - train_Yvar, - input_transform=None, - outcome_transform=None, - surrogateOptions={}, - variables=None, - train_X_added=torch.Tensor([]), - train_Y_added=torch.Tensor([]), - train_Yvar_added=torch.Tensor([]), - ): - """ - _added refers to already-transformed variables that are added from table - """ - - TypeMean = surrogateOptions.get("TypeMean", 0) - TypeKernel = surrogateOptions.get("TypeKernel", 0) - FixedNoise = surrogateOptions.get("FixedNoise", False) - ConstrainNoise = surrogateOptions.get("ConstrainNoise", -1e-4) - learn_additional_noise = surrogateOptions.get("ExtraNoise", False) - print("\t\t* Surrogate model options:") - print( - f"\t\t\t- FixedNoise: {FixedNoise} (extra noise: {learn_additional_noise}), TypeMean: {TypeMean}, TypeKernel: {TypeKernel}, ConstrainNoise: {ConstrainNoise:.1e}" - ) - - # self.store_training( - # train_X, - # train_X_added, - # train_Y, - # train_Y_added, - # train_Yvar, - # train_Yvar_added, - # input_transform, - # outcome_transform, - # ) - - """ - ---------------------------------------------------------------------------------------- - What set_dimensions did, and select things to train (already transformed and normalized) - ---------------------------------------------------------------------------------------- - """ - - # Grab num_outputs - self._num_outputs = train_Y.shape[-1] - - # Grab ard_num_dims - if train_X.shape[0] > 0: - with torch.no_grad(): - transformed_X = self.transform_inputs( - X=train_X, input_transform=input_transform - ) - self.ard_num_dims = transformed_X.shape[-1] - else: - self.ard_num_dims = train_X_added.shape[-1] - transformed_X = torch.empty((0, self.ard_num_dims)).to(train_X) - - # Transform outcomes - if outcome_transform is not None: - train_Y, train_Yvar = outcome_transform(train_X, train_Y, train_Yvar) - - # # Added points are raw transformed, so I need to normalize them - # if train_X_added.shape[0] > 0: - # train_X_added = input_transform["tf2"](train_X_added) - # train_Y_added, train_Yvar_added = outcome_transform["tf2"]( - # train_Y_added, train_Yvar_added - # ) - # ----- - - train_X_usedToTrain = transformed_X #torch.cat((transformed_X, train_X_added), axis=0) - train_Y_usedToTrain = train_Y #torch.cat((train_Y, train_Y_added), axis=0) - train_Yvar_usedToTrain = train_Yvar #torch.cat((train_Yvar, train_Yvar_added), axis=0) - - self._input_batch_shape, self._aug_batch_shape = self.get_batch_dimensions( - train_X=train_X_usedToTrain, train_Y=train_Y_usedToTrain - ) - - # train_Y_usedToTrain = train_Y_usedToTrain.squeeze(-1) - # train_Yvar_usedToTrain = train_Yvar_usedToTrain.squeeze(-1) - - """ - ----------------------------------------------------------------------- - Likelihood and Noise - ----------------------------------------------------------------------- - """ - - self._subset_batch_dict = {} - - if FixedNoise: - # Noise not inferred, given by data - likelihood = ( - gpytorch.likelihoods.gaussian_likelihood.FixedNoiseGaussianLikelihood( - noise=train_Yvar_usedToTrain.clip(1e-6), # I clip the noise to avoid numerical issues (gpytorch would do it anyway, but this way it doesn't throw a warning) - batch_shape=self._aug_batch_shape, - learn_additional_noise=learn_additional_noise, - ) - ) - - else: - # Infer Noise - - noise_prior = gpytorch.priors.torch_priors.GammaPrior(1.1, 0.05) - noise_prior_mode = (noise_prior.concentration - 1) / noise_prior.rate - - if ConstrainNoise < 0: - noise_constraint = gpytorch.constraints.constraints.GreaterThan( - -ConstrainNoise, transform=None, initial_value=noise_prior_mode - ) - else: - noise_constraint = gpytorch.constraints.constraints.Interval( - 1e-6, ConstrainNoise, transform=None, initial_value=noise_prior_mode - ) - - likelihood = gpytorch.likelihoods.gaussian_likelihood.GaussianLikelihood( - noise_prior=noise_prior, - batch_shape=self._aug_batch_shape, - noise_constraint=noise_constraint, - ) - - self._subset_batch_dict["likelihood.noise_covar.raw_noise"] = -2 - - """ - ----------------------------------------------------------------------- - Initialize ExactGP - ----------------------------------------------------------------------- - """ - - gpytorch.models.exact_gp.ExactGP.__init__( - self, - train_inputs=train_X_usedToTrain, - train_targets=train_Y_usedToTrain, - likelihood=likelihood, - ) - - """ - ----------------------------------------------------------------------- - GP Mean - ----------------------------------------------------------------------- - """ - - if TypeMean == 0: - self.mean_module = gpytorch.means.constant_mean.ConstantMean( - batch_shape=self._aug_batch_shape - ) - elif TypeMean == 1: - self.mean_module = gpytorch.means.linear_mean.LinearMean( - self.ard_num_dims, batch_shape=self._aug_batch_shape, bias=True - ) - elif TypeMean == 2: - self.mean_module = MITIM_LinearMeanGradients( - batch_shape=self._aug_batch_shape, variables=variables - ) - elif TypeMean == 3: - self.mean_module = MITIM_CriticalGradient( - batch_shape=self._aug_batch_shape, variables=variables - ) - - """ - ----------------------------------------------------------------------- - GP Kernel - Covariance - ----------------------------------------------------------------------- - """ - - # Priors - lengthscale_prior = gpytorch.priors.torch_priors.GammaPrior(3.0, 6.0) - outputscale_prior = gpytorch.priors.torch_priors.GammaPrior(2.0, 0.15) - - # Do not allow too small lengthscales? - lengthscale_constraint = ( - None # gpytorch.constraints.constraints.GreaterThan(0.05) - ) - - self._subset_batch_dict["covar_module.raw_outputscale"] = -1 - self._subset_batch_dict["covar_module.base_kernel.raw_lengthscale"] = -3 - - if TypeKernel == 0: - self.covar_module = gpytorch.kernels.scale_kernel.ScaleKernel( - base_kernel=gpytorch.kernels.matern_kernel.MaternKernel( - nu=2.5, - ard_num_dims=self.ard_num_dims, - batch_shape=self._aug_batch_shape, - lengthscale_prior=lengthscale_prior, - lengthscale_constraint=lengthscale_constraint, - ), - batch_shape=self._aug_batch_shape, - outputscale_prior=outputscale_prior, - ) - elif TypeKernel == 1: - self.covar_module = gpytorch.kernels.scale_kernel.ScaleKernel( - base_kernel=gpytorch.kernels.rbf_kernel.RBFKernel( - ard_num_dims=self.ard_num_dims, - batch_shape=self._aug_batch_shape, - lengthscale_prior=lengthscale_prior, - lengthscale_constraint=lengthscale_constraint, - ), - batch_shape=self._aug_batch_shape, - outputscale_prior=outputscale_prior, - ) - elif TypeKernel == 2: - self.covar_module = MITIM_ConstantKernel( - ard_num_dims=self.ard_num_dims, - batch_shape=self._aug_batch_shape, - lengthscale_prior=lengthscale_prior, - lengthscale_constraint=lengthscale_constraint, - ) - elif TypeKernel == 3: - self.covar_module = gpytorch.kernels.scale_kernel.ScaleKernel( - base_kernel=MITIM_NNKernel( - ard_num_dims=self.ard_num_dims, - batch_shape=self._aug_batch_shape, - lengthscale_prior=lengthscale_prior, - lengthscale_constraint=lengthscale_constraint, - ), - batch_shape=self._aug_batch_shape, - outputscale_prior=outputscale_prior, - ) - - if outcome_transform is not None: - self.outcome_transform = outcome_transform - if input_transform is not None: - self.input_transform = input_transform - - self.to(train_X) - - def store_training(self, x, xa, y, ya, yv, yva, input_transform, outcome_transform): - - # x, y are raw untransformed, and I want raw transformed - if input_transform is not None: - x_tr = input_transform["tf1"](x) - else: - x_tr = x - if outcome_transform is not None: - y_tr, yv_tr = outcome_transform["tf1"](x, y, yv) - else: - y_tr, yv_tr = y, yv - - # xa, ya are raw transformed - xa_tr = xa - ya_tr, yva_tr = ya, yva - - self.train_X_usedToTrain = torch.cat((xa_tr, x_tr), axis=0) - self.train_Y_usedToTrain = torch.cat((ya_tr, y_tr), axis=0) - self.train_Yvar_usedToTrain = torch.cat((yva_tr, yv_tr), axis=0) - - # Modify posterior call from BatchedMultiOutputGPyTorchModel to call posterior untransform with "X" - - def posterior( - self, - X, - output_indices=None, - observation_noise=False, - posterior_transform=None, - **kwargs, - ): - - self.eval() # make sure model is in eval mode - # input transforms are applied at `posterior` in `eval` mode, and at - # `model.forward()` at the training time - Xtr = self.transform_inputs(X) - with botorch.models.utils.gpt_posterior_settings(): - # insert a dimension for the output dimension - if self._num_outputs > 1: - Xtr, output_dim_idx = botorch.models.utils.add_output_dim( - X=Xtr, original_batch_shape=self._input_batch_shape - ) - # NOTE: BoTorch's GPyTorchModels also inherit from GPyTorch's ExactGP, thus - # self(X) calls GPyTorch's ExactGP's __call__, which computes the posterior, - # rather than e.g. SingleTaskGP's forward, which computes the prior. - mvn = self(Xtr) - mvn = self._apply_noise(X=Xtr, mvn=mvn, observation_noise=observation_noise) - if self._num_outputs > 1: - mean_x = mvn.mean - covar_x = mvn.lazy_covariance_matrix - output_indices = output_indices or range(self._num_outputs) - mvns = [ - gpytorch.distributions.MultivariateNormal( - mean_x.select(dim=output_dim_idx, index=t), - covar_x[(slice(None),) * output_dim_idx + (t,)], - ) - for t in output_indices - ] - mvn = gpytorch.distributions.MultitaskMultivariateNormal.from_independent_mvns(mvns=mvns) - - posterior = botorch.posteriors.gpytorch.GPyTorchPosterior(distribution=mvn) - if hasattr(self, "outcome_transform"): - posterior = self.outcome_transform.untransform_posterior(X, posterior) - if posterior_transform is not None: - return posterior_transform(posterior) - return posterior - # ---------------------------------------------------------------------------------------------------------------------------- # ModelListGP needs to be modified to allow me to have "common" parameters to models, to not run at every transformation again # ---------------------------------------------------------------------------------------------------------------------------- -class ModifiedModelListGP(botorch.models.model_list_gp_regression.ModelListGP): +class ModelListGP_MITIM(botorch.models.model_list_gp_regression.ModelListGP): def __init__(self, *gp_models): super().__init__(*gp_models) @@ -1237,6 +945,7 @@ def untransform_posterior(self, posterior: Posterior) -> Posterior: # could potentially use from_independent_mvns # print(f"{mvn._covar.shape = }") # print(f"{covar.shape=}") - dis = MultitaskMultivariateNormal(mean=mean, covariance_matrix=covar) + from gpytorch.distributions import MultivariateNormal + dis = MultivariateNormal(mean=mean, covariance_matrix=covar) return GPyTorchPosterior(distribution=dis) diff --git a/src/mitim_tools/opt_tools/STEPtools.py b/src/mitim_tools/opt_tools/STEPtools.py index 9cb423f8..7d64a510 100644 --- a/src/mitim_tools/opt_tools/STEPtools.py +++ b/src/mitim_tools/opt_tools/STEPtools.py @@ -262,7 +262,7 @@ def _fit_individual_models(self, fit_output_contains=None): models = () for GP in self.GP["individual_models"]: models += (GP.gpmodel,) - self.GP["combined_model"].gpmodel = BOTORCHtools.ModifiedModelListGP(*models) + self.GP["combined_model"].gpmodel = BOTORCHtools.ModelListGP_MITIM(*models) # ------------------------------------------------------------------------------------------------------ # Make sure each model has the right surrogate_transformation_variables inside the combined model @@ -298,7 +298,7 @@ def defineFunctions(self, scalarized_objective): I create this so that, upon reading a pickle, I re-call it. Otherwise, it is very heavy to store lambdas """ - self.evaluators = {"GP": self.GP["mo_model"]} + self.evaluators = {"GP": self.GP["combined_model"]}#mo_model"]} # ************************************************************************************************** # Objective (multi-objective model -> single objective residual) diff --git a/src/mitim_tools/opt_tools/SURROGATEtools.py b/src/mitim_tools/opt_tools/SURROGATEtools.py index 88eca7fc..e9db228b 100644 --- a/src/mitim_tools/opt_tools/SURROGATEtools.py +++ b/src/mitim_tools/opt_tools/SURROGATEtools.py @@ -85,16 +85,22 @@ def __init__( # Add points from file # ------------------------------------------------------------------------------------- + addition_of_points = ("add_data_from_file" in self.surrogateOptions) and (self.surrogateOptions["add_data_from_file"] is not None) + is_this_single_output = (self.outputs is not None) and (len(self.outputs) == 1) + + if addition_of_points and is_this_single_output: + raise Exception("[MITIM] add_data_from_file can only be used for single output models as of now...") + # Points to be added from file continueAdding = False - if ("extrapointsFile" in self.surrogateOptions) and (self.surrogateOptions["extrapointsFile"] is not None) and (self.output is not None) and (self.output in self.surrogateOptions["extrapointsModels"]): + if addition_of_points and (self.outputs is not None) and (self.outputs[0] in self.surrogateOptions["extrapointsModels"]): print( - f"\t* Requested extension of training set by points in file {self.surrogateOptions['extrapointsFile']}" + f"\t* Requested extension of training set by points in file {self.surrogateOptions['add_data_from_file']}" ) - df = pd.read_csv(self.surrogateOptions["extrapointsFile"]) - df_model = df[df['Model'] == self.output] + df = pd.read_csv(self.surrogateOptions["add_data_from_file"]) + df_model = df[df['Model'] == self.outputs[0]] if len(df_model) == 0: print("\t- No points for this output in the file, nothing to add", typeMsg="i") @@ -110,7 +116,7 @@ def __init__( # Check 2: Is it consistent with the x_names of this run? x_names = df_model['x_names'].apply(ast.literal_eval).iloc[0] - x_names_check = self.surrogate_parameters['surrogate_transformation_variables_lasttime'][self.output] + x_names_check = self.surrogate_parameters['surrogate_transformation_variables_lasttime'][self.outputs[0]] if x_names != x_names_check: print("x_names in file do not match the ones in this run, prone to errors", typeMsg='q') @@ -139,7 +145,7 @@ def __init__( if self.fileTraining is not None: train_X_Complete, _ = self.surrogate_parameters["transformationInputs"]( self.train_X, - self.output, + self.outputs[0], self.surrogate_parameters, self.surrogate_parameters["surrogate_transformation_variables_lasttime"], ) @@ -194,18 +200,18 @@ def __init__( # ------------------------------------------------------------------------------------- if (self.fileTraining is not None) and (self.train_X.shape[0] + self.train_X_added.shape[0] > 0): - self.writeFileTraining(input_transform_physics, outcome_transform_physics) + self.write_datafile(input_transform_physics, outcome_transform_physics) # ------------------------------------------------------------------------------------- # Obtain normalization constants now (although during training this is messed up, so needed later too) # ------------------------------------------------------------------------------------- - self.normalization_pass( - input_transform_physics, - input_transform_normalization, - outcome_transform_physics, - output_transformed_standardization, - ) + # self.normalization_pass( + # input_transform_physics, + # input_transform_normalization, + # outcome_transform_physics, + # output_transformed_standardization, + # ) # ------------------------------------------------------------------------------------ # Combine transformations in chain of PHYSICS + NORMALIZATION @@ -216,14 +222,13 @@ def __init__( ).to(self.dfT) outcome_transform = BOTORCHtools.ChainedOutcomeTransform( - tf1=outcome_transform_physics, tf2=output_transformed_standardization, tf3=BOTORCHtools.OutcomeToBatchDimension() + tf1=outcome_transform_physics, tf2=output_transformed_standardization #, tf3=BOTORCHtools.OutcomeToBatchDimension() ).to(self.dfT) - self.output = 'QeTurb_1' self.variables = ( - self.surrogate_transformation_variables[self.output] + self.surrogate_transformation_variables[self.outputs[0]] if ( - (self.output is not None) + (self.outputs is not None) and ("surrogate_transformation_variables" in self.__dict__) and (self.surrogate_transformation_variables is not None) ) @@ -235,7 +240,7 @@ def __init__( # ************************************************************************************* print( - f'\t- Initializing model{" for "+self.output_transformed if (self.output_transformed is not None) else ""}', + f'\t- Initializing model{" for "+self.outputs_transformed[0] if (self.outputs_transformed is not None and (len(self.outputs)==1)) else ""}', ) """ @@ -300,7 +305,7 @@ def _define_MITIM_transformations(self): # Broadcast the input transformation to all outputs # ------------------------------------------------------------------------------------ - input_transformation_physics = BOTORCHtools.BatchBroadcastedInputTransform(input_transformations_physics) + input_transformation_physics = input_transformations_physics[0] #BOTORCHtools.BatchBroadcastedInputTransform(input_transformations_physics) transformed_X = input_transformation_physics(self.train_X) @@ -390,8 +395,8 @@ def fit(self): """ # Train always in physics-transformed space, to enable mitim re-use training from file - with fundamental_model_context(self): - track_fval = self.perform_model_fit(mll) + #with fundamental_model_context(self): + track_fval = self.perform_model_fit(mll) # --------------------------------------------------------------------------------------------------- # Asses optimization @@ -402,12 +407,12 @@ def fit(self): # Go back to definining the right normalizations, because the optimizer has to work on training mode... # --------------------------------------------------------------------------------------------------- - self.normalization_pass( - self.gpmodel.input_transform["tf1"], - self.gpmodel.input_transform["tf2"], - self.gpmodel.outcome_transform["tf1"], - self.gpmodel.outcome_transform["tf2"], - ) + # self.normalization_pass( + # self.gpmodel.input_transform["tf1"], + # self.gpmodel.input_transform["tf2"], + # self.gpmodel.outcome_transform["tf1"], + # self.gpmodel.outcome_transform["tf2"], + # ) def perform_model_fit(self, mll): self.gpmodel.train() @@ -453,7 +458,7 @@ def callback(x, y, mll=mll): mll.eval() print( - f"\n\t- Marginal log likelihood went from {track_fval[0]:.3f} to {track_fval[-1]:.3f}" + f"\n\t- Marginal log likelihood went from {track_fval[0]} to {track_fval[-1]:.3f}" ) return track_fval @@ -494,94 +499,97 @@ def predict(self, X, produceFundamental=False, nSamples=None): return mean, upper, lower, samples - def writeFileTraining(self, input_transform_physics, outcome_transform_physics): + def write_datafile(self, input_transform_physics, outcome_transform_physics): """ -------------------------------------------------------------------- Write file with surrogate if there are transformations - Note: USE TRANSFORMATIONS AT COMPLETE NUMBER (AFTER TRANSITIONS) for those in this run, but - simply use the info that was in extra_points_file + Note: USE TRANSFORMATIONS AT COMPLETE NUMBER (AFTER TRANSITIONS) + for those in this run, but simply use the info that was in + extra_points_file -------------------------------------------------------------------- """ - # ------------------------------------------------------------------------------------------------------------------------ - # Transform the points without the added from file - # ------------------------------------------------------------------------------------------------------------------------ + for i,output in enumerate(self.outputs): - # I do not use directly input_transform_physics because I need all the columns, not of this specif iteration - train_X_Complete, _ = self.surrogate_parameters["transformationInputs"]( - self.train_X, - self.output, - self.surrogate_parameters, - self.surrogate_parameters["surrogate_transformation_variables_lasttime"], - ) + # ------------------------------------------------------------------------------------------------------------------------ + # Transform the points without the added from file + # ------------------------------------------------------------------------------------------------------------------------ - train_Y, train_Yvar = outcome_transform_physics( - self.train_X, self.train_Y, self.train_Yvar - ) + # I do not use directly input_transform_physics because I need all the columns, not of this specif iteration + train_X_Complete, _ = self.surrogate_parameters["transformationInputs"]( + self.train_X, + output, + self.surrogate_parameters, + self.surrogate_parameters["surrogate_transformation_variables_lasttime"], + ) - dv_names_Complete = ( - self.surrogate_parameters["surrogate_transformation_variables_lasttime"][self.output] - if ( - "surrogate_transformation_variables_lasttime" in self.surrogate_parameters - and self.surrogate_parameters["surrogate_transformation_variables_lasttime"] - is not None + train_Y, train_Yvar = outcome_transform_physics( + self.train_X, self.train_Y[...,i].unsqueeze(-1), self.train_Yvar[...,i].unsqueeze(-1) ) - else [i for i in self.bounds] - ) - if self.train_X_added_full.shape[-1] < train_X_Complete.shape[-1]: - print( - "\t\t- Points from file have less input dimensions, extending with NaNs for writing new file", - typeMsg="w", + dv_names_Complete = ( + self.surrogate_parameters["surrogate_transformation_variables_lasttime"][output] + if ( + "surrogate_transformation_variables_lasttime" in self.surrogate_parameters + and self.surrogate_parameters["surrogate_transformation_variables_lasttime"] + is not None + ) + else [i for i in self.bounds] ) - self.train_X_added_full = torch.cat( - ( - self.train_X_added_full, - torch.full( - ( - self.train_X_added_full.shape[0], - train_X_Complete.shape[-1] - - self.train_X_added_full.shape[-1], + + if self.train_X_added_full.shape[-1] < train_X_Complete.shape[-1]: + print( + "\t\t- Points from file have less input dimensions, extending with NaNs for writing new file", + typeMsg="w", + ) + self.train_X_added_full = torch.cat( + ( + self.train_X_added_full, + torch.full( + ( + self.train_X_added_full.shape[0], + train_X_Complete.shape[-1] + - self.train_X_added_full.shape[-1], + ), + torch.nan, ), - torch.nan, ), - ), - axis=-1, - ) - elif self.train_X_added_full.shape[-1] > train_X_Complete.shape[-1]: - print( - "\t\t- Points from file have more input dimensions, removing last dimensions for writing new file", - typeMsg="w", - ) - self.train_X_added_full = self.train_X_added_full[ - :, : train_X_Complete.shape[-1] - ] + axis=-1, + ) + elif self.train_X_added_full.shape[-1] > train_X_Complete.shape[-1]: + print( + "\t\t- Points from file have more input dimensions, removing last dimensions for writing new file", + typeMsg="w", + ) + self.train_X_added_full = self.train_X_added_full[ + :, : train_X_Complete.shape[-1] + ] - x = torch.cat((self.train_X_added_full, train_X_Complete), axis=0) - y = torch.cat((self.train_Y_added, train_Y), axis=0) - yvar = torch.cat((self.train_Yvar_added, train_Yvar), axis=0) + x = torch.cat((self.train_X_added_full, train_X_Complete), axis=0) + y = torch.cat((self.train_Y_added, train_Y), axis=0) + yvar = torch.cat((self.train_Yvar_added, train_Yvar), axis=0) - # ------------------------------------------------------------------------------------------------------------------------ - # Merged data with existing data frame and write - # ------------------------------------------------------------------------------------------------------------------------ + # ------------------------------------------------------------------------------------------------------------------------ + # Merged data with existing data frame and write + # ------------------------------------------------------------------------------------------------------------------------ - new_df = create_df_portals(x,y,yvar,dv_names_Complete,self.output) + new_df = create_df_portals(x,y,yvar,dv_names_Complete,output) - if self.fileTraining.exists(): + if self.fileTraining.exists(): - # Load the existing DataFrame from the HDF5 file - existing_df = pd.read_csv(self.fileTraining) + # Load the existing DataFrame from the HDF5 file + existing_df = pd.read_csv(self.fileTraining) - # Concatenate the existing DataFrame with the new DataFrame - combined_df = pd.concat([existing_df, new_df], ignore_index=True) + # Concatenate the existing DataFrame with the new DataFrame + combined_df = pd.concat([existing_df, new_df], ignore_index=True) - else: + else: - combined_df = new_df + combined_df = new_df - # Save the combined DataFrame back to the file - combined_df.to_csv(self.fileTraining, index=False) + # Save the combined DataFrame back to the file + combined_df.to_csv(self.fileTraining, index=False) # -------------------------- # PLOTTING AND POST-ANALYSIS diff --git a/src/mitim_tools/opt_tools/utils/TESTtools.py b/src/mitim_tools/opt_tools/utils/TESTtools.py index cab8a417..c23bb619 100644 --- a/src/mitim_tools/opt_tools/utils/TESTtools.py +++ b/src/mitim_tools/opt_tools/utils/TESTtools.py @@ -47,9 +47,14 @@ def testBatchCapabilities(GPs, combinations=[2, 100, 1000]): It stops running if the error gets larger than thrPercent in those cases """ + from mitim_tools.misc_tools import IOtools + with IOtools.speeder("/Users/pablorf/PROJECTS/project_2024_PORTALSdevelopment/speed/profiler_evv.prof") as s: + GPs.predict(GPs.train_X[0:1, :].repeat(50, 1)) + for i in combinations: x = GPs.train_X[0:1, :].repeat(i, 1) + y1 = GPs.predict(x)[0] y2 = GPs.predict(x[0:1, :])[0] diff --git a/templates/main.namelist.json b/templates/main.namelist.json index 03351169..73837d0b 100644 --- a/templates/main.namelist.json +++ b/templates/main.namelist.json @@ -43,7 +43,7 @@ "MinimumRelativeNoise": null, "stds_outside": null, "stds_outside_checker": 5, - "extrapointsFile": null, + "add_data_from_file": null, "extrapointsModels": null, "extrapointsModelsAvoidContent": null },