def test_basic(self): assert_(np.isscalar(3)) assert_(not np.isscalar([3])) assert_(not np.isscalar((3, ))) assert_(np.isscalar(3j)) assert_(np.isscalar(long(10))) assert_(np.isscalar(4.0))
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_r1array(self): """ Test to make sure equivalent Travis O's r1array function """ assert_(atleast_1d(3).shape == (1,)) assert_(atleast_1d(3j).shape == (1,)) assert_(atleast_1d(long(3)).shape == (1,)) assert_(atleast_1d(3.0).shape == (1,)) assert_(atleast_1d([[2, 3], [4, 5]]).shape == (2, 2))
def test_shares_memory_api(): x = np.zeros([4, 5, 6], dtype=np.int8) assert_equal(np.shares_memory(x, x), True) assert_equal(np.shares_memory(x, x.copy()), False) a = x[:,::2,::3] b = x[:,::3,::2] assert_equal(np.shares_memory(a, b), True) assert_equal(np.shares_memory(a, b, max_work=None), True) assert_raises(np.TooHardError, np.shares_memory, a, b, max_work=1) assert_raises(np.TooHardError, np.shares_memory, a, b, max_work=long(1))
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 check_function(self, t): assert_(t(True) == 1, repr(t(True))) assert_(t(False) == 0, repr(t(False))) assert_(t(0) == 0) assert_(t(None) == 0) assert_(t(0.0) == 0) assert_(t(0j) == 0) assert_(t(1j) == 1) assert_(t(234) == 1) assert_(t(234.6) == 1) assert_(t(long(234)) == 1) assert_(t(234.6 + 3j) == 1) assert_(t('234') == 1) assert_(t('aaa') == 1) assert_(t('') == 0) assert_(t([]) == 0) assert_(t(()) == 0) assert_(t({}) == 0) assert_(t(t) == 1) assert_(t(-234) == 1) assert_(t(10**100) == 1) assert_(t([234]) == 1) assert_(t((234, )) == 1) assert_(t(array(234)) == 1) assert_(t(array([234])) == 1) assert_(t(array([[234]])) == 1) assert_(t(array([234], 'b')) == 1) assert_(t(array([234], 'h')) == 1) assert_(t(array([234], 'i')) == 1) assert_(t(array([234], 'l')) == 1) assert_(t(array([234], 'f')) == 1) assert_(t(array([234], 'd')) == 1) assert_(t(array([234 + 3j], 'F')) == 1) assert_(t(array([234], 'D')) == 1) assert_(t(array(0)) == 0) assert_(t(array([0])) == 0) assert_(t(array([[0]])) == 0) assert_(t(array([0j])) == 0) assert_(t(array([1])) == 1) assert_raises(ValueError, t, array([0, 0]))
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 test_permutation_longs(self): np.random.seed(1234) a = np.random.permutation(12) np.random.seed(1234) b = np.random.permutation(long(12)) assert_array_equal(a, b)
def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0, shape=None, order='C'): # Import here to minimize 'import numpy' overhead import mmap import os.path try: mode = mode_equivalents[mode] except KeyError: if mode not in valid_filemodes: raise ValueError( "mode must be one of %s" % (valid_filemodes + list(mode_equivalents.keys()))) if hasattr(filename, 'read'): fid = filename own_file = False elif is_pathlib_path(filename): fid = filename.open((mode == 'c' and 'r' or mode) + 'b') own_file = True else: fid = open(filename, (mode == 'c' and 'r' or mode) + 'b') own_file = True if (mode == 'w+') and shape is None: raise ValueError("shape must be given") fid.seek(0, 2) flen = fid.tell() descr = dtypedescr(dtype) _dbytes = descr.itemsize if shape is None: bytes = flen - offset if (bytes % _dbytes): fid.close() raise ValueError("Size of available data is not a " "multiple of the data-type size.") size = bytes // _dbytes shape = (size, ) else: if not isinstance(shape, tuple): shape = (shape, ) size = np.intp( 1) # avoid default choice of np.int_, which might overflow for k in shape: size *= k bytes = long(offset + size * _dbytes) if mode == 'w+' or (mode == 'r+' and flen < bytes): fid.seek(bytes - 1, 0) fid.write(b'\0') fid.flush() if mode == 'c': acc = mmap.ACCESS_COPY elif mode == 'r': acc = mmap.ACCESS_READ else: acc = mmap.ACCESS_WRITE start = offset - offset % mmap.ALLOCATIONGRANULARITY bytes -= start array_offset = offset - start mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start) self = ndarray.__new__(subtype, shape, dtype=descr, buffer=mm, offset=array_offset, order=order) self._mmap = mm self.offset = offset self.mode = mode if isinstance(filename, basestring): self.filename = os.path.abspath(filename) elif is_pathlib_path(filename): self.filename = filename.resolve() # py3 returns int for TemporaryFile().name elif (hasattr(filename, "name") and isinstance(filename.name, basestring)): self.filename = os.path.abspath(filename.name) # same as memmap copies (e.g. memmap + 1) else: self.filename = None if own_file: fid.close() return self