Python numpy 模块,recfromtxt() 实例源码
我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用numpy.recfromtxt()。
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])
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])
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])
def fromPostSamp(self, burn=None, skipHeader=12):
'''
This method uses lalinference samples. If not burn value is given then the
entire posterior sample is used. If the burn option is supplied then the
initial part of the chain (upto iteration number = burn) is ignored.
Output is a list whose first two elements are the probability that the primary
and secondary object is a NS respectively. The third element gives the remnant
mass outside the black hole in access of the threshold mass supplied.
'''
data = np.recfromtxt(self.inputFile, names=True, skip_header=skipHeader)
burnin = 0
if burn: burnin = burn
mc = data['mc'][burnin:]
massRatio = data['q'][burnin:]
self.chi = data['a1'][burnin:]
self.eta = massRatio/((1 + massRatio)**2)
self.mPrimary = (massRatio**(-0.6)) * mc * (1. + massRatio)**0.2
self.mSecondary = (massRatio**0.4) * mc * (1. + massRatio)**0.2
NS_prob_2 = np.sum(self.mSecondary < self.max_ns_g_mass)*100./len(self.mSecondary) # RE: Max NS mass was hardcoded as 3.0. Should be gotten from class variable
NS_prob_1 = np.sum(self.mPrimary < self.max_ns_g_mass)*100./len(self.mPrimary)
return [NS_prob_1, NS_prob_2, self.computeRemMass()]
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])
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])
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])
def load_data(file_to_read):
"""Load X_train/y_train/X_val/y_val/X_infer for further
processing (e.g. make input queue of tensorflow).
Args:
file_to_read:
Returns:
X_train/y_train/X_val/y_val/X_infer.
"""
data = np.recfromtxt(file_to_read)
data = np.asarray(data)
return data
def __new__(cls, *args,**kwargs):
# if no argument is given, create zero sized recarray
if len(args) == 0:
args = (0,)
elif type(args[0]) is int:
# create empty recarray
d = numpy.recarray(args[0], dtype = SWCFile.swcformat)
else:
# create from file or filename
d = numpy.recfromtxt(args[0], dtype = SWCFile.swcformat)
return d.view(SWCFile)
def ingest_zeta_values(self):
t_values = np.arange(2000, 42000, 2000)
names = ['atomic_number', 'ion_charge']
names += [str(i) for i in t_values]
zeta = np.recfromtxt(
self.data_fn,
usecols=xrange(1, 23),
names=names)
zeta_df = (
pd.DataFrame.from_records(zeta).set_index(
['atomic_number', 'ion_charge']).T
)
data = list()
for i, s in zeta_df.iterrows():
T = Temperature.as_unique(self.session, value=int(i))
if T.id is None:
self.session.flush()
for (atomic_number, ion_charge), rate in s.iteritems():
data.append(
Zeta(
atomic_number=atomic_number,
ion_charge=ion_charge,
data_source=self.data_source,
temp=T,
zeta=rate
)
)
def test_recfromtxt(self):
with temppath(suffix='.txt') as path:
path = Path(path)
with path.open('w') as f:
f.write(u'A,B\n0,1\n2,3')
kwargs = dict(delimiter=",", missing_values="N/A", names=True)
test = np.recfromtxt(path, **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)