Exemple #1
0
 def test_report_fail_percentage(self):
     a = np.array([1, 1, 1, 1])
     b = np.array([1, 1, 1, 2])
     try:
         assert_allclose(a, b)
         msg = ''
     except AssertionError as exc:
         msg = exc.args[0]
     assert_("mismatch 25.0%" in msg)
Exemple #2
0
 def test_equal_nan_default(self):
     # Make sure equal_nan default behavior remains unchanged. (All
     # of these functions use assert_array_compare under the hood.)
     # None of these should raise.
     a = np.array([np.nan])
     b = np.array([np.nan])
     assert_array_equal(a, b)
     assert_array_almost_equal(a, b)
     assert_array_less(a, b)
     assert_allclose(a, b)
Exemple #3
0
 def test_one_bin(self):
     # Ticket 632
     hist, edges = histogram([1, 2, 3, 4], [1, 2])
     assert_array_equal(hist, [
         2,
     ])
     assert_array_equal(edges, [1, 2])
     assert_raises(ValueError, histogram, [1, 2], bins=0)
     h, e = histogram([1, 2], bins=1)
     assert_equal(h, np.array([2]))
     assert_allclose(e, np.array([1., 2.]))
Exemple #4
0
 def test_ddof_corrcoef(self):
     # See gh-3336
     x = np.ma.masked_equal([1, 2, 3, 4, 5], 4)
     y = np.array([2, 2.5, 3.1, 3, 5])
     # this test can be removed after deprecation.
     with suppress_warnings() as sup:
         sup.filter(DeprecationWarning, "bias and ddof have no effect")
         r0 = np.ma.corrcoef(x, y, ddof=0)
         r1 = np.ma.corrcoef(x, y, ddof=1)
         # ddof should not have an effect (it gets cancelled out)
         assert_allclose(r0.data, r1.data)
Exemple #5
0
 def test_check_constant_pad_2d(self):
     arr = np.arange(4).reshape(2, 2)
     test = np.lib.pad(arr, ((1, 2), (1, 3)), mode='constant',
                       constant_values=((1, 2), (3, 4)))
     expected = np.array(
         [[3, 1, 1, 4, 4, 4],
          [3, 0, 1, 4, 4, 4],
          [3, 2, 3, 4, 4, 4],
          [3, 2, 2, 4, 4, 4],
          [3, 2, 2, 4, 4, 4]]
     )
     assert_allclose(test, expected)
Exemple #6
0
 def test_check_2d(self):
     arr = np.arange(20).reshape(4, 5).astype(np.float64)
     test = pad(arr, (2, 2), mode='linear_ramp', end_values=(0, 0))
     expected = np.array(
         [[0.,   0.,   0.,   0.,   0.,   0.,   0.,    0.,   0.],
          [0.,   0.,   0.,  0.5,   1.,  1.5,   2.,    1.,   0.],
          [0.,   0.,   0.,   1.,   2.,   3.,   4.,    2.,   0.],
          [0.,  2.5,   5.,   6.,   7.,   8.,   9.,   4.5,   0.],
          [0.,   5.,  10.,  11.,  12.,  13.,  14.,    7.,   0.],
          [0.,  7.5,  15.,  16.,  17.,  18.,  19.,   9.5,   0.],
          [0., 3.75,  7.5,   8.,  8.5,   9.,  9.5,  4.75,   0.],
          [0.,   0.,   0.,   0.,   0.,   0.,   0.,    0.,   0.]])
     assert_allclose(test, expected)
Exemple #7
0
    def test_check_constant_odd_pad_amount(self):
        arr = np.arange(30).reshape(5, 6)
        test = pad(arr, ((1,), (2,)), mode='constant',
                   constant_values=3)
        expected = np.array(
            [[ 3,  3,  3,  3,  3,  3,  3,  3,  3,  3],

             [ 3,  3,  0,  1,  2,  3,  4,  5,  3,  3],
             [ 3,  3,  6,  7,  8,  9, 10, 11,  3,  3],
             [ 3,  3, 12, 13, 14, 15, 16, 17,  3,  3],
             [ 3,  3, 18, 19, 20, 21, 22, 23,  3,  3],
             [ 3,  3, 24, 25, 26, 27, 28, 29,  3,  3],

             [ 3,  3,  3,  3,  3,  3,  3,  3,  3,  3]]
            )
        assert_allclose(test, expected)
Exemple #8
0
    def test_check_constant_float(self):
        # If input array is int, but constant_values are float, the dtype of
        # the array to be padded is kept
        arr = np.arange(30).reshape(5, 6)
        test = pad(arr, (1, 2), mode='constant',
                   constant_values=1.1)
        expected = np.array(
            [[ 1,  1,  1,  1,  1,  1,  1,  1,  1],

             [ 1,  0,  1,  2,  3,  4,  5,  1,  1],
             [ 1,  6,  7,  8,  9, 10, 11,  1,  1],
             [ 1, 12, 13, 14, 15, 16, 17,  1,  1],
             [ 1, 18, 19, 20, 21, 22, 23,  1,  1],
             [ 1, 24, 25, 26, 27, 28, 29,  1,  1],

             [ 1,  1,  1,  1,  1,  1,  1,  1,  1],
             [ 1,  1,  1,  1,  1,  1,  1,  1,  1]]
            )
        assert_allclose(test, expected)
Exemple #9
0
    def test_check_constant_float2(self):
        # If input array is float, and constant_values are float, the dtype of
        # the array to be padded is kept - here retaining the float constants
        arr = np.arange(30).reshape(5, 6)
        arr_float = arr.astype(np.float64)
        test = pad(arr_float, ((1, 2), (1, 2)), mode='constant',
                   constant_values=1.1)
        expected = np.array(
            [[  1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1],

             [  1.1,   0. ,   1. ,   2. ,   3. ,   4. ,   5. ,   1.1,   1.1],
             [  1.1,   6. ,   7. ,   8. ,   9. ,  10. ,  11. ,   1.1,   1.1],
             [  1.1,  12. ,  13. ,  14. ,  15. ,  16. ,  17. ,   1.1,   1.1],
             [  1.1,  18. ,  19. ,  20. ,  21. ,  22. ,  23. ,   1.1,   1.1],
             [  1.1,  24. ,  25. ,  26. ,  27. ,  28. ,  29. ,   1.1,   1.1],

             [  1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1],
             [  1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1,   1.1]]
            )
        assert_allclose(test, expected)
Exemple #10
0
    def test_basic(self):
        y = geomspace(1, 1e6)
        assert_(len(y) == 50)
        y = geomspace(1, 1e6, num=100)
        assert_(y[-1] == 10**6)
        y = geomspace(1, 1e6, endpoint=False)
        assert_(y[-1] < 10**6)
        y = geomspace(1, 1e6, num=7)
        assert_array_equal(y, [1, 10, 100, 1e3, 1e4, 1e5, 1e6])

        y = geomspace(8, 2, num=3)
        assert_allclose(y, [8, 4, 2])
        assert_array_equal(y.imag, 0)

        y = geomspace(-1, -100, num=3)
        assert_array_equal(y, [-1, -10, -100])
        assert_array_equal(y.imag, 0)

        y = geomspace(-100, -1, num=3)
        assert_array_equal(y, [-100, -10, -1])
        assert_array_equal(y.imag, 0)
Exemple #11
0
    def test_check_constant_float3(self):
        a = np.arange(100, dtype=float)
        a = pad(a, (25, 20), 'constant', constant_values=(-1.1, -1.2))
        b = np.array(
            [-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
             -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
             -1.1, -1.1, -1.1, -1.1, -1.1,

             0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
             10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
             20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
             30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
             40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
             50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
             60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
             70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
             80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
             90, 91, 92, 93, 94, 95, 96, 97, 98, 99,

             -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2,
             -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2]
            )
        assert_allclose(a, b)
Exemple #12
0
    def test_check_simple(self):
        a = np.arange(100).astype('f')
        a = pad(a, (25, 20), 'linear_ramp', end_values=(4, 5))
        b = np.array(
            [4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56,
             2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96,
             0.80, 0.64, 0.48, 0.32, 0.16,

             0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00,
             10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0,
             20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0,
             30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0,
             40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0,
             50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0,
             60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0,
             70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0,
             80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0,
             90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0,

             94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0,
             47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.]
            )
        assert_allclose(a, b, rtol=1e-5, atol=1e-5)
Exemple #13
0
    def test_upgrade(self):
        "Tests the upgrade method."

        converter = StringConverter()
        assert_equal(converter._status, 0)

        # test int
        assert_equal(converter.upgrade('0'), 0)
        assert_equal(converter._status, 1)

        # On systems where long defaults to 32-bit, the statuses will be
        # offset by one, so we check for this here.
        import numpy1.core.numeric as nx
        status_offset = int(
            nx.dtype(nx.int_).itemsize < nx.dtype(nx.int64).itemsize)

        # test int > 2**32
        assert_equal(converter.upgrade('17179869184'), 17179869184)
        assert_equal(converter._status, 1 + status_offset)

        # test float
        assert_allclose(converter.upgrade('0.'), 0.0)
        assert_equal(converter._status, 2 + status_offset)

        # test complex
        assert_equal(converter.upgrade('0j'), complex('0j'))
        assert_equal(converter._status, 3 + status_offset)

        # test str
        # note that the longdouble type has been skipped, so the
        # _status increases by 2. Everything should succeed with
        # unicode conversion (5).
        for s in ['a', u'a', b'a']:
            res = converter.upgrade(s)
            assert_(type(res) is unicode)
            assert_equal(res, u'a')
            assert_equal(converter._status, 5 + status_offset)
Exemple #14
0
 def test_inf_edges(self):
     # Test using +/-inf bin edges works. See #1788.
     with np.errstate(invalid='ignore'):
         x = np.arange(6).reshape(3, 2)
         expected = np.array([[1, 0], [0, 1], [0, 1]])
         h, e = np.histogramdd(x, bins=[3, [-np.inf, 2, 10]])
         assert_allclose(h, expected)
         h, e = np.histogramdd(x, bins=[3, np.array([-1, 2, np.inf])])
         assert_allclose(h, expected)
         h, e = np.histogramdd(x, bins=[3, [-np.inf, 3, np.inf]])
         assert_allclose(h, expected)
Exemple #15
0
    def test_simple(self):
        x = 1e-3
        y = 1e-9

        assert_allclose(x, y, atol=1)
        assert_raises(AssertionError, assert_allclose, x, y)

        a = np.array([x, y, x, y])
        b = np.array([x, y, x, x])

        assert_allclose(a, b, atol=1)
        assert_raises(AssertionError, assert_allclose, a, b)

        b[-1] = y * (1 + 1e-8)
        assert_allclose(a, b)
        assert_raises(AssertionError, assert_allclose, a, b, rtol=1e-9)

        assert_allclose(6, 10, rtol=0.5)
        assert_raises(AssertionError, assert_allclose, 10, 6, rtol=0.5)
Exemple #16
0
 def test_pmt(self):
     res = np.pmt(0.08 / 12, 5 * 12, 15000)
     tgt = -304.145914
     assert_allclose(res, tgt)
     # Test the edge case where rate == 0.0
     res = np.pmt(0.0, 5 * 12, 15000)
     tgt = -250.0
     assert_allclose(res, tgt)
     # Test the case where we use broadcast and
     # the arguments passed in are arrays.
     res = np.pmt([[0.0, 0.8], [0.3, 0.8]], [12, 3], [2000, 20000])
     tgt = np.array([[-166.66667, -19311.258], [-626.90814, -19311.258]])
     assert_allclose(res, tgt)
Exemple #17
0
    def test_array_scalar(self):
        lim1 = array([120, 100], dtype="int8")
        lim2 = array([-120, -100], dtype="int8")
        lim3 = array([1200, 1000], dtype="uint16")
        t1 = geomspace(lim1[0], lim1[1], 5)
        t2 = geomspace(lim2[0], lim2[1], 5)
        t3 = geomspace(lim3[0], lim3[1], 5)
        t4 = geomspace(120.0, 100.0, 5)
        t5 = geomspace(-120.0, -100.0, 5)
        t6 = geomspace(1200.0, 1000.0, 5)

        # t3 uses float32, t6 uses float64
        assert_allclose(t1, t4, rtol=1e-2)
        assert_allclose(t2, t5, rtol=1e-2)
        assert_allclose(t3, t6, rtol=1e-5)
Exemple #18
0
 def test_equal_nan(self):
     a = np.array([np.nan])
     b = np.array([np.nan])
     # Should not raise:
     assert_allclose(a, b, equal_nan=True)
Exemple #19
0
    def test_complex(self):
        # Purely imaginary
        y = geomspace(1j, 16j, num=5)
        assert_allclose(y, [1j, 2j, 4j, 8j, 16j])
        assert_array_equal(y.real, 0)

        y = geomspace(-4j, -324j, num=5)
        assert_allclose(y, [-4j, -12j, -36j, -108j, -324j])
        assert_array_equal(y.real, 0)

        y = geomspace(1 + 1j, 1000 + 1000j, num=4)
        assert_allclose(y, [1 + 1j, 10 + 10j, 100 + 100j, 1000 + 1000j])

        y = geomspace(-1 + 1j, -1000 + 1000j, num=4)
        assert_allclose(y, [-1 + 1j, -10 + 10j, -100 + 100j, -1000 + 1000j])

        # Logarithmic spirals
        y = geomspace(-1, 1, num=3, dtype=complex)
        assert_allclose(y, [-1, 1j, +1])

        y = geomspace(0 + 3j, -3 + 0j, 3)
        assert_allclose(y, [0 + 3j, -3 / sqrt(2) + 3j / sqrt(2), -3 + 0j])
        y = geomspace(0 + 3j, 3 + 0j, 3)
        assert_allclose(y, [0 + 3j, 3 / sqrt(2) + 3j / sqrt(2), 3 + 0j])
        y = geomspace(-3 + 0j, 0 - 3j, 3)
        assert_allclose(y, [-3 + 0j, -3 / sqrt(2) - 3j / sqrt(2), 0 - 3j])
        y = geomspace(0 + 3j, -3 + 0j, 3)
        assert_allclose(y, [0 + 3j, -3 / sqrt(2) + 3j / sqrt(2), -3 + 0j])
        y = geomspace(-2 - 3j, 5 + 7j, 7)
        assert_allclose(y, [
            -2 - 3j, -0.29058977 - 4.15771027j, 2.08885354 - 4.34146838j,
            4.58345529 - 3.16355218j, 6.41401745 - 0.55233457j,
            6.75707386 + 3.11795092j, 5 + 7j
        ])

        # Type promotion should prevent the -5 from becoming a NaN
        y = geomspace(3j, -5, 2)
        assert_allclose(y, [3j, -5])
        y = geomspace(-5, 3j, 2)
        assert_allclose(y, [-5, 3j])
Exemple #20
0
 def test_min_int(self):
     a = np.array([np.iinfo(np.int_).min], dtype=np.int_)
     # Should not raise:
     assert_allclose(a, a)