def test_legacy_stray_comma(self): np.set_printoptions(legacy='1.13') assert_equal(str(np.arange(10000)), '[ 0 1 2 ..., 9997 9998 9999]') np.set_printoptions(legacy=False) assert_equal(str(np.arange(10000)), '[ 0 1 2 ... 9997 9998 9999]')
def test_precision_zero(self): np.set_printoptions(precision=0) for values, string in (([0.], "0."), ([.3], "0."), ([-.3], "-0."), ([.7], "1."), ([1.5], "2."), ([-1.5], "-2."), ([-15.34], "-15."), ([100.], "100."), ([.2, -1, 122.51], " 0., -1., 123."), ([0], "0"), ([-12], "-12"), ([complex(.3, -.7)], "0.-1.j")): x = np.array(values) assert_equal(repr(x), "array([%s])" % string)
def test_legacy_mode_scalars(self): # in legacy mode, str of floats get truncated, and complex scalars # use * for non-finite imaginary part np.set_printoptions(legacy='1.13') assert_equal(str(np.float64(1.123456789123456789)), '1.12345678912') assert_equal(str(np.complex128(complex(1, np.nan))), '(1+nan*j)') np.set_printoptions(legacy=False) assert_equal(str(np.float64(1.123456789123456789)), '1.1234567891234568') assert_equal(str(np.complex128(complex(1, np.nan))), '(1+nanj)')
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_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_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_dtype_linewidth_wrapping(self): np.set_printoptions(linewidth=75) assert_equal( repr(np.arange(10, 20., dtype='f4')), "array([10., 11., 12., 13., 14., 15., 16., 17., 18., 19.], dtype=float32)" ) assert_equal( repr(np.arange(10, 23., dtype='f4')), textwrap.dedent("""\ array([10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22.], dtype=float32)""")) styp = '<U4' if sys.version_info[0] >= 3 else '|S4' assert_equal(repr(np.ones(3, dtype=styp)), "array(['1', '1', '1'], dtype='{}')".format(styp)) assert_equal( repr(np.ones(12, dtype=styp)), textwrap.dedent("""\ array(['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'], dtype='{}')""".format(styp)))
def test_0d_arrays(self): unicode = type(u'') assert_equal(unicode(np.array(u'café', '<U4')), u'café') if sys.version_info[0] >= 3: assert_equal(repr(np.array('café', '<U4')), "array('café', dtype='<U4')") else: assert_equal(repr(np.array(u'café', '<U4')), "array(u'caf\\xe9', dtype='<U4')") assert_equal(str(np.array('test', np.str_)), 'test') a = np.zeros(1, dtype=[('a', '<i4', (3, ))]) assert_equal(str(a[0]), '([0, 0, 0],)') assert_equal(repr(np.datetime64('2005-02-25')[...]), "array('2005-02-25', dtype='datetime64[D]')") assert_equal(repr(np.timedelta64('10', 'Y')[...]), "array(10, dtype='timedelta64[Y]')") # repr of 0d arrays is affected by printoptions x = np.array(1) np.set_printoptions(formatter={'all': lambda x: "test"}) assert_equal(repr(x), "array(test)") # str is unaffected assert_equal(str(x), "1") # check `style` arg raises assert_warns(DeprecationWarning, np.array2string, np.array(1.), style=repr) # but not in legacy mode np.array2string(np.array(1.), style=repr, legacy='1.13') # gh-10934 style was broken in legacy mode, check it works np.array2string(np.array(1.), legacy='1.13')
def test_float_spacing(self): x = np.array([1., 2., 3.]) y = np.array([1., 2., -10.]) z = np.array([100., 2., -1.]) w = np.array([-100., 2., 1.]) assert_equal(repr(x), 'array([1., 2., 3.])') assert_equal(repr(y), 'array([ 1., 2., -10.])') assert_equal(repr(np.array(y[0])), 'array(1.)') assert_equal(repr(np.array(y[-1])), 'array(-10.)') assert_equal(repr(z), 'array([100., 2., -1.])') assert_equal(repr(w), 'array([-100., 2., 1.])') assert_equal(repr(np.array([np.nan, np.inf])), 'array([nan, inf])') assert_equal(repr(np.array([np.nan, -np.inf])), 'array([ nan, -inf])') x = np.array([np.inf, 100000, 1.1234]) y = np.array([np.inf, 100000, -1.1234]) z = np.array([np.inf, 1.1234, -1e120]) np.set_printoptions(precision=2) assert_equal(repr(x), 'array([ inf, 1.00e+05, 1.12e+00])') assert_equal(repr(y), 'array([ inf, 1.00e+05, -1.12e+00])') assert_equal(repr(z), 'array([ inf, 1.12e+000, -1.00e+120])')
def test_sign_spacing(self): a = np.arange(4.) b = np.array([1.234e9]) c = np.array([1.0 + 1.0j, 1.123456789 + 1.123456789j], dtype='c16') assert_equal(repr(a), 'array([0., 1., 2., 3.])') assert_equal(repr(np.array(1.)), 'array(1.)') assert_equal(repr(b), 'array([1.234e+09])') assert_equal(repr(np.array([0.])), 'array([0.])') assert_equal( repr(c), "array([1. +1.j , 1.12345679+1.12345679j])") assert_equal(repr(np.array([0., -0.])), 'array([ 0., -0.])') np.set_printoptions(sign=' ') assert_equal(repr(a), 'array([ 0., 1., 2., 3.])') assert_equal(repr(np.array(1.)), 'array( 1.)') assert_equal(repr(b), 'array([ 1.234e+09])') assert_equal( repr(c), "array([ 1. +1.j , 1.12345679+1.12345679j])") assert_equal(repr(np.array([0., -0.])), 'array([ 0., -0.])') np.set_printoptions(sign='+') assert_equal(repr(a), 'array([+0., +1., +2., +3.])') assert_equal(repr(np.array(1.)), 'array(+1.)') assert_equal(repr(b), 'array([+1.234e+09])') assert_equal( repr(c), "array([+1. +1.j , +1.12345679+1.12345679j])") np.set_printoptions(legacy='1.13') assert_equal(repr(a), 'array([ 0., 1., 2., 3.])') assert_equal(repr(b), 'array([ 1.23400000e+09])') assert_equal(repr(-b), 'array([ -1.23400000e+09])') assert_equal(repr(np.array(1.)), 'array(1.0)') assert_equal(repr(np.array([0.])), 'array([ 0.])') assert_equal( repr(c), "array([ 1.00000000+1.j , 1.12345679+1.12345679j])") # gh-10383 assert_equal(str(np.array([-1., 10])), "[ -1. 10.]") assert_raises(TypeError, np.set_printoptions, wrongarg=True)
def test_edgeitems(self): np.set_printoptions(edgeitems=1, threshold=1) a = np.arange(27).reshape((3, 3, 3)) assert_equal( repr(a), textwrap.dedent("""\ array([[[ 0, ..., 2], ..., [ 6, ..., 8]], ..., [[18, ..., 20], ..., [24, ..., 26]]])""")) b = np.zeros((3, 3, 1, 1)) assert_equal( repr(b), textwrap.dedent("""\ array([[[[0.]], ..., [[0.]]], ..., [[[0.]], ..., [[0.]]]])""")) # 1.13 had extra trailing spaces, and was missing newlines np.set_printoptions(legacy='1.13') assert_equal( repr(a), textwrap.dedent("""\ array([[[ 0, ..., 2], ..., [ 6, ..., 8]], ..., [[18, ..., 20], ..., [24, ..., 26]]])""")) assert_equal( repr(b), textwrap.dedent("""\ array([[[[ 0.]], ..., [[ 0.]]], ..., [[[ 0.]], ..., [[ 0.]]]])"""))
def afterContext(self): numpy1.set_printoptions(**print_state)
def test_basic(self): x = np.array([1.5, 0, 1.234567890]) assert_equal(repr(x), "array([1.5 , 0. , 1.23456789])") np.set_printoptions(precision=4) assert_equal(repr(x), "array([1.5 , 0. , 1.2346])")
def test_formatter(self): x = np.arange(3) np.set_printoptions(formatter={'all': lambda x: str(x - 1)}) assert_equal(repr(x), "array([-1, 0, 1])")
def test_formatter_reset(self): x = np.arange(3) np.set_printoptions(formatter={'all': lambda x: str(x - 1)}) assert_equal(repr(x), "array([-1, 0, 1])") np.set_printoptions(formatter={'int': None}) assert_equal(repr(x), "array([0, 1, 2])") np.set_printoptions(formatter={'all': lambda x: str(x - 1)}) assert_equal(repr(x), "array([-1, 0, 1])") np.set_printoptions(formatter={'all': None}) assert_equal(repr(x), "array([0, 1, 2])") np.set_printoptions(formatter={'int': lambda x: str(x - 1)}) assert_equal(repr(x), "array([-1, 0, 1])") np.set_printoptions(formatter={'int_kind': None}) assert_equal(repr(x), "array([0, 1, 2])") x = np.arange(3.) np.set_printoptions(formatter={'float': lambda x: str(x - 1)}) assert_equal(repr(x), "array([-1.0, 0.0, 1.0])") np.set_printoptions(formatter={'float_kind': None}) assert_equal(repr(x), "array([0., 1., 2.])")
def teardown(self): np.set_printoptions(**self.oldopts)
def test_floatmode(self): x = np.array([ 0.6104, 0.922, 0.457, 0.0906, 0.3733, 0.007244, 0.5933, 0.947, 0.2383, 0.4226 ], dtype=np.float16) y = np.array([ 0.2918820979355541, 0.5064172631089138, 0.2848750619642916, 0.4342965294660567, 0.7326538397312751, 0.3459503329096204, 0.0862072768214508, 0.39112753029631175 ], dtype=np.float64) z = np.arange(6, dtype=np.float16) / 10 c = np.array([1.0 + 1.0j, 1.123456789 + 1.123456789j], dtype='c16') # also make sure 1e23 is right (is between two fp numbers) w = np.array(['1e{}'.format(i) for i in range(25)], dtype=np.float64) # note: we construct w from the strings `1eXX` instead of doing # `10.**arange(24)` because it turns out the two are not equivalent in # python. On some architectures `1e23 != 10.**23`. wp = np.array([1.234e1, 1e2, 1e123]) # unique mode np.set_printoptions(floatmode='unique') assert_equal( repr(x), "array([0.6104 , 0.922 , 0.457 , 0.0906 , 0.3733 , 0.007244,\n" " 0.5933 , 0.947 , 0.2383 , 0.4226 ], dtype=float16)") assert_equal( repr(y), "array([0.2918820979355541 , 0.5064172631089138 , 0.2848750619642916 ,\n" " 0.4342965294660567 , 0.7326538397312751 , 0.3459503329096204 ,\n" " 0.0862072768214508 , 0.39112753029631175])") assert_equal(repr(z), "array([0. , 0.1, 0.2, 0.3, 0.4, 0.5], dtype=float16)") assert_equal( repr(w), "array([1.e+00, 1.e+01, 1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07,\n" " 1.e+08, 1.e+09, 1.e+10, 1.e+11, 1.e+12, 1.e+13, 1.e+14, 1.e+15,\n" " 1.e+16, 1.e+17, 1.e+18, 1.e+19, 1.e+20, 1.e+21, 1.e+22, 1.e+23,\n" " 1.e+24])") assert_equal(repr(wp), "array([1.234e+001, 1.000e+002, 1.000e+123])") assert_equal( repr(c), "array([1. +1.j , 1.123456789+1.123456789j])") # maxprec mode, precision=8 np.set_printoptions(floatmode='maxprec', precision=8) assert_equal( repr(x), "array([0.6104 , 0.922 , 0.457 , 0.0906 , 0.3733 , 0.007244,\n" " 0.5933 , 0.947 , 0.2383 , 0.4226 ], dtype=float16)") assert_equal( repr(y), "array([0.2918821 , 0.50641726, 0.28487506, 0.43429653, 0.73265384,\n" " 0.34595033, 0.08620728, 0.39112753])") assert_equal(repr(z), "array([0. , 0.1, 0.2, 0.3, 0.4, 0.5], dtype=float16)") assert_equal(repr(w[::5]), "array([1.e+00, 1.e+05, 1.e+10, 1.e+15, 1.e+20])") assert_equal(repr(wp), "array([1.234e+001, 1.000e+002, 1.000e+123])") assert_equal( repr(c), "array([1. +1.j , 1.12345679+1.12345679j])") # fixed mode, precision=4 np.set_printoptions(floatmode='fixed', precision=4) assert_equal( repr(x), "array([0.6104, 0.9219, 0.4570, 0.0906, 0.3733, 0.0072, 0.5933, 0.9468,\n" " 0.2383, 0.4226], dtype=float16)") assert_equal( repr(y), "array([0.2919, 0.5064, 0.2849, 0.4343, 0.7327, 0.3460, 0.0862, 0.3911])" ) assert_equal( repr(z), "array([0.0000, 0.1000, 0.2000, 0.3000, 0.3999, 0.5000], dtype=float16)" ) assert_equal( repr(w[::5]), "array([1.0000e+00, 1.0000e+05, 1.0000e+10, 1.0000e+15, 1.0000e+20])" ) assert_equal(repr(wp), "array([1.2340e+001, 1.0000e+002, 1.0000e+123])") assert_equal(repr(np.zeros(3)), "array([0.0000, 0.0000, 0.0000])") assert_equal(repr(c), "array([1.0000+1.0000j, 1.1235+1.1235j])") # for larger precision, representation error becomes more apparent: np.set_printoptions(floatmode='fixed', precision=8) assert_equal( repr(z), "array([0.00000000, 0.09997559, 0.19995117, 0.30004883, 0.39990234,\n" " 0.50000000], dtype=float16)") # maxprec_equal mode, precision=8 np.set_printoptions(floatmode='maxprec_equal', precision=8) assert_equal( repr(x), "array([0.610352, 0.921875, 0.457031, 0.090576, 0.373291, 0.007244,\n" " 0.593262, 0.946777, 0.238281, 0.422607], dtype=float16)") assert_equal( repr(y), "array([0.29188210, 0.50641726, 0.28487506, 0.43429653, 0.73265384,\n" " 0.34595033, 0.08620728, 0.39112753])") assert_equal(repr(z), "array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5], dtype=float16)") assert_equal(repr(w[::5]), "array([1.e+00, 1.e+05, 1.e+10, 1.e+15, 1.e+20])") assert_equal(repr(wp), "array([1.234e+001, 1.000e+002, 1.000e+123])") assert_equal( repr(c), "array([1.00000000+1.00000000j, 1.12345679+1.12345679j])")