-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f48df4
commit dc2031a
Showing
10 changed files
with
171 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Type | ||
|
||
import pytest | ||
from helpers import get_data | ||
|
||
from random_forestry import RandomForest | ||
|
||
|
||
@pytest.fixture | ||
def forest(request: Type[pytest.FixtureRequest]): | ||
X, y = get_data() | ||
|
||
forest_parameters = request.node.get_closest_marker("forest_parameters") | ||
if hasattr(forest_parameters, "kwargs"): | ||
return RandomForest(**forest_parameters.kwargs).fit(X, y) | ||
else: | ||
return RandomForest().fit(X, y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,27 @@ | ||
import pytest | ||
from helpers import get_data | ||
|
||
from random_forestry import RandomForest | ||
|
||
|
||
def test_predict_error(): | ||
X, y = get_data() | ||
|
||
forest = RandomForest() | ||
forest.fit(X, y) | ||
|
||
def test_predict_error(forest): | ||
with pytest.raises(ValueError): | ||
forest.predict(aggregation="average") | ||
|
||
|
||
def test_predict_average(): | ||
X, y = get_data() | ||
|
||
forest = RandomForest() | ||
forest.fit(X, y) | ||
def test_predict_average(forest): | ||
X, _ = get_data() | ||
prediction = forest.predict(X, aggregation="average") | ||
assert len(prediction) == len(X) | ||
|
||
|
||
def test_predict_oob(): | ||
X, y = get_data() | ||
|
||
forest = RandomForest(oob_honest=True) | ||
forest.fit(X, y) | ||
|
||
@pytest.mark.forest_parameters(oob_honest=True) | ||
def test_predict_oob(forest): | ||
X, _ = get_data() | ||
prediction = forest.predict(X, aggregation="oob") | ||
assert len(prediction) == len(X) | ||
|
||
|
||
def test_predict_double_oob(): | ||
X, y = get_data() | ||
|
||
forest = RandomForest(oob_honest=True) | ||
forest.fit(X, y) | ||
|
||
@pytest.mark.forest_parameters(oob_honest=True) | ||
def test_predict_double_oob(forest): | ||
X, _ = get_data() | ||
prediction = forest.predict(X, aggregation="doubleOOB") | ||
assert len(prediction) == len(X) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# import platform | ||
|
||
import time | ||
|
||
import pytest | ||
from helpers import get_data | ||
|
||
from random_forestry import RandomForest | ||
|
||
X, y = get_data() | ||
|
||
|
||
class TestAfterInit: | ||
@pytest.mark.skip | ||
def test_when_default_seed(self): | ||
forest_1 = RandomForest() | ||
time.sleep(1) | ||
forest_2 = RandomForest() | ||
assert forest_1 != forest_2 | ||
|
||
def test_when_equal_seed(self): | ||
forest_1 = RandomForest(seed=123) | ||
forest_2 = RandomForest(seed=123) | ||
assert forest_1 == forest_2 | ||
|
||
def test_when_different_params(self): | ||
forest_1 = RandomForest(seed=56, ntree=34) | ||
forest_2 = RandomForest(seed=56, nthread=6) | ||
assert forest_1 != forest_2 | ||
|
||
|
||
class TestAfterFit: | ||
def test_it_is_different(self): | ||
forest_1 = RandomForest(seed=123) | ||
forest_2 = RandomForest(seed=123).fit(X, y) | ||
assert forest_1 != forest_2 | ||
|
||
def test_no_randomness_added(self): | ||
forest_1 = RandomForest(seed=123).fit(X, y) | ||
forest_2 = RandomForest(seed=123).fit(X, y) | ||
assert forest_1 == forest_2 | ||
|
||
def test_idempotency(self): | ||
forest_1 = RandomForest(seed=123).fit(X, y) | ||
forest_2 = RandomForest(seed=123).fit(X, y).fit(X, y) | ||
assert forest_1 == forest_2 | ||
|
||
@pytest.mark.skip | ||
def test_different_params(self): | ||
forest_1 = RandomForest(seed=123).fit(X, y, double_bootstrap=True) | ||
forest_2 = RandomForest(seed=123).fit(X, y, double_bootstrap=False) | ||
assert forest_1 != forest_2 | ||
|
||
forest_1 = RandomForest(seed=123).fit(X, y, max_obs=4) | ||
forest_2 = RandomForest(seed=123).fit(X, y, max_obs=5) | ||
assert forest_1 != forest_2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Any | ||
|
||
from pytest import mark | ||
|
||
from random_forestry.validators import ( | ||
negative_float, | ||
negative_integer, | ||
positive_float, | ||
positive_integer, | ||
) | ||
|
||
|
||
@mark.parametrize("test_input,expected", [(-1, False), (3, True), (1.4, False), (0, False)]) | ||
def test_positive_integer(test_input: Any, expected: bool): | ||
assert positive_integer(test_input) == expected | ||
|
||
|
||
@mark.parametrize("test_input,expected", [(-1, False), (3, False), (1.4, True), (-2.7, False), (0, False)]) | ||
def test_positive_float(test_input: Any, expected: bool): | ||
assert positive_float(test_input) == expected | ||
|
||
|
||
@mark.parametrize("test_input,expected", [(-1, True), (3, False), (-1.4, False), (0, False)]) | ||
def test_negative_integer(test_input: Any, expected: bool): | ||
assert negative_integer(test_input) == expected | ||
|
||
|
||
@mark.parametrize("test_input,expected", [(-1, False), (3, False), (1.4, False), (0, False), (-3.4, True)]) | ||
def test_negative_float(test_input: Any, expected: bool): | ||
assert negative_float(test_input) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,24 @@ | ||
import pytest | ||
from helpers import get_data | ||
from numpy.testing import assert_array_equal | ||
|
||
from random_forestry import RandomForest | ||
|
||
def _test_predictions(forest, aggregation): | ||
X, _ = get_data() | ||
|
||
def test_average(): | ||
X, y = get_data() | ||
|
||
forest = RandomForest() | ||
forest.fit(X, y) | ||
|
||
pred = forest.predict(X, aggregation="average") | ||
pred_weight_matrix = forest.predict(X, aggregation="average", return_weight_matrix=True) | ||
|
||
assert_array_equal(pred, pred_weight_matrix["predictions"]) | ||
assert pred_weight_matrix["weightMatrix"].shape == (len(X.index), len(X.index)) | ||
|
||
|
||
def test_oob(): | ||
X, y = get_data() | ||
|
||
forest = RandomForest(oob_honest=True) | ||
forest.fit(X, y) | ||
|
||
pred = forest.predict(X, aggregation="oob") | ||
pred_weight_matrix = forest.predict(X, aggregation="oob", return_weight_matrix=True) | ||
pred = forest.predict(X, aggregation=aggregation) | ||
pred_weight_matrix = forest.predict(X, aggregation=aggregation, return_weight_matrix=True) | ||
|
||
assert_array_equal(pred, pred_weight_matrix["predictions"]) | ||
assert pred_weight_matrix["weightMatrix"].shape == (len(X.index), len(X.index)) | ||
|
||
|
||
def test_double_oob(): | ||
X, y = get_data() | ||
@pytest.mark.forest_parameters(oob_honest=True) | ||
@pytest.mark.parametrize("aggregation", ["average", "oob", "doubleOOB"]) | ||
def test_predictions_oob_honest_true(forest, aggregation): | ||
_test_predictions(forest, aggregation) | ||
|
||
forest = RandomForest(oob_honest=True) | ||
forest.fit(X, y) | ||
|
||
pred = forest.predict(X, aggregation="doubleOOB") | ||
pred_weight_matrix = forest.predict(X, aggregation="doubleOOB", return_weight_matrix=True) | ||
|
||
assert_array_equal(pred, pred_weight_matrix["predictions"]) | ||
assert pred_weight_matrix["weightMatrix"].shape == (len(X.index), len(X.index)) | ||
@pytest.mark.parametrize("aggregation", ["average", "oob", pytest.param("doubleOOB", marks=pytest.mark.xfail)]) | ||
def test_predictions_oob_honest_default(forest, aggregation): | ||
_test_predictions(forest, aggregation) |