Skip to content

Commit

Permalink
Fixes RSS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimtsap committed Nov 21, 2022
1 parent 08b5f33 commit 2818b25
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(

self.random_state = random_state
if isinstance(self.random_state, int):
self.random_state = np.random.RandomState(self.random_state)
self.random_state = np.random.default_rng(self.random_state)
elif not isinstance(self.random_state, (type(None), np.random.RandomState)):
raise TypeError('UQpy: random_state must be None, an int or an np.random.Generator object.')
if self.random_state is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def finalize(self, samples, samples_per_iteration):
def identify_bins(strata_metrics, points_to_add, random_state):
bins2break = np.array([])
points_left = points_to_add
while (np.where(strata_metrics == strata_metrics.max())[0].shape[0] < points_left):
while np.where(strata_metrics == strata_metrics.max())[0].shape[0] < points_left:
bin = np.where(strata_metrics == strata_metrics.max())[0]
bins2break = np.hstack([bins2break, bin])
strata_metrics[bin] = 0
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/sampling/test_adaptive_kriging.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_akmcs_u():
random_state=2)
a.run(nsamples=25, samples=x.samples)

assert a.samples[23, 0] == -3.781937137406927
assert a.samples[23, 0] == 4.027342825480197
assert a.samples[20, 1] == 0.17610325620498946


Expand All @@ -75,7 +75,7 @@ def test_akmcs_expected_feasibility():
random_state=2)
a.run(nsamples=25, samples=x.samples)

assert a.samples[23, 0] == 5.423754197908594
assert a.samples[23, 0] == 4.553078100499578
assert a.samples[20, 1] == 2.0355505295053384


Expand Down
45 changes: 18 additions & 27 deletions tests/unit_tests/sampling/test_refined_stratified.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from beartype.roar import BeartypeCallHintPepParamException

from UQpy import GaussianProcessRegression, RBF, LinearRegression
from UQpy.run_model.model_execution.PythonModel import PythonModel
from UQpy.utilities.MinimizeOptimizer import MinimizeOptimizer
from UQpy.sampling.stratified_sampling.refinement.GradientEnhancedRefinement import GradientEnhancedRefinement
Expand Down Expand Up @@ -71,31 +72,22 @@ def test_rect_gerss():
x = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1)
model = PythonModel(model_script='python_model_function.py', model_object_name="y_func")
rmodel = RunModel(model=model)
from UQpy.surrogates.kriging.regression_models import LinearRegression
from UQpy.surrogates.kriging.correlation_models import ExponentialCorrelation

K = Kriging(regression_model=LinearRegression(), correlation_model=ExponentialCorrelation(), optimizations_number=20, random_state=0,
correlation_model_parameters=[1, 1], optimizer=MinimizeOptimizer('l-bfgs-b'), )
K.fit(samples=x.samples, values=rmodel.qoi_list)
kernel1 = RBF()
bounds_1 = [[10 ** (-4), 10 ** 3], [10 ** (-3), 10 ** 2], [10 ** (-3), 10 ** 2]]
optimizer1 = MinimizeOptimizer(method='L-BFGS-B', bounds=bounds_1)
gpr = GaussianProcessRegression(kernel=kernel1, hyperparameters=[1, 10 ** (-3), 10 ** (-2)], optimizer=optimizer1,
optimizations_number=100, noise=False, regression_model=LinearRegression(),
random_state=0)
# gpr.fit(samples=x.samples, values=rmodel.qoi_list)
refinement = GradientEnhancedRefinement(strata=x.strata_object, runmodel_object=rmodel,
surrogate=K, nearest_points_number=4)
surrogate=gpr, nearest_points_number=4)
z = RefinedStratifiedSampling(stratified_sampling=x, random_state=2, refinement_algorithm=refinement)
z.run(nsamples=6)
assert np.allclose(z.samples, np.array([[0.417022, 0.36016225], [1.00011437, 0.15116629],
[0.14675589, 0.5461693], [1.18626021, 0.67278036],
[1.51296312, 0.77483124], [0.74237455, 0.66026822]]))
# assert np.allclose(z.samples, np.array([[0.417022, 0.36016225], [1.00011437, 0.15116629],
# [0.14675589, 0.5461693], [1.18626021, 0.67278036],
# [1.59254104, 0.96577043], [1.97386531, 0.24237455]]))
# assert np.allclose(z.samples, np.array([[0.417022, 0.36016225], [1.00011437, 0.15116629],
# [0.14675589, 0.5461693], [1.18626021, 0.67278036],
# [1.59254104, 0.96577043], [1.7176612, 0.2101839]]))
# assert np.allclose(z.samplesU01, np.array([[0.208511, 0.36016225], [0.50005719, 0.15116629],
# [0.07337795, 0.5461693], [0.59313011, 0.67278036],
# [0.79627052, 0.96577043], [0.98693265, 0.24237455]]))
# assert np.allclose(z.samplesU01, np.array([[0.208511, 0.36016225], [0.50005719, 0.15116629],
# [0.07337795, 0.5461693], [0.59313011, 0.67278036],
# [0.79627052, 0.96577043], [0.8588306 , 0.2101839]]))
[1.64924557, 0.90711287],
[0.54595797, 0.30005026]]))


def test_vor_rss():
Expand Down Expand Up @@ -123,19 +115,18 @@ def test_vor_gerss():
marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
strata_vor = VoronoiStrata(seeds_number=4, dimension=2, random_state=10)
x_vor = TrueStratifiedSampling(distributions=marginals, strata_object=strata_vor, nsamples_per_stratum=1, )
from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation
model = PythonModel(model_script='python_model_function.py', model_object_name="y_func")
rmodel = RunModel(model=model)
K_ = Kriging(regression_model=LinearRegression(), correlation_model=ExponentialCorrelation(), optimizations_number=20,
optimizer=MinimizeOptimizer('l-bfgs-b'), random_state=0,
correlation_model_parameters=[1, 1])

K_.fit(samples=x_vor.samples, values=rmodel.qoi_list)
kernel1 = RBF()
bounds_1 = [[10 ** (-4), 10 ** 3], [10 ** (-3), 10 ** 2], [10 ** (-3), 10 ** 2]]
optimizer1 = MinimizeOptimizer(method='L-BFGS-B', bounds=bounds_1)
gpr = GaussianProcessRegression(kernel=kernel1, hyperparameters=[1, 10 ** (-3), 10 ** (-2)], optimizer=optimizer1,
optimizations_number=100, noise=False, regression_model=LinearRegression(),
random_state=0)
z_vor = RefinedStratifiedSampling(stratified_sampling=x_vor, nsamples=6, random_state=x_vor.random_state,
refinement_algorithm=GradientEnhancedRefinement(strata=x_vor.strata_object,
runmodel_object=rmodel,
surrogate=K_,
surrogate=gpr,
nearest_points_number=4))
assert np.allclose(z_vor.samples, np.array([[1.78345908, 0.01640854], [1.46201137, 0.70862104],
[0.4021338, 0.05290083], [0.1062376, 0.88958226],
Expand Down

0 comments on commit 2818b25

Please sign in to comment.