我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用numpy.testing.assert_raises()。
def test_wrong_values(): m_args = dict(args) m_args["n_samples"] = 0 assert_raises(ValueError,genData,**m_args) m_args = dict(args) m_args["n_features"] = 0 assert_raises(ValueError,genData,**m_args) m_args = dict(args) m_args["flip_y"] = 2 assert_raises(ValueError,genData,**m_args) m_args = dict(args) m_args["strRel"] = 1 m_args["n_redundant"] = 2 m_args["n_repeated"] = 1 m_args["n_features"] = 2 # less total features then specified assert_raises(ValueError,genData,**m_args)
def test_worker_exception_delayed(bucket): c = client_from_commandline_args([ "--kubeface-poll-seconds", "1.1", "--kubeface-backend", "local-process", "--kubeface-storage", bucket, "--kubeface-wait-to-raise-task-exception", ]) mapper = c.map(lambda x: 2 / (x - 2), range(10)) testing.assert_equal(next(mapper), -1) testing.assert_equal(next(mapper), -2) testing.assert_equal(len(c.job_summary(include_done=False)), 1) testing.assert_equal(len(c.job_summary(include_done=True)), 1) testing.assert_raises(ZeroDivisionError, next, mapper) testing.assert_equal(len(c.job_summary(include_done=False)), 0) testing.assert_equal(len(c.job_summary(include_done=True)), 1) testing.assert_raises(StopIteration, next, mapper) testing.assert_equal(len(c.job_summary(include_done=False)), 0) testing.assert_equal(len(c.job_summary(include_done=True)), 1)
def test_worker_exception(bucket): c = client_from_commandline_args([ "--kubeface-poll-seconds", "1.1", "--kubeface-backend", "local-process", "--kubeface-storage", bucket, "--kubeface-cache-key-prefix", "foo", ]) mapper = c.map(lambda x: 2 / (x - 2), range(10)) testing.assert_raises(ZeroDivisionError, next, mapper) # TODO: in the future we may want reruns to not re-use excpetions. # Here is a test for that functionality, which is currently not # implemented. # c = client_from_commandline_args([ # "--kubeface-poll-seconds", "1.1", # "--kubeface-backend", "local-process", # "--kubeface-storage", bucket, # "--kubeface-cache-key-prefix", "foo", # ]) # results = list(c.map(lambda x: 2 / (x - 200), range(10))) # print(results) # should not raise
def test_shape_mismatch(): image = np.ones((3, 3)) label = np.ones((2, 2)) testing.assert_raises(ValueError, label2rgb, image, label)
def check_function(self, t): if t.__doc__.split()[0] in ['t0', 't4', 's0', 's4']: err = 1e-5 else: err = 0.0 assert_(abs(t(234) - 234.0) <= err) assert_(abs(t(234.6) - 234.6) <= err) assert_(abs(t(long(234)) - 234.0) <= err) assert_(abs(t('234') - 234) <= err) assert_(abs(t('234.6') - 234.6) <= err) assert_(abs(t(-234) + 234) <= err) assert_(abs(t([234]) - 234) <= err) assert_(abs(t((234,)) - 234.) <= err) assert_(abs(t(array(234)) - 234.) <= err) assert_(abs(t(array([234])) - 234.) <= err) assert_(abs(t(array([[234]])) - 234.) <= err) assert_(abs(t(array([234], 'b')) + 22) <= err) assert_(abs(t(array([234], 'h')) - 234.) <= err) assert_(abs(t(array([234], 'i')) - 234.) <= err) assert_(abs(t(array([234], 'l')) - 234.) <= err) assert_(abs(t(array([234], 'B')) - 234.) <= err) assert_(abs(t(array([234], 'f')) - 234.) <= err) assert_(abs(t(array([234], 'd')) - 234.) <= err) if t.__doc__.split()[0] in ['t0', 't4', 's0', 's4']: assert_(t(1e200) == t(1e300)) # inf #assert_raises(ValueError, t, array([234], 'S1')) assert_raises(ValueError, t, 'abc') assert_raises(IndexError, t, []) assert_raises(IndexError, t, ()) assert_raises(Exception, t, t) assert_raises(Exception, t, {}) try: r = t(10 ** 400) assert_(repr(r) in ['inf', 'Infinity'], repr(r)) except OverflowError: pass
def check_function(self, t): assert_(t(123) == 123, repr(t(123))) assert_(t(123.6) == 123) assert_(t(long(123)) == 123) assert_(t('123') == 123) assert_(t(-123) == -123) assert_(t([123]) == 123) assert_(t((123,)) == 123) assert_(t(array(123)) == 123) assert_(t(array([123])) == 123) assert_(t(array([[123]])) == 123) assert_(t(array([123], 'b')) == 123) assert_(t(array([123], 'h')) == 123) assert_(t(array([123], 'i')) == 123) assert_(t(array([123], 'l')) == 123) assert_(t(array([123], 'B')) == 123) assert_(t(array([123], 'f')) == 123) assert_(t(array([123], 'd')) == 123) #assert_raises(ValueError, t, array([123],'S3')) assert_raises(ValueError, t, 'abc') assert_raises(IndexError, t, []) assert_raises(IndexError, t, ()) assert_raises(Exception, t, t) assert_raises(Exception, t, {}) if t.__doc__.split()[0] in ['t8', 's8']: assert_raises(OverflowError, t, 100000000000000000000000) assert_raises(OverflowError, t, 10000000011111111111111.23)
def test_bisect(): xtol = 1e-6 ## simple tests # ascending root = noisyopt.bisect(lambda x: x, -2, 2, xtol=xtol, errorcontrol=False) npt.assert_allclose(root, 0.0, atol=xtol) root = noisyopt.bisect(lambda x: x-1, -2, 2, xtol=xtol, errorcontrol=False) npt.assert_allclose(root, 1.0, atol=xtol) # descending root = noisyopt.bisect(lambda x: -x, -2, 2, xtol=xtol, errorcontrol=False) npt.assert_allclose(root, 0.0, atol=xtol) ## extrapolate if 0 outside of interval root = noisyopt.bisect(lambda x: x, 1, 2, xtol=xtol, errorcontrol=False) npt.assert_allclose(root, 0.0, atol=xtol) npt.assert_raises(noisyopt.BisectException, noisyopt.bisect, lambda x: x, 1, 2, xtol=xtol, outside='raise', errorcontrol=False) ## extrapolate with nonlinear function root = noisyopt.bisect(lambda x: x+x**2, 1.0, 2, xtol=xtol, errorcontrol=False) assert root < 1.0 ## test with stochastic function xtol = 1e-1 func = lambda x: x - 0.25 + np.random.normal(scale=0.01) root = noisyopt.bisect(noisyopt.AveragedFunction(func), -2, 2, xtol=xtol, errorcontrol=True) npt.assert_allclose(root, 0.25, atol=xtol)
def test_shape(): X,y = genData(**args) # Equal length assert_equal(len(X),len(y)) assert_equal(len(X),n) assert_equal(X.shape[1],d) #assert_raises(ValueError,genData.genData,)
def test_invalid_client(): with testing.assert_raises(ValueError): client_from_commandline_args([ "--kubeface-poll-seconds", "1.1", "--kubeface-backend", "kubernetes", "--kubeface-storage", "/tmp", ])
def check_function(self, t): tname = t.__doc__.split()[0] if tname in ['t0', 't8', 's0', 's8']: err = 1e-5 else: err = 0.0 assert_(abs(t(234j) - 234.0j) <= err) assert_(abs(t(234.6) - 234.6) <= err) assert_(abs(t(long(234)) - 234.0) <= err) assert_(abs(t(234.6 + 3j) - (234.6 + 3j)) <= err) #assert_( abs(t('234')-234.)<=err) #assert_( abs(t('234.6')-234.6)<=err) assert_(abs(t(-234) + 234.) <= err) assert_(abs(t([234]) - 234.) <= err) assert_(abs(t((234,)) - 234.) <= err) assert_(abs(t(array(234)) - 234.) <= err) assert_(abs(t(array(23 + 4j, 'F')) - (23 + 4j)) <= err) assert_(abs(t(array([234])) - 234.) <= err) assert_(abs(t(array([[234]])) - 234.) <= err) assert_(abs(t(array([234], 'b')) + 22.) <= err) assert_(abs(t(array([234], 'h')) - 234.) <= err) assert_(abs(t(array([234], 'i')) - 234.) <= err) assert_(abs(t(array([234], 'l')) - 234.) <= err) assert_(abs(t(array([234], 'q')) - 234.) <= err) assert_(abs(t(array([234], 'f')) - 234.) <= err) assert_(abs(t(array([234], 'd')) - 234.) <= err) assert_(abs(t(array([234 + 3j], 'F')) - (234 + 3j)) <= err) assert_(abs(t(array([234], 'D')) - 234.) <= err) #assert_raises(TypeError, t, array([234], 'a1')) assert_raises(TypeError, t, 'abc') assert_raises(IndexError, t, []) assert_raises(IndexError, t, ()) assert_raises(TypeError, t, t) assert_raises(TypeError, t, {}) try: r = t(10 ** 400) assert_(repr(r) in ['(inf+0j)', '(Infinity+0j)'], repr(r)) except OverflowError: pass
def runTest(self): """ The first party of this tests to see if the solver routine spits out any errors that it can raise internally. We sequencially check the ucounts, bcounts, r, w, and cpars values. The second part checks some results. It compares the lam, p_mem, and wt values given by the solver to the values contained in the data file, which are precomputed solver outputs (from IDL). """ file_name = 'test_solver_data.fit' file_path = 'data_for_tests' data=fitsio.read('%s/%s' % (file_path,file_name),ext=1) #need to transpose cpars data[0]['CPARS'] = data[0]['CPARS'][::-1] # check some common errors... testing.assert_raises(ValueError,redmapper.solver_nfw.Solver,data[0]['R0'],data[0]['BETA'],data[0]['UCOUNTS'][0:10],data[0]['BCOUNTS'],data[0]['R'],data[0]['W'],cpars=data[0]['CPARS'],rsig=data[0]['RSIG']) testing.assert_raises(ValueError,redmapper.solver_nfw.Solver,data[0]['R0'],data[0]['BETA'],data[0]['UCOUNTS'],data[0]['BCOUNTS'][0:10],data[0]['R'],data[0]['W'],cpars=data[0]['CPARS'],rsig=data[0]['RSIG']) testing.assert_raises(ValueError,redmapper.solver_nfw.Solver,data[0]['R0'],data[0]['BETA'],data[0]['UCOUNTS'],data[0]['BCOUNTS'],data[0]['R'][0:10],data[0]['W'],cpars=data[0]['CPARS'],rsig=data[0]['RSIG']) testing.assert_raises(ValueError,redmapper.solver_nfw.Solver,data[0]['R0'],data[0]['BETA'],data[0]['UCOUNTS'],data[0]['BCOUNTS'],data[0]['R'],data[0]['W'][0:10],cpars=data[0]['CPARS'],rsig=data[0]['RSIG']) testing.assert_raises(ValueError,redmapper.solver_nfw.Solver,data[0]['R0'],data[0]['BETA'],data[0]['UCOUNTS'],data[0]['BCOUNTS'],data[0]['R'],data[0]['W'],cpars=data[0]['CPARS'][0:1],rsig=data[0]['RSIG']) # and test the results solver=redmapper.solver_nfw.Solver(data[0]['R0'],data[0]['BETA'],data[0]['UCOUNTS'],data[0]['BCOUNTS'],data[0]['R'],data[0]['W'],cpars=data[0]['CPARS'],rsig=data[0]['RSIG']) """ solve_nfw() spits out: lambda, p_mem, wt = p_mem*theta^L*theta^R """ lam,p,wt,rlambda,theta_r=solver.solve_nfw() testing.assert_almost_equal(lam,data[0]['LAMBDA']) testing.assert_array_almost_equal(p,data[0]['PVALS']) testing.assert_array_almost_equal(wt,data[0]['WTVALS']) testing.assert_almost_equal(rlambda,data[0]['R0']*(data[0]['LAMBDA']/100.)**data[0]['BETA'],4) # need new test data with theta_r