Exemplo n.º 1
0
 def test_skip_footer_with_invalid(self):
     warn_ctx = WarningManager()
     warn_ctx.__enter__()
     try:
         basestr = '1 1\n2 2\n3 3\n4 4\n5  \n6  \n7  \n'
         warnings.filterwarnings("ignore")
         # Footer too small to get rid of all invalid values
         assert_raises(ValueError,
                       textadapter.genfromtxt,
                       StringIO(basestr),
                       skip_footer=1)
         a = textadapter.genfromtxt(StringIO(basestr),
                                    skip_footer=1,
                                    invalid_raise=False)
         assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
         #
         a = textadapter.genfromtxt(StringIO(basestr), skip_footer=3)
         assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
         #
         basestr = '1 1\n2  \n3 3\n4 4\n5  \n6 6\n7 7\n'
         a = textadapter.genfromtxt(StringIO(basestr),
                                    skip_footer=1,
                                    invalid_raise=False)
         assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]]))
         a = textadapter.genfromtxt(StringIO(basestr),
                                    skip_footer=3,
                                    invalid_raise=False)
         assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]]))
     finally:
         warn_ctx.__exit__()
Exemplo n.º 2
0
    def test_inconsistent_dtype(self):
        "Test inconsistent dtype"
        data = ["1, 1, 1, 1, -1.1"] * 50
        mdata = StringIO("\n".join(data))

        converters = {4: lambda x:"(%s)" % x}
        kwargs = dict(delimiter=",", converters=converters,
                      dtype=[(_, int) for _ in 'abcde'],)
        assert_raises(ValueError, np.genfromtxt, mdata, **kwargs)
Exemplo n.º 3
0
    def test_ndmin_keyword(self):
        c = StringIO()
        c.write('1,2,3\n4,5,6')
        c.seek(0)
        assert_raises(textadapter.DataTypeError,
                      textadapter.loadtxt,
                      c,
                      ndmin=3)
        c.seek(0)
        assert_raises(textadapter.DataTypeError,
                      textadapter.loadtxt,
                      c,
                      ndmin=1.5)
        c.seek(0)
        x = textadapter.loadtxt(c, dtype=int, delimiter=',', ndmin=1)
        a = np.array([[1, 2, 3], [4, 5, 6]])
        assert_array_equal(x, a)
        d = StringIO()
        d.write('0,1,2')
        d.seek(0)
        x = textadapter.loadtxt(d, dtype=int, delimiter=',', ndmin=2)
        assert_(x.shape == (1, 3))
        d.seek(0)
        x = textadapter.loadtxt(d, dtype=int, delimiter=',', ndmin=1)
        assert_(x.shape == (3, ))
        d.seek(0)
        x = textadapter.loadtxt(d, dtype=int, delimiter=',', ndmin=0)
        assert_(x.shape == (3, ))
        e = StringIO()
        e.write('0\n1\n2')
        e.seek(0)
        x = textadapter.loadtxt(e, dtype=int, delimiter=',', ndmin=2)
        assert_(x.shape == (3, 1))
        e.seek(0)
        x = textadapter.loadtxt(e, dtype=int, delimiter=',', ndmin=1)
        assert_(x.shape == (3, ))
        e.seek(0)
        x = textadapter.loadtxt(e, dtype=int, delimiter=',', ndmin=0)
        assert_(x.shape == (3, ))

        # Test ndmin kw with empty file.
        warn_ctx = WarningManager()
        warn_ctx.__enter__()
        try:
            warnings.filterwarnings("ignore",
                                    message="loadtxt: Empty input file:")
            f = StringIO()
            assert_(textadapter.loadtxt(f, ndmin=2).shape == (
                0,
                1,
            ))
            assert_(textadapter.loadtxt(f, ndmin=1).shape == (0, ))
        finally:
            warn_ctx.__exit__()
Exemplo n.º 4
0
 def test_invalid_converter(self):
     strip_rand = lambda x : float((asbytes('r') in x.lower() and x.split()[-1]) or
                                   (not asbytes('r') in x.lower() and x.strip() or 0.0))
     strip_per = lambda x : float((asbytes('%') in x.lower() and x.split()[0]) or
                                  (not asbytes('%') in x.lower() and x.strip() or 0.0))
     s = StringIO("D01N01,10/1/2003 ,1 %,R 75,400,600\r\n" \
                           "L24U05,12/5/2003, 2 %,1,300, 150.5\r\n"
                           "D02N03,10/10/2004,R 1,,7,145.55")
     kwargs = dict(converters={2 : strip_per, 3 : strip_rand}, delimiter=",",
                   dtype=None)
     assert_raises(ConverterError, np.genfromtxt, s, **kwargs)
Exemplo n.º 5
0
    def test_vs_nonmasked(self):
        x = np.array((-2, -1, 0, 1, 2, 3) * 4) ** 2
        assert_array_almost_equal(mstats.normaltest(x), stats.normaltest(x))
        assert_array_almost_equal(mstats.skewtest(x), stats.skewtest(x))
        assert_array_almost_equal(mstats.kurtosistest(x), stats.kurtosistest(x))

        funcs = [stats.normaltest, stats.skewtest, stats.kurtosistest]
        mfuncs = [mstats.normaltest, mstats.skewtest, mstats.kurtosistest]
        x = [1, 2, 3, 4]
        for func, mfunc in zip(funcs, mfuncs):
            assert_raises(ValueError, func, x)
            assert_raises(ValueError, mfunc, x)
Exemplo n.º 6
0
    def test_vs_nonmasked(self):
        x = np.array((-2,-1,0,1,2,3)*4)**2
        assert_array_almost_equal(mstats.normaltest(x), stats.normaltest(x))
        assert_array_almost_equal(mstats.skewtest(x), stats.skewtest(x))
        assert_array_almost_equal(mstats.kurtosistest(x),
                                  stats.kurtosistest(x))

        funcs = [stats.normaltest, stats.skewtest, stats.kurtosistest]
        mfuncs = [mstats.normaltest, mstats.skewtest, mstats.kurtosistest]
        x = [1, 2, 3, 4]
        for func, mfunc in zip(funcs, mfuncs):
            assert_raises(ValueError, func, x)
            assert_raises(ValueError, mfunc, x)
Exemplo n.º 7
0
    def test_skip_footer_with_invalid(self):
        import warnings
        basestr = '1 1\n2 2\n3 3\n4 4\n5  \n6  \n7  \n'
        warnings.filterwarnings("ignore")
        # Footer too small to get rid of all invalid values 
        assert_raises(ValueError, np.genfromtxt,
                      StringIO(basestr), skip_footer=1)
#        except ValueError:
#            pass
        a = np.genfromtxt(StringIO(basestr), skip_footer=1, invalid_raise=False)
        assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
        #
        a = np.genfromtxt(StringIO(basestr), skip_footer=3)
        assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
        #
        basestr = '1 1\n2  \n3 3\n4 4\n5  \n6 6\n7 7\n'
        a = np.genfromtxt(StringIO(basestr), skip_footer=1, invalid_raise=False)
        assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]]))
        a = np.genfromtxt(StringIO(basestr), skip_footer=3, invalid_raise=False)
        assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]]))
        warnings.resetwarnings()
Exemplo n.º 8
0
 def test_invalid_raise(self):
     "Test invalid raise"
     data = ["1, 1, 1, 1, 1"] * 50
     for i in range(5):
         data[10 * i] = "2, 2, 2, 2 2"
     data.insert(0, "a, b, c, d, e")
     mdata = StringIO("\n".join(data))
     #
     kwargs = dict(delimiter=",", dtype=None, names=True)
     # XXX: is there a better way to get the return value of the callable in
     # assert_warns ?
     ret = {}
     def f(_ret={}):
         _ret['mtest'] = np.ndfromtxt(mdata, invalid_raise=False, **kwargs)
     assert_warns(ConversionWarning, f, _ret=ret)
     mtest = ret['mtest']
     assert_equal(len(mtest), 45)
     assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in 'abcde']))
     #
     mdata.seek(0)
     assert_raises(ValueError, np.ndfromtxt, mdata,
                   delimiter=",", names=True)
Exemplo n.º 9
0
 def test_invalid_input_size(self):
     assert_raises(ValueError, mstats.ttest_rel,
                   np.arange(10), np.arange(11))
     x = np.arange(24)
     assert_raises(ValueError, mstats.ttest_rel,
                   x.reshape(2, 3, 4), x.reshape(2, 4, 3), axis=1)
     assert_raises(ValueError, mstats.ttest_rel,
                   x.reshape(2, 3, 4), x.reshape(2, 4, 3), axis=2)
Exemplo n.º 10
0
 def test_invalid_input_size(self):
     assert_raises(ValueError, mstats.ttest_rel,
                   np.arange(10), np.arange(11))
     x = np.arange(24)
     assert_raises(ValueError, mstats.ttest_rel,
                   x.reshape(2, 3, 4), x.reshape(2, 4, 3), axis=1)
     assert_raises(ValueError, mstats.ttest_rel,
                   x.reshape(2, 3, 4), x.reshape(2, 4, 3), axis=2)
Exemplo n.º 11
0
 def test_matrix_builder(self):
     assert_raises(np.ma.MAError, lambda: mr_['1, 2; 3, 4'])
Exemplo n.º 12
0
 def test_matrix_builder(self):
     assert_raises(np.ma.MAError, lambda: mr_['1, 2; 3, 4'])
Exemplo n.º 13
0
 def test_empty_file(self):
     "Test that an empty file raises the proper exception"
     data = StringIO()
     assert_raises(IOError, np.ndfromtxt, data)
Exemplo n.º 14
0
 def test_empty_file(self):
     c = StringIO()
     assert_raises(IOError, np.loadtxt, c)