本文整理汇总了Python中numpy.genfromtxt方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.genfromtxt方法的具体用法?Python numpy.genfromtxt怎么用?Python numpy.genfromtxt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.genfromtxt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_results
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def load_results(file):
if not os.path.exists(file):
return None
with open(file, 'r') as f:
lines = [line for line in f]
if len(lines) < 2:
return None
keys = [name.strip() for name in lines[0].split(',')]
data = np.genfromtxt(file, delimiter=',', skip_header=1, filling_values=0.)
if data.ndim == 1:
data = data.reshape(1, -1)
assert data.ndim == 2
assert data.shape[-1] == len(keys)
result = {}
for idx, key in enumerate(keys):
result[key] = data[:, idx]
return result
示例2: read_data
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def read_data(labelsname, distancename):
## Extract labels
rawlabels = np.genfromtxt(labelsname, delimiter=',', dtype=None)
labelmap = {}
row_len = 0
for row in rawlabels:
row_len = max(row_len, len(row)-1)
name = row[0]
labelmap[name] = list(row)[1:]
## Extract distances
rawdistances = np.genfromtxt(distancename, delimiter=',', dtype=None)
names = rawdistances[0][1:]
distances = np.array(rawdistances[1:, 1:], dtype=float)
labels = np.zeros((len(names), row_len))
for i, name in enumerate(names):
labels[i, 0:(len(row))] = labelmap[name]
del labelmap
return distances, labels, names
示例3: load_edges
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def load_edges(fpath, delimiter=None, has_header=False):
"""Load edges in CSV format as numpy ndarray of strings.
Args:
fpath (str): edges file
delimiter (str): alternative argument name for sep (default=None)
has_header (bool): True if has header row
Returns:
np.ndarray: array of edges
"""
if PANDAS_INSTALLED:
header = 'infer' if has_header else None
df = pd.read_csv(fpath, delimiter=delimiter, header=header)
edges = df.values
else:
logger.warning("Pandas not installed. Using numpy to load csv, which "
"is slower.")
header = 1 if has_header else 0
edges = np.genfromtxt(fpath, delimiter=delimiter, skip_header=header,
dtype=object)
return edges.astype('str')
示例4: test_skip_footer_with_invalid
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def test_skip_footer_with_invalid(self):
with suppress_warnings() as sup:
sup.filter(ConversionWarning)
basestr = '1 1\n2 2\n3 3\n4 4\n5 \n6 \n7 \n'
# Footer too small to get rid of all invalid values
assert_raises(ValueError, np.genfromtxt,
TextIO(basestr), skip_footer=1)
# except ValueError:
# pass
a = np.genfromtxt(
TextIO(basestr), skip_footer=1, invalid_raise=False)
assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
#
a = np.genfromtxt(TextIO(basestr), skip_footer=3)
assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
#
basestr = '1 1\n2 \n3 3\n4 4\n5 \n6 6\n7 7\n'
a = np.genfromtxt(
TextIO(basestr), skip_footer=1, invalid_raise=False)
assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]]))
a = np.genfromtxt(
TextIO(basestr), skip_footer=3, invalid_raise=False)
assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]]))
示例5: test_dtype_with_object
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def test_dtype_with_object(self):
# Test using an explicit dtype with an object
data = """ 1; 2001-01-01
2; 2002-01-31 """
ndtype = [('idx', int), ('code', object)]
func = lambda s: strptime(s.strip(), "%Y-%m-%d")
converters = {1: func}
test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype,
converters=converters)
control = np.array(
[(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))],
dtype=ndtype)
assert_equal(test, control)
ndtype = [('nest', [('idx', int), ('code', object)])]
with assert_raises_regex(NotImplementedError,
'Nested fields.* not supported.*'):
test = np.genfromtxt(TextIO(data), delimiter=";",
dtype=ndtype, converters=converters)
示例6: test_replace_space
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def test_replace_space(self):
# Test the 'replace_space' option
txt = "A.A, B (B), C:C\n1, 2, 3.14"
# Test default: replace ' ' by '_' and delete non-alphanum chars
test = np.genfromtxt(TextIO(txt),
delimiter=",", names=True, dtype=None)
ctrl_dtype = [("AA", int), ("B_B", int), ("CC", float)]
ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
assert_equal(test, ctrl)
# Test: no replace, no delete
test = np.genfromtxt(TextIO(txt),
delimiter=",", names=True, dtype=None,
replace_space='', deletechars='')
ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", float)]
ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
assert_equal(test, ctrl)
# Test: no delete (spaces are replaced by _)
test = np.genfromtxt(TextIO(txt),
delimiter=",", names=True, dtype=None,
deletechars='')
ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", float)]
ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
assert_equal(test, ctrl)
示例7: test_replace_space_known_dtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def test_replace_space_known_dtype(self):
# Test the 'replace_space' (and related) options when dtype != None
txt = "A.A, B (B), C:C\n1, 2, 3"
# Test default: replace ' ' by '_' and delete non-alphanum chars
test = np.genfromtxt(TextIO(txt),
delimiter=",", names=True, dtype=int)
ctrl_dtype = [("AA", int), ("B_B", int), ("CC", int)]
ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
assert_equal(test, ctrl)
# Test: no replace, no delete
test = np.genfromtxt(TextIO(txt),
delimiter=",", names=True, dtype=int,
replace_space='', deletechars='')
ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", int)]
ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
assert_equal(test, ctrl)
# Test: no delete (spaces are replaced by _)
test = np.genfromtxt(TextIO(txt),
delimiter=",", names=True, dtype=int,
deletechars='')
ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", int)]
ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
assert_equal(test, ctrl)
示例8: test_names_with_usecols_bug1636
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def test_names_with_usecols_bug1636(self):
# Make sure we pick up the right names w/ usecols
data = "A,B,C,D,E\n0,1,2,3,4\n0,1,2,3,4\n0,1,2,3,4"
ctrl_names = ("A", "C", "E")
test = np.genfromtxt(TextIO(data),
dtype=(int, int, int), delimiter=",",
usecols=(0, 2, 4), names=True)
assert_equal(test.dtype.names, ctrl_names)
#
test = np.genfromtxt(TextIO(data),
dtype=(int, int, int), delimiter=",",
usecols=("A", "C", "E"), names=True)
assert_equal(test.dtype.names, ctrl_names)
#
test = np.genfromtxt(TextIO(data),
dtype=int, delimiter=",",
usecols=("A", "C", "E"), names=True)
assert_equal(test.dtype.names, ctrl_names)
示例9: test_utf8_file
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def test_utf8_file(self):
utf8 = b"\xcf\x96"
with temppath() as path:
with open(path, "wb") as f:
f.write((b"test1,testNonethe" + utf8 + b",test3\n") * 2)
test = np.genfromtxt(path, dtype=None, comments=None,
delimiter=',', encoding="UTF-8")
ctl = np.array([
["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"],
["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"]],
dtype=np.unicode)
assert_array_equal(test, ctl)
# test a mixed dtype
with open(path, "wb") as f:
f.write(b"0,testNonethe" + utf8)
test = np.genfromtxt(path, dtype=None, comments=None,
delimiter=',', encoding="UTF-8")
assert_equal(test['f0'], 0)
assert_equal(test['f1'], "testNonethe" + utf8.decode("UTF-8"))
示例10: read_emission_spectra_text
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def read_emission_spectra_text(filename):
"""
Read text-formatted emission spectra.
Parameters
----------
filename : str
The Tecan Infinite output filen to be read.
Returns
-------
SRC_280 : numpy.array
SRC_280_x : numpy.array
SRC_280_x_num : numpy.array
Examples
--------
"""
SRC_280 = np.genfromtxt(filename, dtype='str')
SRC_280_x = SRC_280[0,:]
SRC_280_x_num = re.findall(r'\d+', str(SRC_280_x )[1:-1])
return [SRC_280, SRC_280_x, SRC_280_x_num]
示例11: crawl_folders
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def crawl_folders(self, sequence_length):
sequence_set = []
demi_length = (sequence_length-1)//2
shifts = list(range(-demi_length, demi_length + 1))
shifts.pop(demi_length)
for scene in self.scenes:
intrinsics = np.genfromtxt(scene/'cam.txt').astype(np.float32).reshape((3, 3))
imgs = sorted(scene.files('*.jpg'))
if len(imgs) < sequence_length:
continue
for i in range(demi_length, len(imgs)-demi_length):
sample = {'intrinsics': intrinsics, 'tgt': imgs[i], 'ref_imgs': []}
for j in shifts:
sample['ref_imgs'].append(imgs[i+j])
sequence_set.append(sample)
random.shuffle(sequence_set)
self.samples = sequence_set
示例12: read_scene_data
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def read_scene_data(data_root, sequence_set, seq_length=3, step=1):
data_root = Path(data_root)
im_sequences = []
poses_sequences = []
indices_sequences = []
demi_length = (seq_length - 1) // 2
shift_range = np.array([step*i for i in range(-demi_length, demi_length + 1)]).reshape(1, -1)
sequences = set()
for seq in sequence_set:
corresponding_dirs = set((data_root/'sequences').dirs(seq))
sequences = sequences | corresponding_dirs
print('getting test metadata for theses sequences : {}'.format(sequences))
for sequence in tqdm(sequences):
poses = np.genfromtxt(data_root/'poses'/'{}.txt'.format(sequence.name)).astype(np.float64).reshape(-1, 3, 4)
imgs = sorted((sequence/'image_2').files('*.png'))
# construct 5-snippet sequences
tgt_indices = np.arange(demi_length, len(imgs) - demi_length).reshape(-1, 1)
snippet_indices = shift_range + tgt_indices
im_sequences.append(imgs)
poses_sequences.append(poses)
indices_sequences.append(snippet_indices)
return im_sequences, poses_sequences, indices_sequences
示例13: get_displacements_from_speed
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def get_displacements_from_speed(root, date, scene, indices, tgt_index):
"""get displacement magnitudes by integrating over speed values.
Might be a good alternative if the GPS is not good enough"""
if len(indices) == 0:
return []
oxts_root = root/date/scene/'oxts'
with open(oxts_root/'timestamps.txt') as f:
timestamps = np.array([datetime.datetime.strptime(ts[:-3], "%Y-%m-%d %H:%M:%S.%f").timestamp() for ts in f.read().splitlines()])
speeds = np.zeros((len(indices), 3))
for i, index in enumerate(indices):
oxts_data = np.genfromtxt(oxts_root/'data'/'{:010d}.txt'.format(index))
speeds[i] = oxts_data[[6,7,10]]
displacements = np.zeros((len(indices), 3))
# Perform the integration operation, using trapezoidal method
for i0, (i1, i2) in enumerate(zip(indices, indices[1:])):
displacements[i0 + 1] = displacements[i0] + 0.5*(speeds[i0] + speeds[i0 + 1]) * (timestamps[i1] - timestamps[i2])
# Set the origin of displacements at tgt_index
displacements -= displacements[tgt_index]
# Finally, get the displacement magnitude relative to tgt and discard the middle value (which is supposed to be 0)
displacements_mag = np.linalg.norm(displacements, axis=1)
return np.concatenate([displacements_mag[:tgt_index], displacements_mag[tgt_index + 1:]])
示例14: test_commented_header
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def test_commented_header(self):
# Check that names can be retrieved even if the line is commented out.
data = TextIO("""
#gender age weight
M 21 72.100000
F 35 58.330000
M 33 21.99
""")
# The # is part of the first name and should be deleted automatically.
test = np.genfromtxt(data, names=True, dtype=None)
ctrl = np.array([('M', 21, 72.1), ('F', 35, 58.33), ('M', 33, 21.99)],
dtype=[('gender', '|S1'), ('age', int), ('weight', float)])
assert_equal(test, ctrl)
# Ditto, but we should get rid of the first element
data = TextIO(b"""
# gender age weight
M 21 72.100000
F 35 58.330000
M 33 21.99
""")
test = np.genfromtxt(data, names=True, dtype=None)
assert_equal(test, ctrl)
示例15: test_dtype_with_object
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import genfromtxt [as 别名]
def test_dtype_with_object(self):
# Test using an explicit dtype with an object
data = """ 1; 2001-01-01
2; 2002-01-31 """
ndtype = [('idx', int), ('code', np.object)]
func = lambda s: strptime(s.strip(), "%Y-%m-%d")
converters = {1: func}
test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype,
converters=converters)
control = np.array(
[(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))],
dtype=ndtype)
assert_equal(test, control)
ndtype = [('nest', [('idx', int), ('code', np.object)])]
try:
test = np.genfromtxt(TextIO(data), delimiter=";",
dtype=ndtype, converters=converters)
except NotImplementedError:
pass
else:
errmsg = "Nested dtype involving objects should be supported."
raise AssertionError(errmsg)