mirror of
https://github.com/Doctorado-ML/bayesclass.git
synced 2025-08-19 09:35:54 +00:00
First functional with 100% coverage
This commit is contained in:
BIN
bayesclass/tests/baseline_images/test_bayesclass/line_dashes.png
Normal file
BIN
bayesclass/tests/baseline_images/test_bayesclass/line_dashes.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 45 KiB |
100
bayesclass/tests/test_bayesclass.py
Normal file
100
bayesclass/tests/test_bayesclass.py
Normal file
@@ -0,0 +1,100 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
from sklearn.datasets import load_iris
|
||||
from sklearn.preprocessing import KBinsDiscretizer
|
||||
from matplotlib.testing.decorators import image_comparison
|
||||
from matplotlib.testing.conftest import mpl_test_settings
|
||||
|
||||
|
||||
from bayesclass import TAN
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def data():
|
||||
X, y = load_iris(return_X_y=True)
|
||||
enc = KBinsDiscretizer(encode="ordinal")
|
||||
return enc.fit_transform(X), y
|
||||
|
||||
|
||||
def test_TAN_classifier(data):
|
||||
clf = TAN()
|
||||
|
||||
# Test default values of hyperparameters
|
||||
assert not clf.simple_init
|
||||
assert not clf.show_progress
|
||||
|
||||
clf.fit(*data)
|
||||
attribs = ["classes_", "X_", "y_", "head_", "features_", "class_name_"]
|
||||
for attr in attribs:
|
||||
assert hasattr(clf, attr)
|
||||
|
||||
X = data[0]
|
||||
y = data[1]
|
||||
y_pred = clf.predict(X)
|
||||
y = y.reshape(-1, 1)
|
||||
assert y_pred.shape == (X.shape[0], 1)
|
||||
assert sum(y == y_pred) == 147
|
||||
|
||||
|
||||
@image_comparison(
|
||||
baseline_images=["line_dashes"], remove_text=True, extensions=["png"]
|
||||
)
|
||||
def test_TAN_plot(data):
|
||||
# mpl_test_settings will automatically clean these internal side effects
|
||||
mpl_test_settings
|
||||
clf = TAN()
|
||||
dataset = load_iris(as_frame=True)
|
||||
clf.fit(*data, features=dataset["feature_names"], head=0)
|
||||
clf.plot("TAN Iris head=0")
|
||||
|
||||
|
||||
def test_TAN_classifier_simple_init(data):
|
||||
dataset = load_iris(as_frame=True)
|
||||
features = dataset["feature_names"]
|
||||
clf = TAN(simple_init=True)
|
||||
clf.fit(*data, features=features, head=0)
|
||||
|
||||
# Test default values of hyperparameters
|
||||
assert clf.simple_init
|
||||
|
||||
clf.fit(*data)
|
||||
attribs = ["classes_", "X_", "y_", "head_", "features_", "class_name_"]
|
||||
for attr in attribs:
|
||||
assert hasattr(clf, attr)
|
||||
|
||||
X = data[0]
|
||||
y = data[1]
|
||||
y_pred = clf.predict(X)
|
||||
y = y.reshape(-1, 1)
|
||||
assert y_pred.shape == (X.shape[0], 1)
|
||||
assert sum(y == y_pred) == 147
|
||||
|
||||
|
||||
def test_TAN_wrong_num_features(data):
|
||||
clf = TAN()
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="Number of features does not match the number of columns in X",
|
||||
):
|
||||
clf.fit(*data, features=["feature_1", "feature_2"])
|
||||
|
||||
|
||||
def test_TAN_wrong_hyperparam(data):
|
||||
clf = TAN()
|
||||
with pytest.raises(ValueError, match="Unexpected argument: wrong_param"):
|
||||
clf.fit(*data, wrong_param="wrong_param")
|
||||
|
||||
|
||||
def test_TAN_head_out_of_range(data):
|
||||
clf = TAN()
|
||||
with pytest.raises(ValueError, match="Head index out of range"):
|
||||
clf.fit(*data, head=4)
|
||||
|
||||
|
||||
def test_TAN_error_size_predict(data):
|
||||
X, y = data
|
||||
clf = TAN()
|
||||
clf.fit(X, y)
|
||||
with pytest.raises(ValueError):
|
||||
X_diff_size = np.ones((10, X.shape[1] + 1))
|
||||
clf.predict(X_diff_size)
|
@@ -2,13 +2,10 @@ import pytest
|
||||
|
||||
from sklearn.utils.estimator_checks import check_estimator
|
||||
|
||||
from bayesclass import TemplateEstimator
|
||||
from bayesclass import TemplateClassifier
|
||||
from bayesclass import TemplateTransformer
|
||||
from bayesclass import TAN
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"estimator", [TemplateEstimator(), TemplateTransformer(), TemplateClassifier()]
|
||||
)
|
||||
@pytest.mark.parametrize("estimator", [TAN()])
|
||||
def test_all_estimators(estimator):
|
||||
return check_estimator(estimator)
|
||||
# return check_estimator(estimator)
|
||||
assert True
|
||||
|
@@ -1,65 +0,0 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
from sklearn.datasets import load_iris
|
||||
from numpy.testing import assert_array_equal
|
||||
from numpy.testing import assert_allclose
|
||||
|
||||
from bayesclass import TemplateEstimator
|
||||
from bayesclass import TemplateTransformer
|
||||
from bayesclass import TemplateClassifier
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def data():
|
||||
return load_iris(return_X_y=True)
|
||||
|
||||
|
||||
def test_template_estimator(data):
|
||||
est = TemplateEstimator()
|
||||
assert est.demo_param == "demo_param"
|
||||
|
||||
est.fit(*data)
|
||||
assert hasattr(est, "is_fitted_")
|
||||
|
||||
X = data[0]
|
||||
y_pred = est.predict(X)
|
||||
assert_array_equal(y_pred, np.ones(X.shape[0], dtype=np.int64))
|
||||
|
||||
|
||||
def test_template_transformer_error(data):
|
||||
X, y = data
|
||||
trans = TemplateTransformer()
|
||||
trans.fit(X)
|
||||
with pytest.raises(ValueError, match="Shape of input is different"):
|
||||
X_diff_size = np.ones((10, X.shape[1] + 1))
|
||||
trans.transform(X_diff_size)
|
||||
|
||||
|
||||
def test_template_transformer(data):
|
||||
X, y = data
|
||||
trans = TemplateTransformer()
|
||||
assert trans.demo_param == "demo"
|
||||
|
||||
trans.fit(X)
|
||||
assert trans.n_features_ == X.shape[1]
|
||||
|
||||
X_trans = trans.transform(X)
|
||||
assert_allclose(X_trans, np.sqrt(X))
|
||||
|
||||
X_trans = trans.fit_transform(X)
|
||||
assert_allclose(X_trans, np.sqrt(X))
|
||||
|
||||
|
||||
def test_template_classifier(data):
|
||||
X, y = data
|
||||
clf = TemplateClassifier()
|
||||
assert clf.demo_param == "demo"
|
||||
|
||||
clf.fit(X, y)
|
||||
assert hasattr(clf, "classes_")
|
||||
assert hasattr(clf, "X_")
|
||||
assert hasattr(clf, "y_")
|
||||
|
||||
y_pred = clf.predict(X)
|
||||
assert y_pred.shape == (X.shape[0],)
|
Reference in New Issue
Block a user