def testNRAFromRA(self): """Check the array function with a RecArray instance. """ buffer_ = [('Cuke', 123, (45, 67)), ('Tader', 321, (76, 54))] names = ['name', 'value', 'pair'] formats = ['a6', 'Int8', '(2,)Int16'] ra = numarray.records.array( buffer_, names=names, formats=formats) ## buffer_, names=names, formats=formats, aligned=True) names1 = ['newName', 'newValue', 'newPair'] nra0 = nra.array(buffer=ra, descr=zip(names1, formats)) nra1 = nra.array(buffer=buffer_, descr=zip(names1, formats)) self.assert_(common.areArraysEqual(nra0, nra1)) # Bad number of fields badFormats = ['Int8', '(2,)Int16'] self.assertRaises(ValueError, nra.array, buffer=ra, formats=badFormats) # Bad format in the first field badFormats = ['a9', 'Int8', '(2,)Int16'] self.assertRaises(ValueError, nra.array, buffer=ra, formats=badFormats)
def testNRAFromNRA(self): """Check the array function with a NestedRecArray instance. """ nra0 = nra.array(buffer=self.buffer, descr=self.descr) my_Descr = [('ID', 'Int64'), ('data', [('name', [('first', 'a9'), ('second', 'a9')]), ('coord', [('x', 'Float32'), ('y', 'f4'), ('z', 'f4')])])] nra1 = nra.array(buffer=self.buffer, descr=my_Descr) nra2 = nra.array(buffer=nra0, descr=my_Descr) self.assert_(common.areArraysEqual(nra2, nra1)) # Bad number of fields badDescr = [('data', [('name', [('first', 'a9'), ('second', 'a9')]), ('coord', [('x', 'Float32'), ('y', 'f4'), ('z', 'f4')])])] self.assertRaises(ValueError, nra.array, buffer=nra0, descr=badDescr) # Bad format in the first field badDescr = [('ID', 'b1'), ('data', [('name', [('first', 'a9'), ('second', 'a9')]), ('coord', [('x', 'Float32'), ('y', 'f4'), ('z', 'f4')])])] self.assertRaises(ValueError, nra.array, buffer=nra0, descr=badDescr)
def testNRAFromNRA(self): """Check the array function with a NestedRecArray instance. """ nra0 = nra.array(buffer=self.buffer, descr=self.descr) my_Descr = [('ID', 'Int64'), ('data', [('name', [('first','a9'), ('second','a9')]), ('coord', [('x','Float32'), ('y', 'f4'), ('z', 'f4')])])] nra1 = nra.array(buffer=self.buffer, descr=my_Descr) nra2 = nra.array(buffer=nra0, descr=my_Descr) self.assert_(common.areArraysEqual(nra2, nra1)) # Bad number of fields badDescr = [ ('data', [('name', [('first','a9'), ('second','a9')]), ('coord', [('x','Float32'), ('y', 'f4'), ('z', 'f4')])])] self.assertRaises(ValueError, nra.array, buffer=nra0, descr=badDescr) # Bad format in the first field badDescr = [('ID', 'b1'), ('data', [('name', [('first','a9'), ('second','a9')]), ('coord', [('x','Float32'), ('y', 'f4'), ('z', 'f4')])])] self.assertRaises(ValueError, nra.array, buffer=nra0, descr=badDescr)
def testGetNestedField(self): """Check the NestedRecArray.field method. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) # Test top level nested fields # The info field buffer = [ [('Paco', 'Perez'), (10, 20, 30)], [('Maria', 'Luisa'), (0, 2.0, 10)], [('C3Peanut', 'Tofu'), (10, 30, 20)] ] my_descr = [('name', [('first','a9'), ('second','a9')]), ('coord', [('x','Float32'), ('y', 'f4'), ('z', 'f4')])] model = nra.array(buffer, descr=my_descr) modelFirst = model.field('name/first') nra1 = nra0.field('info') nra1First = nra1.field('name/first') nra2 = nra1.field('name') nra3=nra2.field('first') self.assert_(common.areArraysEqual(model, nra1)) self.assert_(common.areArraysEqual(modelFirst, nra1First)) self.assert_(common.areArraysEqual(modelFirst, nra3))
def testSetSlice(self): """Set a nested array slice. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) buffer = [[10, (('Paco', 'Perez'), (10, 20, 30))], [20, (('Maria', 'Luisa'), (0, 2.0, 10))], [30, (('C3Peanut', 'Tofu'), (10, 30, 20))]] model = nra.array(buffer, descr=self.descr) nra0[0:3] = model[0:3] self.assert_(common.areArraysEqual(nra0, model))
def test05c_modifyingColumns(self): """Checking modifying several columns using a numarray buffer.""" table = self.fileh.root.table dtype = [('x', '(2,)i4'), ('y', '(2,2)f8'), ('z', 'u1')] nparray = nra.array(shape=(3, ), descr=dtype) nparray.field('x')[:] = 1 nparray.field('y')[:] = 1 nparray.field('z')[:] = 2 table.modifyColumns(3, 6, 1, nparray, ['x', 'y', 'z']) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table ycol = ones((3, 2, 2), 'Float64') data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertEqual(str(data), str(ycol))
def test05c_modifyingColumns(self): """Checking modifying several columns using a numarray buffer.""" table = self.fileh.root.table dtype=[('x', '(2,)i4'), ('y', '(2,2)f8'), ('z', 'u1')] nparray = nra.array(shape=(3,), descr=dtype) nparray.field('x')[:] = 1 nparray.field('y')[:] = 1 nparray.field('z')[:] = 2 table.modifyColumns(3, 6, 1, nparray, ['x', 'y', 'z']) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table ycol = ones((3, 2, 2), 'Float64') data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertEqual(str(data), str(ycol))
def testSetRow2NestedRecord(self): """Check the NestedRecArray.__setitem__ with NestedRecord instances. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) buffer = [[10, (('Paco', 'Perez'), (10, 20, 30))], [20, (('Maria', 'Luisa'), (0, 2.0, 10))], [30, (('C3Peanut', 'Tofu'), (10, 30, 20))]] model = nra.array(buffer, descr=self.descr) nra0[0] = model[0] nra0[1] = model[1] nra0[2] = model[2] self.assert_(common.areArraysEqual(nra0, model))
def testSetSlice(self): """Set a nested array slice. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) buffer = [ [10, (('Paco', 'Perez'), (10, 20, 30))], [20, (('Maria', 'Luisa'), (0, 2.0, 10))], [30, (('C3Peanut', 'Tofu'), (10, 30, 20))] ] model = nra.array(buffer, descr=self.descr) nra0[0:3] = model[0:3] self.assert_(common.areArraysEqual(nra0, model))
def testNRAadd(self): """Check the addition of nested arrays. """ ra1 = numarray.records.array([[1, 2], [3, 4]], formats=['Int32', 'Int32']) ra2 = numarray.records.array([[5, 6], [7, 8]], formats=['Int32', 'Int32']) ra3 = ra1 + ra2 nra1 = nra.array(buffer=ra1, descr=[('c1', 'Int32'), ('c2', 'Int32')]) nra2 = nra.array(buffer=ra2, descr=[('a', 'Int32'), ('b', 'Int32')]) nra3 = nra1 + nra2 nra4 = nra1 + ra1 self.assert_(common.areArraysEqual(nra3._flatArray, ra3)) self.assertEqual(nra3.descr, nra1.descr) self.assert_( common.areArraysEqual(nra4._flatArray, nra1._flatArray + ra1)) self.assertRaises(TypeError, nra.NestedRecArray.__add__, nra1, 3)
def testSetRow2NestedRecord(self): """Check the NestedRecArray.__setitem__ with NestedRecord instances. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) buffer = [ [10, (('Paco', 'Perez'), (10, 20, 30))], [20, (('Maria', 'Luisa'), (0, 2.0, 10))], [30, (('C3Peanut', 'Tofu'), (10, 30, 20))] ] model = nra.array(buffer, descr=self.descr) nra0[0] = model[0] nra0[1] = model[1] nra0[2] = model[2] self.assert_(common.areArraysEqual(nra0, model))
def testNRAFromArrayList(self): """Check the fromarrays function. """ # arrayList argument is a list of lists nra0 = nra.array(buffer=self.buffer, descr=self.descr) nra1 = nra.fromarrays(self.array_list, formats=self.formats) nra2 = nra.fromarrays(self.array_list, formats=self.formats, names=self.names) nra3 = nra.fromarrays(self.array_list, descr=self.descr) self.assertEqual(common.areArraysEqual(nra1, nra2), False) self.assert_(common.areArraysEqual(nra2, nra3)) self.assert_(common.areArraysEqual(nra0, nra2)) # arrayList argument is a list of NestedRecArrays nra0 = nra.array(buffer=[[1,4],[2,4]], formats=['f8','f4']) self.assertRaises(TypeError, nra.fromarrays, [nra0, nra0.field('c2')], formats=[['f8','f4'],'f4'])
def testGetBottomLevelField(self): """Check the NestedRecArray.field method. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) # Test bottom level fields nra1 = nra0.field('info/coord/x') ra1 = numarray.array([10, 0, 10], type='Float32') self.assert_(common.areArraysEqual(nra1, ra1))
def testNestedRecordCreation(self): """Check the creation of NestedRecord instances from NestedRecArrays. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) nrecord = nra0[0] self.assert_(isinstance(nrecord, nra.NestedRecord)) self.assert_(common.areArraysEqual(nra0, nrecord.array)) self.assertEqual(nrecord.row, 0)
def testGetTopLevelFlatField(self): """Check the NestedRecArray.field method. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) # Test top level flat fields nra1 = nra0.field('position') ra1 = numarray.array([1, 2, 3], type='Int64') self.assert_(common.areArraysEqual(nra1, ra1))
def testFlattenNestedRecord(self): """Check the flattening of NestedRecord instances. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) nrecord = nra0[0] frecord = nrecord.asRecord() self.assert_(isinstance(frecord, numarray.records.Record)) self.assert_(common.areArraysEqual(nra0.asRecArray(), frecord.array)) self.assertEqual(nrecord.row, frecord.row)
def testNestedRecordNestedField(self): """Get nested fields from nested records. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) name = nra0.field('info/name')[0] self.assertEqual(name.array._names, ['first', 'second']) self.assertEqual(name.field('first'), 'Paco') self.assertEqual(name.field('second'), 'Perez')
def testNRAadd(self): """Check the addition of nested arrays. """ ra1 = numarray.records.array([[1, 2], [3, 4]], formats=['Int32', 'Int32']) ra2 = numarray.records.array([[5, 6], [7, 8]], formats=['Int32', 'Int32']) ra3 = ra1 + ra2 nra1 = nra.array(buffer=ra1, descr=[('c1', 'Int32'), ('c2', 'Int32')]) nra2 = nra.array(buffer=ra2, descr=[('a', 'Int32'), ('b', 'Int32')]) nra3 = nra1 + nra2 nra4 = nra1 + ra1 self.assert_(common.areArraysEqual(nra3._flatArray, ra3)) self.assertEqual(nra3.descr, nra1.descr) self.assert_(common.areArraysEqual(nra4._flatArray, nra1._flatArray + ra1)) self.assertRaises(TypeError, nra.NestedRecArray.__add__, nra1, 3)
def testNestedRecordFlatField(self): """Retrieving flat fields from nested records. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) position = nra0.field('position')[0] firstName = nra0.field('info/name/first')[0] self.assertEqual(position, 1) self.assertEqual(firstName, 'Paco')
def testCreateNestedRecArray(self): """Check the array function. """ flatarray = numarray.records.array(self.flat_buffer, self.flat_formats) common.verbosePrint("""\nTesting the creation of a nested """ """recarray: buffer + formats""") nra1 = nra.array(formats=self.formats, buffer=self.buffer) common.verbosePrint( """\nTesting the creation of a nested recarray: buffer + """ """formats + names""") nra2 = nra.array(names=self.names, formats=self.formats, buffer=self.buffer) common.verbosePrint( """\nTesting the creation of a nested recarray: buffer + descr""") nra3 = nra.array(descr=self.descr, buffer=self.buffer) self.assertEqual(common.areArraysEqual(nra1, nra2), False) self.assert_(common.areArraysEqual(nra2, nra3))
def testNRAFromArrayList(self): """Check the fromarrays function. """ # arrayList argument is a list of lists nra0 = nra.array(buffer=self.buffer, descr=self.descr) nra1 = nra.fromarrays(self.array_list, formats=self.formats) nra2 = nra.fromarrays(self.array_list, formats=self.formats, names=self.names) nra3 = nra.fromarrays(self.array_list, descr=self.descr) self.assertEqual(common.areArraysEqual(nra1, nra2), False) self.assert_(common.areArraysEqual(nra2, nra3)) self.assert_(common.areArraysEqual(nra0, nra2)) # arrayList argument is a list of NestedRecArrays nra0 = nra.array(buffer=[[1, 4], [2, 4]], formats=['f8', 'f4']) self.assertRaises(TypeError, nra.fromarrays, [nra0, nra0.field('c2')], formats=[['f8', 'f4'], 'f4'])
def testCreateNestedRecArray(self): """Check the array function. """ flatarray = numarray.records.array(self.flat_buffer, self.flat_formats) common.verbosePrint( """\nTesting the creation of a nested """ """recarray: buffer + formats""") nra1 = nra.array(formats=self.formats, buffer=self.buffer) common.verbosePrint( """\nTesting the creation of a nested recarray: buffer + """ """formats + names""") nra2 = nra.array(names=self.names, formats=self.formats, buffer=self.buffer) common.verbosePrint( """\nTesting the creation of a nested recarray: buffer + descr""") nra3 = nra.array(descr=self.descr, buffer=self.buffer) self.assertEqual(common.areArraysEqual(nra1, nra2), False) self.assert_(common.areArraysEqual(nra2, nra3))
def testNRAFromRA(self): """Check the array function with a RecArray instance. """ buffer_ = [('Cuke', 123, (45, 67)), ('Tader', 321, (76, 54))] names = ['name', 'value', 'pair'] formats = ['a6', 'Int8', '(2,)Int16'] ra = numarray.records.array(buffer_, names=names, formats=formats) ## buffer_, names=names, formats=formats, aligned=True) names1 = ['newName', 'newValue', 'newPair'] nra0 = nra.array(buffer=ra, descr=zip(names1, formats)) nra1 = nra.array(buffer=buffer_, descr=zip(names1, formats)) self.assert_(common.areArraysEqual(nra0, nra1)) # Bad number of fields badFormats = ['Int8', '(2,)Int16'] self.assertRaises(ValueError, nra.array, buffer=ra, formats=badFormats) # Bad format in the first field badFormats = ['a9', 'Int8', '(2,)Int16'] self.assertRaises(ValueError, nra.array, buffer=ra, formats=badFormats)
def testNestedRecordSetNestedField(self): """Set nested fields of nested records. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) nrecord = nra0[0] ## nra2 = nra.array( ## buffer = [['Joan', 'Clos']], ## descr = [('first', 'a9'), ('second', 'a9')]) nra2 = nra.array(buffer=[[1, (('Joan', 'Clos'), (10, 20, 30))]], descr=self.descr) ## nrecord.setfield('info/name', nra2[0]) nrecord.setfield('info', nra2.field('info')[0]) my_buffer = [[1, (('Joan', 'Clos'), (10, 20, 30))], [2, (('Maria', 'Luisa'), (0, 2.0, 10))], [3, (('C3Peanut', 'Tofu'), (10.0, 30.0, 20.0))]] nra3 = nra.array(buffer=my_buffer, descr=self.descr) self.assert_(common.areArraysEqual(nra0, nra3))
def testNestedRecordSetFlatField(self): """Set flat fields of nested records. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) nrecord = nra0[0] nrecord.setfield('position', 24) nrecord.setfield('info/name/first', 'Joan') position = nrecord.field('position') firstName = nrecord.field('info/name/first') self.assertEqual(position, 24) self.assertEqual(firstName, 'Joan')
def testNestedRecordSetNestedField(self): """Set nested fields of nested records. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) nrecord = nra0[0] ## nra2 = nra.array( ## buffer = [['Joan', 'Clos']], ## descr = [('first', 'a9'), ('second', 'a9')]) nra2 = nra.array( buffer = [[1, (('Joan', 'Clos'), (10, 20, 30))]], descr = self.descr) ## nrecord.setfield('info/name', nra2[0]) nrecord.setfield('info', nra2.field('info')[0]) my_buffer = [[1, (('Joan', 'Clos'), (10, 20, 30))], [2, (('Maria', 'Luisa'), (0, 2.0, 10))], [3, (('C3Peanut', 'Tofu'), (10.0, 30.0, 20.0))]] nra3 = nra.array(buffer=my_buffer, descr=self.descr) self.assert_(common.areArraysEqual(nra0, nra3))
def testGetNestedField(self): """Check the NestedRecArray.field method. """ nra0 = nra.array(descr=self.descr, buffer=self.buffer) # Test top level nested fields # The info field buffer = [[('Paco', 'Perez'), (10, 20, 30)], [('Maria', 'Luisa'), (0, 2.0, 10)], [('C3Peanut', 'Tofu'), (10, 30, 20)]] my_descr = [('name', [('first', 'a9'), ('second', 'a9')]), ('coord', [('x', 'Float32'), ('y', 'f4'), ('z', 'f4')])] model = nra.array(buffer, descr=my_descr) modelFirst = model.field('name/first') nra1 = nra0.field('info') nra1First = nra1.field('name/first') nra2 = nra1.field('name') nra3 = nra2.field('first') self.assert_(common.areArraysEqual(model, nra1)) self.assert_(common.areArraysEqual(modelFirst, nra1First)) self.assert_(common.areArraysEqual(modelFirst, nra3))
print repr(table.description.info2.info3) print repr(table.description._v_nested_names) print repr(table.description.info1._v_nested_names) print print "**** now some for nested records, take that ****" print repr(table.description._v_nested_descr) print repr(numpy.rec.array(None, shape=0, dtype=table.description._v_nested_descr)) print repr(numpy.rec.array(None, shape=0, dtype=table.description.info2._v_nested_descr)) # NumPy recarrays doesn't have the machinery to understand the idiom below, # please use the above form instead. ###print repr(numpy.rec.array(None, shape=1, ### names=table.description._v_nested_names, ### formats=table.description._v_nested_formats)) from tables import nra print repr(nra.array(None, descr=table.description._v_nested_descr)) print repr(nra.array(None, names=table.description._v_nested_names, formats=table.description._v_nested_formats)) print print "**** and some iteration over descriptions, too ****" for coldescr in table.description._f_walk(): print "column-->", coldescr print print "**** info2 sub-structure description:\n", table.description.info2 print print "**** table representation (long form):\n", repr(table) # Remember to always close the file fileh.close()
print "**** now some for nested records, take that ****" print repr(table.description._v_nestedDescr) print repr( numpy.rec.array(None, shape=0, dtype=table.description._v_nestedDescr)) print repr( numpy.rec.array(None, shape=0, dtype=table.description.info2._v_nestedDescr)) # NumPy recarrays doesn't have the machinery to understand the idiom below, # please use the above form instead. ###print repr(numpy.rec.array(None, shape=1, ### names=table.description._v_nestedNames, ### formats=table.description._v_nestedFormats)) from tables import nra print repr(nra.array(None, descr=table.description._v_nestedDescr)) print repr( nra.array(None, names=table.description._v_nestedNames, formats=table.description._v_nestedFormats)) print print "**** and some iteration over descriptions, too ****" for coldescr in table.description._f_walk(): print "column-->", coldescr print print "**** info2 sub-structure description:\n", table.description.info2 print print "**** table representation (long form):\n", ` table ` # Remember to always close the file fileh.close()
def testGetSlice(self): """Get a nested array slice. """ my_buffer = [[1, (('Cuke', 'Skywalker'), (10, 20, 30))], [2, (('Princess', 'Lettuce'), (0, 2.0, 10))], [3, (('Ham', 'Solo'), (0, 2.0, 10))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))], [5, (('Chew', 'Brocoli'), (0, 2.0, 10))], [6, (('Master', 'Yoda'), (0, 2.0, 10))], [7, (('Tofu', 'Robot'), (0, 2.0, 10))], [8, (('C3Peanut', 'Robot'), (10, 30, 20))]] nra0 = nra.array(descr=self.descr, buffer=my_buffer) slice_ = nra0[1:2] model = nra.array( [[2, (('Princess', 'Lettuce'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[1:4] model = nra.array( [[2, (('Princess', 'Lettuce'), (0, 2.0, 10))], [3, (('Ham', 'Solo'), (0, 2.0, 10))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[1:4:2] model = nra.array( [[2, (('Princess', 'Lettuce'), (0, 2.0, 10))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[:4] model = nra.array( [[1, (('Cuke', 'Skywalker'), (10, 20, 30))], [2, (('Princess', 'Lettuce'), (0, 2.0, 10))], [3, (('Ham', 'Solo'), (0, 2.0, 10))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[:7:3] model = nra.array( [[1, (('Cuke', 'Skywalker'), (10, 20, 30))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))], [7, (('Tofu', 'Robot'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[:] self.assert_(common.areArraysEqual(slice_, nra0)) slice_ = nra0[::2] model = nra.array( [[1, (('Cuke', 'Skywalker'), (10, 20, 30))], [3, (('Ham', 'Solo'), (0, 2.0, 10))], [5, (('Chew', 'Brocoli'), (0, 2.0, 10))], [7, (('Tofu', 'Robot'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[4:-2] model = nra.array( [[5, (('Chew', 'Brocoli'), (0, 2.0, 10))], [6, (('Master', 'Yoda'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[-1:-3] model = nra.array(None, descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[-5::2] model = nra.array( [[4, (('Obi', 'Cannoli'), (0, 2.0, 10))], [6, (('Master', 'Yoda'), (0, 2.0, 10))], [8, (('C3Peanut', 'Robot'), (10, 30, 20))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model))
class TableNativeFlavorTestCase(common.PyTablesTestCase): nrows = 100 dtype = [('value', 'c16'), ('y2', 'f8'), ('Info2', [('name', 'a2'), ('value', '(2,)c16'), ('y3', '(2,)f8')]), ('name', 'a2'), ('z2', 'u1')] _infozeros = nra.array(descr=dtype, shape=3) # Set the contents to zero (or empty strings) _infozeros.field('value')[:] = 0 _infozeros.field('y2')[:] = 0 _infozeros.field('Info2/name')[:] = "\0" _infozeros.field('Info2/value')[:] = 0 _infozeros.field('Info2/y3')[:] = 0 _infozeros.field('name')[:] = "\0" _infozeros.field('z2')[:] = 0 _infoones = nra.array(descr=dtype, shape=3) # Set the contents to one (or blank strings) _infoones.field('value')[:] = 1 _infoones.field('y2')[:] = 1 _infoones.field('Info2/name')[:] = " " _infoones.field('Info2/value')[:] = 1 _infoones.field('Info2/y3')[:] = 1 _infoones.field('name')[:] = " " _infoones.field('z2')[:] = 1 def setUp(self): # Create an instance of an HDF5 Table self.file = tempfile.mktemp(".h5") fileh = openFile(self.file, "w") table = fileh.createTable(fileh.root, 'table', TestTDescr, expectedrows=self.nrows) table.flavor = 'numarray' for i in range(self.nrows): table.row.append() # Fill 100 rows with default values table.flush() self.fileh = fileh def tearDown(self): self.fileh.close() os.remove(self.file) common.cleanup(self) def test01a_basicTableRead(self): """Checking the return of a numarray in read().""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table[:] if common.verbose: print "Type of read:", type(data) print "Formats of the record:", data._formats print "First 3 elements of read:", data[:3] # Check the type of the recarray self.assertTrue(isinstance(data, records.RecArray)) # Check the value of some columns # A flat column col = table.cols.x[:3] self.assertTrue(isinstance(col, NumArray)) npcol = zeros((3, 2), type="Int32") if common.verbose: print "Plain column:" print "read column-->", col print "should look like-->", npcol self.assertTrue(allequal(col, npcol, "numarray")) # A nested column col = table.cols.Info[:3] self.assertTrue(isinstance(col, records.RecArray)) npcol = self._infozeros if common.verbose: print "Nested column:" print "read column-->", col print "should look like-->", npcol self.assertEqual(col.descr, npcol.descr) self.assertEqual(str(col), str(npcol)) def test01b_basicTableRead(self): """Checking the return of a numarray in read() (strided version).""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table[::3] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "First 3 elements of read:", data[:3] # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check the value of some columns # A flat column col = table.cols.x[:9:3] self.assertTrue(isinstance(col, NumArray)) npcol = zeros((3, 2), dtype="Int32") if common.verbose: print "Plain column:" print "read column-->", col print "should look like-->", npcol self.assertTrue(allequal(col, npcol, "numarray")) # A nested column col = table.cols.Info[:9:3] self.assertTrue(isinstance(col, records.RecArray)) npcol = self._infozeros if common.verbose: print "Nested column:" print "read column-->", col print "should look like-->", npcol self.assertEqual(col.descr, npcol.descr) self.assertEqual(str(col), str(npcol)) def test02_getWhereList(self): """Checking the return of numarray in getWhereList method.""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.getWhereList('z == 1') if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check that all columns have been selected self.assertEqual(len(data), 100) # Finally, check that the contents are ok self.assertTrue(allequal(data, arange(100, type="Int64"), "numarray")) def test03a_readWhere(self): """Checking the return of numarray in readWhere method (strings).""" table = self.fileh.root.table table.cols.color.createIndex() if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.readWhere('color == "ab"') if common.verbose: print "Type of read:", type(data) print "Length of the data read:", len(data) # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check that all columns have been selected self.assertEqual(len(data), self.nrows) def test03b_readWhere(self): """Checking the return of numarray in readWhere method (numeric).""" table = self.fileh.root.table table.cols.z.createIndex() if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.readWhere('z == 0') if common.verbose: print "Type of read:", type(data) print "Length of the data read:", len(data) # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check that all columns have been selected self.assertEqual(len(data), 0) def test04a_createTable(self): """Checking the Table creation from a numarray recarray.""" npdata = self._infozeros table = self.fileh.createTable(self.fileh.root, 'table2', npdata) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table2 data = table[:] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "npdata-->", npdata print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check the type self.assertEqual(data.descr, npdata.descr) self.assertEqual(str(data), str(npdata)) def test04b_appendTable(self): """Checking appending a numarray recarray.""" table = self.fileh.root.table npdata = table[3:6] table.append(npdata) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table[-3:] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "Last 3 elements of read:", data[-3:] print "Length of the data read:", len(data) if common.verbose: print "npdata-->", npdata print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check the type self.assertEqual(data.descr, npdata.descr) self.assertEqual(str(data), str(npdata)) def test05a_assignColumn(self): """Checking assigning to a column.""" table = self.fileh.root.table table.cols.z[:] = ones((100, ), dtype='UInt8') if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.cols.z[:] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check that all columns have been selected self.assertEqual(len(data), 100) # Finally, check that the contents are ok self.assertTrue( allequal(data, ones((100, ), dtype="UInt8"), "numarray")) def test05b_modifyingColumns(self): """Checking modifying several columns at once.""" table = self.fileh.root.table xcol = ones((3, 2), 'Int32') ycol = ones((3, 2, 2), 'Float64') zcol = zeros((3, ), 'UInt8') table.modifyColumns(3, 6, 1, [xcol, ycol, zcol], ['x', 'y', 'z']) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(data, ycol, "numarray")) def test05c_modifyingColumns(self): """Checking modifying several columns using a numarray buffer.""" table = self.fileh.root.table dtype = [('x', '(2,)i4'), ('y', '(2,2)f8'), ('z', 'u1')] nparray = nra.array(shape=(3, ), descr=dtype) nparray.field('x')[:] = 1 nparray.field('y')[:] = 1 nparray.field('z')[:] = 2 table.modifyColumns(3, 6, 1, nparray, ['x', 'y', 'z']) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table ycol = ones((3, 2, 2), 'Float64') data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertEqual(str(data), str(ycol)) def test06a_assignNestedColumn(self): """Checking assigning a nested column (using modifyColumn).""" npdata = self._infoones table = self.fileh.root.table data = table.cols.Info[3:6] table.modifyColumn(3, 6, 1, column=npdata, colname='Info') if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.cols.Info[3:6] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "npdata-->", npdata print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check the type self.assertEqual(data.descr, npdata.descr) self.assertEqual(str(data), str(npdata)) def test06b_assignNestedColumn(self): """Checking assigning a nested column (using the .cols accessor).""" table = self.fileh.root.table npdata = self._infoones table.cols.Info[3:6] = npdata if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.cols.Info[3:6] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "npdata-->", npdata print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check the type self.assertEqual(data.descr, npdata.descr) self.assertEqual(str(data), str(npdata)) def test07a_modifyingRows(self): """Checking modifying several rows at once (using modifyRows).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[0:3] # Modify it somewhat chunk.field('y')[:] = -1 table.modifyRows(3, 6, 1, rows=chunk) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table ycol = zeros((3, 2, 2), 'Float64') - 1 data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(ycol, data, "numarray")) def test07b_modifyingRows(self): """Checking modifying several rows at once (using cols accessor).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[0:3] # Modify it somewhat chunk.field('y')[:] = -1 table.cols[3:6] = chunk if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((3, 2, 2), 'Float64') - 1 data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(ycol, data, "numarray")) def test08a_modifyingRows(self): """Checking modifying just one row at once (using modifyRows).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[3] # Modify it somewhat chunk.field('y')[:] = -1 table.modifyRows(6, 7, 1, chunk) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((2, 2), 'Float64') - 1 data = table.cols.y[6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(ycol, data, "numarray")) def test08b_modifyingRows(self): """Checking modifying just one row at once (using cols accessor).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[3] # Modify it somewhat chunk['y'][:] = -1 table.cols[6] = chunk if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((2, 2), 'Float64') - 1 data = table.cols.y[6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(ycol, data, "numarray")) def test09a_getStrings(self): """Checking the return of string columns with spaces.""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table rdata = table.getWhereList('color == "ab"') data = table.readCoordinates(rdata) if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "First 3 elements of read:", data[:3] # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check that all columns have been selected self.assertEqual(len(data), 100) # Finally, check that the contents are ok for idata in data.field('color'): self.assertEqual(idata, "ab") def test09b_getStrings(self): """Checking the return of string columns with spaces. (modify)""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table for i in range(50): table.cols.color[i] = "a " table.flush() data = table[:] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "First 3 elements of read:", data[:3] # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check that all columns have been selected self.assertEqual(len(data), 100) # Finally, check that the contents are ok for i in range(100): idata = data.field('color')[i] if i >= 50: self.assertEqual(idata, "ab") else: self.assertEqual(idata, "a") def test09c_getStrings(self): """Checking the return of string columns with spaces. (append)""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table row = table.row for i in range(50): row["color"] = "a " # note the trailing spaces row.append() table.flush() if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") data = self.fileh.root.table[:] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "First 3 elements of read:", data[:3] # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check that all columns have been selected self.assertEqual(len(data), 150) # Finally, check that the contents are ok # Finally, check that the contents are ok for i in range(150): idata = data.field('color')[i] if i < 100: self.assertEqual(idata, "ab") else: self.assertEqual(idata, "a")
def testGetSlice(self): """Get a nested array slice. """ my_buffer = [[1, (('Cuke', 'Skywalker'), (10, 20, 30))], [2, (('Princess', 'Lettuce'), (0, 2.0, 10))], [3, (('Ham', 'Solo'), (0, 2.0, 10))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))], [5, (('Chew', 'Brocoli'), (0, 2.0, 10))], [6, (('Master', 'Yoda'), (0, 2.0, 10))], [7, (('Tofu', 'Robot'), (0, 2.0, 10))], [8, (('C3Peanut', 'Robot'), (10, 30, 20))]] nra0 = nra.array(descr=self.descr, buffer=my_buffer) slice_ = nra0[1:2] model = nra.array([[2, (('Princess', 'Lettuce'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[1:4] model = nra.array( [[2, (('Princess', 'Lettuce'), (0, 2.0, 10))], [3, (('Ham', 'Solo'), (0, 2.0, 10))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[1:4:2] model = nra.array( [[2, (('Princess', 'Lettuce'), (0, 2.0, 10))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[:4] model = nra.array( [[1, (('Cuke', 'Skywalker'), (10, 20, 30))], [2, (('Princess', 'Lettuce'), (0, 2.0, 10))], [3, (('Ham', 'Solo'), (0, 2.0, 10))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[:7:3] model = nra.array( [[1, (('Cuke', 'Skywalker'), (10, 20, 30))], [4, (('Obi', 'Cannoli'), (0, 2.0, 10))], [7, (('Tofu', 'Robot'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[:] self.assert_(common.areArraysEqual(slice_, nra0)) slice_ = nra0[::2] model = nra.array( [[1, (('Cuke', 'Skywalker'), (10, 20, 30))], [3, (('Ham', 'Solo'), (0, 2.0, 10))], [5, (('Chew', 'Brocoli'), (0, 2.0, 10))], [7, (('Tofu', 'Robot'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[4:-2] model = nra.array( [[5, (('Chew', 'Brocoli'), (0, 2.0, 10))], [6, (('Master', 'Yoda'), (0, 2.0, 10))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[-1:-3] model = nra.array(None, descr=self.descr) self.assert_(common.areArraysEqual(slice_, model)) slice_ = nra0[-5::2] model = nra.array( [[4, (('Obi', 'Cannoli'), (0, 2.0, 10))], [6, (('Master', 'Yoda'), (0, 2.0, 10))], [8, (('C3Peanut', 'Robot'), (10, 30, 20))]], descr=self.descr) self.assert_(common.areArraysEqual(slice_, model))