Skip to content
Snippets Groups Projects
test_InputSpace.py 15.8 KiB
Newer Older
# -*- coding: utf-8 -*-
"""
Test the InputSpace class in bayesvalidrox.
Tests are available for the following functions
Class InputSpace: 
    check_valid_inputs  - x
    init_param_space    - x
    build_polytypes     - x
    transform           - x

"""
import sys
import pytest
import numpy as np

sys.path.append("src/")
sys.path.append("../src/")

from bayesvalidrox.surrogate_models.inputs import Input
from bayesvalidrox.surrogate_models.input_space import InputSpace

kohlhaasrebecca's avatar
kohlhaasrebecca committed


#%% Test ExpDesign.check_valid_input

def test_check_valid_input_hasmarg() -> None:
    """
    Distribution not built if no marginals set
    """
    inp = Input()
    with pytest.raises(AssertionError) as excinfo:
    assert str(excinfo.value) == 'Cannot build distributions if no marginals are given'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_check_valid_input_haspriors() -> None:
    """
    Distribution not built if no distribution set for the marginals
    """
    inp = Input()
    inp.add_marginals()
    with pytest.raises(AssertionError) as excinfo:
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    assert str(excinfo.value) == 'Not all marginals were provided priors'


def test_check_valid_input_priorsmatch() -> None:
    """
    Distribution not built if dist types do not align
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    inp.add_marginals()
    inp.Marginals[1].dist_type = 'normal'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[1].parameters = [0, 1]
    with pytest.raises(AssertionError) as excinfo:
    assert str(excinfo.value) == 'Distributions cannot be built as the priors have different types'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_check_valid_input_samples() -> None:
    """
    Design built correctly - samples
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    inp.add_marginals()
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[1].input_data = x + 2
    InputSpace(inp)


def test_check_valid_input_both() -> None:
    """
    Design no built - samples and dist type given
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    inp.Marginals[0].dist_type = 'normal'
    with pytest.raises(AssertionError) as excinfo:
    assert str(excinfo.value) == 'Both samples and distribution type are given. Please choose only one.'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

# def test_check_valid_input_distnotok() -> None:
#    """
#    Design built incorrectly - dist types without parameters
#    """
#    inp = Input()
#    inp.add_marginals()
#    inp.Marginals[0].dist_type = 'normal'
#    inp.add_marginals()
#    inp.Marginals[1].dist_type = 'normal'
#    with pytest.raises(AssertionError) as excinfo:
#        exp = ExpDesigns(inp)
#    assert str(excinfo.value) == 'Some distributions do not have characteristic values'
kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_check_valid_input_distok() -> None:
    """
    Design built correctly - dist types
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'normal'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0, 1]
    inp.add_marginals()
    inp.Marginals[1].dist_type = 'normal'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[1].parameters = [0, 1]
    InputSpace(inp)


def test_check_valid_input_noapc() -> None:
    """
    Design built correctly - no apc
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'normal'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0, 1]
    inp.add_marginals()
    inp.Marginals[1].dist_type = 'normal'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[1].parameters = [0, 1]
    InputSpace(inp, meta_Model_type='gpe')


#%% Test ExpDesign.build_polytypes
def test_build_polytypes_normalerr() -> None:
    """
    Build dist 'normal' - too few params
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'normal'
    inp.Marginals[0].parameters = []
    exp = InputSpace(inp)
    with pytest.raises(AssertionError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'Distribution has too few parameters!'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_build_polytypes_normal() -> None:
    """
    Build dist 'normal'
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'normal'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0, 1]
    exp = InputSpace(inp)
    exp.build_polytypes(False)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_build_polytypes_uniferr() -> None:
    """
    Build dist 'unif' - too few params
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'unif'
    inp.Marginals[0].parameters = []
    exp = InputSpace(inp)
    with pytest.raises(AssertionError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'Distribution has too few parameters!'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_build_polytypes_unif() -> None:
    """
    Build dist 'unif'
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'unif'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0, 1]
    exp = InputSpace(inp)
    exp.build_polytypes(False)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_build_polytypes_gammaerr() -> None:
    """
    Build dist 'gamma' - too few params
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'gamma'
    inp.Marginals[0].parameters = []
    exp = InputSpace(inp)
    with pytest.raises(AssertionError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'Distribution has too few parameters!'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

# noinspection SpellCheckingInspection
def test_build_polytypes_gamma() -> None:
    """
    Build dist 'gamma'
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'gamma'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0, 1, 0]
    exp = InputSpace(inp)
    with pytest.raises(ValueError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'Parameter values are not valid, please set differently'
kohlhaasrebecca's avatar
kohlhaasrebecca committed


# noinspection SpellCheckingInspection
def test_build_polytypes_betaerr() -> None:
    """
    Build dist 'beta' - too few params
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'beta'
    inp.Marginals[0].parameters = []
    exp = InputSpace(inp)
    with pytest.raises(AssertionError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'Distribution has too few parameters!'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_build_polytypes_beta() -> None:
    """
    Build dist 'beta'
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'beta'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0.5, 1, 2, 3]
    exp = InputSpace(inp)
    exp.build_polytypes(False)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


# noinspection SpellCheckingInspection
def test_build_polytypes_lognormerr() -> None:
    """
    Build dist 'lognorm' - too few params
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'lognorm'
    inp.Marginals[0].parameters = []
    exp = InputSpace(inp)
    with pytest.raises(AssertionError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'Distribution has too few parameters!'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_build_polytypes_lognorm() -> None:
    """
    Build dist 'lognorm'
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'lognorm'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0.5, 1, 2, 3]
    exp = InputSpace(inp)
    exp.build_polytypes(False)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_build_polytypes_exponerr() -> None:
    """
    Build dist 'expon' - too few params
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'beta'
    inp.Marginals[0].parameters = []
    exp = InputSpace(inp)
    with pytest.raises(AssertionError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'Distribution has too few parameters!'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_build_polytypes_expon() -> None:
    """
    Build dist 'expon'
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'expon'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0.5, 1, 2, 3]
    exp = InputSpace(inp)
    exp.build_polytypes(False)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_build_polytypes_weibullerr() -> None:
    """
    Build dist 'weibull' - too few params
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'weibull'
    inp.Marginals[0].parameters = []
    exp = InputSpace(inp)
    with pytest.raises(AssertionError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'Distribution has too few parameters!'

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_build_polytypes_weibull() -> None:
    """
    Build dist 'weibull'
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'weibull'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0.5, 1, 2, 3]
    exp = InputSpace(inp)
    exp.build_polytypes(False)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_build_polytypes_arbitrary() -> None:
    """
    Build poly 'arbitrary'
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.build_polytypes(False)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_build_polytypes_rosenblatt() -> None:
    """
    Build dist with rosenblatt
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.build_polytypes(True)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_build_polytypes_samples() -> None:
    """
    Build dist from samples
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.build_polytypes(False)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_build_polytypes_samples2d() -> None:
    """
    Build dist from samples - samples too high dim
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, (2, 1000))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    with pytest.raises(ValueError) as excinfo:
        exp.build_polytypes(False)
    assert str(excinfo.value) == 'The samples provided to the Marginals should be 1D only'
kohlhaasrebecca's avatar
kohlhaasrebecca committed


#%% Test ExpDesign.init_param_space

def test_init_param_space_nomaxdegsample() -> None:
    """
    Init param space without max_deg for given samples
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.init_param_space()

kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_init_param_space_maxdeg() -> None:
    """
    Init param space with max_deg for given samples
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_init_param_space_maxdegdist() -> None:
    """
    Init param space with max_deg for given dist (not uniform)
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'expon'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0.5, 1, 2, 3]
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_init_param_space_maxdegdistunif() -> None:
    """
    Init param space with max_deg for given dist (uniform)
    """
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'unif'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0.5, 1, 2, 3]
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


#%% Test ExpDesign.transform
kohlhaasrebecca's avatar
kohlhaasrebecca committed

def test_transform_noparamspace() -> None:
    """
    Call transform without a built JDist
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    y = np.random.uniform(0, 1, (2, 1000))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    with pytest.raises(AttributeError) as excinfo:
        exp.transform(y)
    assert str(excinfo.value) == 'Call function init_param_space first to create JDist'
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_transform_dimerrlow() -> None:
    """
    Call transform with too few dimensions
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
    with pytest.raises(AttributeError) as excinfo:
        exp.transform(x)
    assert str(excinfo.value) == 'X should have two dimensions'
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_transform_dimerrhigh() -> None:
    """
    Call transform with too many dimensions
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    y = np.random.uniform(0, 1, (1, 1, 1000))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
    with pytest.raises(AttributeError) as excinfo:
        exp.transform(y)
    assert str(excinfo.value) == 'X should have two dimensions'
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_transform_dimerr0() -> None:
    """
    Call transform with wrong X.shape[0]
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    y = np.random.uniform(0, 1, (2, 1000))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
    with pytest.raises(AttributeError) as excinfo:
        exp.transform(y)
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    assert str(
        excinfo.value) == 'The second dimension of X should be the same size as the number of marginals in the InputObj'


def test_transform_paramspace() -> None:
    """
    Transform successfully
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    y = np.random.uniform(0, 1, (1000, 1))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
    exp.transform(y)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


kohlhaasrebecca's avatar
kohlhaasrebecca committed
if 0: # See Issue #41, use these again when the issue is removed
    def test_transform_rosenblatt() -> None:
        """
        Transform with rosenblatt
        """
        x = np.random.uniform(0, 1, 1000)
        y = np.random.uniform(0, 1, (1000, 1))
        inp = Input()
        inp.Rosenblatt = True
        inp.add_marginals()
        inp.Marginals[0].input_data = x
        exp = InputSpace(inp)
        exp.init_param_space(max_deg=2)
        exp.transform(y)
    
    
    def test_transform_rosenblattuser() -> None:
        """
        Transform with rosenblatt and method 'user'
        """
        x = np.random.uniform(0, 1, 1000)
        y = np.random.uniform(0, 1, (1000, 1))
        inp = Input()
        inp.Rosenblatt = True
        inp.add_marginals()
        inp.Marginals[0].input_data = x
        exp = InputSpace(inp)
        exp.init_param_space(max_deg=2)
        exp.transform(y, method='user')
    
    
def test_transform_user() -> None:
    """
    Transform with method 'user'
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    x = np.random.uniform(0, 1, 1000)
    y = np.random.uniform(0, 1, (1000, 1))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].input_data = x
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    exp.transform(y, method='user')

def test_transform_uniform() -> None:
    """
    Transform uniform dist
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    y = np.random.uniform(0, 1, (1000, 1))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'unif'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0, 1]
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
    exp.transform(y)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_transform_norm() -> None:
    """
    Transform normal dist
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    y = np.random.uniform(0, 1, (1000, 1))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'norm'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [0, 1]
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
    exp.transform(y)
kohlhaasrebecca's avatar
kohlhaasrebecca committed


# TODO: what are these other params here???
def test_transform_gammanoparam() -> None:
    """
    Transform gamma dist - no parameters
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    y = np.random.uniform(0, 1, (1000, 1))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'gamma'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [1, 1, 0]
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
    with pytest.raises(AttributeError) as excinfo:
        exp.transform(y)
    assert str(excinfo.value) == 'Additional parameters have to be set for the gamma distribution!'
kohlhaasrebecca's avatar
kohlhaasrebecca committed


def test_transform_gammaparam() -> None:
    """
    Transform gamma dist - with parameters
    """
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    y = np.random.uniform(0, 1, (1000, 1))
    inp = Input()
    inp.add_marginals()
    inp.Marginals[0].dist_type = 'gamma'
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    inp.Marginals[0].parameters = [1, 1, 0]
    exp = InputSpace(inp)
    exp.init_param_space(max_deg=2)
kohlhaasrebecca's avatar
kohlhaasrebecca committed
    exp.transform(y, params=[1, 1])