本文整理匯總了Python中numpy.fromfile方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.fromfile方法的具體用法?Python numpy.fromfile怎麽用?Python numpy.fromfile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.fromfile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: collectdata
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def collectdata(self,):
print 'Start Collect Data...'
train_x_path = os.path.join(self.input_dir, 'unlabeled_X.bin')
train_xf = open(train_x_path, 'rb')
train_x = np.fromfile(train_xf, dtype=np.uint8)
train_x = np.reshape(train_x, (-1, 3, 96, 96))
train_x = np.transpose(train_x, (0, 3, 2, 1))
idx = 0
for i in xrange(train_x.shape[0]):
if not self.skipimg:
transform_and_save(img_arr=train_x[i], output_filename=os.path.join(self.unlabeldir, str(idx) + '.jpg'))
self.trainpairlist[os.path.join('images', 'unlabeled', str(idx) + '.jpg')] = 'labels/11.txt'
idx += 1
print 'Finished Collect Data...'
示例2: load_rf_mapping_stimulus
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def load_rf_mapping_stimulus(session_path, stim_metadata):
"""
extract frames of rf mapping stimulus
:param session_path: absolute path of a session, i.e. /mnt/data/Subjects/ZM_1887/2019-07-10/001
:type session_path: str
:param stim_metadata: dictionary of stimulus/task metadata
:type stim_metadata: dict
:return: stimulus frames
:rtype: np.ndarray of shape (y_pix, x_pix, n_frames)
"""
idx_rfm = get_stim_num_from_name(stim_metadata['VISUAL_STIMULI'], 'receptive_field_mapping')
if idx_rfm is not None:
stim_filename = stim_metadata['VISUAL_STIM_%i' % idx_rfm].get(
'stim_data_file_name', '*RFMapStim.raw*')
stim_file = glob.glob(os.path.join(session_path, 'raw_behavior_data', stim_filename))[0]
frame_array = np.fromfile(stim_file, dtype='uint8')
y_pix, x_pix, _ = stim_metadata['VISUAL_STIM_%i' % idx_rfm]['stim_file_shape']
frames = np.transpose(np.reshape(frame_array, [y_pix, x_pix, -1], order='F'), [2, 1, 0])
else:
frames = np.array([])
return frames
示例3: parse_data
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def parse_data(path, dataset, flatten):
if dataset != 'train' and dataset != 't10k':
raise NameError('dataset must be train or t10k')
label_file = os.path.join(path, dataset + '-labels-idx1-ubyte')
with open(label_file, 'rb') as file:
_, num = struct.unpack(">II", file.read(8))
labels = np.fromfile(file, dtype=np.int8) # int8
new_labels = np.zeros((num, 10))
new_labels[np.arange(num), labels] = 1
img_file = os.path.join(path, dataset + '-images-idx3-ubyte')
with open(img_file, 'rb') as file:
_, num, rows, cols = struct.unpack(">IIII", file.read(16))
imgs = np.fromfile(file, dtype=np.uint8).reshape(num, rows, cols) # uint8
imgs = imgs.astype(np.float32) / 255.0
if flatten:
imgs = imgs.reshape([num, -1])
return imgs, new_labels
示例4: _read
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def _read(self, stream, text, byte_order):
'''
Read the actual data from a PLY file.
'''
if text:
self._read_txt(stream)
else:
if self._have_list:
# There are list properties, so a simple load is
# impossible.
self._read_bin(stream, byte_order)
else:
# There are no list properties, so loading the data is
# much more straightforward.
self._data = _np.fromfile(stream,
self.dtype(byte_order),
self.count)
if len(self._data) < self.count:
k = len(self._data)
del self._data
raise PlyParseError("early end-of-file", self, k)
self._check_sanity()
示例5: _fread3_many
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def _fread3_many(fobj, n):
"""Read 3-byte ints from an open binary file object.
Parameters
----------
fobj : file
File descriptor
Returns
-------
out : 1D array
An array of 3 byte int
"""
b1, b2, b3 = np.fromfile(fobj, ">u1", 3 * n).reshape(-1,
3).astype(np.int).T
return (b1 << 16) + (b2 << 8) + b3
示例6: test_payload_getitem_setitem
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def test_payload_getitem_setitem(self, item):
with open(SAMPLE_FILE, 'rb') as fh:
fh.seek(0xa88)
header = mark4.Mark4Header.fromfile(fh, ntrack=64, decade=2010)
payload = mark4.Mark4Payload.fromfile(fh, header)
sel_data = payload.data[item]
assert np.all(payload[item] == sel_data)
payload2 = mark4.Mark4Payload(payload.words.copy(), header)
assert payload2 == payload
payload2[item] = -sel_data
check = payload.data
check[item] = -sel_data
assert np.all(payload2[item] == -sel_data)
assert np.all(payload2.data == check)
assert payload2 != payload
payload2[item] = sel_data
assert np.all(payload2[item] == sel_data)
assert payload2 == payload
示例7: test_binary_file_reader
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def test_binary_file_reader(self):
with mark4.open(SAMPLE_FILE, 'rb', decade=2010, ntrack=64) as fh:
locations = fh.locate_frames()
assert locations == [0xa88, 0xa88+64*2500]
fh.seek(0xa88)
header = mark4.Mark4Header.fromfile(fh, decade=2010, ntrack=64)
fh.seek(0xa88)
header2 = fh.read_header()
current_pos = fh.tell()
assert header2 == header
frame_rate = fh.get_frame_rate()
assert abs(frame_rate
- 32 * u.MHz / header.samples_per_frame) < 1 * u.nHz
assert fh.tell() == current_pos
repr_fh = repr(fh)
assert repr_fh.startswith('Mark4FileReader')
assert 'ntrack=64, decade=2010, ref_time=None' in repr_fh
示例8: test_header_times
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def test_header_times(self):
with mark4.open(SAMPLE_FILE, 'rb', decade=2010, ntrack=64) as fh:
fh.seek(0xa88)
header0 = mark4.Mark4Header.fromfile(fh, ntrack=64, decade=2010)
start_time = header0.time
# Use frame size, since header adds to payload.
samples_per_frame = header0.frame_nbytes * 8 // 2 // 8
frame_rate = 32. * u.MHz / samples_per_frame
frame_duration = 1. / frame_rate
fh.seek(0xa88)
for frame_nr in range(100):
try:
frame = fh.read_frame()
except EOFError:
break
header_time = frame.header.time
expected = start_time + frame_nr * frame_duration
assert abs(header_time - expected) < 1. * u.ns
示例9: test_header
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def test_header(self):
with open(SAMPLE_32TRACK, 'rb') as fh:
fh.seek(9656)
header = mark4.Mark4Header.fromfile(fh, ntrack=32, decade=2010)
# Try initialising with properties instead of keywords.
# Here, we let
# * time imply the decade, bcd_unit_year, bcd_day, bcd_hour,
# bcd_minute, bcd_second, bcd_fraction;
# * ntrack, samples_per_frame, bps define headstack_id, bcd_track_id,
# fan_out, and magnitude_bit;
# * nsb defines lsb_output and converter_id.
header1 = mark4.Mark4Header.fromvalues(
ntrack=32, samples_per_frame=80000, bps=2, nsb=2, time=header.time,
system_id=108)
assert header1 == header
示例10: _read_row
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def _read_row(self, row):
data = np.empty((self.size[1], self.n_image, self.size[0]),
self.datatype)
for i, fid in enumerate(self.files):
# Find where we need to seek to
offset = np.dtype(self.datatype).itemsize * \
(row * self.size[0]) * self.size[1]
# Seek relative to current position
fid.seek(offset - fid.tell(), 1)
# Read
data[:, i, :] = np.fromfile(fid,
dtype=self.datatype,
count=self.size[0] * self.size[1],
).reshape(self.size).T
return data
示例11: load_fashion_mnist
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def load_fashion_mnist(path, split):
split = split.lower()
image_file, label_file = [os.path.join(path, file_name) for file_name in MNIST_FILES[split]]
with open(image_file) as fd:
images = np.fromfile(file=fd, dtype=np.uint8)
images = images[16:].reshape(-1, 784).astype(np.float32)
if split == "train":
images = images[:55000]
elif split == "eval":
images = images[55000:]
with open(label_file) as fd:
labels = np.fromfile(file=fd, dtype=np.uint8)
labels = labels[8:].astype(np.int32)
if split == "train":
labels = labels[:55000]
elif split == "eval":
labels = labels[55000:]
return(zip(images, labels))
示例12: load_mnist
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def load_mnist(path, split):
split = split.lower()
image_file, label_file = [os.path.join(path, file_name) for file_name in MNIST_FILES[split]]
with open(image_file) as fd:
images = np.fromfile(file=fd, dtype=np.uint8)
images = images[16:].reshape(-1, 784).astype(np.float32)
if split == "train":
images = images[:55000]
elif split == "eval":
images = images[55000:]
with open(label_file) as fd:
labels = np.fromfile(file=fd, dtype=np.uint8)
labels = labels[8:].astype(np.int32)
if split == "train":
labels = labels[:55000]
elif split == "eval":
labels = labels[55000:]
return(zip(images, labels))
示例13: get_tile
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def get_tile(name):
"""
Get tile with the given name.
Check the cache for the tile with the given name. If not found, the
tile is download.
Args:
name(str): The name of the tile.
"""
dem_file = os.path.join(_get_data_path(), (name + ".dem").upper())
if not (os.path.exists(dem_file)):
SRTM30.download_tile(name)
y = np.fromfile(dem_file, dtype = np.dtype('>i2')).reshape(SRTM30._tile_height,
SRTM30._tile_width)
return y
示例14: from_xml
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def from_xml(cls, xmlelement):
"""Loads a Sparse object from an existing file."""
binaryfp = xmlelement.binaryfp
nelem = int(xmlelement[0].attrib['nelem'])
nrows = int(xmlelement.attrib['nrows'])
ncols = int(xmlelement.attrib['ncols'])
if binaryfp is None:
rowindex = np.fromstring(xmlelement[0].text, sep=' ').astype(int)
colindex = np.fromstring(xmlelement[1].text, sep=' ').astype(int)
sparsedata = np.fromstring(xmlelement[2].text, sep=' ')
else:
rowindex = np.fromfile(binaryfp, dtype='<i4', count=nelem)
colindex = np.fromfile(binaryfp, dtype='<i4', count=nelem)
sparsedata = np.fromfile(binaryfp, dtype='<d', count=nelem)
return cls((sparsedata, (rowindex, colindex)), [nrows, ncols])
示例15: Vector
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import fromfile [as 別名]
def Vector(elem):
nelem = int(elem.attrib['nelem'])
if nelem == 0:
arr = np.ndarray((0,))
else:
# sep=' ' seems to work even when separated by newlines, see
# http://stackoverflow.com/q/31882167/974555
if elem.binaryfp is not None:
arr = np.fromfile(elem.binaryfp, dtype='<d', count=nelem)
else:
arr = np.fromstring(elem.text, sep=' ')
if arr.size != nelem:
raise RuntimeError(
'Expected {:s} elements in Vector, found {:d}'
' elements!'.format(elem.attrib['nelem'],
arr.size))
return arr