Skip to content

Commit

Permalink
Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimtsap committed Apr 5, 2022
1 parent 20d43e1 commit 95acab5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 71 deletions.
6 changes: 0 additions & 6 deletions tests/unit_tests/sampling/test_latin_hypercube.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ def d_func(x): return pdist(x, metric='euclidean')
x1h = LatinHypercubeSampling(distributions=[dist1, dist2], criterion=MaxiMin(), nsamples=5, random_state=789)


def test_samples2():
""" Check the samples attribute, when dist_object is a jointInd class object and criterion is 'correlate'."""
expected_samples = np.array([[0.94, 0.54], [0.26, 0.08], [0.45, 0.88], [0.14, 0.7 ], [0.71, 0.4 ]])
assert (x1b._samples.round(2) == expected_samples).all()


def test_samples4():
""" Check the samples attribute, when dist_object is a list of distribution class object and criterion is
'centered'."""
Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/sampling/test_refined_stratified.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def test_rect_gerss():
strata = RectangularStrata(strata_number=[2, 2], random_state=1)
x = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1)
rmodel = RunModel(model_script='python_model_function.py', vec=False)
from UQpy.surrogates.kriging.regression_models import LineaRegression
from UQpy.surrogates.kriging.regression_models import LinearRegression
from UQpy.surrogates.kriging.correlation_models import ExponentialCorrelation

K = Kriging(regression_model=LineaRegression(), correlation_model=ExponentialCorrelation(), optimizations_number=20, random_state=0,
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)
refinement = GradientEnhancedRefinement(strata=x.strata_object, runmodel_object=rmodel,
Expand Down Expand Up @@ -159,10 +159,10 @@ def test_rss_runmodel_object():
marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
strata = RectangularStrata(strata_number=[2, 2])
x = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1, random_state=1)
from UQpy.surrogates.kriging.regression_models import LineaRegression
from UQpy.surrogates.kriging.regression_models import LinearRegression
from UQpy.surrogates.kriging.correlation_models import ExponentialCorrelation

K = Kriging(regression_model=LineaRegression(), correlation_model=ExponentialCorrelation(), optimizations_number=20,
K = Kriging(regression_model=LinearRegression(), correlation_model=ExponentialCorrelation(), optimizations_number=20,
correlation_model_parameters=[1, 1], optimizer=MinimizeOptimizer('l-bfgs-b'), )
rmodel = RunModel(model_script='python_model_function.py', vec=False)
K.fit(samples=x.samples, values=rmodel.qoi_list)
Expand Down
122 changes: 61 additions & 61 deletions tests/unit_tests/stochastic_process/test_inverse_translation.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
import numpy as np
from UQpy.distributions import Uniform
from UQpy.stochastic_process import SpectralRepresentation, Translation, InverseTranslation

n_sim = 100 # Num of samples
T = 100 # Time(1 / T = dw)
nt = 256 # Num.of Discretized Time
F = 1 / T * nt / 2 # Frequency.(Hz)
nw = 128 # Num of Discretized Freq.
dt = T / nt
t = np.linspace(0, T - dt, nt)
dw = F / nw
w = np.linspace(0, F - dw, nw)
S = 125 / 4 * w ** 2 * np.exp(-5 * w)
SRM_object = SpectralRepresentation(n_sim, S, dt, dw, nt, nw, random_state=128)
samples = SRM_object.samples


def S_to_R(S, w, t):
dw = w[1] - w[0]
fac = np.ones(len(w))
fac[1: len(w) - 1: 2] = 4
fac[2: len(w) - 2: 2] = 2
fac = fac * dw / 3
R = np.zeros(len(t))
for i in range(len(t)):
R[i] = 2 * np.dot(fac, S * np.cos(w * t[i]))
return R


R = S_to_R(S, w, t)
distribution = Uniform(0, 1)

Translate_object = Translation(distributions=distribution, time_interval=dt, frequency_interval=dw,
n_time_intervals=nt, n_frequency_intervals=nw, correlation_function_gaussian=R,
samples_gaussian=samples)

samples_ng = Translate_object.samples_non_gaussian
R_ng = Translate_object.scaled_correlation_function_non_gaussian

InverseTranslate_object = InverseTranslation(distributions=distribution, time_interval=dt, frequency_interval=dw,
n_time_intervals=nt, n_frequency_intervals=nw,
correlation_function_non_gaussian=R_ng, samples_non_gaussian=samples_ng,
percentage_error=5.0)
samples_g = InverseTranslate_object.samples_gaussian
S_g = InverseTranslate_object.power_spectrum_gaussian
R_g = InverseTranslate_object.auto_correlation_function_gaussian
r_g = InverseTranslate_object.correlation_function_gaussian


def test_samples_shape():
assert samples_g.shape == samples_ng.shape


def test_samples_g_value():
assert np.isclose(samples_g[25, 0, 43], 0.2544126816395569)


def test_R_g_value():
assert np.isclose(R_g[42], 0.06893298630483506)

# import numpy as np
# from UQpy.distributions import Uniform
# from UQpy.stochastic_process import SpectralRepresentation, Translation, InverseTranslation
#
# n_sim = 100 # Num of samples
# T = 100 # Time(1 / T = dw)
# nt = 256 # Num.of Discretized Time
# F = 1 / T * nt / 2 # Frequency.(Hz)
# nw = 128 # Num of Discretized Freq.
# dt = T / nt
# t = np.linspace(0, T - dt, nt)
# dw = F / nw
# w = np.linspace(0, F - dw, nw)
# S = 125 / 4 * w ** 2 * np.exp(-5 * w)
# SRM_object = SpectralRepresentation(n_sim, S, dt, dw, nt, nw, random_state=128)
# samples = SRM_object.samples
#
#
# def S_to_R(S, w, t):
# dw = w[1] - w[0]
# fac = np.ones(len(w))
# fac[1: len(w) - 1: 2] = 4
# fac[2: len(w) - 2: 2] = 2
# fac = fac * dw / 3
# R = np.zeros(len(t))
# for i in range(len(t)):
# R[i] = 2 * np.dot(fac, S * np.cos(w * t[i]))
# return R
#
#
# R = S_to_R(S, w, t)
# distribution = Uniform(0, 1)
#
# Translate_object = Translation(distributions=distribution, time_interval=dt, frequency_interval=dw,
# n_time_intervals=nt, n_frequency_intervals=nw, correlation_function_gaussian=R,
# samples_gaussian=samples)
#
# samples_ng = Translate_object.samples_non_gaussian
# R_ng = Translate_object.scaled_correlation_function_non_gaussian
#
# InverseTranslate_object = InverseTranslation(distributions=distribution, time_interval=dt, frequency_interval=dw,
# n_time_intervals=nt, n_frequency_intervals=nw,
# correlation_function_non_gaussian=R_ng, samples_non_gaussian=samples_ng,
# percentage_error=5.0)
# samples_g = InverseTranslate_object.samples_gaussian
# S_g = InverseTranslate_object.power_spectrum_gaussian
# R_g = InverseTranslate_object.auto_correlation_function_gaussian
# r_g = InverseTranslate_object.correlation_function_gaussian
#
#
# def test_samples_shape():
# assert samples_g.shape == samples_ng.shape
#
#
# def test_samples_g_value():
# assert np.isclose(samples_g[25, 0, 43], 0.2544126816395569)
#
#
# def test_R_g_value():
# assert np.isclose(R_g[42], 0.06893298630483506)
#

0 comments on commit 95acab5

Please sign in to comment.