def test_fail_on_nan_data(bad_data_file, input_data_file, output_data_file):

    with pytest.raises(ValueError):
        ModelFromData(bad_data_file, output_data_file, 1.)

    with pytest.raises(ValueError):
        ModelFromData(input_data_file, bad_data_file, 1.)
def test_fails_on_duplicate_input_data(input_data_file_with_duplicates,
                                       output_data_file):

    with pytest.raises(ValueError):

        data_model = ModelFromData(input_data_file_with_duplicates,
                                   output_data_file, 1.)
        data_model.evaluate([1, 2, 3, 4, 5])
Ejemplo n.º 3
0
def test_fail_on_nan_data(bad_data_file, input_data_file, output_data_file):
    """
    Ensure an exception is raised if data with any NaNs is provided.
    """
    with pytest.raises(ValueError):
        ModelFromData(bad_data_file, output_data_file, 1.)

    with pytest.raises(ValueError):
        ModelFromData(input_data_file, bad_data_file, 1.)
Ejemplo n.º 4
0
def test_fails_on_unmatched_input(input_data_file, output_data_file):
    """
    Ensure an exception is raised if an input supplied to evaluate does not
    have a match in the input data.
    """
    data_model = ModelFromData(input_data_file, output_data_file, 1.)

    with pytest.raises(ValueError):
        data_model.evaluate(-1)
Ejemplo n.º 5
0
def test_single_row_2d_input_data(input_data_file_2d, output_data_file_2d):
    """
    Test ability to evaluate one row of input data passed in as 2d array.
    """
    # Initialize model from spring-mass example data files:
    data_model = ModelFromData(input_data_file_2d, output_data_file_2d, 1.)

    # Should be able to do this without an exception.
    one_row_2d_input = np.array([data_model._inputs[0]])
    data_model.evaluate(one_row_2d_input)
Ejemplo n.º 6
0
def test_fails_on_duplicate_input_data(input_data_file_with_duplicates,
                                       output_data_file):
    """
    Ensure an exception is raised if there are duplicate inputs found in the
    input data.
    """
    with pytest.raises(ValueError):

        data_model = ModelFromData(input_data_file_with_duplicates,
                                   output_data_file, 1.)
        data_model.evaluate([1, 2, 3, 4, 5])
def test_evaluate_2d_data(input_data_file_2d, output_data_file_2d, index):

    # Initialize model from spring-mass example data files:
    data_model = ModelFromData(input_data_file_2d, output_data_file_2d, 1.)

    input_data = np.genfromtxt(input_data_file_2d)
    output_data = np.genfromtxt(output_data_file_2d)

    # Model expects arrays as inputs/outputs
    model_output = data_model.evaluate(input_data[index])

    true_output = output_data[index]
    assert np.all(np.isclose(model_output, true_output))
def test_evaluate_with_cost_delay(cost, input_data_file, output_data_file):

    model = ModelFromData(input_data_file, output_data_file, cost=cost,
                          wait_cost_duration=True)

    sample = model._inputs[0]

    start_time = timeit.default_timer()
    model.evaluate(sample)
    evaluation_time = timeit.default_timer() - start_time

    # Ensure evaluation time was close to specified cost.
    assert np.abs(evaluation_time - cost) < .01
def test_skip_rows(input_data_file_2d, output_data_file_2d, rows_to_skip):

    normal_model = ModelFromData(input_data_file_2d, output_data_file_2d, 1.)

    normal_row_count = normal_model._inputs.shape[0]

    skipped_row_model = ModelFromData(input_data_file_2d,
                                      output_data_file_2d,
                                      skip_header=rows_to_skip,
                                      cost=1.)

    skipped_row_count = skipped_row_model._inputs.shape[0]

    assert normal_row_count - rows_to_skip == skipped_row_count
Ejemplo n.º 10
0
def test_evaluate_1d_data(input_data_file, output_data_file, index):
    """
    Test ability to evaluate one dimensional data.
    """
    # Initialize model from spring-mass example data files:
    data_model = ModelFromData(input_data_file, output_data_file, 1.)

    input_data = np.genfromtxt(input_data_file)
    output_data = np.genfromtxt(output_data_file)

    # Model expects arrays as inputs/outputs.
    model_output = data_model.evaluate(input_data[index])

    true_output = output_data[index]
    assert np.all(np.isclose(model_output, true_output))
Ejemplo n.º 11
0
def models_from_2d_data():
    """
    Creates a list of three ModelFromData objects with a small amount of
    two dimensional data.
    """
    input_filepath = os.path.join(data_path, "2D_test_data.csv")
    output1_filepath = os.path.join(data_path, "2D_test_data_output.csv")
    output2_filepath = os.path.join(data_path, "2D_test_data_output.csv")
    output3_filepath = os.path.join(data_path, "2D_test_data_output.csv")

    model1 = ModelFromData(input_filepath, output1_filepath, 1.)
    model2 = ModelFromData(input_filepath, output2_filepath, 4.)
    model3 = ModelFromData(input_filepath, output3_filepath, 16.)

    return [model1, model2, model3]
def models_from_data():
    """
    Creates a list of three ModelFromData objects of increasing fidelity.
    :return:
    """
    input_filepath = os.path.join(data_path, "spring_mass_1D_inputs.txt")
    output1_filepath = os.path.join(data_path, "spring_mass_1D_outputs_1.0.txt")
    output2_filepath = os.path.join(data_path, "spring_mass_1D_outputs_0.1.txt")
    output3_filepath = os.path.join(data_path,
                                    "spring_mass_1D_outputs_0.01.txt")

    model1 = ModelFromData(input_filepath, output1_filepath, 1.)
    model2 = ModelFromData(input_filepath, output2_filepath, 4.)
    model3 = ModelFromData(input_filepath, output3_filepath, 16.)

    return [model1, model2, model3]
Ejemplo n.º 13
0
def test_fail_if_model_outputs_do_not_match_shapes(filename_2d_5_column_data,
                                                   filename_2d_3_column_data):
    """
    Ensures simulator throws an exception if inputs and outputs with differing
    numbers of samples are provided.
    """
    model1 = ModelFromData(filename_2d_5_column_data,
                           filename_2d_5_column_data, 1.)

    model2 = ModelFromData(filename_2d_5_column_data,
                           filename_2d_3_column_data, 4.)

    data_input = InputFromData(filename_2d_5_column_data)

    with pytest.raises(ValueError):
        MLMCSimulator(models=[model1, model2], data=data_input)
Ejemplo n.º 14
0
def test_input_output_with_differing_column_count(filename_2d_5_column_data,
                                                  filename_2d_3_column_data):
    """
    Ensures that simulator handles input and output data with differing numbers
    of columns.
    """
    model1 = ModelFromData(filename_2d_5_column_data,
                           filename_2d_3_column_data, 1.)

    model2 = ModelFromData(filename_2d_5_column_data,
                           filename_2d_3_column_data, 4.)

    data_input = InputFromData(filename_2d_5_column_data)

    sim = MLMCSimulator(models=[model1, model2], data=data_input)
    sim.simulate(100., 10)
Ejemplo n.º 15
0
def test_evaluate_with_cost_delay(cost, input_data_file, output_data_file):
    """
    Test ability to wait for the duration of the cost specified for the model
    when evaluating a sample.
    """
    model = ModelFromData(input_data_file,
                          output_data_file,
                          cost=cost,
                          wait_cost_duration=True)

    sample = model._inputs[0]

    start_time = timeit.default_timer()
    model.evaluate(sample)
    evaluation_time = timeit.default_timer() - start_time

    # Ensure evaluation time was close to specified cost.
    assert np.abs(evaluation_time - cost) < .01
def test_evaluate_fails_on_invalid_input(input_data_file, output_data_file):

    data_model = ModelFromData(input_data_file, output_data_file, 1.)

    bogus_input = "five"

    with pytest.raises(TypeError):
        data_model.evaluate(bogus_input)

    bogus_input = [[1, 2], [3, 4]]

    with pytest.raises(ValueError):
        data_model.evaluate(bogus_input)
Ejemplo n.º 17
0
def test_evaluate_fails_on_invalid_input(input_data_file, output_data_file):
    """
    Ensure that an invalid input triggers an exception in evaluate.
    """
    data_model = ModelFromData(input_data_file, output_data_file, 1.)

    bogus_input = "five"

    with pytest.raises(TypeError):
        data_model.evaluate(bogus_input)

    bogus_input = [[1, 2], [3, 4]]

    with pytest.raises(ValueError):
        data_model.evaluate(bogus_input)
def test_init_fails_on_invalid_cost(input_data_file, output_data_file, cost):

    with pytest.raises(Exception):
        ModelFromData(input_data_file, output_data_file, cost)
Ejemplo n.º 19
0
from MLMCPy.mlmc import MLMCSimulator
from MLMCPy.input import InputFromData
from MLMCPy.model import ModelFromData

my_path = os.path.dirname(os.path.abspath(__file__))
data_path = my_path + "/../../tests/testing_data"
data_input = InputFromData(os.path.join(data_path,
                                        "spring_mass_1D_inputs.txt"),
                           shuffle_data=False)

input_filepath = os.path.join(data_path, "spring_mass_1D_inputs.txt")
output1_filepath = os.path.join(data_path, "spring_mass_1D_outputs_1.0.txt")
output2_filepath = os.path.join(data_path, "spring_mass_1D_outputs_0.1.txt")
output3_filepath = os.path.join(data_path, "spring_mass_1D_outputs_0.01.txt")

model1 = ModelFromData(input_filepath, output1_filepath, 1.)
model2 = ModelFromData(input_filepath, output2_filepath, 4.)
model3 = ModelFromData(input_filepath, output3_filepath, 16.)

models_from_data = [model1, model2, model3]

np.random.seed(1)
initial_sample_size = 200
epsilon = 1.

# Get output data for each layer.
level_0_data = np.zeros(initial_sample_size)
level_1_data = np.zeros(initial_sample_size)

input_samples = data_input.draw_samples(initial_sample_size)
Ejemplo n.º 20
0
from MLMCPy.input import InputFromData
from MLMCPy.mlmc import MLMCSimulator
from MLMCPy.model import ModelFromData


# Define I/O files
inputfile = "data/spring_mass_1D_inputs.txt"
outputfile_level1 = "data/spring_mass_1D_outputs_1.0.txt"
outputfile_level2 = "data/spring_mass_1D_outputs_0.1.txt"
outputfile_level3 = "data/spring_mass_1D_outputs_0.01.txt"

# Initialize random input & model objects
data_input = InputFromData(inputfile)

model_level1 = ModelFromData(inputfile, outputfile_level1, cost=1.0)
model_level2 = ModelFromData(inputfile, outputfile_level2, cost=10.0)
model_level3 = ModelFromData(inputfile, outputfile_level3, cost=100.0)

models = [model_level1, model_level2, model_level3]

mlmc_simulator = MLMCSimulator(data_input, models)
[estimates, sample_sizes, variances] = \
    mlmc_simulator.simulate(epsilon=1e-1,
                            initial_sample_sizes=100)

print 'Estimate: %s' % estimates
print 'Sample sizes used: %s' % sample_sizes
print 'Variance: %s' % variances
Ejemplo n.º 21
0
def test_init_fails_on_invalid_cost(input_data_file, output_data_file, cost):
    """
    Ensure that an invalid cost parameter triggers an exception in init.
    """
    with pytest.raises(Exception):
        ModelFromData(input_data_file, output_data_file, cost)
Ejemplo n.º 22
0
def test_init_fails_on_incompatible_data(input_data_file, output_data_file_2d):
    """
    Ensure an exception is raised if input and output data are incompatible.
    """
    with pytest.raises(ValueError):
        ModelFromData(input_data_file, output_data_file_2d, 1.)
def test_init_fails_on_incompatible_data(input_data_file, output_data_file_2d):

    with pytest.raises(ValueError):
        ModelFromData(input_data_file, output_data_file_2d, 1.)