# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
from hypothesis import given, settings
from hypothesis.strategies import integers

import bayesmark.space as sp
from bayesmark.np_util import linear_rescale
from bayesmark.random_search import suggest_dict
from hypothesis_util import close_enough, gufunc_floats, seeds
from util import space_configs


@given(space_configs(allow_missing=True), integers(min_value=1, max_value=8),
       seeds())
@settings(deadline=None)
def test_random_search_suggest_sanity(api_args, n_suggest, seed):
    meta, X, y, _ = api_args

    # Get the unwarped X
    S = sp.JointSpace(meta)
    lower, upper = S.get_bounds().T
    S.validate(X)

    N = len(X)
    # Split history and call twice with diff histories but same seed
    M = N // 2
    X1, X2 = X[:M], X[M:]
    y1, y2 = y[:M], y[M:]
Example #2
0
            assert self.random.rand() <= 0.5


class FlakyProblem(TestFunction):
    def __init__(self, api_config, random):
        TestFunction.__init__(self)
        self.api_config = api_config
        self.random = random

    def evaluate(self, params):
        assert self.random.rand() <= 0.5
        return 0.0


@given(
    space_configs(allow_missing=True),
    sampled_from(MODEL_NAMES),
    sampled_from(DATA_LOADER_NAMES),
    sampled_from(METRICS),
    integers(0, 5),
    integers(1, 3),
    seeds(),
)
@settings(max_examples=10, deadline=None)
def test_run_study(api_config, model_name, dataset, scorer, n_calls,
                   n_suggestions, seed):
    api_config, _, _, _ = api_config

    prob_type = data.get_problem_type(dataset)
    assume(scorer in data.METRICS_LOOKUP[prob_type])
Example #3
0
    lower, upper = S.get_bounds().T
    assert np.all(lower <= y)
    assert np.all(y <= upper)

    y2 = S.validate_warped(y)
    assert close_enough(y, y2)

    x2 = S.unwarp(y)
    assert x2.shape == x.shape
    x3 = S.validate(x2)
    assert close_enough(x2, x3)

    assert close_enough(x, x2)


@given(space_configs())
def test_joint_space_unwarp_warp(args):
    meta, X, _, _ = args

    S = sp.JointSpace(meta)
    S.validate(X)

    X_w2 = S.warp(X)
    assert X_w2.dtype == sp.WARPED_DTYPE

    # Test bounds
    lower, upper = S.get_bounds().T
    assert np.all(lower <= X_w2)
    assert np.all(X_w2 <= upper)

    X2 = S.unwarp(X_w2)
Example #4
0
        return signatures, signatures_ref

    sig_dict = dictionaries(text(), tuples(bsigs()) | tuples(bsigs(), bsigs()))
    S = sig_dict.map(separate)
    return S


def some_mock_f(x):
    """Some arbitrary deterministic test function.
    """
    random_stream = pyrandom.Random(json.dumps(x, sort_keys=True))
    y = random_stream.gauss(0, 1)
    return y


@given(space_configs())
def test_get_func_signature(api_config):
    api_config, _, _, _ = api_config

    signature_x, signature_y = ss.get_func_signature(some_mock_f, api_config)


@given(dictionaries(text(), sigs()))
def test_analyze_signatures(signatures):
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=RuntimeWarning)
        sig_errs, signatures_median = ss.analyze_signatures(signatures)


@given(sig_pair())
def test_analyze_signature_pair(args):