Skip to content

Commit

Permalink
Move tests to the new version of RunModel
Browse files Browse the repository at this point in the history
  • Loading branch information
dimtsap committed Apr 6, 2022
1 parent dae5ab2 commit 2878db9
Show file tree
Hide file tree
Showing 36 changed files with 93 additions and 98 deletions.
2 changes: 1 addition & 1 deletion docs/code/RunModel/abaqus_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import time

from UQpy.distributions import Normal, Uniform
from UQpy.run_model.RunModel import *
from UQpy.run_model.RunModel_New import *
from UQpy.sampling import MonteCarloSampling

calling_directory = os.getcwd()
Expand Down
2 changes: 1 addition & 1 deletion docs/code/RunModel/ls_dyna_example_multijob.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# %%
from UQpy.distributions import Uniform
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.sampling import MonteCarloSampling

# %% md
Expand Down
2 changes: 1 addition & 1 deletion docs/code/RunModel/ls_dyna_example_singlejob.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# %%
from UQpy.distributions import Uniform
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.sampling import MonteCarloSampling

# %% md
Expand Down
2 changes: 1 addition & 1 deletion docs/code/RunModel/matlab_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
# %%

from UQpy.sampling import MonteCarloSampling
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions import Normal
import time
import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion docs/code/RunModel/opensees_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import numpy as np

from UQpy.distributions import Uniform
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.sampling import MonteCarloSampling

# %% md
Expand Down
2 changes: 1 addition & 1 deletion docs/code/RunModel/python_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
# %%

from UQpy.sampling import MonteCarloSampling
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions import Normal
import time
import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import matplotlib.pyplot as plt
from UQpy.sampling.mcmc import MetropolisHastings
from UQpy.inference import BayesModelSelection, BayesParameterEstimation, ComputationalModel
from UQpy.run_model.RunModel import RunModel # required to run the quadratic model
from UQpy.run_model.RunModel_New import RunModel_New # required to run the quadratic model
from UQpy.distributions import Normal, JointIndependent
from scipy.stats import multivariate_normal, norm

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from UQpy.sampling.ImportanceSampling import ImportanceSampling
from UQpy.inference import BayesParameterEstimation, ComputationalModel
from UQpy.run_model.RunModel import RunModel # required to run the quadratic model
from UQpy.run_model.RunModel_New import RunModel_New # required to run the quadratic model
from sklearn.neighbors import KernelDensity # for the plots
from UQpy.distributions import JointIndependent, Normal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from UQpy.sampling.mcmc.MetropolisHastings import MetropolisHastings
from UQpy.inference.inference_models.ComputationalModel import ComputationalModel
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.inference import BayesParameterEstimation
from sklearn.neighbors import KernelDensity # for the plots
from UQpy.distributions import JointIndependent, Normal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import shutil

from UQpy.inference import InformationModelSelection, MLE
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
import numpy as np
from UQpy.inference import BIC
import matplotlib.pyplot as plt
Expand Down
2 changes: 1 addition & 1 deletion docs/code/inference/mle/plot_regression_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from UQpy.inference import ComputationalModel, MLE
from UQpy.distributions import Normal
from UQpy.inference import MinimizeOptimizer
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New

#%% md
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import shutil

from UQpy.reliability import SubsetSimulation
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.sampling import Stretch, ModifiedMetropolisHastings, MonteCarloSampling
import numpy as np
import scipy.stats as stats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import numpy as np
# Import this newly defined Rosenbrock distribution into the Distributions module
from UQpy.distributions import Normal
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
import scipy.stats as stats

# %% md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

from UQpy.surrogates import GaussianProcessRegression
from UQpy.sampling import MonteCarloSampling, AdaptiveKriging
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions import Uniform
from local_BraninHoo import function
import time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from UQpy.surrogates.gaussian_process import GaussianProcessRegression
from UQpy.sampling import MonteCarloSampling, AdaptiveKriging
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions import Normal
from local_series import series
import matplotlib.pyplot as plt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from UQpy.sampling import TrueStratifiedSampling, RefinedStratifiedSampling
from UQpy.surrogates import GaussianProcessRegression
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions import Uniform
import matplotlib.pyplot as plt
from matplotlib import cm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from UQpy.sampling import TrueStratifiedSampling, RefinedStratifiedSampling
from UQpy.sampling import VoronoiStrata
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions import Uniform
import matplotlib.pyplot as plt
from matplotlib import cm
Expand Down
2 changes: 1 addition & 1 deletion docs/code/surrogates/gpr/plot_gpr_custom2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from UQpy.surrogates.gaussian_process.regression_models import ConstantRegression
from UQpy.sampling import RectangularStrata
from UQpy.sampling import TrueStratifiedSampling
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions import Uniform
import numpy as np
import matplotlib.pyplot as plt
Expand Down
2 changes: 1 addition & 1 deletion docs/code/surrogates/gpr/plot_gpr_sine.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from UQpy.sampling.stratified_sampling.strata import RectangularStrata
from UQpy.sampling import TrueStratifiedSampling
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions import Gamma
import numpy as np
import matplotlib.pyplot as plt
Expand Down
2 changes: 1 addition & 1 deletion docs/code/surrogates/srom/plot_srom_eigenvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from UQpy.sampling import RectangularStrata
from UQpy.distributions import Gamma
from UQpy.surrogates import SROM
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from scipy.stats import gamma
import numpy as np
import matplotlib.pyplot as plt
Expand Down
2 changes: 1 addition & 1 deletion src/UQpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import UQpy.dimension_reduction
import UQpy.inference
import UQpy.reliability
import UQpy.run_model.RunModel
import UQpy.run_model.RunModel_New
import UQpy.sampling
import UQpy.stochastic_process
import UQpy.surrogates
Expand Down
4 changes: 2 additions & 2 deletions src/UQpy/inference/inference_models/ComputationalModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from beartype import beartype

from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions.baseclass import Distribution
from UQpy.inference.inference_models.baseclass.InferenceModel import *
from UQpy.distributions.collection.MultivariateNormal import MultivariateNormal
Expand All @@ -16,7 +16,7 @@

class ComputationalModel(InferenceModel):
@beartype
def __init__(self, n_parameters: PositiveInteger, runmodel_object: RunModel,
def __init__(self, n_parameters: PositiveInteger, runmodel_object: RunModel_New,
error_covariance: Union[np.ndarray, float] = 1.0, name: str = "", prior: Distribution = None,
log_likelihood: Callable = None):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/UQpy/reliability/taylor_series/SORM.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import scipy.stats as stats
from beartype import beartype

from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions.baseclass import Distribution
from UQpy.utilities.ValidationTypes import PositiveInteger
from UQpy.reliability.taylor_series.FORM import FORM
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(
def build_from_first_order(
cls,
distributions: Union[None, Distribution, list[Distribution]],
runmodel_object: RunModel,
runmodel_object: RunModel_New,
seed_x: Union[list, np.ndarray] = None,
seed_u: Union[list, np.ndarray] = None,
df_step: Union[int, float] = 0.01,
Expand Down
1 change: 0 additions & 1 deletion src/UQpy/run_model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New

from UQpy.run_model.model_execution import *
4 changes: 2 additions & 2 deletions src/UQpy/sampling/AdaptiveKriging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from beartype import beartype
from sklearn.gaussian_process import GaussianProcessRegressor

from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.distributions.baseclass import Distribution
from UQpy.sampling.stratified_sampling.LatinHypercubeSampling import LatinHypercubeSampling
from UQpy.sampling.adaptive_kriging_functions.baseclass.LearningFunction import (
Expand All @@ -23,7 +23,7 @@ class AdaptiveKriging:
def __init__(
self,
distributions: Union[Distribution, list[Distribution]],
runmodel_object: RunModel,
runmodel_object: RunModel_New,
surrogate: SurrogateType,
learning_function: LearningFunction,
samples: Numpy2DFloatArray = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from UQpy.surrogates.baseclass import Surrogate
from UQpy.utilities.ValidationTypes import *
from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.sampling.stratified_sampling.refinement.baseclass.Refinement import *
from UQpy.utilities.Utilities import gradient
from UQpy.sampling.stratified_sampling.strata.VoronoiStrata import VoronoiStrata
Expand All @@ -17,7 +17,7 @@ class GradientEnhancedRefinement(Refinement):
def __init__(
self,
strata: Strata,
runmodel_object: RunModel,
runmodel_object: RunModel_New,
surrogate: Union[Surrogate, GaussianProcessRegressor, CompatibleSurrogate],
nearest_points_number: int = None,
qoi_name: str = None,
Expand Down
10 changes: 4 additions & 6 deletions src/UQpy/utilities/Utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import scipy.stats as stats
from UQpy.utilities.ValidationTypes import RandomStateType

from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New


def svd(matrix, rank=None, tol=None):
Expand Down Expand Up @@ -156,14 +156,12 @@ def gradient(runmodel_object=None, point=None, order="first", df_step=None):
if len(df_step) == 1:
df_step = [df_step[0]] * dimension

if not callable(runmodel_object) and not isinstance(runmodel_object, RunModel):
raise RuntimeError(
"A RunModel object or callable function must be provided as model."
)
if not callable(runmodel_object) and not isinstance(runmodel_object, RunModel_New):
raise RuntimeError("A RunModel object or callable function must be provided as model.")

def func(m):
def func_eval(x):
if isinstance(m, RunModel):
if isinstance(m, RunModel_New):
m.run(samples=x, append_samples=False)
return np.array(m.qoi_list).flatten()
else:
Expand Down
15 changes: 6 additions & 9 deletions tests/unit_tests/dimension_reduction/test_grassman.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,11 @@ def test_parsimonious():
X, X_color = make_s_curve(n, random_state=3, noise=0)
kernel = GaussianKernel()

dmaps_object = DiffusionMaps.build_from_data(data=X,
alpha=1.0, n_eigenvectors=9,
is_sparse=True, n_neighbors=100,
optimize_parameters=True,
kernel=kernel)
dmaps_object = DiffusionMaps(data=X, alpha=1.0, n_eigenvectors=9,
is_sparse=True, n_neighbors=100,
kernel=kernel)

dmaps_object.fit()
index, residuals = DiffusionMaps.parsimonious(dmaps_object.eigenvectors, 2)
dmaps_object.parsimonious(dim=2)

assert index[0] == 1
assert index[1] == 5
assert dmaps_object.parsimonious_indices[0] == 1
assert dmaps_object.parsimonious_indices[1] == 5
33 changes: 16 additions & 17 deletions tests/unit_tests/dimension_reduction/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def test_kernel_projection():
xk = GrassmannPoint(np.array([[-0.69535592, -0.0546034], [-0.34016974, -0.85332868],
[-0.63305978, 0.51850616]]))
points = [xi, xj, xk]
kernel = np.matrix.round(ProjectionKernel().calculate_kernel_matrix(points), 4)
kernel = ProjectionKernel()
kernel.calculate_kernel_matrix(points)
kernel = np.matrix.round(kernel.kernel_matrix, 4)

assert np.allclose(kernel, np.array([[2, 1.0063, 1.2345], [1.0063, 2, 1.0101], [1.2345, 1.0101, 2]]))

Expand All @@ -26,7 +28,10 @@ def test_kernel_binet_cauchy():
xj = GrassmannPoint(np.array([[0, np.sqrt(2) / 2], [1, 0], [0, -np.sqrt(2) / 2]]))
xk = GrassmannPoint(np.array([[-0.69535592, -0.0546034], [-0.34016974, -0.85332868], [-0.63305978, 0.51850616]]))
points = [xi, xj, xk]
kernel = np.matrix.round(BinetCauchyKernel().calculate_kernel_matrix(points), 4)

kernel = BinetCauchyKernel()
kernel.calculate_kernel_matrix(points)
kernel = np.matrix.round(kernel.kernel_matrix, 4)

assert np.allclose(kernel, np.array([[1, 0.0063, 0.2345], [0.0063, 1, 0.0101], [0.2345, 0.0101, 1]]))

Expand All @@ -37,10 +42,10 @@ def test_kernel_gaussian_1d():
xk = np.array([1, 2, 3, 4])
points = [xi, xj, xk]
gaussian = GaussianKernel(epsilon=2.0)
kernel = gaussian.calculate_kernel_matrix(points)
gaussian.calculate_kernel_matrix(points)

assert np.allclose(np.matrix.round(kernel, 4), np.array([[1., 0.2645, 1.], [0.2645, 1., 0.2645], [1, 0.2645, 1]]),
atol=1e-04)
assert np.allclose(np.matrix.round(gaussian.kernel_matrix, 4),
np.array([[1., 0.2645, 1.], [0.2645, 1., 0.2645], [1, 0.2645, 1]]), atol=1e-04)
assert np.round(gaussian.epsilon, 4) == 2


Expand All @@ -50,11 +55,11 @@ def test_kernel_gaussian_2d():
xk = np.array([[-0.69535592, -0.0546034], [-0.34016974, -0.85332868], [-0.63305978, 0.51850616]])
points = [xi, xj, xk]
gaussian = GaussianKernel()
kernel = gaussian.calculate_kernel_matrix(points)
gaussian.calculate_kernel_matrix(points)

assert np.allclose(np.matrix.round(kernel, 4), np.array([[1., 0.628, 0.3912],
[0.628, 1., 0.2534],
[0.3912, 0.2534, 1.]]))
assert np.allclose(np.matrix.round(gaussian.kernel_matrix, 4), np.array([[1., 0.628, 0.3912],
[0.628, 1., 0.2534],
[0.3912, 0.2534, 1.]]))
assert np.round(gaussian.epsilon, 4) == 1.0


Expand Down Expand Up @@ -88,22 +93,16 @@ def test_kernel_gaussian_2d():


def test_kernel():
D1 = 6
r0 = 2 # rank sample 0
r1 = 3 # rank sample 1
r2 = 4 # rank sample 2
r3 = 3 # rank sample 2

np.random.seed(1111) # For reproducibility.
from numpy.random import RandomState
rnd = RandomState(0)

# Creating a list of solutions.
Solutions = [sol0, sol1, sol2, sol3]
from UQpy.dimension_reduction.grassmann_manifold.GrassmannOperations import GrassmannOperations
manifold_projection = SVDProjection(Solutions, p="max")
kernel = ProjectionKernel()

kernel = kernel.calculate_kernel_matrix(manifold_projection.u)
kernel.calculate_kernel_matrix(manifold_projection.u)

assert np.round(kernel[0, 1], 8) == 6.0
assert np.round(kernel.kernel_matrix[0, 1], 8) == 6.0
3 changes: 0 additions & 3 deletions tests/unit_tests/inference/test_bayes_model_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,3 @@ def test_models():
assert selection.candidate_models[1].name == 'model_cubic'
assert selection.candidate_models[2].name == 'model_linear'

shutil.rmtree(runmodel4.model.model_dir)
shutil.rmtree(runmodel5.model.model_dir)
shutil.rmtree(runmodel6.model.model_dir)
Loading

0 comments on commit 2878db9

Please sign in to comment.