Example #1
0
def test_parameter_not_modified(monkeypatch, space):
    """Tests the original dataframe is not modified"""
    original = copy.deepcopy(data)

    mock_train_regressor(monkeypatch)
    partial_dependency(data, space, n_samples=10)

    pd.testing.assert_frame_equal(data, original)
Example #2
0
def test_n_grid_points(monkeypatch, space):
    """Test that number of points on grid is correct, including adjustment for categorical dims"""
    mock_train_regressor(monkeypatch)

    n_grid_points = 5
    partial_dependencies = partial_dependency(data,
                                              space,
                                              params=["x", "z"],
                                              n_grid_points=n_grid_points,
                                              n_samples=10)

    assert set(partial_dependencies.keys()) == {"x", ("x", "z"), "z"}
    assert partial_dependencies["z"][1].shape == (3, )
    assert partial_dependencies["z"][2].shape == (3, )

    assert len(partial_dependencies["x"][0]["x"]) == n_grid_points
    assert len(partial_dependencies[("x", "z")][0]["x"]) == n_grid_points
    assert partial_dependencies[("x", "z")][0]["z"] == ["a", "b", "c"]
    assert partial_dependencies["z"][0]["z"] == ["a", "b", "c"]
    assert partial_dependencies["x"][1].shape == (n_grid_points, )
    assert partial_dependencies["x"][2].shape == (n_grid_points, )
    assert partial_dependencies["z"][1].shape == (3, )
    assert partial_dependencies["z"][2].shape == (3, )
    assert partial_dependencies[("x", "z")][1].shape == (3, n_grid_points)
    assert partial_dependencies[("x", "z")][2].shape == (3, n_grid_points)
Example #3
0
def test_multidim(monkeypatch, hspace):
    """Test computing for multiple params in multidim space"""
    mock_train_regressor(monkeypatch)

    partial_dependencies = partial_dependency(hdata,
                                              hspace,
                                              params=["x", "y[0]", "y[1]"],
                                              n_samples=10)
    assert set(partial_dependencies.keys()) == {
        "x",
        "y[0]",
        "y[1]",
        ("x", "y[0]"),
        ("x", "y[1]"),
        ("y[0]", "y[1]"),
    }
    assert len(partial_dependencies["x"][0]["x"]) == 10
    assert len(partial_dependencies["y[0]"][0]["y[0]"]) == 10
    assert len(partial_dependencies["y[1]"][0]["y[1]"]) == 10
    assert len(partial_dependencies[("x", "y[0]")][0]["x"]) == 10
    assert len(partial_dependencies[("x", "y[0]")][0]["y[0]"]) == 10
    assert partial_dependencies["x"][1].shape == (10, )
    assert partial_dependencies["x"][2].shape == (10, )
    assert partial_dependencies["y[0]"][1].shape == (10, )
    assert partial_dependencies["y[0]"][2].shape == (10, )
    assert partial_dependencies[("x", "y[0]")][1].shape == (10, 10)
    assert partial_dependencies[("x", "y[0]")][2].shape == (10, 10)
Example #4
0
def test_accept_empty(space):
    """Tests an empty dataframe is returned if you give an empty dataframe"""
    empty_frame = pd.DataFrame()
    results = partial_dependency(empty_frame,
                                 space,
                                 n_grid_points=3,
                                 n_samples=5)

    assert results == {}

    empty_frame = pd.DataFrame(columns=["x", "y", "z", "objective"])
    results = partial_dependency(empty_frame,
                                 space,
                                 n_grid_points=3,
                                 n_samples=5)

    assert results == {}
Example #5
0
def test_n_samples(monkeypatch, space):
    """Test n_samples is used properly when sampling random points"""
    mock_train_regressor(monkeypatch)

    PARAMS = ["x", "z"]
    N_SAMPLES = numpy.random.randint(20, 50)

    def mock_partial_dependency_grid(space, model, params, samples, n_points):
        assert samples.shape == (N_SAMPLES, len(space))
        return partial_dependency_grid(space, model, params, samples, n_points)

    monkeypatch.setattr(
        "orion.analysis.partial_dependency_utils.partial_dependency_grid",
        mock_partial_dependency_grid,
    )

    partial_dependency(data,
                       space,
                       params=PARAMS,
                       n_grid_points=5,
                       n_samples=N_SAMPLES)
Example #6
0
def test_single_param(monkeypatch, space):
    """Test computing for a single param"""
    mock_train_regressor(monkeypatch)

    partial_dependencies = partial_dependency(data,
                                              space,
                                              params=["x"],
                                              n_samples=10)
    assert list(partial_dependencies.keys()) == ["x"]
    assert len(partial_dependencies["x"][0]["x"]) == 10
    assert partial_dependencies["x"][1].shape == (10, )
    assert partial_dependencies["x"][2].shape == (10, )
Example #7
0
def test_categorical(monkeypatch, space):
    """Test computing for categorical"""
    mock_train_regressor(monkeypatch)

    partial_dependencies = partial_dependency(data,
                                              space,
                                              params=["z"],
                                              n_samples=10)

    assert set(partial_dependencies.keys()) == {"z"}
    assert partial_dependencies["z"][0]["z"] == ["a", "b", "c"]
    assert partial_dependencies["z"][1].shape == (3, )
    assert partial_dependencies["z"][2].shape == (3, )