我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用numpy.record()。
def test_recarray_from_repr(self): a = np.array([(1,'ABC'), (2, "DEF")], dtype=[('foo', int), ('bar', 'S4')]) recordarr = np.rec.array(a) recarr = a.view(np.recarray) recordview = a.view(np.dtype((np.record, a.dtype))) recordarr_r = eval("numpy." + repr(recordarr), {'numpy': np}) recarr_r = eval("numpy." + repr(recarr), {'numpy': np}) recordview_r = eval("numpy." + repr(recordview), {'numpy': np}) assert_equal(type(recordarr_r), np.recarray) assert_equal(recordarr_r.dtype.type, np.record) assert_equal(recordarr, recordarr_r) assert_equal(type(recarr_r), np.recarray) assert_equal(recarr_r.dtype.type, np.record) assert_equal(recarr, recarr_r) assert_equal(type(recordview_r), np.ndarray) assert_equal(recordview.dtype.type, np.record) assert_equal(recordview, recordview_r)
def test_recarray_repr(self): # make sure non-structured dtypes also show up as rec.array a = np.array(np.ones(4, dtype='f8')) assert_(repr(np.rec.array(a)).startswith('rec.array')) # check that the 'np.record' part of the dtype isn't shown a = np.rec.array(np.ones(3, dtype='i4,i4')) assert_equal(repr(a).find('numpy.record'), -1) a = np.rec.array(np.ones(3, dtype='i4')) assert_(repr(a).find('dtype=int32') != -1)
def test_recarray_returntypes(self): qux_fields = {'C': (np.dtype('S5'), 0), 'D': (np.dtype('S5'), 6)} a = np.rec.array([('abc ', (1,1), 1, ('abcde', 'fgehi')), ('abc', (2,3), 1, ('abcde', 'jklmn'))], dtype=[('foo', 'S4'), ('bar', [('A', int), ('B', int)]), ('baz', int), ('qux', qux_fields)]) assert_equal(type(a.foo), np.ndarray) assert_equal(type(a['foo']), np.ndarray) assert_equal(type(a.bar), np.recarray) assert_equal(type(a['bar']), np.recarray) assert_equal(a.bar.dtype.type, np.record) assert_equal(type(a['qux']), np.recarray) assert_equal(a.qux.dtype.type, np.record) assert_equal(dict(a.qux.dtype.fields), qux_fields) assert_equal(type(a.baz), np.ndarray) assert_equal(type(a['baz']), np.ndarray) assert_equal(type(a[0].bar), np.record) assert_equal(type(a[0]['bar']), np.record) assert_equal(a[0].bar.A, 1) assert_equal(a[0].bar['A'], 1) assert_equal(a[0]['bar'].A, 1) assert_equal(a[0]['bar']['A'], 1) assert_equal(a[0].qux.D, asbytes('fgehi')) assert_equal(a[0].qux['D'], asbytes('fgehi')) assert_equal(a[0]['qux'].D, asbytes('fgehi')) assert_equal(a[0]['qux']['D'], asbytes('fgehi'))
def fromRecArray(recarr): '''Takes a numpy recarray and returns each field as a dmarray in a SpaceData container Parameters ---------- recarr : numpy record array object to parse into SpaceData container Returns ------- sd: spacepy.datamodel.SpaceData dict-like containing arrays of named records in recarr Examples -------- >>> import numpy as np >>> import spacepy.datamodel as dm >>> x = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) >>> print(x, x.dtype) array([(1.0, 2), (3.0, 4)], dtype=[('x', '<f8'), ('y', '<i4')]) >>> sd = dm.fromRecArray(x) >>> sd.tree(verbose=1) + |____x (spacepy.datamodel.dmarray (2,)) |____y (spacepy.datamodel.dmarray (2,)) ''' sd = SpaceData() for key in recarr.dtype.fields.keys(): sd[key] = dmarray(recarr[key]) return sd
def toRecArray(sdo): '''Takes a SpaceData and creates a numpy recarray Parameters ---------- sdo : SpaceData SpaceData to change to a numpy recarray Returns ------- recarr: numpy record array numpy.recarray object with the same values (attributes are lost) Examples -------- >>> import numpy as np >>> import spacepy.datamodel as dm >>> sd = dm.SpaceData() >>> sd['x'] = dm.dmarray([1.0, 2.0]) >>> sd['y'] = dm.dmarray([2,4]) >>> sd.tree(verbose=1) + |____x (spacepy.datamodel.dmarray (2,)) |____y (spacepy.datamodel.dmarray (2,)) >>> ra = dm.toRecArray(sd) >>> print(ra, ra.dtype) [(2, 1.0) (4, 2.0)] (numpy.record, [('y', '<i8'), ('x', '<f8')]) ''' keys = list(sdo.keys()) recarr = numpy.rec.fromarrays( [sdo[k] for k in sdo], names=keys) return recarr