Skip to content

migrate unittest to pytest #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions boruta/test/test_boruta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import numpy as np
import pandas as pd
import pytest
from sklearn.ensemble import RandomForestClassifier

from boruta import BorutaPy


@pytest.mark.parametrize("tree_n,expected", [(10, 44), (100, 141)])
def test_get_tree_num(tree_n, expected):
rfc = RandomForestClassifier(max_depth=10)
bt = BorutaPy(rfc)
assert bt._get_tree_num(tree_n) == expected


@pytest.fixture(scope="module")
def Xy():
np.random.seed(42)
y = np.random.binomial(1, 0.5, 1000)
X = np.zeros((1000, 10))

z = (y - np.random.binomial(1, 0.1, 1000) +
np.random.binomial(1, 0.1, 1000))
z[z == -1] = 0
z[z == 2] = 1

# 5 relevant features
X[:, 0] = z
X[:, 1] = (y * np.abs(np.random.normal(0, 1, 1000))
+ np.random.normal(0, 0.1, 1000))
X[:, 2] = y + np.random.normal(0, 1, 1000)
X[:, 3] = y**2 + np.random.normal(0, 1, 1000)
X[:, 4] = np.sqrt(y) + np.random.binomial(2, 0.1, 1000)

# 5 irrelevant features
X[:, 5] = np.random.normal(0, 1, 1000)
X[:, 6] = np.random.poisson(1, 1000)
X[:, 7] = np.random.binomial(1, 0.3, 1000)
X[:, 8] = np.random.normal(0, 1, 1000)
X[:, 9] = np.random.poisson(1, 1000)

return X, y


def test_if_boruta_extracts_relevant_features(Xy):
X, y = Xy
rfc = RandomForestClassifier()
bt = BorutaPy(rfc)
bt.fit(X, y)
assert list(range(5)) == list(np.where(bt.support_)[0])


def test_if_it_works_with_dataframe_input(Xy):
X, y = Xy
X_df, y_df = pd.DataFrame(X), pd.Series(y)
bt = BorutaPy(RandomForestClassifier())
bt.fit(X_df, y_df)
assert list(range(5)) == list(np.where(bt.support_)[0])


def test_dataframe_is_returned(Xy):
X, y = Xy
X_df, y_df = pd.DataFrame(X), pd.Series(y)
rfc = RandomForestClassifier()
bt = BorutaPy(rfc)
bt.fit(X_df, y_df)
assert isinstance(bt.transform(X_df, return_df=True), pd.DataFrame)
57 changes: 0 additions & 57 deletions boruta/test/unit_tests.py

This file was deleted.

7 changes: 7 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-r requirements.txt
pytest>=5.4.1

# repo maintenance tooling
black>=21.5b1
flake8>=3.9.2
isort>=5.8.0