Python numpy.compat.asbytes_nested() Examples

The following are code examples for showing how to use numpy.compat.asbytes_nested(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: LaserTOF   Author: kyleuckert   File: test_core.py    MIT License 6 votes vote down vote up
def test_fillvalue_conversion(self):
        # Tests the behavior of fill_value during conversion
        # We had a tailored comment to make sure special attributes are
        # properly dealt with
        a = array(asbytes_nested(['3', '4', '5']))
        a._optinfo.update({'comment':"updated!"})

        b = array(a, dtype=int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))

        b = array(a, dtype=float)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0.))

        b = a.astype(int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        assert_equal(b._optinfo['comment'], "updated!")

        b = a.astype([('a', '|S3')])
        assert_equal(b['a']._data, a._data)
        assert_equal(b['a'].fill_value, a.fill_value) 
Example 2
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_from_string_array(self):
        A = np.array(asbytes_nested([['abc', 'foo'],
                                     ['long   ', '0123456789']]))
        assert_equal(A.dtype.type, np.string_)
        B = np.char.array(A)
        assert_array_equal(B, A)
        assert_equal(B.dtype, A.dtype)
        assert_equal(B.shape, A.shape)
        B[0, 0] = 'changed'
        assert_(B[0, 0] != A[0, 0])
        C = np.char.asarray(A)
        assert_array_equal(C, A)
        assert_equal(C.dtype, A.dtype)
        C[0, 0] = 'changed again'
        assert_(C[0, 0] != B[0, 0])
        assert_(C[0, 0] == A[0, 0]) 
Example 3
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_lstrip(self):
        tgt = asbytes_nested([['abc ', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345 \0 ', 'UPPER']])
        assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
        assert_array_equal(self.A.lstrip(), tgt)

        tgt = asbytes_nested([[' abc', ''],
                              ['2345', 'ixedCase'],
                              ['23 \t 345 \x00', 'UPPER']])
        assert_array_equal(self.A.lstrip(asbytes_nested(['1', 'M'])), tgt)

        tgt = [[sixu('\u03a3 '), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345 \0 ', 'UPPER']]
        assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.lstrip(), tgt) 
Example 4
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_replace(self):
        R = self.A.replace(asbytes_nested(['3', 'a']),
                           asbytes_nested(['##########', '@']))
        tgt = asbytes_nested([[' abc ', ''],
                              ['12##########45', '[email protected]'],
                              ['12########## \t ##########45 \x00', 'UPPER']])
        assert_(issubclass(R.dtype.type, np.string_))
        assert_array_equal(R, tgt)

        if sys.version_info[0] < 3:
            # NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
            R = self.A.replace(asbytes('a'), sixu('\u03a3'))
            tgt = [[sixu(' \u03a3bc '), ''],
                   ['12345', sixu('MixedC\u03a3se')],
                   ['123 \t 345 \x00', 'UPPER']]
            assert_(issubclass(R.dtype.type, np.unicode_))
            assert_array_equal(R, tgt) 
Example 5
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_rjust(self):
        assert_(issubclass(self.A.rjust(10).dtype.type, np.string_))

        C = self.A.rjust([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])

        C = self.A.rjust(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_array_equal(C.endswith(asbytes('#')),
                           [[False, True], [False, False], [False, False]])

        C = np.char.rjust(asbytes('FOO'), [[10, 20], [15, 8]])
        tgt = asbytes_nested([['       FOO', '                 FOO'],
                              ['            FOO', '     FOO']])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, tgt) 
Example 6
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_rstrip(self):
        assert_(issubclass(self.A.rstrip().dtype.type, np.string_))

        tgt = asbytes_nested([[' abc', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345', 'UPPER']])
        assert_array_equal(self.A.rstrip(), tgt)

        tgt = asbytes_nested([[' abc ', ''],
                              ['1234', 'MixedCase'],
                              ['123 \t 345 \x00', 'UPP']
                              ])
        assert_array_equal(self.A.rstrip(asbytes_nested(['5', 'ER'])), tgt)

        tgt = [[sixu(' \u03a3'), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345', 'UPPER']]
        assert_(issubclass(self.B.rstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.rstrip(), tgt) 
Example 7
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_strip(self):
        tgt = asbytes_nested([['abc', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345', 'UPPER']])
        assert_(issubclass(self.A.strip().dtype.type, np.string_))
        assert_array_equal(self.A.strip(), tgt)

        tgt = asbytes_nested([[' abc ', ''],
                              ['234', 'ixedCas'],
                              ['23 \t 345 \x00', 'UPP']])
        assert_array_equal(self.A.strip(asbytes_nested(['15', 'EReM'])), tgt)

        tgt = [[sixu('\u03a3'), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345', 'UPPER']]
        assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
        assert_array_equal(self.B.strip(), tgt) 
Example 8
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_core.py    GNU General Public License v3.0 6 votes vote down vote up
def test_fillvalue_conversion(self):
        # Tests the behavior of fill_value during conversion
        # We had a tailored comment to make sure special attributes are
        # properly dealt with
        a = array(asbytes_nested(['3', '4', '5']))
        a._optinfo.update({'comment':"updated!"})
        #
        b = array(a, dtype=int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        #
        b = array(a, dtype=float)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0.))
        #
        b = a.astype(int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        assert_equal(b._optinfo['comment'], "updated!")
        #
        b = a.astype([('a', '|S3')])
        assert_equal(b['a']._data, a._data)
        assert_equal(b['a'].fill_value, a.fill_value) 
Example 9
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 6 votes vote down vote up
def test_from_string_array(self):
        A = np.array(asbytes_nested([['abc', 'foo'],
                                     ['long   ', '0123456789']]))
        assert_equal(A.dtype.type, np.string_)
        B = np.char.array(A)
        assert_array_equal(B, A)
        assert_equal(B.dtype, A.dtype)
        assert_equal(B.shape, A.shape)
        B[0, 0] = 'changed'
        assert_(B[0, 0] != A[0, 0])
        C = np.char.asarray(A)
        assert_array_equal(C, A)
        assert_equal(C.dtype, A.dtype)
        C[0, 0] = 'changed again'
        assert_(C[0, 0] != B[0, 0])
        assert_(C[0, 0] == A[0, 0]) 
Example 10
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 6 votes vote down vote up
def test_lstrip(self):
        assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
        assert_array_equal(self.A.lstrip(), asbytes_nested([
                ['abc ', ''],
                ['12345', 'MixedCase'],
                ['123 \t 345 \0 ', 'UPPER']]))
        assert_array_equal(self.A.lstrip(asbytes_nested(['1', 'M'])),
                           asbytes_nested([
                [' abc', ''],
                ['2345', 'ixedCase'],
                ['23 \t 345 \x00', 'UPPER']]))
        assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.lstrip(), [
                [sixu('\u03a3 '), ''],
                ['12345', 'MixedCase'],
                ['123 \t 345 \0 ', 'UPPER']]) 
Example 11
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 6 votes vote down vote up
def test_replace(self):
        R = self.A.replace(asbytes_nested(['3', 'a']),
                           asbytes_nested(['##########', '@']))
        assert_(issubclass(R.dtype.type, np.string_))
        assert_array_equal(R, asbytes_nested([
                [' abc ', ''],
                ['12##########45', '[email protected]'],
                ['12########## \t ##########45 \x00', 'UPPER']]))

        if sys.version_info[0] < 3:
            # NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
            R = self.A.replace(asbytes('a'), sixu('\u03a3'))
            assert_(issubclass(R.dtype.type, np.unicode_))
            assert_array_equal(R, [
                    [sixu(' \u03a3bc '), ''],
                    ['12345', sixu('MixedC\u03a3se')],
                    ['123 \t 345 \x00', 'UPPER']]) 
Example 12
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 6 votes vote down vote up
def test_rstrip(self):
        assert_(issubclass(self.A.rstrip().dtype.type, np.string_))
        assert_array_equal(self.A.rstrip(), asbytes_nested([
                [' abc', ''],
                ['12345', 'MixedCase'],
                ['123 \t 345', 'UPPER']]))
        assert_array_equal(self.A.rstrip(asbytes_nested(['5', 'ER'])),
                           asbytes_nested([
                [' abc ', ''],
                ['1234', 'MixedCase'],
                ['123 \t 345 \x00', 'UPP']]))
        assert_(issubclass(self.B.rstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.rstrip(), [
                [sixu(' \u03a3'), ''],
                ['12345', 'MixedCase'],
                ['123 \t 345', 'UPPER']]) 
Example 13
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 6 votes vote down vote up
def test_strip(self):
        assert_(issubclass(self.A.strip().dtype.type, np.string_))
        assert_array_equal(self.A.strip(), asbytes_nested([
                ['abc', ''],
                ['12345', 'MixedCase'],
                ['123 \t 345', 'UPPER']]))
        assert_array_equal(self.A.strip(asbytes_nested(['15', 'EReM'])),
                           asbytes_nested([
                [' abc ', ''],
                ['234', 'ixedCas'],
                ['23 \t 345 \x00', 'UPP']]))
        assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
        assert_array_equal(self.B.strip(), [
                [sixu('\u03a3'), ''],
                ['12345', 'MixedCase'],
                ['123 \t 345', 'UPPER']]) 
Example 14
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_core.py    MIT License 6 votes vote down vote up
def test_fillvalue_conversion(self):
        # Tests the behavior of fill_value during conversion
        # We had a tailored comment to make sure special attributes are
        # properly dealt with
        a = array(asbytes_nested(['3', '4', '5']))
        a._optinfo.update({'comment':"updated!"})

        b = array(a, dtype=int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))

        b = array(a, dtype=float)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0.))

        b = a.astype(int)
        assert_equal(b._data, [3, 4, 5])
        assert_equal(b.fill_value, default_fill_value(0))
        assert_equal(b._optinfo['comment'], "updated!")

        b = a.astype([('a', '|S3')])
        assert_equal(b['a']._data, a._data)
        assert_equal(b['a'].fill_value, a.fill_value) 
Example 15
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_from_string_array(self):
        A = np.array(asbytes_nested([['abc', 'foo'],
                                     ['long   ', '0123456789']]))
        assert_equal(A.dtype.type, np.string_)
        B = np.char.array(A)
        assert_array_equal(B, A)
        assert_equal(B.dtype, A.dtype)
        assert_equal(B.shape, A.shape)
        B[0, 0] = 'changed'
        assert_(B[0, 0] != A[0, 0])
        C = np.char.asarray(A)
        assert_array_equal(C, A)
        assert_equal(C.dtype, A.dtype)
        C[0, 0] = 'changed again'
        assert_(C[0, 0] != B[0, 0])
        assert_(C[0, 0] == A[0, 0]) 
Example 16
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_lstrip(self):
        tgt = asbytes_nested([['abc ', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345 \0 ', 'UPPER']])
        assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
        assert_array_equal(self.A.lstrip(), tgt)

        tgt = asbytes_nested([[' abc', ''],
                              ['2345', 'ixedCase'],
                              ['23 \t 345 \x00', 'UPPER']])
        assert_array_equal(self.A.lstrip(asbytes_nested(['1', 'M'])), tgt)

        tgt = [[sixu('\u03a3 '), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345 \0 ', 'UPPER']]
        assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.lstrip(), tgt) 
Example 17
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_replace(self):
        R = self.A.replace(asbytes_nested(['3', 'a']),
                           asbytes_nested(['##########', '@']))
        tgt = asbytes_nested([[' abc ', ''],
                              ['12##########45', '[email protected]'],
                              ['12########## \t ##########45 \x00', 'UPPER']])
        assert_(issubclass(R.dtype.type, np.string_))
        assert_array_equal(R, tgt)

        if sys.version_info[0] < 3:
            # NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
            R = self.A.replace(asbytes('a'), sixu('\u03a3'))
            tgt = [[sixu(' \u03a3bc '), ''],
                   ['12345', sixu('MixedC\u03a3se')],
                   ['123 \t 345 \x00', 'UPPER']]
            assert_(issubclass(R.dtype.type, np.unicode_))
            assert_array_equal(R, tgt) 
Example 18
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_rjust(self):
        assert_(issubclass(self.A.rjust(10).dtype.type, np.string_))

        C = self.A.rjust([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])

        C = self.A.rjust(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_array_equal(C.endswith(asbytes('#')),
                           [[False, True], [False, False], [False, False]])

        C = np.char.rjust(asbytes('FOO'), [[10, 20], [15, 8]])
        tgt = asbytes_nested([['       FOO', '                 FOO'],
                              ['            FOO', '     FOO']])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, tgt) 
Example 19
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_rstrip(self):
        assert_(issubclass(self.A.rstrip().dtype.type, np.string_))

        tgt = asbytes_nested([[' abc', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345', 'UPPER']])
        assert_array_equal(self.A.rstrip(), tgt)

        tgt = asbytes_nested([[' abc ', ''],
                              ['1234', 'MixedCase'],
                              ['123 \t 345 \x00', 'UPP']
                              ])
        assert_array_equal(self.A.rstrip(asbytes_nested(['5', 'ER'])), tgt)

        tgt = [[sixu(' \u03a3'), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345', 'UPPER']]
        assert_(issubclass(self.B.rstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.rstrip(), tgt) 
Example 20
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 6 votes vote down vote up
def test_strip(self):
        tgt = asbytes_nested([['abc', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345', 'UPPER']])
        assert_(issubclass(self.A.strip().dtype.type, np.string_))
        assert_array_equal(self.A.strip(), tgt)

        tgt = asbytes_nested([[' abc ', ''],
                              ['234', 'ixedCas'],
                              ['23 \t 345 \x00', 'UPP']])
        assert_array_equal(self.A.strip(asbytes_nested(['15', 'EReM'])), tgt)

        tgt = [[sixu('\u03a3'), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345', 'UPPER']]
        assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
        assert_array_equal(self.B.strip(), tgt) 
Example 21
Project: LaserTOF   Author: kyleuckert   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_no_delimiter(self):
        "Test LineSplitter w/o delimiter"
        strg = asbytes(" 1 2 3 4  5 # test")
        test = LineSplitter()(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5']))
        test = LineSplitter('')(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5'])) 
Example 22
Project: LaserTOF   Author: kyleuckert   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_space_delimiter(self):
        "Test space delimiter"
        strg = asbytes(" 1 2 3 4  5 # test")
        test = LineSplitter(asbytes(' '))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
        test = LineSplitter(asbytes('  '))(strg)
        assert_equal(test, asbytes_nested(['1 2 3 4', '5'])) 
Example 23
Project: LaserTOF   Author: kyleuckert   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_tab_delimiter(self):
        "Test tab delimiter"
        strg = asbytes(" 1\t 2\t 3\t 4\t 5  6")
        test = LineSplitter(asbytes('\t'))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5  6']))
        strg = asbytes(" 1  2\t 3  4\t 5  6")
        test = LineSplitter(asbytes('\t'))(strg)
        assert_equal(test, asbytes_nested(['1  2', '3  4', '5  6'])) 
Example 24
Project: LaserTOF   Author: kyleuckert   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_other_delimiter(self):
        "Test LineSplitter on delimiter"
        strg = asbytes("1,2,3,4,,5")
        test = LineSplitter(asbytes(','))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
        #
        strg = asbytes(" 1,2,3,4,,5 # test")
        test = LineSplitter(asbytes(','))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5'])) 
Example 25
Project: LaserTOF   Author: kyleuckert   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_constant_fixed_width(self):
        "Test LineSplitter w/ fixed-width fields"
        strg = asbytes("  1  2  3  4     5   # test")
        test = LineSplitter(3)(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5', '']))
        #
        strg = asbytes("  1     3  4  5  6# test")
        test = LineSplitter(20)(strg)
        assert_equal(test, asbytes_nested(['1     3  4  5  6']))
        #
        strg = asbytes("  1     3  4  5  6# test")
        test = LineSplitter(30)(strg)
        assert_equal(test, asbytes_nested(['1     3  4  5  6'])) 
Example 26
Project: LaserTOF   Author: kyleuckert   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_keep_missing_values(self):
        "Check that we're not losing missing values"
        converter = StringConverter(int, default=0,
                                    missing_values=asbytes("N/A"))
        assert_equal(
            converter.missing_values, set(asbytes_nested(['', 'N/A']))) 
Example 27
Project: LaserTOF   Author: kyleuckert   File: test_mrecords.py    MIT License 5 votes vote down vote up
def setup(self):
        # Generic setup
        ilist = [1, 2, 3, 4, 5]
        flist = [1.1, 2.2, 3.3, 4.4, 5.5]
        slist = asbytes_nested(['one', 'two', 'three', 'four', 'five'])
        ddtype = [('a', int), ('b', float), ('c', '|S8')]
        mask = [0, 1, 0, 0, 1]
        self.base = ma.array(list(zip(ilist, flist, slist)),
                             mask=mask, dtype=ddtype) 
Example 28
Project: LaserTOF   Author: kyleuckert   File: test_mrecords.py    MIT License 5 votes vote down vote up
def test_set_elements(self):
        base = self.base.copy()
        # Set an element to mask .....................
        mbase = base.view(mrecarray).copy()
        mbase[-2] = masked
        assert_equal(
            mbase._mask.tolist(),
            np.array([(0, 0, 0), (1, 1, 1), (0, 0, 0), (1, 1, 1), (1, 1, 1)],
                     dtype=bool))
        # Used to be mask, now it's recordmask!
        assert_equal(mbase.recordmask, [0, 1, 0, 1, 1])
        # Set slices .................................
        mbase = base.view(mrecarray).copy()
        mbase[:2] = (5, 5, 5)
        assert_equal(mbase.a._data, [5, 5, 3, 4, 5])
        assert_equal(mbase.a._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.b._data, [5., 5., 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['5', '5', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])

        mbase = base.view(mrecarray).copy()
        mbase[:2] = masked
        assert_equal(mbase.a._data, [1, 2, 3, 4, 5])
        assert_equal(mbase.a._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['one', 'two', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1]) 
Example 29
Project: LaserTOF   Author: kyleuckert   File: test_mrecords.py    MIT License 5 votes vote down vote up
def test_setslices_hardmask(self):
        # Tests setting slices w/ hardmask.
        base = self.base.copy()
        mbase = base.view(mrecarray)
        mbase.harden_mask()
        try:
            mbase[-2:] = (5, 5, 5)
            assert_equal(mbase.a._data, [1, 2, 3, 5, 5])
            assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 5, 5.5])
            assert_equal(mbase.c._data,
                         asbytes_nested(['one', 'two', 'three', '5', 'five']))
            assert_equal(mbase.a._mask, [0, 1, 0, 0, 1])
            assert_equal(mbase.b._mask, mbase.a._mask)
            assert_equal(mbase.b._mask, mbase.c._mask)
        except NotImplementedError:
            # OK, not implemented yet...
            pass
        except AssertionError:
            raise
        else:
            raise Exception("Flexible hard masks should be supported !")
        # Not using a tuple should crash
        try:
            mbase[-2:] = 3
        except (NotImplementedError, TypeError):
            pass
        else:
            raise TypeError("Should have expected a readable buffer object!") 
Example 30
Project: LaserTOF   Author: kyleuckert   File: test_core.py    MIT License 5 votes vote down vote up
def test_set_record_slice(self):
        base = self.data['base']
        (base_a, base_b, base_c) = (base['a'], base['b'], base['c'])
        base[:3] = (pi, pi, 'pi')

        assert_equal(base_a.dtype, int)
        assert_equal(base_a._data, [3, 3, 3, 4, 5])

        assert_equal(base_b.dtype, float)
        assert_equal(base_b._data, [pi, pi, pi, 4.4, 5.5])

        assert_equal(base_c.dtype, '|S8')
        assert_equal(base_c._data,
                     asbytes_nested(['pi', 'pi', 'pi', 'four', 'five'])) 
Example 31
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_from_object_array(self):
        A = np.array([['abc', 2],
                      ['long   ', '0123456789']], dtype='O')
        B = np.char.array(A)
        assert_equal(B.dtype.itemsize, 10)
        assert_array_equal(B, asbytes_nested([['abc', '2'],
                                              ['long', '0123456789']])) 
Example 32
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_capitalize(self):
        tgt = asbytes_nested([[' abc ', ''],
                              ['12345', 'Mixedcase'],
                              ['123 \t 345 \0 ', 'Upper']])
        assert_(issubclass(self.A.capitalize().dtype.type, np.string_))
        assert_array_equal(self.A.capitalize(), tgt)

        tgt = [[sixu(' \u03c3 '), ''],
               ['12345', 'Mixedcase'],
               ['123 \t 345 \0 ', 'Upper']]
        assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_))
        assert_array_equal(self.B.capitalize(), tgt) 
Example 33
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_center(self):
        assert_(issubclass(self.A.center(10).dtype.type, np.string_))
        C = self.A.center([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])

        C = self.A.center(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_(np.all(C.endswith(asbytes('#'))))

        C = np.char.center(asbytes('FOO'), [[10, 20], [15, 8]])
        tgt = asbytes_nested([['   FOO    ', '        FOO         '],
                              ['      FOO      ', '  FOO   ']])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, tgt) 
Example 34
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_lower(self):
        tgt = asbytes_nested([[' abc ', ''],
                              ['12345', 'mixedcase'],
                              ['123 \t 345 \0 ', 'upper']])
        assert_(issubclass(self.A.lower().dtype.type, np.string_))
        assert_array_equal(self.A.lower(), tgt)

        tgt = [[sixu(' \u03c3 '), sixu('')],
               [sixu('12345'), sixu('mixedcase')],
               [sixu('123 \t 345 \0 '), sixu('upper')]]
        assert_(issubclass(self.B.lower().dtype.type, np.unicode_))
        assert_array_equal(self.B.lower(), tgt) 
Example 35
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_partition(self):
        P = self.A.partition(asbytes_nested(['3', 'M']))
        tgt = asbytes_nested([[(' abc ', '', ''), ('', '', '')],
                             [('12', '3', '45'), ('', 'M', 'ixedCase')],
                             [('12', '3', ' \t 345 \0 '), ('UPPER', '', '')]])
        assert_(issubclass(P.dtype.type, np.string_))
        assert_array_equal(P, tgt) 
Example 36
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_rsplit(self):
        A = self.A.rsplit(asbytes('3'))
        tgt = asbytes_nested([[[' abc '], ['']],
                              [['12', '45'], ['MixedCase']],
                              [['12', ' \t ', '45 \x00 '], ['UPPER']]])
        assert_(issubclass(A.dtype.type, np.object_))
        assert_equal(A.tolist(), tgt) 
Example 37
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_split(self):
        A = self.A.split(asbytes('3'))
        tgt = asbytes_nested([
                              [[' abc '], ['']],
                              [['12', '45'], ['MixedCase']],
                              [['12', ' \t ', '45 \x00 '], ['UPPER']]])
        assert_(issubclass(A.dtype.type, np.object_))
        assert_equal(A.tolist(), tgt) 
Example 38
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_swapcase(self):
        tgt = asbytes_nested([[' ABC ', ''],
                              ['12345', 'mIXEDcASE'],
                              ['123 \t 345 \0 ', 'upper']])
        assert_(issubclass(self.A.swapcase().dtype.type, np.string_))
        assert_array_equal(self.A.swapcase(), tgt)

        tgt = [[sixu(' \u03c3 '), sixu('')],
               [sixu('12345'), sixu('mIXEDcASE')],
               [sixu('123 \t 345 \0 '), sixu('upper')]]
        assert_(issubclass(self.B.swapcase().dtype.type, np.unicode_))
        assert_array_equal(self.B.swapcase(), tgt) 
Example 39
Project: LaserTOF   Author: kyleuckert   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_upper(self):
        tgt = asbytes_nested([[' ABC ', ''],
                              ['12345', 'MIXEDCASE'],
                              ['123 \t 345 \0 ', 'UPPER']])
        assert_(issubclass(self.A.upper().dtype.type, np.string_))
        assert_array_equal(self.A.upper(), tgt)

        tgt = [[sixu(' \u03a3 '), sixu('')],
               [sixu('12345'), sixu('MIXEDCASE')],
               [sixu('123 \t 345 \0 '), sixu('UPPER')]]
        assert_(issubclass(self.B.upper().dtype.type, np.unicode_))
        assert_array_equal(self.B.upper(), tgt) 
Example 40
Project: LaserTOF   Author: kyleuckert   File: test_regression.py    MIT License 5 votes vote down vote up
def test_char_array_creation(self, level=rlevel):
        a = np.array('123', dtype='c')
        b = np.array(asbytes_nested(['1', '2', '3']))
        assert_equal(a, b) 
Example 41
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_mrecords.py    GNU General Public License v3.0 5 votes vote down vote up
def setup(self):
        # Generic setup
        ilist = [1, 2, 3, 4, 5]
        flist = [1.1, 2.2, 3.3, 4.4, 5.5]
        slist = asbytes_nested(['one', 'two', 'three', 'four', 'five'])
        ddtype = [('a', int), ('b', float), ('c', '|S8')]
        mask = [0, 1, 0, 0, 1]
        self.base = ma.array(list(zip(ilist, flist, slist)),
                             mask=mask, dtype=ddtype) 
Example 42
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_mrecords.py    GNU General Public License v3.0 5 votes vote down vote up
def test_set_elements(self):
        base = self.base.copy()
        # Set an element to mask .....................
        mbase = base.view(mrecarray).copy()
        mbase[-2] = masked
        assert_equal(
            mbase._mask.tolist(),
            np.array([(0, 0, 0), (1, 1, 1), (0, 0, 0), (1, 1, 1), (1, 1, 1)],
                     dtype=bool))
        # Used to be mask, now it's recordmask!
        assert_equal(mbase.recordmask, [0, 1, 0, 1, 1])
        # Set slices .................................
        mbase = base.view(mrecarray).copy()
        mbase[:2] = (5, 5, 5)
        assert_equal(mbase.a._data, [5, 5, 3, 4, 5])
        assert_equal(mbase.a._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.b._data, [5., 5., 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['5', '5', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
        #
        mbase = base.view(mrecarray).copy()
        mbase[:2] = masked
        assert_equal(mbase.a._data, [1, 2, 3, 4, 5])
        assert_equal(mbase.a._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['one', 'two', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1]) 
Example 43
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_mrecords.py    GNU General Public License v3.0 5 votes vote down vote up
def test_setslices_hardmask(self):
        # Tests setting slices w/ hardmask.
        base = self.base.copy()
        mbase = base.view(mrecarray)
        mbase.harden_mask()
        try:
            mbase[-2:] = (5, 5, 5)
            assert_equal(mbase.a._data, [1, 2, 3, 5, 5])
            assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 5, 5.5])
            assert_equal(mbase.c._data,
                         asbytes_nested(['one', 'two', 'three', '5', 'five']))
            assert_equal(mbase.a._mask, [0, 1, 0, 0, 1])
            assert_equal(mbase.b._mask, mbase.a._mask)
            assert_equal(mbase.b._mask, mbase.c._mask)
        except NotImplementedError:
            # OK, not implemented yet...
            pass
        except AssertionError:
            raise
        else:
            raise Exception("Flexible hard masks should be supported !")
        # Not using a tuple should crash
        try:
            mbase[-2:] = 3
        except (NotImplementedError, TypeError):
            pass
        else:
            raise TypeError("Should have expected a readable buffer object!") 
Example 44
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_core.py    GNU General Public License v3.0 5 votes vote down vote up
def test_set_record_element(self):
        # Check setting an element of a record)
        base = self.data['base']
        (base_a, base_b, base_c) = (base['a'], base['b'], base['c'])
        base[0] = (pi, pi, 'pi')

        assert_equal(base_a.dtype, int)
        assert_equal(base_a._data, [3, 2, 3, 4, 5])

        assert_equal(base_b.dtype, float)
        assert_equal(base_b._data, [pi, 2.2, 3.3, 4.4, 5.5])

        assert_equal(base_c.dtype, '|S8')
        assert_equal(base_c._data,
                     asbytes_nested(['pi', 'two', 'three', 'four', 'five'])) 
Example 45
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_from_object_array(self):
        A = np.array([['abc', 2],
                      ['long   ', '0123456789']], dtype='O')
        B = np.char.array(A)
        assert_equal(B.dtype.itemsize, 10)
        assert_array_equal(B, asbytes_nested([['abc', '2'],
                                              ['long', '0123456789']])) 
Example 46
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_capitalize(self):
        assert_(issubclass(self.A.capitalize().dtype.type, np.string_))
        assert_array_equal(self.A.capitalize(), asbytes_nested([
                [' abc ', ''],
                ['12345', 'Mixedcase'],
                ['123 \t 345 \0 ', 'Upper']]))
        assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_))
        assert_array_equal(self.B.capitalize(), [
                [sixu(' \u03c3 '), ''],
                ['12345', 'Mixedcase'],
                ['123 \t 345 \0 ', 'Upper']]) 
Example 47
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_center(self):
        assert_(issubclass(self.A.center(10).dtype.type, np.string_))
        widths = np.array([[10, 20]])
        C = self.A.center([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
        C = self.A.center(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_(np.all(C.endswith(asbytes('#'))))
        C = np.char.center(asbytes('FOO'), [[10, 20], [15, 8]])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, asbytes_nested([
                ['   FOO    ', '        FOO         '],
                ['      FOO      ', '  FOO   ']])) 
Example 48
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_ljust(self):
        assert_(issubclass(self.A.ljust(10).dtype.type, np.string_))
        widths = np.array([[10, 20]])
        C = self.A.ljust([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
        C = self.A.ljust(20, asbytes('#'))
        assert_array_equal(C.startswith(asbytes('#')), [
                [False, True], [False, False], [False, False]])
        assert_(np.all(C.endswith(asbytes('#'))))
        C = np.char.ljust(asbytes('FOO'), [[10, 20], [15, 8]])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, asbytes_nested([
                ['FOO       ', 'FOO                 '],
                ['FOO            ', 'FOO     ']])) 
Example 49
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_partition(self):
        P = self.A.partition(asbytes_nested(['3', 'M']))
        assert_(issubclass(P.dtype.type, np.string_))
        assert_array_equal(P, asbytes_nested([
                [(' abc ', '', ''), ('', '', '')],
                [('12', '3', '45'), ('', 'M', 'ixedCase')],
                [('12', '3', ' \t 345 \0 '), ('UPPER', '', '')]])) 
Example 50
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_rjust(self):
        assert_(issubclass(self.A.rjust(10).dtype.type, np.string_))
        widths = np.array([[10, 20]])
        C = self.A.rjust([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
        C = self.A.rjust(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_array_equal(C.endswith(asbytes('#')),
                           [[False, True], [False, False], [False, False]])
        C = np.char.rjust(asbytes('FOO'), [[10, 20], [15, 8]])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, asbytes_nested([
                ['       FOO', '                 FOO'],
                ['            FOO', '     FOO']])) 
Example 51
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_rpartition(self):
        P = self.A.rpartition(asbytes_nested(['3', 'M']))
        assert_(issubclass(P.dtype.type, np.string_))
        assert_array_equal(P, asbytes_nested([
                [('', '', ' abc '), ('', '', '')],
                [('12', '3', '45'), ('', 'M', 'ixedCase')],
                [('123 \t ', '3', '45 \0 '), ('', '', 'UPPER')]])) 
Example 52
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_split(self):
        A = self.A.split(asbytes('3'))
        assert_(issubclass(A.dtype.type, np.object_))
        assert_equal(A.tolist(), asbytes_nested([
                [[' abc '], ['']],
                [['12', '45'], ['MixedCase']],
                [['12', ' \t ', '45 \x00 '], ['UPPER']]])) 
Example 53
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_swapcase(self):
        assert_(issubclass(self.A.swapcase().dtype.type, np.string_))
        assert_array_equal(self.A.swapcase(), asbytes_nested([
                [' ABC ', ''],
                ['12345', 'mIXEDcASE'],
                ['123 \t 345 \0 ', 'upper']]))
        assert_(issubclass(self.B.swapcase().dtype.type, np.unicode_))
        assert_array_equal(self.B.swapcase(), [
                [sixu(' \u03c3 '), sixu('')],
                [sixu('12345'), sixu('mIXEDcASE')],
                [sixu('123 \t 345 \0 '), sixu('upper')]]) 
Example 54
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_defchararray.py    GNU General Public License v3.0 5 votes vote down vote up
def test_title(self):
        assert_(issubclass(self.A.title().dtype.type, np.string_))
        assert_array_equal(self.A.title(), asbytes_nested([
                [' Abc ', ''],
                ['12345', 'Mixedcase'],
                ['123 \t 345 \0 ', 'Upper']]))
        assert_(issubclass(self.B.title().dtype.type, np.unicode_))
        assert_array_equal(self.B.title(), [
                [sixu(' \u03a3 '), sixu('')],
                [sixu('12345'), sixu('Mixedcase')],
                [sixu('123 \t 345 \0 '), sixu('Upper')]]) 
Example 55
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_regression.py    GNU General Public License v3.0 5 votes vote down vote up
def test_char_array_creation(self, level=rlevel):
        a = np.array('123', dtype='c')
        b = np.array(asbytes_nested(['1', '2', '3']))
        assert_equal(a, b) 
Example 56
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_no_delimiter(self):
        "Test LineSplitter w/o delimiter"
        strg = asbytes(" 1 2 3 4  5 # test")
        test = LineSplitter()(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5']))
        test = LineSplitter('')(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5'])) 
Example 57
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_space_delimiter(self):
        "Test space delimiter"
        strg = asbytes(" 1 2 3 4  5 # test")
        test = LineSplitter(asbytes(' '))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
        test = LineSplitter(asbytes('  '))(strg)
        assert_equal(test, asbytes_nested(['1 2 3 4', '5'])) 
Example 58
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_tab_delimiter(self):
        "Test tab delimiter"
        strg = asbytes(" 1\t 2\t 3\t 4\t 5  6")
        test = LineSplitter(asbytes('\t'))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5  6']))
        strg = asbytes(" 1  2\t 3  4\t 5  6")
        test = LineSplitter(asbytes('\t'))(strg)
        assert_equal(test, asbytes_nested(['1  2', '3  4', '5  6'])) 
Example 59
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_other_delimiter(self):
        "Test LineSplitter on delimiter"
        strg = asbytes("1,2,3,4,,5")
        test = LineSplitter(asbytes(','))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
        #
        strg = asbytes(" 1,2,3,4,,5 # test")
        test = LineSplitter(asbytes(','))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5'])) 
Example 60
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_constant_fixed_width(self):
        "Test LineSplitter w/ fixed-width fields"
        strg = asbytes("  1  2  3  4     5   # test")
        test = LineSplitter(3)(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5', '']))
        #
        strg = asbytes("  1     3  4  5  6# test")
        test = LineSplitter(20)(strg)
        assert_equal(test, asbytes_nested(['1     3  4  5  6']))
        #
        strg = asbytes("  1     3  4  5  6# test")
        test = LineSplitter(30)(strg)
        assert_equal(test, asbytes_nested(['1     3  4  5  6'])) 
Example 61
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_keep_missing_values(self):
        "Check that we're not losing missing values"
        converter = StringConverter(int, default=0,
                                    missing_values=asbytes("N/A"))
        assert_equal(
            converter.missing_values, set(asbytes_nested(['', 'N/A']))) 
Example 62
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_mrecords.py    MIT License 5 votes vote down vote up
def setup(self):
        # Generic setup
        ilist = [1, 2, 3, 4, 5]
        flist = [1.1, 2.2, 3.3, 4.4, 5.5]
        slist = asbytes_nested(['one', 'two', 'three', 'four', 'five'])
        ddtype = [('a', int), ('b', float), ('c', '|S8')]
        mask = [0, 1, 0, 0, 1]
        self.base = ma.array(list(zip(ilist, flist, slist)),
                             mask=mask, dtype=ddtype) 
Example 63
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_mrecords.py    MIT License 5 votes vote down vote up
def test_set_elements(self):
        base = self.base.copy()
        # Set an element to mask .....................
        mbase = base.view(mrecarray).copy()
        mbase[-2] = masked
        assert_equal(
            mbase._mask.tolist(),
            np.array([(0, 0, 0), (1, 1, 1), (0, 0, 0), (1, 1, 1), (1, 1, 1)],
                     dtype=bool))
        # Used to be mask, now it's recordmask!
        assert_equal(mbase.recordmask, [0, 1, 0, 1, 1])
        # Set slices .................................
        mbase = base.view(mrecarray).copy()
        mbase[:2] = (5, 5, 5)
        assert_equal(mbase.a._data, [5, 5, 3, 4, 5])
        assert_equal(mbase.a._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.b._data, [5., 5., 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['5', '5', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])

        mbase = base.view(mrecarray).copy()
        mbase[:2] = masked
        assert_equal(mbase.a._data, [1, 2, 3, 4, 5])
        assert_equal(mbase.a._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 4.4, 5.5])
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])
        assert_equal(mbase.c._data,
                     asbytes_nested(['one', 'two', 'three', 'four', 'five']))
        assert_equal(mbase.b._mask, [1, 1, 0, 0, 1]) 
Example 64
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_mrecords.py    MIT License 5 votes vote down vote up
def test_setslices_hardmask(self):
        # Tests setting slices w/ hardmask.
        base = self.base.copy()
        mbase = base.view(mrecarray)
        mbase.harden_mask()
        try:
            mbase[-2:] = (5, 5, 5)
            assert_equal(mbase.a._data, [1, 2, 3, 5, 5])
            assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 5, 5.5])
            assert_equal(mbase.c._data,
                         asbytes_nested(['one', 'two', 'three', '5', 'five']))
            assert_equal(mbase.a._mask, [0, 1, 0, 0, 1])
            assert_equal(mbase.b._mask, mbase.a._mask)
            assert_equal(mbase.b._mask, mbase.c._mask)
        except NotImplementedError:
            # OK, not implemented yet...
            pass
        except AssertionError:
            raise
        else:
            raise Exception("Flexible hard masks should be supported !")
        # Not using a tuple should crash
        try:
            mbase[-2:] = 3
        except (NotImplementedError, TypeError):
            pass
        else:
            raise TypeError("Should have expected a readable buffer object!") 
Example 65
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_core.py    MIT License 5 votes vote down vote up
def test_set_record_slice(self):
        base = self.data['base']
        (base_a, base_b, base_c) = (base['a'], base['b'], base['c'])
        base[:3] = (pi, pi, 'pi')

        assert_equal(base_a.dtype, int)
        assert_equal(base_a._data, [3, 3, 3, 4, 5])

        assert_equal(base_b.dtype, float)
        assert_equal(base_b._data, [pi, pi, pi, 4.4, 5.5])

        assert_equal(base_c.dtype, '|S8')
        assert_equal(base_c._data,
                     asbytes_nested(['pi', 'pi', 'pi', 'four', 'five'])) 
Example 66
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_from_object_array(self):
        A = np.array([['abc', 2],
                      ['long   ', '0123456789']], dtype='O')
        B = np.char.array(A)
        assert_equal(B.dtype.itemsize, 10)
        assert_array_equal(B, asbytes_nested([['abc', '2'],
                                              ['long', '0123456789']])) 
Example 67
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_capitalize(self):
        tgt = asbytes_nested([[' abc ', ''],
                              ['12345', 'Mixedcase'],
                              ['123 \t 345 \0 ', 'Upper']])
        assert_(issubclass(self.A.capitalize().dtype.type, np.string_))
        assert_array_equal(self.A.capitalize(), tgt)

        tgt = [[sixu(' \u03c3 '), ''],
               ['12345', 'Mixedcase'],
               ['123 \t 345 \0 ', 'Upper']]
        assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_))
        assert_array_equal(self.B.capitalize(), tgt) 
Example 68
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_center(self):
        assert_(issubclass(self.A.center(10).dtype.type, np.string_))
        C = self.A.center([10, 20])
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])

        C = self.A.center(20, asbytes('#'))
        assert_(np.all(C.startswith(asbytes('#'))))
        assert_(np.all(C.endswith(asbytes('#'))))

        C = np.char.center(asbytes('FOO'), [[10, 20], [15, 8]])
        tgt = asbytes_nested([['   FOO    ', '        FOO         '],
                              ['      FOO      ', '  FOO   ']])
        assert_(issubclass(C.dtype.type, np.string_))
        assert_array_equal(C, tgt) 
Example 69
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_lower(self):
        tgt = asbytes_nested([[' abc ', ''],
                              ['12345', 'mixedcase'],
                              ['123 \t 345 \0 ', 'upper']])
        assert_(issubclass(self.A.lower().dtype.type, np.string_))
        assert_array_equal(self.A.lower(), tgt)

        tgt = [[sixu(' \u03c3 '), sixu('')],
               [sixu('12345'), sixu('mixedcase')],
               [sixu('123 \t 345 \0 '), sixu('upper')]]
        assert_(issubclass(self.B.lower().dtype.type, np.unicode_))
        assert_array_equal(self.B.lower(), tgt) 
Example 70
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_partition(self):
        P = self.A.partition(asbytes_nested(['3', 'M']))
        tgt = asbytes_nested([[(' abc ', '', ''), ('', '', '')],
                             [('12', '3', '45'), ('', 'M', 'ixedCase')],
                             [('12', '3', ' \t 345 \0 '), ('UPPER', '', '')]])
        assert_(issubclass(P.dtype.type, np.string_))
        assert_array_equal(P, tgt) 
Example 71
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_rsplit(self):
        A = self.A.rsplit(asbytes('3'))
        tgt = asbytes_nested([[[' abc '], ['']],
                              [['12', '45'], ['MixedCase']],
                              [['12', ' \t ', '45 \x00 '], ['UPPER']]])
        assert_(issubclass(A.dtype.type, np.object_))
        assert_equal(A.tolist(), tgt) 
Example 72
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_split(self):
        A = self.A.split(asbytes('3'))
        tgt = asbytes_nested([
                              [[' abc '], ['']],
                              [['12', '45'], ['MixedCase']],
                              [['12', ' \t ', '45 \x00 '], ['UPPER']]])
        assert_(issubclass(A.dtype.type, np.object_))
        assert_equal(A.tolist(), tgt) 
Example 73
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_swapcase(self):
        tgt = asbytes_nested([[' ABC ', ''],
                              ['12345', 'mIXEDcASE'],
                              ['123 \t 345 \0 ', 'upper']])
        assert_(issubclass(self.A.swapcase().dtype.type, np.string_))
        assert_array_equal(self.A.swapcase(), tgt)

        tgt = [[sixu(' \u03c3 '), sixu('')],
               [sixu('12345'), sixu('mIXEDcASE')],
               [sixu('123 \t 345 \0 '), sixu('upper')]]
        assert_(issubclass(self.B.swapcase().dtype.type, np.unicode_))
        assert_array_equal(self.B.swapcase(), tgt) 
Example 74
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_defchararray.py    MIT License 5 votes vote down vote up
def test_upper(self):
        tgt = asbytes_nested([[' ABC ', ''],
                              ['12345', 'MIXEDCASE'],
                              ['123 \t 345 \0 ', 'UPPER']])
        assert_(issubclass(self.A.upper().dtype.type, np.string_))
        assert_array_equal(self.A.upper(), tgt)

        tgt = [[sixu(' \u03a3 '), sixu('')],
               [sixu('12345'), sixu('MIXEDCASE')],
               [sixu('123 \t 345 \0 '), sixu('UPPER')]]
        assert_(issubclass(self.B.upper().dtype.type, np.unicode_))
        assert_array_equal(self.B.upper(), tgt) 
Example 75
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_regression.py    MIT License 5 votes vote down vote up
def test_char_array_creation(self, level=rlevel):
        a = np.array('123', dtype='c')
        b = np.array(asbytes_nested(['1', '2', '3']))
        assert_equal(a, b) 
Example 76
Project: Computable   Author: ktraunmueller   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_no_delimiter(self):
        "Test LineSplitter w/o delimiter"
        strg = asbytes(" 1 2 3 4  5 # test")
        test = LineSplitter()(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5']))
        test = LineSplitter('')(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5'])) 
Example 77
Project: Computable   Author: ktraunmueller   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_space_delimiter(self):
        "Test space delimiter"
        strg = asbytes(" 1 2 3 4  5 # test")
        test = LineSplitter(asbytes(' '))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
        test = LineSplitter(asbytes('  '))(strg)
        assert_equal(test, asbytes_nested(['1 2 3 4', '5'])) 
Example 78
Project: Computable   Author: ktraunmueller   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_tab_delimiter(self):
        "Test tab delimiter"
        strg = asbytes(" 1\t 2\t 3\t 4\t 5  6")
        test = LineSplitter(asbytes('\t'))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5  6']))
        strg = asbytes(" 1  2\t 3  4\t 5  6")
        test = LineSplitter(asbytes('\t'))(strg)
        assert_equal(test, asbytes_nested(['1  2', '3  4', '5  6'])) 
Example 79
Project: Computable   Author: ktraunmueller   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_other_delimiter(self):
        "Test LineSplitter on delimiter"
        strg = asbytes("1,2,3,4,,5")
        test = LineSplitter(asbytes(','))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
        #
        strg = asbytes(" 1,2,3,4,,5 # test")
        test = LineSplitter(asbytes(','))(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5'])) 
Example 80
Project: Computable   Author: ktraunmueller   File: test__iotools.py    MIT License 5 votes vote down vote up
def test_constant_fixed_width(self):
        "Test LineSplitter w/ fixed-width fields"
        strg = asbytes("  1  2  3  4     5   # test")
        test = LineSplitter(3)(strg)
        assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5', '']))
        #
        strg = asbytes("  1     3  4  5  6# test")
        test = LineSplitter(20)(strg)
        assert_equal(test, asbytes_nested(['1     3  4  5  6']))
        #
        strg = asbytes("  1     3  4  5  6# test")
        test = LineSplitter(30)(strg)
        assert_equal(test, asbytes_nested(['1     3  4  5  6']))