numpy.testing.assert_warns

Here are the examples of the python api numpy.testing.assert_warns taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

127 Examples 7

Example 1

Project: scikit-image
Source File: test_denoise.py
View license
def test_estimate_sigma_color():
    rstate = np.random.RandomState(1234)
    # astronaut image
    img = astro.copy()
    sigma = 0.1
    # add noise to astronaut
    img += sigma * rstate.standard_normal(img.shape)

    sigma_est = restoration.estimate_sigma(img, multichannel=True,
                                           average_sigmas=True)
    assert_almost_equal(sigma, sigma_est, decimal=2)

    sigma_list = restoration.estimate_sigma(img, multichannel=True,
                                            average_sigmas=False)
    assert_equal(len(sigma_list), img.shape[-1])
    assert_almost_equal(sigma_list[0], sigma_est, decimal=2)

    # default multichannel=False should raise a warning about last axis size
    assert_warns(UserWarning, restoration.estimate_sigma, img)

Example 2

Project: scikit-image
Source File: test_felzenszwalb.py
View license
def test_3D():
    grey_img = np.zeros((10, 10))
    rgb_img = np.zeros((10, 10, 3))
    three_d_img = np.zeros((10, 10, 10))
    with assert_no_warnings():
        felzenszwalb(grey_img, multichannel=True)
        felzenszwalb(grey_img, multichannel=False)
        felzenszwalb(rgb_img, multichannel=True)
    with assert_warns(RuntimeWarning):
        felzenszwalb(three_d_img, multichannel=True)
    with assert_raises(ValueError):
        felzenszwalb(rgb_img, multichannel=False)
        felzenszwalb(three_d_img, multichannel=False)

Example 3

Project: scikit-image
Source File: test_shape.py
View license
def test_views_non_contiguous():
    A = np.arange(16).reshape((4, 4))
    A = A[::2, :]

    assert_warns(RuntimeWarning, view_as_blocks, A, (2, 2))
    assert_warns(RuntimeWarning, view_as_windows, A, (2, 2))

Example 4

Project: scikit-learn
Source File: test_theil_sen.py
View license
def test_spatial_median_2d():
    X = np.array([0., 0., 1., 1., 0., 1.]).reshape(3, 2)
    _, median = _spatial_median(X, max_iter=100, tol=1.e-6)

    def cost_func(y):
        dists = np.array([norm(x - y) for x in X])
        return np.sum(dists)

    # Check if median is solution of the Fermat-Weber location problem
    fermat_weber = fmin_bfgs(cost_func, median, disp=False)
    assert_array_almost_equal(median, fermat_weber)
    # Check when maximum iteration is exceeded a warning is emitted
    assert_warns(ConvergenceWarning, _spatial_median, X, max_iter=30, tol=0.)

Example 5

Project: imbalanced-learn
Source File: test_smote_enn.py
View license
def test_smote_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    smote = SMOTEENN(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, smote.fit, X, y_single_class)

Example 6

Project: imbalanced-learn
Source File: test_smote_enn.py
View license
def test_senn_multiclass_error():
    """ Test either if an error is raised when the target are not binary
    type. """

    # continuous case
    y = np.linspace(0, 1, 20)
    sm = SMOTEENN(random_state=RND_SEED)
    assert_warns(UserWarning, sm.fit, X, y)

    # multiclass case
    y = np.array([0] * 3 + [1] * 2 + [2] * 15)
    sm = SMOTEENN(random_state=RND_SEED)
    assert_warns(UserWarning, sm.fit, X, y)

Example 7

View license
def test_smote_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    smote = SMOTETomek(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, smote.fit, X, y_single_class)

Example 8

View license
def test_multiclass_error():
    """ Test either if an error is raised when the target are not binary
    type. """

    # continuous case
    y = np.linspace(0, 1, 20)
    sm = SMOTETomek(random_state=RND_SEED)
    assert_warns(UserWarning, sm.fit, X, y)

    # multiclass case
    y = np.array([0] * 3 + [1] * 2 + [2] * 15)
    sm = SMOTETomek(random_state=RND_SEED)
    assert_warns(UserWarning, sm.fit, X, y)

Example 9

View license
def test_bc_fit_single_class():
    """Test either if an error when there is a single class"""

    # Define the parameter for the under-sampling
    ratio = 'auto'

    # Create the object
    bc = BalanceCascade(ratio=ratio, random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, bc.fit, X, y_single_class)

Example 10

View license
def test_multiclass_error():
    """ Test either if an error is raised when the target are not binary
    type. """

    # continuous case
    y = np.linspace(0, 1, 20)
    bc = BalanceCascade(random_state=RND_SEED)
    assert_warns(UserWarning, bc.fit, X, y)

    # multiclass case
    y = np.array([0] * 3 + [1] * 2 + [2] * 15)
    bc = BalanceCascade(random_state=RND_SEED)
    assert_warns(UserWarning, bc.fit, X, y)

Example 11

View license
def test_ee_fit_single_class():
    """Test either if an error when there is a single class"""

    # Define the parameter for the under-sampling
    ratio = 'auto'

    # Create the object
    ee = EasyEnsemble(ratio=ratio, random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, ee.fit, X, y_single_class)

Example 12

View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 10)
    ee = EasyEnsemble(random_state=RND_SEED)
    assert_warns(UserWarning, ee.fit, X, y)

Example 13

Project: imbalanced-learn
Source File: test_adasyn.py
View license
def test_ada_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    ada = ADASYN(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, ada.fit, X, y_single_class)

Example 14

Project: imbalanced-learn
Source File: test_adasyn.py
View license
def test_multiclass_error():
    """ Test either if an error is raised when the target are not binary
    type. """

    # continuous case
    y = np.linspace(0, 1, 20)
    ada = ADASYN(random_state=RND_SEED)
    assert_warns(UserWarning, ada.fit, X, y)

    # multiclass case
    y = np.array([0] * 3 + [1] * 2 + [2] * 15)
    ada = ADASYN(random_state=RND_SEED)
    assert_warns(UserWarning, ada.fit, X, y)

Example 15

View license
def test_ros_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    ros = RandomOverSampler(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, ros.fit, X, y_single_class)

Example 16

View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 10)
    ros = RandomOverSampler(random_state=RND_SEED)
    assert_warns(UserWarning, ros.fit, X, y)

Example 17

Project: imbalanced-learn
Source File: test_smote.py
View license
def test_smote_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    smote = SMOTE(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, smote.fit, X, y_single_class)

Example 18

Project: imbalanced-learn
Source File: test_smote.py
View license
def test_multiclass_error():
    """ Test either if an error is raised when the target are not binary
    type. """

    # continuous case
    y = np.linspace(0, 1, 20)
    sm = SMOTE(random_state=RND_SEED)
    assert_warns(UserWarning, sm.fit, X, y)

    # multiclass case
    y = np.array([0] * 3 + [1] * 2 + [2] * 15)
    sm = SMOTE(random_state=RND_SEED)
    assert_warns(UserWarning, sm.fit, X, y)

Example 19

Project: imbalanced-learn
Source File: test_allknn.py
View license
def test_allknn_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    allknn = AllKNN(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, allknn.fit, X, y_single_class)

Example 20

Project: imbalanced-learn
Source File: test_allknn.py
View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 40)
    ann = AllKNN(random_state=RND_SEED)
    assert_warns(UserWarning, ann.fit, X, y)

Example 21

View license
def test_cc_fit_single_class():
    """Test either if an error when there is a single class"""

    # Define the parameter for the under-sampling
    ratio = 'auto'

    # Create the object
    cc = ClusterCentroids(ratio=ratio, random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, cc.fit, X, y_single_class)

Example 22

View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 10)
    cc = ClusterCentroids(random_state=RND_SEED)
    assert_warns(UserWarning, cc.fit, X, y)

Example 23

View license
def test_cnn_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    cnn = CondensedNearestNeighbour(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, cnn.fit, X, y_single_class)

Example 24

View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 20)
    cnn = CondensedNearestNeighbour(random_state=RND_SEED)
    assert_warns(UserWarning, cnn.fit, X, y)

Example 25

View license
def test_enn_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    enn = EditedNearestNeighbours(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, enn.fit, X, y_single_class)

Example 26

View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 20)
    enn = EditedNearestNeighbours(random_state=RND_SEED)
    assert_warns(UserWarning, enn.fit, X, y)

Example 27

View license
def test_iht_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    iht = InstanceHardnessThreshold(ESTIMATOR, random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, iht.fit, X, y_single_class)

Example 28

View license
def test_multiclass_error():
    """ Test either if an error is raised when the target are not binary
    type. """

    # continuous case
    y = np.linspace(0, 1, 15)
    iht = InstanceHardnessThreshold(random_state=RND_SEED)
    assert_warns(UserWarning, iht.fit, X, y)

    # multiclass case
    y = np.array([0] * 10 + [1] * 3 + [2] *2)
    iht = InstanceHardnessThreshold(random_state=RND_SEED)
    assert_warns(UserWarning, iht.fit, X, y)

Example 29

Project: imbalanced-learn
Source File: test_nearmiss_1.py
View license
def test_nearmiss_fit_single_class():
    """Test either if an error when there is a single class"""

    # Define the parameter for the under-sampling
    ratio = 'auto'

    # Create the object
    nm1 = NearMiss(ratio=ratio, random_state=RND_SEED,
                   version=VERSION_NEARMISS)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, nm1.fit, X, y_single_class)

Example 30

Project: imbalanced-learn
Source File: test_nearmiss_1.py
View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 15)
    nm1 = NearMiss(random_state=RND_SEED, version=VERSION_NEARMISS)
    assert_warns(UserWarning, nm1.fit, X, y)

Example 31

Project: imbalanced-learn
Source File: test_nearmiss_2.py
View license
def test_nearmiss_fit_single_class():
    """Test either if an error when there is a single class"""

    # Define the parameter for the under-sampling
    ratio = 'auto'

    # Create the object
    nm2 = NearMiss(ratio=ratio, random_state=RND_SEED,
                   version=VERSION_NEARMISS)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, nm2.fit, X, y_single_class)

Example 32

Project: imbalanced-learn
Source File: test_nearmiss_2.py
View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 15)
    nm = NearMiss(random_state=RND_SEED, version=VERSION_NEARMISS)
    assert_warns(UserWarning, nm.fit, X, y)

Example 33

Project: imbalanced-learn
Source File: test_nearmiss_3.py
View license
def test_nearmiss_fit_single_class():
    """Test either if an error when there is a single class"""

    # Define the parameter for the under-sampling
    ratio = 'auto'

    # Create the object
    nm3 = NearMiss(ratio=ratio, random_state=RND_SEED,
                   version=VERSION_NEARMISS)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, nm3.fit, X, y_single_class)

Example 34

Project: imbalanced-learn
Source File: test_nearmiss_3.py
View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 15)
    nm = NearMiss(random_state=RND_SEED, version=VERSION_NEARMISS)
    assert_warns(UserWarning, nm.fit, X, y)

Example 35

View license
def test_ncr_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    ncr = NeighbourhoodCleaningRule(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, ncr.fit, X, y_single_class)

Example 36

View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 15)
    ncr = NeighbourhoodCleaningRule(random_state=RND_SEED)
    assert_warns(UserWarning, ncr.fit, X, y)

Example 37

View license
def test_oss_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    oss = OneSidedSelection(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, oss.fit, X, y_single_class)

Example 38

View license
def test_multiclass_error():
    """ Test either if an error is raised when the target are not binary
    type. """

    # continuous case
    y = np.linspace(0, 1, 15)
    oss = OneSidedSelection(random_state=RND_SEED)
    assert_warns(UserWarning, oss.fit, X, y)

    # multiclass case
    y = np.array([0] * 10 + [1] * 3 + [2] * 2)
    oss = OneSidedSelection(random_state=RND_SEED)
    assert_warns(UserWarning, oss.fit, X, y)

Example 39

View license
def test_rus_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    rus = RandomUnderSampler(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, rus.fit, X, y_single_class)

Example 40

View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 10)
    rus = RandomUnderSampler(random_state=RND_SEED)
    assert_warns(UserWarning, rus.fit, X, y)

Example 41

View license
def test_renn_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    renn = RepeatedEditedNearestNeighbours(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, renn.fit, X, y_single_class)

Example 42

View license
def test_continuous_error():
    """Test either if an error is raised when the target are continuous
    type"""

    # continuous case
    y = np.linspace(0, 1, 40)
    enn = RepeatedEditedNearestNeighbours(random_state=RND_SEED)
    assert_warns(UserWarning, enn.fit, X, y)

Example 43

View license
def test_tl_fit_single_class():
    """Test either if an error when there is a single class"""

    # Create the object
    tl = TomekLinks(random_state=RND_SEED)
    # Resample the data
    # Create a wrong y
    y_single_class = np.zeros((X.shape[0], ))
    assert_warns(UserWarning, tl.fit, X, y_single_class)

Example 44

View license
def test_multiclass_error():
    """ Test either if an error is raised when the target are not binary
    type. """

    # continuous case
    y = np.linspace(0, 1, 20)
    tl = TomekLinks(random_state=RND_SEED)
    assert_warns(UserWarning, tl.fit, X, y)

    # multiclass case
    y = np.array([0] * 3 + [1] * 7 + [2] * 10)
    tl = TomekLinks(random_state=RND_SEED)
    assert_warns(UserWarning, tl.fit, X, y)

Example 45

Project: scipy
Source File: test_odr.py
View license
    def test_empty_data(self):
        beta0 = [0.02, 0.0]
        linear = Model(self.empty_data_func)

        empty_dat = Data([], [])
        assert_warns(OdrWarning, ODR,
                     empty_dat, linear, beta0=beta0)

        empty_dat = RealData([], [])
        assert_warns(OdrWarning, ODR,
                     empty_dat, linear, beta0=beta0)

Example 46

Project: scipy
Source File: test_optimize.py
View license
def test_neldermead_xatol_fatol():
    # gh4484
    # test we can call with fatol, xatol specified
    func = lambda x: x[0]**2 + x[1]**2

    optimize._minimize._minimize_neldermead(func, [1, 1], maxiter=2,
                                            xatol=1e-3, fatol=1e-3)
    assert_warns(DeprecationWarning,
                 optimize._minimize._minimize_neldermead,
                 func, [1, 1], xtol=1e-3, ftol=1e-3, maxiter=2)

Example 47

Project: scipy
Source File: test_fir_filter_design.py
View license
    def test_bad_args(self):
        # not enough taps
        assert_raises(ValueError, minimum_phase, [1.])
        assert_raises(ValueError, minimum_phase, [1., 1.])
        assert_raises(ValueError, minimum_phase, np.ones(10) * 1j)
        assert_raises(ValueError, minimum_phase, 'foo')
        assert_raises(ValueError, minimum_phase, np.ones(10), n_fft=8)
        assert_raises(ValueError, minimum_phase, np.ones(10), method='foo')
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            assert_warns(RuntimeWarning, minimum_phase, np.arange(3))

Example 48

Project: statsmodels
Source File: test_poisson.py
View license
    def test_exog_names_warning(self):
        mod = self.res.model
        mod1 = PoissonOffsetGMLE(mod.endog, mod.exog, offset=mod.offset)
        from numpy.testing import assert_warns
        mod1.data.xnames = mod1.data.xnames * 2
        assert_warns(ValueWarning, mod1.fit, disp=0)

Example 49

Project: qiita
Source File: test_util.py
View license
    def test_load_template_to_dataframe_empty_columns(self):
        obs = npt.assert_warns(
            qdb.exceptions.QiitaDBWarning,
            qdb.metadata_template.util.load_template_to_dataframe,
            StringIO(EXP_ST_SPACES_EMPTY_COLUMN))
        exp = pd.DataFrame.from_dict(SAMPLE_TEMPLATE_DICT_FORM, dtype=str)
        exp.index.name = 'sample_name'
        assert_frame_equal(obs, exp)

Example 50

Project: qiita
Source File: test_util.py
View license
    def test_load_template_to_dataframe_empty_column(self):
        obs = npt.assert_warns(
            qdb.exceptions.QiitaDBWarning,
            qdb.metadata_template.util.load_template_to_dataframe,
            StringIO(SAMPLE_TEMPLATE_EMPTY_COLUMN))
        exp = pd.DataFrame.from_dict(ST_EMPTY_COLUMN_DICT_FORM, dtype=str)
        exp.index.name = 'sample_name'
        assert_frame_equal(obs, exp)