def test_basic_properties(self): ectx = nd.eval_context() # The default settings of an evaluation context self.assertEqual(ectx.errmode, 'fractional') self.assertEqual(ectx.cuda_device_errmode, 'nocheck'), self.assertEqual(ectx.date_parse_order, 'NoAmbig') self.assertEqual(ectx.century_window, 70)
def test_assignment_arrfunc(self): af = _lowlevel.make_arrfunc_from_assignment( ndt.float32, ndt.int64, "nocheck") self.assertEqual(nd.as_py(af.proto), ndt.type("(int64) -> float32")) a = nd.array(1234, type=ndt.int64) b = af(a) self.assertEqual(nd.type_of(b), ndt.float32) self.assertEqual(nd.as_py(b), 1234) # Instantiate as a strided kernel with _lowlevel.ckernel.CKernelBuilder() as ckb: meta = (ctypes.c_void_p * 2)() ectx = nd.eval_context() _lowlevel.arrfunc_instantiate(af, ckb, 0, ndt.float32, 0, [ndt.int64], [0], "strided", ectx._ectx_ptr) ck = ckb.ckernel(_lowlevel.ExprStridedOperation) # Do an assignment using ctypes i64 = (ctypes.c_int64 * 3)() for i, v in enumerate([3,7,21]): i64[i] = v pi64 = ctypes.pointer(i64) i64_stride = c_ssize_t(8) f32 = (ctypes.c_float * 3)() ck(ctypes.addressof(f32), 4, ctypes.addressof(pi64), ctypes.pointer(i64_stride), 3) self.assertEqual([f32[i] for i in range(3)], [3,7,21])
def test_eval_errmode(self): a = nd.array(1.5).cast(ndt.int32) # Default error mode raises self.assertRaises(RuntimeError, a.eval) # But with an evaluation context with a 'nocheck' default error mode... self.assertEqual(nd.as_py(a.eval(ectx=nd.eval_context(errmode='nocheck'))), 1)
def test_eval_errmode(self): a = nd.array(1.5).cast(ndt.int32) # Default error mode raises self.assertRaises(RuntimeError, a.eval) # But with an evaluation context with a 'nocheck' default error mode... self.assertEqual( nd.as_py(a.eval(ectx=nd.eval_context(errmode='nocheck'))), 1)
def test_json_date_parse(self): a = nd.parse_json('var * date', '["2012-03-17", "1922-12-30"]') self.assertEqual(nd.as_py(a), [date(2012, 3, 17), date(1922, 12, 30)]) self.assertRaises(ValueError, nd.parse_json, 'var * date', '["2012-03-17T17:00:15-0600", "1922-12-30 Thursday"]') a = nd.parse_json('var * date', '["2012-06-17T17:00:15-0600", "1921-12-30 Thursday"]', ectx=nd.eval_context(errmode='nocheck')) self.assertEqual(nd.as_py(a), [date(2012, 6, 17), date(1921, 12, 30)])
def test_date_parse(self): # By default, don't allow ambiguous year interpretations self.assertRaises(ValueError, nd.array('01/02/03').cast('date').eval) self.assertEqual(nd.as_py(nd.array('01/02/03').cast('date').eval( ectx=nd.eval_context(date_parse_order='YMD'))), date(2001, 2, 3)) self.assertEqual(nd.as_py(nd.array('01/02/03').cast('date').eval( ectx=nd.eval_context(date_parse_order='MDY'))), date(2003, 1, 2)) self.assertEqual(nd.as_py(nd.array('01/02/03').cast('date').eval( ectx=nd.eval_context(date_parse_order='DMY'))), date(2003, 2, 1)) # Can also change the two year handling # century_window of 0 means don't allow self.assertRaises(ValueError, nd.array('01/02/03').cast('date').eval, ectx=nd.eval_context(date_parse_order='YMD', century_window=0)) # century_window of 10 means a sliding window starting 10 years ago self.assertEqual(nd.as_py(nd.array('01/02/03').cast('date').eval( ectx=nd.eval_context(date_parse_order='YMD', century_window=10))), date(2101, 2, 3)) # century_window of 1850 means a fixed window starting at 1850 self.assertEqual(nd.as_py(nd.array('01/02/03').cast('date').eval( ectx=nd.eval_context(date_parse_order='YMD', century_window=1850))), date(1901, 2, 3))
def test_date_parse(self): # By default, don't allow ambiguous year interpretations self.assertRaises(ValueError, nd.array('01/02/03').cast('date').eval) self.assertEqual( nd.as_py( nd.array('01/02/03').cast('date').eval(ectx=nd.eval_context( date_parse_order='YMD'))), date(2001, 2, 3)) self.assertEqual( nd.as_py( nd.array('01/02/03').cast('date').eval(ectx=nd.eval_context( date_parse_order='MDY'))), date(2003, 1, 2)) self.assertEqual( nd.as_py( nd.array('01/02/03').cast('date').eval(ectx=nd.eval_context( date_parse_order='DMY'))), date(2003, 2, 1)) # Can also change the two year handling # century_window of 0 means don't allow self.assertRaises(ValueError, nd.array('01/02/03').cast('date').eval, ectx=nd.eval_context(date_parse_order='YMD', century_window=0)) # century_window of 10 means a sliding window starting 10 years ago self.assertEqual( nd.as_py( nd.array('01/02/03').cast('date').eval(ectx=nd.eval_context( date_parse_order='YMD', century_window=10))), date(2101, 2, 3)) # century_window of 1850 means a fixed window starting at 1850 self.assertEqual( nd.as_py( nd.array('01/02/03').cast('date').eval(ectx=nd.eval_context( date_parse_order='YMD', century_window=1850))), date(1901, 2, 3))
def test_modified_properties(self): ectx = nd.eval_context(errmode='overflow', cuda_device_errmode='fractional', date_parse_order='YMD', century_window=1929) self.assertEqual(ectx.errmode, 'overflow') self.assertEqual(ectx.cuda_device_errmode, 'fractional'), self.assertEqual(ectx.date_parse_order, 'YMD') self.assertEqual(ectx.century_window, 1929) self.assertEqual(repr(ectx), "nd.eval_context(errmode='overflow',\n" + " cuda_device_errmode='fractional',\n" + " date_parse_order='YMD',\n" + " century_window=1929)")
def test_modified_properties(self): ectx = nd.eval_context(errmode='overflow', cuda_device_errmode='fractional', date_parse_order='YMD', century_window=1929) self.assertEqual(ectx.errmode, 'overflow') self.assertEqual(ectx.cuda_device_errmode, 'fractional'), self.assertEqual(ectx.date_parse_order, 'YMD') self.assertEqual(ectx.century_window, 1929) self.assertEqual( repr(ectx), "nd.eval_context(errmode='overflow',\n" + " cuda_device_errmode='fractional',\n" + " date_parse_order='YMD',\n" + " century_window=1929)")