diff --git a/imblearn/base.py b/imblearn/base.py index 610896113..af0c4bffd 100644 --- a/imblearn/base.py +++ b/imblearn/base.py @@ -76,16 +76,17 @@ def fit(self, X, y): self.logger.info('Compute classes statistics ...') - # Get all the unique elements in the target array - uniques = np.unique(y) - # # Raise an error if there is only one class # if uniques.size == 1: # raise RuntimeError("Only one class detected, aborting...") # Raise a warning for the moment to be compatible with BaseEstimator - if uniques.size == 1: - warnings.warn('Only one class detected, something will get wrong', - RuntimeWarning) + self.logger.debug('The number of classes is %s', np.unique(y).size) + self.logger.debug('Shall we raise a warning: %s', + np.unique(y).size == 1) + if np.unique(y).size == 1: + warnings.simplefilter('always', UserWarning) + warnings.warn('Only one class detected, something will get wrong') + self.logger.debug('The warning should has been raised.') # Store the size of X to check at sampling time if we have the # same data @@ -98,7 +99,7 @@ def fit(self, X, y): self.min_c_ = min(self.stats_c_, key=self.stats_c_.get) self.maj_c_ = max(self.stats_c_, key=self.stats_c_.get) - self.logger.info('%s classes detected: %s', uniques.size, + self.logger.info('%s classes detected: %s', np.unique(y).size, self.stats_c_) # Check if the ratio provided at initialisation make sense @@ -254,6 +255,7 @@ def fit(self, X, y): # Check that the target type is binary if not type_of_target(y) == 'binary': + warnings.simplefilter('always', UserWarning) warnings.warn('The target type should be binary.') return self @@ -290,6 +292,7 @@ def fit(self, X, y): # Check that the target type is either binary or multiclass if not (type_of_target(y) == 'binary' or type_of_target(y) == 'multiclass'): + warnings.simplefilter('always', UserWarning) warnings.warn('The target type should be binary or multiclass.') return self diff --git a/imblearn/combine/tests/test_smote_enn.py b/imblearn/combine/tests/test_smote_enn.py index f3506eab4..e83c98b83 100644 --- a/imblearn/combine/tests/test_smote_enn.py +++ b/imblearn/combine/tests/test_smote_enn.py @@ -74,7 +74,7 @@ def test_smote_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, smote.fit, X, y_single_class) + assert_warns(UserWarning, smote.fit, X, y_single_class) def test_smote_fit(): diff --git a/imblearn/combine/tests/test_smote_tomek.py b/imblearn/combine/tests/test_smote_tomek.py index 423faf3f6..9582e77f1 100644 --- a/imblearn/combine/tests/test_smote_tomek.py +++ b/imblearn/combine/tests/test_smote_tomek.py @@ -74,7 +74,7 @@ def test_smote_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, smote.fit, X, y_single_class) + assert_warns(UserWarning, smote.fit, X, y_single_class) def test_smote_fit(): diff --git a/imblearn/ensemble/tests/test_balance_cascade.py b/imblearn/ensemble/tests/test_balance_cascade.py index eff8a809c..c8d1bb088 100644 --- a/imblearn/ensemble/tests/test_balance_cascade.py +++ b/imblearn/ensemble/tests/test_balance_cascade.py @@ -90,7 +90,7 @@ def test_bc_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, bc.fit, X, y_single_class) + assert_warns(UserWarning, bc.fit, X, y_single_class) def test_bc_fit_invalid_ratio(): diff --git a/imblearn/ensemble/tests/test_easy_ensemble.py b/imblearn/ensemble/tests/test_easy_ensemble.py index 9852cf2f3..02aef5bee 100644 --- a/imblearn/ensemble/tests/test_easy_ensemble.py +++ b/imblearn/ensemble/tests/test_easy_ensemble.py @@ -81,7 +81,7 @@ def test_ee_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, ee.fit, X, y_single_class) + assert_warns(UserWarning, ee.fit, X, y_single_class) def test_ee_fit_invalid_ratio(): diff --git a/imblearn/over_sampling/tests/test_adasyn.py b/imblearn/over_sampling/tests/test_adasyn.py index 7937cebdc..9c38d3c5f 100644 --- a/imblearn/over_sampling/tests/test_adasyn.py +++ b/imblearn/over_sampling/tests/test_adasyn.py @@ -84,7 +84,7 @@ def test_ada_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, ada.fit, X, y_single_class) + assert_warns(UserWarning, ada.fit, X, y_single_class) def test_ada_fit_invalid_ratio(): diff --git a/imblearn/over_sampling/tests/test_random_over_sampler.py b/imblearn/over_sampling/tests/test_random_over_sampler.py index 20a65b9fd..db6f50462 100644 --- a/imblearn/over_sampling/tests/test_random_over_sampler.py +++ b/imblearn/over_sampling/tests/test_random_over_sampler.py @@ -75,7 +75,7 @@ def test_ros_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, ros.fit, X, y_single_class) + assert_warns(UserWarning, ros.fit, X, y_single_class) def test_ros_fit_invalid_ratio(): diff --git a/imblearn/over_sampling/tests/test_smote.py b/imblearn/over_sampling/tests/test_smote.py index 33c93620e..46c01ceec 100644 --- a/imblearn/over_sampling/tests/test_smote.py +++ b/imblearn/over_sampling/tests/test_smote.py @@ -83,7 +83,7 @@ def test_smote_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, smote.fit, X, y_single_class) + assert_warns(UserWarning, smote.fit, X, y_single_class) def test_smote_fit(): diff --git a/imblearn/under_sampling/tests/test_allknn.py b/imblearn/under_sampling/tests/test_allknn.py index e8afa702e..3bd0f2075 100644 --- a/imblearn/under_sampling/tests/test_allknn.py +++ b/imblearn/under_sampling/tests/test_allknn.py @@ -83,7 +83,7 @@ def test_allknn_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, allknn.fit, X, y_single_class) + assert_warns(UserWarning, allknn.fit, X, y_single_class) def test_allknn_fit(): diff --git a/imblearn/under_sampling/tests/test_cluster_centroids.py b/imblearn/under_sampling/tests/test_cluster_centroids.py index eb0a4cbdc..bacf03b48 100644 --- a/imblearn/under_sampling/tests/test_cluster_centroids.py +++ b/imblearn/under_sampling/tests/test_cluster_centroids.py @@ -78,7 +78,7 @@ def test_cc_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, cc.fit, X, y_single_class) + assert_warns(UserWarning, cc.fit, X, y_single_class) def test_cc_fit_invalid_ratio(): diff --git a/imblearn/under_sampling/tests/test_condensed_nearest_neighbour.py b/imblearn/under_sampling/tests/test_condensed_nearest_neighbour.py index 60f078609..2ddcd1a19 100644 --- a/imblearn/under_sampling/tests/test_condensed_nearest_neighbour.py +++ b/imblearn/under_sampling/tests/test_condensed_nearest_neighbour.py @@ -60,7 +60,7 @@ def test_cnn_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, cnn.fit, X, y_single_class) + assert_warns(UserWarning, cnn.fit, X, y_single_class) def test_cnn_fit(): diff --git a/imblearn/under_sampling/tests/test_edited_nearest_neighbours.py b/imblearn/under_sampling/tests/test_edited_nearest_neighbours.py index 4b8240552..6471dcb79 100644 --- a/imblearn/under_sampling/tests/test_edited_nearest_neighbours.py +++ b/imblearn/under_sampling/tests/test_edited_nearest_neighbours.py @@ -61,7 +61,7 @@ def test_enn_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, enn.fit, X, y_single_class) + assert_warns(UserWarning, enn.fit, X, y_single_class) def test_enn_fit(): diff --git a/imblearn/under_sampling/tests/test_instance_hardness_threshold.py b/imblearn/under_sampling/tests/test_instance_hardness_threshold.py index 8ab19f9ad..02a057d4f 100644 --- a/imblearn/under_sampling/tests/test_instance_hardness_threshold.py +++ b/imblearn/under_sampling/tests/test_instance_hardness_threshold.py @@ -93,7 +93,7 @@ def test_iht_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, iht.fit, X, y_single_class) + assert_warns(UserWarning, iht.fit, X, y_single_class) def test_iht_fit_invalid_ratio(): diff --git a/imblearn/under_sampling/tests/test_nearmiss_1.py b/imblearn/under_sampling/tests/test_nearmiss_1.py index 1852e93a4..67b1aba6f 100644 --- a/imblearn/under_sampling/tests/test_nearmiss_1.py +++ b/imblearn/under_sampling/tests/test_nearmiss_1.py @@ -97,7 +97,7 @@ def test_nearmiss_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, nm1.fit, X, y_single_class) + assert_warns(UserWarning, nm1.fit, X, y_single_class) def test_nm_fit_invalid_ratio(): diff --git a/imblearn/under_sampling/tests/test_nearmiss_2.py b/imblearn/under_sampling/tests/test_nearmiss_2.py index cf23c0af6..142b377b6 100644 --- a/imblearn/under_sampling/tests/test_nearmiss_2.py +++ b/imblearn/under_sampling/tests/test_nearmiss_2.py @@ -97,7 +97,7 @@ def test_nearmiss_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, nm2.fit, X, y_single_class) + assert_warns(UserWarning, nm2.fit, X, y_single_class) def test_nm_fit_invalid_ratio(): diff --git a/imblearn/under_sampling/tests/test_nearmiss_3.py b/imblearn/under_sampling/tests/test_nearmiss_3.py index 62fc7c297..c73fd8087 100644 --- a/imblearn/under_sampling/tests/test_nearmiss_3.py +++ b/imblearn/under_sampling/tests/test_nearmiss_3.py @@ -97,7 +97,7 @@ def test_nearmiss_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, nm3.fit, X, y_single_class) + assert_warns(UserWarning, nm3.fit, X, y_single_class) def test_nm_fit_invalid_ratio(): diff --git a/imblearn/under_sampling/tests/test_neighbourhood_cleaning_rule.py b/imblearn/under_sampling/tests/test_neighbourhood_cleaning_rule.py index e8997e55a..24cd9c556 100644 --- a/imblearn/under_sampling/tests/test_neighbourhood_cleaning_rule.py +++ b/imblearn/under_sampling/tests/test_neighbourhood_cleaning_rule.py @@ -55,7 +55,7 @@ def test_ncr_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, ncr.fit, X, y_single_class) + assert_warns(UserWarning, ncr.fit, X, y_single_class) def test_ncr_fit(): diff --git a/imblearn/under_sampling/tests/test_one_sided_selection.py b/imblearn/under_sampling/tests/test_one_sided_selection.py index 09269df7c..80eb4aaea 100644 --- a/imblearn/under_sampling/tests/test_one_sided_selection.py +++ b/imblearn/under_sampling/tests/test_one_sided_selection.py @@ -57,7 +57,7 @@ def test_oss_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, oss.fit, X, y_single_class) + assert_warns(UserWarning, oss.fit, X, y_single_class) def test_oss_fit(): diff --git a/imblearn/under_sampling/tests/test_random_under_sampler.py b/imblearn/under_sampling/tests/test_random_under_sampler.py index 5d1a714ca..a77b65e25 100644 --- a/imblearn/under_sampling/tests/test_random_under_sampler.py +++ b/imblearn/under_sampling/tests/test_random_under_sampler.py @@ -74,7 +74,7 @@ def test_rus_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, rus.fit, X, y_single_class) + assert_warns(UserWarning, rus.fit, X, y_single_class) def test_rus_fit_invalid_ratio(): diff --git a/imblearn/under_sampling/tests/test_repeated_edited_nearest_neighbours.py b/imblearn/under_sampling/tests/test_repeated_edited_nearest_neighbours.py index 1f5536b06..ba24e3189 100644 --- a/imblearn/under_sampling/tests/test_repeated_edited_nearest_neighbours.py +++ b/imblearn/under_sampling/tests/test_repeated_edited_nearest_neighbours.py @@ -94,7 +94,7 @@ def test_renn_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, renn.fit, X, y_single_class) + assert_warns(UserWarning, renn.fit, X, y_single_class) def test_renn_fit(): diff --git a/imblearn/under_sampling/tests/test_tomek_links.py b/imblearn/under_sampling/tests/test_tomek_links.py index 670dfe58a..a2e979799 100644 --- a/imblearn/under_sampling/tests/test_tomek_links.py +++ b/imblearn/under_sampling/tests/test_tomek_links.py @@ -59,7 +59,7 @@ def test_tl_fit_single_class(): # Resample the data # Create a wrong y y_single_class = np.zeros((X.shape[0], )) - assert_warns(RuntimeWarning, tl.fit, X, y_single_class) + assert_warns(UserWarning, tl.fit, X, y_single_class) def test_tl_fit():