本文整理匯總了Python中numpy.recarray方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.recarray方法的具體用法?Python numpy.recarray怎麽用?Python numpy.recarray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.recarray方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def __init__(self,fileobj, hdr):
""" gets list of frames and subheaders in pet file
Parameters
-----------
fileobj : ECAT file <filename>.v fileholder or file object
with read, seek methods
Returns
-------
mlist : numpy recarray nframes X 4 columns
1 - Matrix identifier.
2 - subheader record number
3 - Last record number of matrix data block.
4 - Matrix status:
1 - exists - rw
2 - exists - ro
3 - matrix deleted
"""
self.hdr = hdr
self._mlist = self.get_mlist(fileobj)
示例2: _keep_fields
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def _keep_fields(base, keep_names, usemask=True, asrecarray=False):
"""
Return a new array keeping only the fields in `keep_names`,
and preserving the order of those fields.
Parameters
----------
base : array
Input array
keep_names : string or sequence
String or sequence of strings corresponding to the names of the
fields to keep. Order of the names will be preserved.
usemask : {False, True}, optional
Whether to return a masked array or not.
asrecarray : string or sequence, optional
Whether to return a recarray or a mrecarray (`asrecarray=True`) or
a plain ndarray or masked array with flexible dtype. The default
is False.
"""
newdtype = [(n, base.dtype[n]) for n in keep_names]
output = np.empty(base.shape, dtype=newdtype)
output = recursive_fill_fields(base, output)
return _fix_output(output, usemask=usemask, asrecarray=asrecarray)
示例3: test_recfromtxt
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def test_recfromtxt(self):
#
data = TextIO('A,B\n0,1\n2,3')
kwargs = dict(delimiter=",", missing_values="N/A", names=True)
test = np.recfromtxt(data, **kwargs)
control = np.array([(0, 1), (2, 3)],
dtype=[('A', int), ('B', int)])
assert_(isinstance(test, np.recarray))
assert_equal(test, control)
#
data = TextIO('A,B\n0,1\n2,N/A')
test = np.recfromtxt(data, dtype=None, usemask=True, **kwargs)
control = ma.array([(0, 1), (2, -1)],
mask=[(False, False), (False, True)],
dtype=[('A', int), ('B', int)])
assert_equal(test, control)
assert_equal(test.mask, control.mask)
assert_equal(test.A, [0, 2])
示例4: test_recfromtxt
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def test_recfromtxt(self):
#
data = TextIO('A,B\n0,1\n2,3')
kwargs = dict(delimiter=",", missing_values="N/A", names=True)
test = np.recfromtxt(data, **kwargs)
control = np.array([(0, 1), (2, 3)],
dtype=[('A', np.int), ('B', np.int)])
self.assertTrue(isinstance(test, np.recarray))
assert_equal(test, control)
#
data = TextIO('A,B\n0,1\n2,N/A')
test = np.recfromtxt(data, dtype=None, usemask=True, **kwargs)
control = ma.array([(0, 1), (2, -1)],
mask=[(False, False), (False, True)],
dtype=[('A', np.int), ('B', np.int)])
assert_equal(test, control)
assert_equal(test.mask, control.mask)
assert_equal(test.A, [0, 2])
示例5: test_recarray_from_repr
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
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)
示例6: get_mlist
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def get_mlist(self, fileobj):
fileobj.seek(512)
dat=fileobj.read(128*32)
dt = np.dtype([('matlist',np.int32)])
if not self.hdr.endianness is native_code:
dt = dt.newbyteorder(self.hdr.endianness)
nframes = self.hdr['num_frames']
mlist = np.zeros((nframes,4), dtype='uint32')
record_count = 0
done = False
while not done: #mats['matlist'][0,1] == 2:
mats = np.recarray(shape=(32,4), dtype=dt, buf=dat)
if not (mats['matlist'][0,0] + mats['matlist'][0,3]) == 31:
mlist = []
return mlist
nrecords = mats['matlist'][0,3]
mlist[record_count:nrecords+record_count,:] = mats['matlist'][1:nrecords+1,:]
record_count+= nrecords
if mats['matlist'][0,1] == 2 or mats['matlist'][0,1] == 0:
done = True
else:
# Find next subheader
tmp = int(mats['matlist'][0,1]-1)#cast to int
fileobj.seek(0)
fileobj.seek(tmp*512)
dat = fileobj.read(128*32)
return mlist
示例7: _get_subheaders
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def _get_subheaders(self):
"""retreive all subheaders and return list of subheader recarrays
"""
subheaders = []
header = self._header
endianness = self.endianness
dt = self._subhdrdtype
if not self.endianness is native_code:
dt = self._subhdrdtype.newbyteorder(self.endianness)
if self._header['num_frames'] > 1:
for item in self._mlist._mlist:
if item[1] == 0:
break
self.fileobj.seek(0)
offset = (int(item[1])-1)*512
self.fileobj.seek(offset)
tmpdat = self.fileobj.read(512)
sh = (np.recarray(shape=(), dtype=dt,
buf=tmpdat))
subheaders.append(sh.copy())
else:
self.fileobj.seek(0)
offset = (int(self._mlist._mlist[0][1])-1)*512
self.fileobj.seek(offset)
tmpdat = self.fileobj.read(512)
sh = (np.recarray(shape=(), dtype=dt,
buf=tmpdat))
subheaders.append(sh)
return subheaders
示例8: test_mlist
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def test_mlist(self):
fid = open(self.example_file, 'rb')
hdr = self.header_class.from_fileobj(fid)
mlist = self.mlist_class(fid, hdr)
fid.seek(0)
fid.seek(512)
dat=fid.read(128*32)
dt = np.dtype([('matlist',np.int32)])
dt = dt.newbyteorder('>')
mats = np.recarray(shape=(32,4), dtype=dt, buf=dat)
fid.close()
#tests
assert_true(mats['matlist'][0,0] + mats['matlist'][0,3] == 31)
assert_true(mlist.get_frame_order()[0][0] == 0)
assert_true(mlist.get_frame_order()[0][1] == 16842758.0)
# test badly ordered mlist
badordermlist = mlist
badordermlist._mlist = np.array([[ 1.68427540e+07, 3.00000000e+00,
1.20350000e+04, 1.00000000e+00],
[ 1.68427530e+07, 1.20360000e+04,
2.40680000e+04, 1.00000000e+00],
[ 1.68427550e+07, 2.40690000e+04,
3.61010000e+04, 1.00000000e+00],
[ 1.68427560e+07, 3.61020000e+04,
4.81340000e+04, 1.00000000e+00],
[ 1.68427570e+07, 4.81350000e+04,
6.01670000e+04, 1.00000000e+00],
[ 1.68427580e+07, 6.01680000e+04,
7.22000000e+04, 1.00000000e+00]])
assert_true(badordermlist.get_frame_order()[0][0] == 1)
示例9: _datetime64_index
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def _datetime64_index(self, recarr):
""" Given a np.recarray find the first datetime64 column """
# TODO: Handle multi-indexes
names = recarr.dtype.names
for name in names:
if recarr[name].dtype == DTN64_DTYPE:
return name
return None
示例10: _fix_output
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def _fix_output(output, usemask=True, asrecarray=False):
"""
Private function: return a recarray, a ndarray, a MaskedArray
or a MaskedRecords depending on the input parameters
"""
if not isinstance(output, MaskedArray):
usemask = False
if usemask:
if asrecarray:
output = output.view(MaskedRecords)
else:
output = ma.filled(output)
if asrecarray:
output = output.view(recarray)
return output
示例11: rec_drop_fields
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def rec_drop_fields(base, drop_names):
"""
Returns a new numpy.recarray with fields in `drop_names` dropped.
"""
return drop_fields(base, drop_names, usemask=False, asrecarray=True)
示例12: rename_fields
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def rename_fields(base, namemapper):
"""
Rename the fields from a flexible-datatype ndarray or recarray.
Nested fields are supported.
Parameters
----------
base : ndarray
Input array whose fields must be modified.
namemapper : dictionary
Dictionary mapping old field names to their new version.
Examples
--------
>>> from numpy.lib import recfunctions as rfn
>>> a = np.array([(1, (2, [3.0, 30.])), (4, (5, [6.0, 60.]))],
... dtype=[('a', int),('b', [('ba', float), ('bb', (float, 2))])])
>>> rfn.rename_fields(a, {'a':'A', 'bb':'BB'})
array([(1, (2.0, [3.0, 30.0])), (4, (5.0, [6.0, 60.0]))],
dtype=[('A', '<i4'), ('b', [('ba', '<f8'), ('BB', '<f8', 2)])])
"""
def _recursive_rename_fields(ndtype, namemapper):
newdtype = []
for name in ndtype.names:
newname = namemapper.get(name, name)
current = ndtype[name]
if current.names:
newdtype.append(
(newname, _recursive_rename_fields(current, namemapper))
)
else:
newdtype.append((newname, current))
return newdtype
newdtype = _recursive_rename_fields(base.dtype, namemapper)
return base.view(newdtype)
示例13: rec_append_fields
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def rec_append_fields(base, names, data, dtypes=None):
"""
Add new fields to an existing array.
The names of the fields are given with the `names` arguments,
the corresponding values with the `data` arguments.
If a single field is appended, `names`, `data` and `dtypes` do not have
to be lists but just values.
Parameters
----------
base : array
Input array to extend.
names : string, sequence
String or sequence of strings corresponding to the names
of the new fields.
data : array or sequence of arrays
Array or sequence of arrays storing the fields to add to the base.
dtypes : sequence of datatypes, optional
Datatype or sequence of datatypes.
If None, the datatypes are estimated from the `data`.
See Also
--------
append_fields
Returns
-------
appended_array : np.recarray
"""
return append_fields(base, names, data=data, dtypes=dtypes,
asrecarray=True, usemask=False)
示例14: test_recfromcsv
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def test_recfromcsv(self):
#
data = TextIO('A,B\n0,1\n2,3')
kwargs = dict(missing_values="N/A", names=True, case_sensitive=True)
test = np.recfromcsv(data, dtype=None, **kwargs)
control = np.array([(0, 1), (2, 3)],
dtype=[('A', int), ('B', int)])
assert_(isinstance(test, np.recarray))
assert_equal(test, control)
#
data = TextIO('A,B\n0,1\n2,N/A')
test = np.recfromcsv(data, dtype=None, usemask=True, **kwargs)
control = ma.array([(0, 1), (2, -1)],
mask=[(False, False), (False, True)],
dtype=[('A', int), ('B', int)])
assert_equal(test, control)
assert_equal(test.mask, control.mask)
assert_equal(test.A, [0, 2])
#
data = TextIO('A,B\n0,1\n2,3')
test = np.recfromcsv(data, missing_values='N/A',)
control = np.array([(0, 1), (2, 3)],
dtype=[('a', int), ('b', int)])
assert_(isinstance(test, np.recarray))
assert_equal(test, control)
#
data = TextIO('A,B\n0,1\n2,3')
dtype = [('a', int), ('b', float)]
test = np.recfromcsv(data, missing_values='N/A', dtype=dtype)
control = np.array([(0, 1), (2, 3)],
dtype=dtype)
assert_(isinstance(test, np.recarray))
assert_equal(test, control)
#gh-10394
data = TextIO('color\n"red"\n"blue"')
test = np.recfromcsv(data, converters={0: lambda x: x.strip(b'\"')})
control = np.array([('red',), ('blue',)], dtype=[('color', (bytes, 4))])
assert_equal(test.dtype, control.dtype)
assert_equal(test, control)
示例15: recfromtxt
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import recarray [as 別名]
def recfromtxt(fname, **kwargs):
"""
Load ASCII data from a file and return it in a record array.
If ``usemask=False`` a standard `recarray` is returned,
if ``usemask=True`` a MaskedRecords array is returned.
Parameters
----------
fname, kwargs : For a description of input parameters, see `genfromtxt`.
See Also
--------
numpy.genfromtxt : generic function
Notes
-----
By default, `dtype` is None, which means that the data-type of the output
array will be determined from the data.
"""
kwargs.setdefault("dtype", None)
usemask = kwargs.get('usemask', False)
output = genfromtxt(fname, **kwargs)
if usemask:
from numpy.ma.mrecords import MaskedRecords
output = output.view(MaskedRecords)
else:
output = output.view(np.recarray)
return output