Example #1
0
def test_add_function():
    feeder = func.FunctionFeeder()
    # Let error be raised if non-TestFunction is added
    sphere = func.Sphere()
    feeder.add_function(sphere)
    feeder_2 = func.FunctionFeeder()
    with pytest.raises(Exception):
        feeder.add_function(feeder_2)
def test_testfunction_sphere():
    function = func.Sphere()
    minima = [[0, 0, 0]]
    value_minima = 0
    # Validate the default configuration
    assert function.get_dimensionality() == 3
    # Validate function properties
    assert function.is_bounded() is False
    assert function.is_differentiable() is True
    # Validate minima
    y = function(np.array(minima))
    assert np.sum(1.0 * (np.around(y, 8) == value_minima)) == len(y)
    # Validate that these minima are indeed minima
    x = np.random.rand(10000, function.get_dimensionality())
    x = x * 20 - 10
    y = function(x)
    assert np.sum(1.0 * (np.around(y, 8) == value_minima)) <= len(y)
    # Validate output shape
    assert y.shape == (10000, 1)
    assert function(x, True).shape == (10000, function.get_dimensionality())
Example #3
0
def test_results_df():
    # Create experiment
    procedure = TmpProcedure()
    path = "./tmpresultsexp3"
    experiment = TmpExperimentCorrect2(procedure, path)
    experiment.run(func.GaussianShells(), finish_line=1000)
    experiment.run(func.Sphere(), finish_line=1000)
    r = results.Result(path)
    # Get df
    df = r.get_results()
    assert isinstance(df, pd.DataFrame)
    assert len(df) == 2
    # Create df through function
    df2 = results.make_dataframe({'standard': path})
    print(df2)
    del (df2['experiment'])
    assert df.equals(df2)
    # Check if indicating multiple folders works
    _ = results.make_dataframe({'standard': path, 'other': path})
    # Remove folder
    shutil.rmtree(path)
Example #4
0
def test_results_plots():
    # Create experiment
    procedure = TmpProcedure()
    path = "./tmpresultsexp5"
    experiment = TmpExperimentCorrect(procedure, path)
    experiment.run(func.GaussianShells(), finish_line=1000)
    experiment.run(func.Sphere(), finish_line=1000)
    # Get results df
    df = results.make_dataframe({'standard': path, 'other': path})

    # Boxplot experiment
    with pytest.raises(Exception):
        results.boxplot_experiment(df, 'time', 'not-an-experiment')
    with pytest.raises(Exception):
        results.boxplot_experiment(df, 'not-a-metric', 'standard')
    results.boxplot_experiment(df,
                               'time',
                               'standard',
                               logarithmic=True,
                               path='./plot.png')
    assert os.path.exists('./plot.png') is True
    results.boxplot_experiment(df,
                               'time',
                               'standard',
                               logarithmic=False,
                               path='./plot.png')
    os.remove('./plot.png')

    # Boxplot function
    with pytest.raises(Exception):
        results.boxplot_function(df, 'time', 'not-a-function')
    with pytest.raises(Exception):
        results.boxplot_function(df, 'not-a-metric', 'sphere')
    results.boxplot_function(df,
                             'time',
                             'sphere',
                             logarithmic=True,
                             path='./plot.png')
    assert os.path.exists('./plot.png') is True
    results.boxplot_function(df,
                             'time',
                             'sphere',
                             logarithmic=False,
                             path='./plot.png')
    os.remove('./plot.png')

    # Histogram experiment
    with pytest.raises(Exception):
        results.histogram_experiment(df, 'time', 'not-an-experiment')
    with pytest.raises(Exception):
        results.histogram_experiment(df, 'not-a-metric', 'standard')
    results.histogram_experiment(df,
                                 'time',
                                 'standard',
                                 logarithmic=True,
                                 path='./plot.png')
    assert os.path.exists('./plot.png') is True
    results.histogram_experiment(df,
                                 'time',
                                 'standard',
                                 logarithmic=False,
                                 path='./plot.png')
    os.remove('./plot.png')

    # Histogram function
    with pytest.raises(Exception):
        results.histogram_function(df, 'time', 'not-a-function')
    with pytest.raises(Exception):
        results.histogram_function(df, 'not-a-metric', 'sphere')
    results.histogram_function(df,
                               'time',
                               'sphere',
                               logarithmic=True,
                               path='./plot.png')
    assert os.path.exists('./plot.png') is True
    results.histogram_function(df,
                               'time',
                               'sphere',
                               logarithmic=False,
                               path='./plot.png')
    os.remove('./plot.png')

    shutil.rmtree(path)
Example #5
0
def test_results_tables():
    # Create experiment
    procedure = TmpProcedure()
    path = "./tmpresultsexp4"
    experiment = TmpExperimentCorrect(procedure, path)
    experiment.run(func.GaussianShells(), finish_line=1000)
    experiment.run(func.Sphere(), finish_line=1000)
    experiment.run(func.GaussianShells(), finish_line=1000)
    experiment.run(func.Sphere(), finish_line=1000)
    # Get results df
    df = results.make_dataframe({'standard': path, 'other': path})

    # Check that exceptions are raised correctly
    with pytest.raises(Exception):
        _, _ = results.tabulate_result(df, 'x', 'not-an-experiment')
    with pytest.raises(Exception):
        _, _ = results.tabulate_result(df, 'y', 'standard')
    # Check that the method works in general
    content, rows = results.tabulate_result(df, 'x', 'other')
    # Check content and rows format
    assert rows.tolist() == ['gaussianshells', 'sphere']
    assert isinstance(content, list)
    # Check that the method works with functions specified
    _, _ = results.tabulate_result(df, 'x', 'other', ['Sphere'])
    # Check that results can be stored to csv and tex (validate csv)
    _, _ = results.tabulate_result(df, 'x', 'other', path='./tmp_out.csv')
    _, _ = results.tabulate_result(df, 'x', 'other', path='./tmp_out.tex')
    _ = pd.read_csv('./tmp_out.csv')
    # Validate that allowed table output formats are only csv and tex
    _ = results.create_table_string(content, rows, ['x'], 'csv', '')
    _ = results.create_table_string(content, rows, ['x'], 'tex', '')
    with pytest.raises(Exception):
        _ = results.create_table_string(content, rows, ['None'], 'txt', '')
    # Remove folder and output files
    os.remove('./tmp_out.csv')
    os.remove('./tmp_out.tex')

    # Check that exceptions are raised correctly
    print(df)
    with pytest.raises(Exception):
        _, _, _ = results.tabulate_all_aggregated(
            df, 'time', 'mean', experiment_names=['not-an-experiment'])
    with pytest.raises(Exception):
        _, _, _ = results.tabulate_all_aggregated(df, 'y', 'mean')
    _, _, _ = results.tabulate_all_aggregated(df,
                                              'time',
                                              'mean',
                                              functions=['sphere'])
    _, _, _ = results.tabulate_all_aggregated(df,
                                              'time',
                                              'mean',
                                              experiment_names=['standard'])
    _, _, _ = results.tabulate_all_aggregated(df,
                                              'time',
                                              'mean',
                                              path='./tmp_out.csv')
    _ = pd.read_csv('./tmp_out.csv')
    content, rows, cols = results.tabulate_all_aggregated(df,
                                                          'time',
                                                          'mean',
                                                          path='./tmp_out.tex')
    assert isinstance(rows, list) is True
    assert isinstance(cols, list) is True
    assert isinstance(content, list)

    # Remove folder and output files
    os.remove('./tmp_out.csv')
    os.remove('./tmp_out.tex')
    shutil.rmtree(path)