def test_linewidth_repr(self): a = np.full(7, fill_value=2) np.set_printoptions(linewidth=17) assert_equal( repr(a), textwrap.dedent("""\ array([2, 2, 2, 2, 2, 2, 2])""")) np.set_printoptions(linewidth=17, legacy='1.13') assert_equal( repr(a), textwrap.dedent("""\ array([2, 2, 2, 2, 2, 2, 2])""")) a = np.full(8, fill_value=2) np.set_printoptions(linewidth=18, legacy=False) assert_equal( repr(a), textwrap.dedent("""\ array([2, 2, 2, 2, 2, 2, 2, 2])""")) np.set_printoptions(linewidth=18, legacy='1.13') assert_equal( repr(a), textwrap.dedent("""\ array([2, 2, 2, 2, 2, 2, 2, 2])"""))
def test_uncontiguous_subspace_assignment(self): # During development there was a bug activating a skip logic # based on ndim instead of size. a = np.full((3, 4, 2), -1) b = np.full((3, 4, 2), -1) a[[0, 1]] = np.arange(2 * 4 * 2).reshape(2, 4, 2).T b[[0, 1]] = np.arange(2 * 4 * 2).reshape(2, 4, 2).T.copy() assert_equal(a, b)
def test_check_large_integers(self): uint64_max = 2 ** 64 - 1 arr = np.full(5, uint64_max, dtype=np.uint64) test = np.pad(arr, 1, mode="constant", constant_values=arr.min()) expected = np.full(7, uint64_max, dtype=np.uint64) assert_array_equal(test, expected) int64_max = 2 ** 63 - 1 arr = np.full(5, int64_max, dtype=np.int64) test = np.pad(arr, 1, mode="constant", constant_values=arr.min()) expected = np.full(7, int64_max, dtype=np.int64) assert_array_equal(test, expected)
def _append_const(arr, pad_amt, val, axis=-1): """ Append constant `val` along `axis` of `arr`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to append. val : scalar Constant value to use. For best results should be of type `arr.dtype`; if not `arr.dtype` will be cast to `arr.dtype`. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` constant `val` appended along `axis`. """ if pad_amt == 0: return arr padshape = tuple(x if i != axis else pad_amt for (i, x) in enumerate(arr.shape)) return _do_append(arr, np.full(padshape, val, dtype=arr.dtype), axis)
def test_no_seq_repeat_basic_array_like(self): # Test that an array-like which does not know how to be multiplied # does not attempt sequence repeat (raise TypeError). # See also gh-7428. class ArrayLike(object): def __init__(self, arr): self.arr = arr def __array__(self): return self.arr # Test for simple ArrayLike above and memoryviews (original report) for arr_like in (ArrayLike(np.ones(3)), memoryview(np.ones(3))): assert_array_equal(arr_like * np.float32(3.), np.full(3, 3.)) assert_array_equal(np.float32(3.) * arr_like, np.full(3, 3.)) assert_array_equal(arr_like * np.int_(3), np.full(3, 3)) assert_array_equal(np.int_(3) * arr_like, np.full(3, 3))
def test_structure_format(self): dt = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2, ))]) x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt) assert_equal(np.array2string(x), "[('Sarah', [8., 7.]) ('John', [6., 7.])]") np.set_printoptions(legacy='1.13') try: # for issue #5692 A = np.zeros(shape=10, dtype=[("A", "M8[s]")]) A[5:].fill(np.datetime64('NaT')) assert_equal( np.array2string(A), textwrap.dedent("""\ [('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('NaT',) ('NaT',) ('NaT',) ('NaT',) ('NaT',)]""")) finally: np.set_printoptions(legacy=False) # same again, but with non-legacy behavior assert_equal( np.array2string(A), textwrap.dedent("""\ [('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ( 'NaT',) ( 'NaT',) ( 'NaT',) ( 'NaT',) ( 'NaT',)]""")) # and again, with timedeltas A = np.full(10, 123456, dtype=[("A", "m8[s]")]) A[5:].fill(np.datetime64('NaT')) assert_equal( np.array2string(A), textwrap.dedent("""\ [(123456,) (123456,) (123456,) (123456,) (123456,) ( 'NaT',) ( 'NaT',) ( 'NaT',) ( 'NaT',) ( 'NaT',)]""")) # See #8160 struct_int = np.array([([1, -1], ), ([123, 1], )], dtype=[('B', 'i4', 2)]) assert_equal(np.array2string(struct_int), "[([ 1, -1],) ([123, 1],)]") struct_2dint = np.array([([[0, 1], [2, 3]], ), ([[12, 0], [0, 0]], )], dtype=[('B', 'i4', (2, 2))]) assert_equal(np.array2string(struct_2dint), "[([[ 0, 1], [ 2, 3]],) ([[12, 0], [ 0, 0]],)]") # See #8172 array_scalar = np.array((1., 2.1234567890123456789, 3.), dtype=('f8,f8,f8')) assert_equal(np.array2string(array_scalar), "(1., 2.12345679, 3.)")
def test_linewidth_str(self): a = np.full(18, fill_value=2) np.set_printoptions(linewidth=18) assert_equal( str(a), textwrap.dedent("""\ [2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]""")) np.set_printoptions(linewidth=18, legacy='1.13') assert_equal( str(a), textwrap.dedent("""\ [2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]"""))
def test_linewidth(self): a = np.full(6, 1) def make_str(a, width, **kw): return np.array2string(a, separator="", max_line_width=width, **kw) assert_equal(make_str(a, 8, legacy='1.13'), '[111111]') assert_equal(make_str(a, 7, legacy='1.13'), '[111111]') assert_equal(make_str(a, 5, legacy='1.13'), '[1111\n' ' 11]') assert_equal(make_str(a, 8), '[111111]') assert_equal(make_str(a, 7), '[11111\n' ' 1]') assert_equal(make_str(a, 5), '[111\n' ' 111]') b = a[None, None, :] assert_equal(make_str(b, 12, legacy='1.13'), '[[[111111]]]') assert_equal(make_str(b, 9, legacy='1.13'), '[[[111111]]]') assert_equal(make_str(b, 8, legacy='1.13'), '[[[11111\n' ' 1]]]') assert_equal(make_str(b, 12), '[[[111111]]]') assert_equal(make_str(b, 9), '[[[111\n' ' 111]]]') assert_equal(make_str(b, 8), '[[[11\n' ' 11\n' ' 11]]]')
def test_array_array(): tobj = type(object) ones11 = np.ones((1, 1), np.float64) tndarray = type(ones11) # Test is_ndarray assert_equal(np.array(ones11, dtype=np.float64), ones11) if HAS_REFCOUNT: old_refcount = sys.getrefcount(tndarray) np.array(ones11) assert_equal(old_refcount, sys.getrefcount(tndarray)) # test None assert_equal(np.array(None, dtype=np.float64), np.array(np.nan, dtype=np.float64)) if HAS_REFCOUNT: old_refcount = sys.getrefcount(tobj) np.array(None, dtype=np.float64) assert_equal(old_refcount, sys.getrefcount(tobj)) # test scalar assert_equal(np.array(1.0, dtype=np.float64), np.ones((), dtype=np.float64)) if HAS_REFCOUNT: old_refcount = sys.getrefcount(np.float64) np.array(np.array(1.0, dtype=np.float64), dtype=np.float64) assert_equal(old_refcount, sys.getrefcount(np.float64)) # test string S2 = np.dtype((str, 2)) S3 = np.dtype((str, 3)) S5 = np.dtype((str, 5)) assert_equal(np.array("1.0", dtype=np.float64), np.ones((), dtype=np.float64)) assert_equal(np.array("1.0").dtype, S3) assert_equal(np.array("1.0", dtype=str).dtype, S3) assert_equal(np.array("1.0", dtype=S2), np.array("1.")) assert_equal(np.array("1", dtype=S5), np.ones((), dtype=S5)) # test unicode _unicode = globals().get("unicode") if _unicode: U2 = np.dtype((_unicode, 2)) U3 = np.dtype((_unicode, 3)) U5 = np.dtype((_unicode, 5)) assert_equal(np.array(_unicode("1.0"), dtype=np.float64), np.ones((), dtype=np.float64)) assert_equal(np.array(_unicode("1.0")).dtype, U3) assert_equal(np.array(_unicode("1.0"), dtype=_unicode).dtype, U3) assert_equal(np.array(_unicode("1.0"), dtype=U2), np.array(_unicode("1."))) assert_equal(np.array(_unicode("1"), dtype=U5), np.ones((), dtype=U5)) builtins = getattr(__builtins__, '__dict__', __builtins__) assert_(hasattr(builtins, 'get')) # test buffer _buffer = builtins.get("buffer") if _buffer and sys.version_info[:3] >= (2, 7, 5): # This test fails for earlier versions of Python. # Evidently a bug got fixed in 2.7.5. dat = np.array(_buffer('1.0'), dtype=np.float64) assert_equal(dat, [49.0, 46.0, 48.0]) assert_(dat.dtype.type is np.float64) dat = np.array(_buffer(b'1.0')) assert_equal(dat, [49, 46, 48]) assert_(dat.dtype.type is np.uint8) # test memoryview, new version of buffer _memoryview = builtins.get("memoryview") if _memoryview: dat = np.array(_memoryview(b'1.0'), dtype=np.float64) assert_equal(dat, [49.0, 46.0, 48.0]) assert_(dat.dtype.type is np.float64) dat = np.array(_memoryview(b'1.0')) assert_equal(dat, [49, 46, 48]) assert_(dat.dtype.type is np.uint8) # test array interface a = np.array(100.0, dtype=np.float64) o = type("o", (object, ), dict(__array_interface__=a.__array_interface__)) assert_equal(np.array(o, dtype=np.float64), a) # test array_struct interface a = np.array([(1, 4.0, 'Hello'), (2, 6.0, 'World')], dtype=[('f0', int), ('f1', float), ('f2', str)]) o = type("o", (object, ), dict(__array_struct__=a.__array_struct__)) ## wasn't what I expected... is np.array(o) supposed to equal a ? ## instead we get a array([...], dtype=">V18") assert_equal(bytes(np.array(o).data), bytes(a.data)) # test array o = type("o", (object, ), dict(__array__=lambda *x: np.array(100.0, dtype=np.float64)))() assert_equal(np.array(o, dtype=np.float64), np.array(100.0, np.float64)) # test recursion nested = 1.5 for i in range(np.MAXDIMS): nested = [nested] # no error np.array(nested) # Exceeds recursion limit assert_raises(ValueError, np.array, [nested], dtype=np.float64) # Try with lists... assert_equal(np.array([None] * 10, dtype=np.float64), np.full((10, ), np.nan, dtype=np.float64)) assert_equal(np.array([[None]] * 10, dtype=np.float64), np.full((10, 1), np.nan, dtype=np.float64)) assert_equal(np.array([[None] * 10], dtype=np.float64), np.full((1, 10), np.nan, dtype=np.float64)) assert_equal(np.array([[None] * 10] * 10, dtype=np.float64), np.full((10, 10), np.nan, dtype=np.float64)) assert_equal(np.array([1.0] * 10, dtype=np.float64), np.ones((10, ), dtype=np.float64)) assert_equal(np.array([[1.0]] * 10, dtype=np.float64), np.ones((10, 1), dtype=np.float64)) assert_equal(np.array([[1.0] * 10], dtype=np.float64), np.ones((1, 10), dtype=np.float64)) assert_equal(np.array([[1.0] * 10] * 10, dtype=np.float64), np.ones((10, 10), dtype=np.float64)) # Try with tuples assert_equal(np.array((None, ) * 10, dtype=np.float64), np.full((10, ), np.nan, dtype=np.float64)) assert_equal(np.array([(None, )] * 10, dtype=np.float64), np.full((10, 1), np.nan, dtype=np.float64)) assert_equal(np.array([(None, ) * 10], dtype=np.float64), np.full((1, 10), np.nan, dtype=np.float64)) assert_equal(np.array([(None, ) * 10] * 10, dtype=np.float64), np.full((10, 10), np.nan, dtype=np.float64)) assert_equal(np.array((1.0, ) * 10, dtype=np.float64), np.ones((10, ), dtype=np.float64)) assert_equal(np.array([(1.0, )] * 10, dtype=np.float64), np.ones((10, 1), dtype=np.float64)) assert_equal(np.array([(1.0, ) * 10], dtype=np.float64), np.ones((1, 10), dtype=np.float64)) assert_equal(np.array([(1.0, ) * 10] * 10, dtype=np.float64), np.ones((10, 10), dtype=np.float64))