Exemple #1
0
def test_scoring_kwargs():
    """Assert that the scoring functions uses the kwargs."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run("LR")
    score_1 = atom.lr.scoring("accuracy")
    score_2 = atom.lr.scoring("accuracy", normalize=False)
    assert score_1 != score_2
Exemple #2
0
def test_X_train_setter():
    """Assert that the X_train setter changes the training feature set."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    new_X_train = atom.X_train
    new_X_train.iloc[0, 0] = 999
    atom.X_train = new_X_train
    assert atom.X_train.iloc[0, 0] == 999
Exemple #3
0
def test_data():
    """Assert that data can be loaded."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.save(FILE_DIR + "atom", save_data=False)

    atom2 = ATOMLoader(FILE_DIR + "atom", data=(X_bin, y_bin))
    assert atom2.dataset.equals(atom.dataset)
Exemple #4
0
def test_attrs_are_passed():
    """Assert that the attributes from the parent are passed."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.balance()
    atom.branch = "branch_2"
    assert atom.branch_2.idx is not atom.master.idx
    assert atom.branch_2.adasyn is atom.master.adasyn
Exemple #5
0
def test_repr():
    """Assert that the __repr__ method visualizes the pipeline(s)."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.scale()
    assert "Branches: master" in str(atom)
    atom.branch = "branch_2"
    assert "Branches:\n   >>> master\n   >>> branch_2 !" in str(atom)
Exemple #6
0
def test_setter_error_unequal_columns():
    """Assert that an error is raised when the setter has unequal columns."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    with pytest.raises(ValueError, match="number of columns"):
        new_X = atom.train
        new_X.insert(0, "new_column", 1)
        atom.train = new_X
Exemple #7
0
def test_X_test_setter():
    """Assert that the X_test setter changes the test feature set."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    new_X_test = atom.X_test
    new_X_test.iloc[0, 0] = 999
    atom.X_test = new_X_test
    assert atom.X_test.iloc[0, 0] == 999
Exemple #8
0
def test_setter_error_unequal_column_names():
    """Assert that an error is raised with different column names."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    with pytest.raises(ValueError, match="the same columns"):
        new_X = atom.train.drop(atom.train.columns[0], axis=1)
        new_X.insert(0, "new_column", 1)
        atom.train = new_X
Exemple #9
0
def test_reset_predictions():
    """Assert that reset_predictions removes the made predictions."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run("MNB")
    print(atom.mnb.score_test)
    atom.mnb.reset_predictions()
    assert atom.mnb._pred_attrs[9] is None
Exemple #10
0
def test_shape_property():
    """Assert that the shape property returns the shape of the dataset."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    assert atom.branch.shape == (len(X_bin), X_bin.shape[1] + 1)

    atom = ATOMClassifier(*mnist, random_state=1)
    assert atom.branch.shape == (70000, (28, 28, 1), 2)
Exemple #11
0
def test_setter_error_unequal_target_names():
    """Assert that an error is raised with different target names."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    with pytest.raises(ValueError, match="the same name"):
        new_y_train = atom.y_train
        new_y_train.name = "different_name"
        atom.y_train = new_y_train
Exemple #12
0
def test_sample_weights_fit():
    """Assert that sample weights can be used with the BO."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run(
        models="LGB",
        n_calls=5,
        est_params={"sample_weight_fit": list(range(len(atom.y_train)))},
    )
Exemple #13
0
def test_rename():
    """Assert that the model's tag can be changed."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run("MNB")
    atom.mnb.rename("_2")
    assert atom.models == "MNB_2"
    atom.mnb_2.rename("mnb_3")
    assert atom.models == "MNB_3"
Exemple #14
0
def test_score_with_sample_weights():
    """Assert that the score method works when sample weights are provided."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run("Tree")
    score = atom.tree.score(X_bin,
                            y_bin,
                            sample_weight=list(range(len(y_bin))))
    assert isinstance(score, np.float64)
Exemple #15
0
def test_CatNB():
    """Assert that the CatNB model works. Separated because of special dataset."""
    X = np.random.randint(5, size=(100, 100))
    y = np.random.randint(2, size=100)

    atom = ATOMClassifier(X, y, random_state=1)
    atom.run(models="CatNB", n_calls=2, n_initial_points=1)
    assert not atom.errors
    assert hasattr(atom, "CatNB") and hasattr(atom, "catnb")
Exemple #16
0
def test_load_ignores_n_rows_parameter():
    """Assert that n_rows is not used when transform_data=False."""
    atom = ATOMClassifier(X_bin, y_bin, n_rows=0.6, random_state=1)
    atom.save(FILE_DIR + "atom", save_data=False)

    atom2 = ATOMLoader(FILE_DIR + "atom",
                       data=(X_bin, y_bin),
                       transform_data=False)
    assert len(atom2.dataset) == len(X_bin)
Exemple #17
0
def test_scoring_custom_metrics(on_set):
    """Assert that the scoring methods works for custom metrics."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run("MNB")
    assert isinstance(atom.mnb.scoring("cm", on_set), np.ndarray)
    for metric in ["tn", "fp", "fn", "tp"]:
        assert isinstance(atom.mnb.scoring(metric, on_set), int)
    for metric in ["lift", "fpr", "tpr", "sup"]:
        assert isinstance(atom.mnb.scoring(metric, on_set), float)
Exemple #18
0
def test_add_pipeline():
    """Assert that adding a pipeline adds every individual step."""
    pipeline = Pipeline(steps=[
        ("scaler", StandardScaler()),
        ("sfm", SelectFromModel(RandomForestClassifier())),
    ])
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.add(pipeline)
    assert isinstance(atom.pipeline[0], StandardScaler)
    assert isinstance(atom.pipeline[1], SelectFromModel)
Exemple #19
0
def test_task_assignment():
    """Assert that the correct task is assigned."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    assert atom.task == "binary classification"

    atom = ATOMClassifier(X_class, y_class, random_state=1)
    assert atom.task == "multiclass classification"

    atom = ATOMRegressor(X_reg, y_reg, random_state=1)
    assert atom.task == "regression"
Exemple #20
0
def test_plot_distribution(columns):
    """Assert that the plot_distribution method work as intended."""
    atom = ATOMClassifier(X10_str, y10, random_state=1)
    pytest.raises(ValueError,
                  atom.plot_distribution,
                  columns=2,
                  show=-1,
                  display=False)
    atom.plot_distribution(columns=columns,
                           distribution="pearson3",
                           display=False)
Exemple #21
0
def test_canvas_too_many_plots():
    """Assert that the canvas works."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run("Tree")
    with atom.canvas(1, 2, display=False):
        atom.plot_prc()
        atom.plot_roc()
        pytest.raises(RuntimeError, atom.plot_prc)
Exemple #22
0
def test_models_binary(model):
    """Assert that all models work with binary classification."""
    atom = ATOMClassifier(X_bin, y_bin, test_size=0.24, random_state=1)
    atom.run(
        models=model,
        metric="auc",
        n_calls=2,
        n_initial_points=1,
        bo_params={"base_estimator": "rf", "cv": 1},
    )
    assert not atom.errors  # Assert that the model ran without errors
    assert hasattr(atom, model) and hasattr(atom, model.lower())
Exemple #23
0
def test_models_multiclass(model):
    """Assert that all models work with multiclass classification."""
    atom = ATOMClassifier(X_class2, y_class2, test_size=0.24, random_state=1)
    atom.run(
        models=model,
        metric="f1_micro",
        n_calls=2,
        n_initial_points=1,
        bo_params={"base_estimator": "rf", "cv": 1},
    )
    assert not atom.errors
    assert hasattr(atom, model) and hasattr(atom, model.lower())
Exemple #24
0
def test_figure_is_saved_canvas(func):
    """Assert that the figure is only saved after finishing the canvas."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run("Tree")
    with atom.canvas(1, 2, filename="canvas", display=False):
        atom.plot_prc()
        func.assert_not_called()
        atom.plot_roc()
        func.assert_not_called()
    func.assert_called_with("canvas")  # Only at the end it is saved
Exemple #25
0
def test_all_prediction_properties():
    """Assert that all prediction properties are saved as attributes when called."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run(["LR", "SGD"])
    assert isinstance(atom.lr.predict_train, np.ndarray)
    assert isinstance(atom.lr.predict_test, np.ndarray)
    assert isinstance(atom.lr.predict_proba_train, np.ndarray)
    assert isinstance(atom.lr.predict_proba_test, np.ndarray)
    assert isinstance(atom.lr.predict_log_proba_train, np.ndarray)
    assert isinstance(atom.lr.predict_log_proba_test, np.ndarray)
    assert isinstance(atom.lr.decision_function_train, np.ndarray)
    assert isinstance(atom.lr.decision_function_test, np.ndarray)
    assert isinstance(atom.lr.score_train, np.float64)
    assert isinstance(atom.lr.score_test, np.float64)
Exemple #26
0
def test_scoring_metric_is_given():
    """Assert that the scoring method works for a specified metric_."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run(["GNB", "PA"])
    atom.scoring("logloss")  # For _ProbaScorer
    atom.scoring("ap")  # For _ThresholdScorer
    atom.scoring("cm")  # For special case
Exemple #27
0
def test_plot_rfecv(scoring):
    """Assert that the plot_rfecv method work as intended """
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    pytest.raises(PermissionError, atom.plot_rfecv)  # No RFECV in pipeline
    atom.run("lr", metric="precision")
    atom.feature_selection(strategy="RFECV", n_features=10, scoring=scoring)
    atom.plot_rfecv(display=False)
Exemple #28
0
def test_plot_errors():
    """Assert that the plot_errors method work as intended."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    atom.run("LR")
    pytest.raises(PermissionError, atom.plot_errors)  # Task is not regression

    atom = ATOMRegressor(X_reg, y_reg, random_state=1)
    pytest.raises(NotFittedError, atom.plot_errors)
    atom.run(["Tree", "Bag"], metric="MSE")
    atom.plot_errors(display=False)
    atom.tree.plot_errors(display=False)
Exemple #29
0
def test_export_pipeline():
    """Assert that we can export the pipeline."""
    atom = ATOMClassifier(X_bin, y_bin, random_state=1)
    pytest.raises(RuntimeError, atom.export_pipeline)
    atom.clean()
    atom.run(["GNB", "LGB"])
    assert len(atom.export_pipeline("GNB")) == 2  # Without scaler
    assert isinstance(atom.export_pipeline("LGB")[1], Scaler)  # With scaler
Exemple #30
0
def test_force_plot():
    """Assert that the force_plot method work as intended."""
    atom = ATOMClassifier(X_class, y_class, random_state=1)
    pytest.raises(NotFittedError, atom.force_plot)
    atom.run("Tree", metric="MSE")
    with atom.canvas(display=False):
        pytest.raises(PermissionError, atom.force_plot, matplotlib=True)
    atom.force_plot(index=100, matplotlib=True, display=False)
    atom.force_plot(matplotlib=False,
                    filename=FILE_DIR + "force",
                    display=True)
    assert glob.glob(FILE_DIR + "force.html")