def test_quality(mufi_data): space = np.array(mufi_data.space) max_points_nb = space.shape[0] surrogate = SurrogateModel('rbf', mufi_data.space.corners, np.array(mufi_data.space.plabels)[1:]) surrogate.fit(mufi_data.space.values[10:, 1:], mufi_data.target_space[10:]) assert surrogate.estimate_quality()[0] == pytest.approx(1, 0.1)
def test_pdf_surrogate(self, mock_show, ishigami_data): dist = ot.ComposedDistribution(ishigami_data.dists) surrogate = SurrogateModel('rbf', ishigami_data.space.corners, ishigami_data.space.plabels) surrogate.fit(ishigami_data.space, ishigami_data.target_space) settings = { "dist": dist, "model": surrogate, "method": 'kriging', "bounds": ishigami_data.space.corners } pdf(settings)
def test_refiner_basics(tmp, branin_data, settings_ishigami, seed): f_2d = branin_data.func space = branin_data.space space.sampling(11, 'halton') surrogate = SurrogateModel('kriging', space.corners, space.plabels) surrogate.fit(space, f_2d(space)) refiner = Refiner(surrogate, space.corners, delta_space=0.08) distance_min = refiner.distance_min(refiner.space.values[0]) assert distance_min == pytest.approx(0.163461, abs=0.001) hypercube = refiner.hypercube_distance(refiner.space.values[0], distance_min) npt.assert_almost_equal(hypercube, [[-0.62, 3.62], [3.04, 6.96]], decimal=2) hypercube_optim = refiner.hypercube_optim(refiner.space.values[0]) npt.assert_almost_equal(hypercube_optim, [[-0.61, 5.74], [1.0, 11.66]], decimal=2)
def test_block(mascaret_data, settings_ishigami): test_settings = copy.deepcopy(settings_ishigami) test_settings['uq']['type'] = 'block' test_settings['uq'].pop('test') test_settings['snapshot']['plabels'] = ['Ks', 'Q'] surrogate = SurrogateModel('rbf', mascaret_data.space.corners, mascaret_data.space.plabels) surrogate.fit(mascaret_data.space, mascaret_data.target_space) analyse = UQ(surrogate, nsample=test_settings['uq']['sample'], dists=['Uniform(15., 60.)', 'Normal(4035., 400.)'], plabels=test_settings['snapshot']['plabels'], method=test_settings['uq']['method'], indices=test_settings['uq']['type']) indices = analyse.sobol() print(indices)
def test_indices(tmp, ishigami_data, settings_ishigami): surrogate = SurrogateModel('kriging', ishigami_data.space.corners, ishigami_data.space.plabels) surrogate.fit(ishigami_data.space, ishigami_data.target_space) analyse = UQ(surrogate, nsample=settings_ishigami['uq']['sample'], dists=settings_ishigami['uq']['pdf'], plabels=settings_ishigami['snapshot']['plabels'], method=settings_ishigami['uq']['method'], indices=settings_ishigami['uq']['type'], test=settings_ishigami['uq']['test']) indices = analyse.sobol() errors = analyse.error_model(indices, 'Ishigami') assert errors[0] == pytest.approx(1, 0.2) # 2nd order assert errors[2] == pytest.approx(0.1, abs=0.2) # 1st order assert errors[3] == pytest.approx(0.05, abs=0.05) # total order assert errors[4] == pytest.approx(0.05, abs=0.05)
for i in range(len(x_test)): print("Test#" + str(i)) x_c, y, q = fl(x_test[i]) y_test.append(y) # Surrogate ## Polynomial Chaos ### Quad #Changing degree for truncation error pc_predictor_q10 = SurrogateModel('pc', corners, init_size, plabels, strategy='Quad', degree=10, distributions=dists_ot, N_quad=121) pc_predictor_q9 = SurrogateModel('pc', corners, init_size, plabels, strategy='Quad', degree=9, distributions=dists_ot, N_quad=121) pc_predictor_q8 = SurrogateModel('pc', corners, init_size, plabels,
#k_predictor = SurrogateModel('kriging', np.array(corners).transpose(), Nl, plabels, global_optimizer=False, kernel = Matern(length_scale=(1.0,)*in_dim, nu=2.5, length_scale_bounds=[(0.01,1000)]*in_dim)) #k_predictor.fit(x_a,np.array(y_l)) #y_pred_krig, _ = k_predictor(x_b) #k_uq = UQ(k_predictor, dists=dists, nsample=1000, plabels = plabels, xlabel='x(m)', flabel='H(m)', fname='uq_k') #k_uq.error_propagation() #k_sobol = k_uq.sobol() #rmse_k = np.sqrt(mean_squared_error(y_t, y_pred_krig)) #q2_k = r2_score(y_t, y_pred_krig) #PC surrogate with Least square method PCls_predictor = SurrogateModel('pc', corners, Nl, plabels, strategy='LS', degree=3, distributions=distsOT) PCls_predictor.fit(x_l, y_l) y_pred_PCls, _ = PCls_predictor(x_t) rmse_pc_ls = np.sqrt(mean_squared_error(y_t, y_pred_PCls)) q2_pc_ls = r2_score(y_t, y_pred_PCls) Ls_uq = UQ(PCls_predictor, dists=dists, nsample=100, plabels=plabels, xlabel='x(m)', flabel='H(m)',
def test_SurrogateModel_class(tmp, ishigami_data, settings_ishigami): space_ = copy.deepcopy(ishigami_data.space) space_.max_points_nb = 500 sample = space_.sampling(500, 'halton') path = os.path.join(tmp, 'surrogate') path_space = os.path.join(tmp, 'space') try: os.makedirs(path) except OSError: pass try: os.makedirs(path_space) except OSError: pass # PC pc_settings = {'strategy': 'LS', 'degree': 10, 'distributions': ishigami_data.dists, 'sample': sample} surrogate = SurrogateModel('pc', ishigami_data.space.corners, ishigami_data.space.plabels, **pc_settings) input_ = surrogate.predictor.sample output = ishigami_data.func(input_) surrogate.fit(input_, output) pred, sigma = surrogate(ishigami_data.point) assert sigma is None assert pred == pytest.approx(ishigami_data.target_point, 0.5) surrogate.write(path) assert os.path.isfile(os.path.join(path, 'surrogate.dat')) # Kriging surrogate = SurrogateModel('kriging', ishigami_data.space.corners, ishigami_data.space.plabels) surrogate.fit(ishigami_data.space, ishigami_data.target_space) ishigami_data.space.write(path_space, 'space.dat') surrogate.write(path) assert os.path.isfile(os.path.join(path, 'surrogate.dat')) surrogate = SurrogateModel('kriging', ishigami_data.space.corners, ishigami_data.space.plabels) surrogate.read(path) assert surrogate.predictor is not None npt.assert_array_equal(surrogate.space.values, ishigami_data.space.values) pred, _ = surrogate(ishigami_data.point) assert pred == pytest.approx(ishigami_data.target_point, 0.2)
def test_resampling(tmp, branin_data, settings_ishigami, seed): f_2d = branin_data.func space = branin_data.space test_settings = copy.deepcopy(settings_ishigami) test_settings['snapshot']['plabels'] = ['x1', 'x2'] space.empty() max_points_nb = 5 space.sampling(max_points_nb, 'halton') space.max_points_nb = 100 surrogate = SurrogateModel('kriging', space.corners, space.plabels) surrogate.fit(space, f_2d(space)) # Larger dataset to ensure stable results space.empty() max_points_nb = 11 space.sampling(max_points_nb, 'halton') surrogate = SurrogateModel('kriging', space.corners, space.plabels) surrogate.fit(space, f_2d(space)) for _ in range(2): space.refine(surrogate, 'sigma') surrogate.fit(space, f_2d(space)) assert len(space) == 13 refiner = Refiner(surrogate, space.corners, delta_space=0.15) point_loo = refiner.space.values[5] loo_si = refiner.leave_one_out_sigma(point_loo) npt.assert_almost_equal(loo_si, [-2.76, 2.], decimal=2) loo_so = refiner.leave_one_out_sobol(point_loo, ['Uniform(-5, 0)', 'Uniform(10, 15)']) npt.assert_almost_equal(loo_so, [-2.86, 2.28], decimal=2) sigma = refiner.sigma() npt.assert_almost_equal(sigma, [4.85, 6.561], decimal=1) optim_EI_min = refiner.optimization(method='EI') npt.assert_almost_equal(optim_EI_min, [-2.176, 9.208], decimal=1) optim_EI_max = refiner.optimization(extremum='max') npt.assert_almost_equal(optim_EI_max, [6.59, 12.999], decimal=1) optim_PI = refiner.optimization(method='PI') npt.assert_almost_equal(optim_PI, [-2.328, 9.441], decimal=1) disc = refiner.discrepancy() npt.assert_almost_equal(disc, [7, 13.], decimal=1) extrema = np.array(refiner.extrema([])[0]) # npt.assert_almost_equal(extrema, [[-2.694, 2.331], [2.576, 2.242]], decimal=1) base_sigma_disc = refiner.sigma_discrepancy() npt.assert_almost_equal(base_sigma_disc, refiner.sigma_discrepancy([0.5, 0.5]), decimal=1) assert (base_sigma_disc != refiner.sigma_discrepancy([-0.1, 1.])).any() # Refiner without surrogate refiner = Refiner(surrogate, space.corners, delta_space=0.1) disc2 = refiner.discrepancy() npt.assert_almost_equal(disc2, [8., 13.], decimal=1)
y_learning1.append(Output['z']) idx+=1 idx = 0 for k in range(N_learning): x = x_learning_dicor[k] print("Study learningr#"+str(idx)) Study.initialize_model() Output = Study(x) y_learningr.append(Output['z']) idx+=1 #Build Surrogates #Changing degree for truncation error PC_lsp9 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=9, distributions=dists_ot) PC_lsp8 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=8, distributions=dists_ot) PC_lsp7 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=7, distributions=dists_ot) PC_lsp6 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=6, distributions=dists_ot) PC_lsp5 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=5, distributions=dists_ot) PC_lsp4 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=4, distributions=dists_ot) PC_lsp3 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=3, distributions=dists_ot) #Changing sampling size for samling error PC_ls5 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=7, distributions=dists_ot) PC_ls4 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=7, distributions=dists_ot) PC_ls3 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=7, distributions=dists_ot) PC_ls2 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=7, distributions=dists_ot) PC_ls1 = SurrogateModel('pc', corners, plabels, strategy='LS', degree=7, distributions=dists_ot) # fitting of the validation samples
iprint=0, working_directory='study_data_quad') plabels = ['Ks1', 'Ks2', 'Ks3', 'Q'] dists = [ 'Uniform(30., 40.)', 'Uniform(30., 40.)', 'Uniform(30., 40.)', 'BetaMuSigma(5000, 1500, 3000,7000).getDistribution()' ] dists_ot = dists_to_ot(dists) corners = ([30., 30., 30., 3000], [40., 40., 40, 7000]) # Learning sample # Build the surrogate and get the quadrature points PC_data = SurrogateModel('pc', corners, plabels, strategy='QUAD', degree=3, distributions=dists_ot) x_quad = PC_data.predictor.sample x_quad = np.array(x_quad) np.save('x_quad.npy', x_quad) (N_quad, _) = np.shape(x_quad) # Build dictionnairy form of x_quad : x_quad_dico x_quad_dico = [] for i in range(N_quad): x_quad_dico.append({ 'friction_coefficients': [{ "type": "zone", "index": 0,
def __init__(self, samples, data, corners, fsizes=None, pod=None, standard=True, local_method=None, pca_percentage=0.8, clusterer='cluster.KMeans(n_clusters=2)', classifier='gaussian_process.GaussianProcessClassifier()'): """Cluster data and fit local models. 1. If :attr:`data` is not scalar, compute PCA on :attr:`data`. 2. Cluster data. 3. Each sample is affiliated to a cluster. 4. Fit a classifier to handle new samples. 5. A local model for each cluster is built. If :attr:`local_method` is not None, set as list of dict with options. Ex: `[{'kriging': {**args}}]` :param array_like sample: Sample of parameters of Shape (n_samples, n_params). :param array_like data: Sample of realization which corresponds to the sample of parameters :attr:`sample` (n_samples, n_features). :param array_like corners: Hypercube ([min, n_features], [max, n_features]). :param int fsizes: Number of components of output features. :param dict pod: Whether to compute POD or not in local models. - **tolerance** (float) -- Basis modes filtering criteria. - **dim_max** (int) -- Number of basis modes to keep. :param bool standard: Whether to standardize data before clustering. :param lst(dict) local_method: List of local surrrogate models for clusters or None for Kriging local surrogate models. :param float pca_percentage: Percentage of information kept for PCA. :param str clusterer: Clusterer from sklearn (unsupervised machine learning). http://scikit-learn.org/stable/modules/clustering.html#clustering :param str classifier: Classifier from sklearn (supervised machine learning). http://scikit-learn.org/stable/supervised_learning.html """ self.scaler = preprocessing.MinMaxScaler() self.scaler.fit(np.array(corners)) samples = self.scaler.transform(samples) corners = [[0 for i in range(samples.shape[1])], [1 for i in range(samples.shape[1])]] # Only do the clustering on the sensor if fsizes is None: self.fsizes = data.shape[1] else: self.fsizes = fsizes if data.shape[1] > self.fsizes: clust = data[:, self.fsizes:] else: clust = data # Computation of PCA for vector output if clust.shape[1] > 1: pca = PCA(n_components=pca_percentage) pca.fit(clust.T) clust = pca.components_.T if standard is True: scaler = preprocessing.StandardScaler() clust = scaler.fit_transform(clust) # Acquisition of clusterer try: # Clusterer is already a sklearn object self.logger.debug('Clusterer info:\n{}'.format( clusterer.get_params)) except AttributeError: # Instanciate clusterer from str try: clusterer = eval( "sklearn." + clusterer, {'__builtins__': None}, { 'sklearn': __import__('sklearn'), 'sklearn.cluster': __import__('sklearn.cluster'), 'sklearn.mixture': __import__('sklearn.mixture') }) except (TypeError, AttributeError): raise AttributeError('Clusterer unknown from sklearn.') self.logger.debug('Clusterer info:\n{}'.format( clusterer.get_params())) # Clustering try: labels = clusterer.fit_predict(clust) except AttributeError: clusterer.fit(clust) labels = clusterer.predict(clust) self.logger.debug('Cluster of data :{}'.format(samples, labels)) self.clusters_id = np.unique(labels) # Acqusition of Classifier try: # Classifier is already a sklearn object self.logger.debug('Classifier info:\n{}'.format( classifier.get_params)) except AttributeError: # Instanciate classifier from str try: classifier = eval('ske.' + classifier, {'__builtins__': None}, { 'ske': __import__('sklearn'), 'sklearn.svm': __import__('sklearn.svm'), 'sklearn.naive_bayes': __import__('sklearn.naive_bayes'), 'sklearn.gaussian_process': __import__('sklearn.gaussian_process'), 'sklearn.neighbors': __import__('sklearn.neighbors'), 'sklearn.ensemble': __import__('sklearn.ensemble') }) except (TypeError, AttributeError): raise AttributeError('Classifier unknown from sklearn.') self.logger.debug('Classifier info:\n{}'.format( classifier.get_params())) # Classification self.classifier = classifier self.classifier.fit(samples, labels) self.indice_clt = {} self.local_models = {} # Creation of local models for i, k in enumerate(self.clusters_id): idx = np.where(labels == k)[0] self.indice_clt[k] = list(idx) sample_ = [samples[j] for j in self.indice_clt[k]] data_ = [data[j, :self.fsizes] for j in self.indice_clt[k]] if pod is not None: from batman.pod import Pod local_pod = Pod(corners, **pod) snapshots = Sample(space=sample_, data=data_) local_pod.fit(snapshots) data_ = local_pod.VS else: local_pod = None from batman.surrogate import SurrogateModel if local_method is None: self.local_models[k] = SurrogateModel('kriging', corners, plabels=None) else: method = [lm for lm in local_method[i]][0] self.local_models[k] = SurrogateModel( method, corners, plabels=None, **local_method[i][method]) self.local_models[k].fit(np.asarray(sample_), np.asarray(data_), pod=local_pod)
60968.18181818182, 61072.72727272728, 61177.272727272735, 61281.81818181819, 61386.36363636365, 61490.9090909091, 61595.45454545456, 61700.0, 61818.75, 61937.5, 62056.25, 62175.0 ] ## PC-LS Strategy #Learning sample (x_sample) N_learning = 5000 learning_db = db_Mascaret(file_name=['/x_sampling.npy', '/y_sampling.npy'], multizone=True) x_learning = learning_db.data_input y_learning = learning_db.data_output PC_LS = SurrogateModel('pc', corners, plabels, strategy='LS', degree=7, distributions=dists_ot) PC_LS.fit(x_learning, y_learning) ## PC-quad Strategy # Create PC surrogate from (x_learning, y_learning) with QUADRATURE method PC_QUAD = SurrogateModel('pc', corners, plabels, strategy='Quad', degree=7, distributions=dists_ot) # Get Quadrature points x_quad = PC_QUAD.predictor.sample
#k_predictor = SurrogateModel('kriging', np.array(corners).transpose(), Nl, plabels, global_optimizer=False, kernel = Matern(length_scale=(1.0,)*in_dim, nu=2.5, length_scale_bounds=[(0.01,1000)]*in_dim)) #k_predictor.fit(x_a,np.array(y_l)) #y_pred_krig, _ = k_predictor(x_b) #k_uq = UQ(k_predictor, dists=dists, nsample=1000, plabels = plabels, xlabel='x(m)', flabel='H(m)', fname='uq_k') #k_uq.error_propagation() #k_sobol = k_uq.sobol() #rmse_k = np.sqrt(mean_squared_error(y_t, y_pred_krig)) #q2_k = r2_score(y_t, y_pred_krig) #PC surrogate with Least square method PCls_predictor = SurrogateModel('pc', corners, N_l, plabels, strategy='LS', degree=3, distributions=dists_ot) PCls_predictor.fit(x_learn, y_l) y_pred_PCls, _ = PCls_predictor(x_t) print(y_pred_PCls) print(y_pred_PCls - y_t[:len(y_pred_PCls)]) #rmse_pc_ls = np.sqrt(mean_squared_error(y_t, y_pred_PCls)) #q2_pc_ls = r2_score(y_t, y_pred_PCls) Ls_uq = UQ(PCls_predictor, dists=dists, nsample=1000, plabels=plabels,