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
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
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)
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
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)
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
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
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
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
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)
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
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)))}, )
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"
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)
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")
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)
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)
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)
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"
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)
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)
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())
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())
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
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)
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
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)
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)
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
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")