本文整理汇总了Python中numpy.fromstring函数的典型用法代码示例。如果您正苦于以下问题:Python fromstring函数的具体用法?Python fromstring怎么用?Python fromstring使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fromstring函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, source):
"""Reader MEG data from texfiles or file-like objects.
Parameters
----------
source : str or file-like
Strings are assumed to be filenames (with `.gz` suffix
compressed), while all other object types are treated as file-like
objects.
"""
self.ntimepoints = None
self.timepoints = None
self.nsamples = None
self.channelids = []
self.data = []
self.samplingrate = None
# open textfiles
if isinstance(source, str):
if source.endswith('.gz'):
externals.exists('gzip', raise_=True)
import gzip
source = gzip.open(source, 'r')
else:
source = open(source, 'r')
# read file
for line in source:
# split ID
colon = line.find(':')
# ignore lines without id
if colon == -1:
continue
id = line[:colon]
data = line[colon+1:].strip()
if id == 'Sample Number':
timepoints = np.fromstring(data, dtype=int, sep='\t')
# one more as it starts with zero
self.ntimepoints = int(timepoints.max()) + 1
self.nsamples = int(len(timepoints) / self.ntimepoints)
elif id == 'Time':
self.timepoints = np.fromstring(data,
dtype=float,
count=self.ntimepoints,
sep='\t')
self.samplingrate = self.ntimepoints \
/ (self.timepoints[-1] - self.timepoints[0])
else:
# load data
self.data.append(
np.fromstring(data, dtype=float, sep='\t').reshape(
self.nsamples, self.ntimepoints))
# store id
self.channelids.append(id)
# reshape data from (channels x samples x timepoints) to
# (samples x chanels x timepoints)
self.data = np.swapaxes(np.array(self.data), 0, 1)
示例2: get_stream
def get_stream(self, chan, time_interval):
self.fpga.write_int('pps_start', 1)
#self.phases = np.empty((len(self.freqs),Npackets))
Npackets = np.int(time_interval * self.accum_freq)
Is = np.empty(Npackets)
Qs = np.empty(Npackets)
phases = np.empty(Npackets)
count = 0
while count < Npackets:
packet = self.s.recv(8192 + 42) # total number of bytes including 42 byte header
header = np.fromstring(packet[:42],dtype = '<B')
roach_mac = header[6:12]
filter_on = np.array([2, 68, 1, 2, 13, 33])
if np.array_equal(roach_mac,filter_on):
data = np.fromstring(packet[42:],dtype = '<i').astype('float')
data /= 2.0**17
data /= (self.accum_len/512.)
ts = (np.fromstring(packet[-4:],dtype = '<i').astype('float')/ self.fpga_samp_freq)*1.0e3 # ts in ms
# To stream one channel, make chan an argument
if (chan % 2) > 0:
I = data[1024 + ((chan - 1) / 2)]
Q = data[1536 + ((chan - 1) /2)]
else:
I = data[0 + (chan/2)]
Q = data[512 + (chan/2)]
phase = np.arctan2([Q],[I])
Is[count]=I
Qs[count]=Q
phases[count]=phase
else:
continue
count += 1
return Is, Qs, phases
示例3: get_UDP
def get_UDP(self, Npackets, LO_freq, skip_packets=2, channels = None):
#Npackets = np.int(time_interval * self.accum_freq)
I_buffer = np.empty((Npackets + skip_packets, len(channels)))
Q_buffer = np.empty((Npackets + skip_packets, len(channels)))
self.fpga.write_int('pps_start', 1)
count = 0
while count < Npackets + skip_packets:
packet = self.s.recv(8192)
data = np.fromstring(packet,dtype = '<i').astype('float')
data /= 2.0**17
data /= (self.accum_len/512.)
ts = (np.fromstring(packet[-4:],dtype = '<i').astype('float')/ self.fpga_samp_freq)*1.0e3 # ts in ms
odd_chan = channels[1::2]
even_chan = channels[0::2]
I_odd = data[1024 + ((odd_chan - 1) / 2)]
Q_odd = data[1536 + ((odd_chan - 1) /2)]
I_even = data[0 + (even_chan/2)]
Q_even = data[512 + (even_chan/2)]
even_phase = np.arctan2(Q_even,I_even)
odd_phase = np.arctan2(Q_odd,I_odd)
if len(channels) % 2 > 0:
I = np.hstack(zip(I_even[:len(I_odd)], I_odd))
Q = np.hstack(zip(Q_even[:len(Q_odd)], Q_odd))
I = np.hstack((I, I_even[-1]))
Q = np.hstack((Q, Q_even[-1]))
I_buffer[count] = I
Q_buffer[count] = Q
else:
I = np.hstack(zip(I_even, I_odd))
Q = np.hstack(zip(Q_even, Q_odd))
I_buffer[count] = I
Q_buffer[count] = Q
count += 1
return I_buffer[skip_packets:],Q_buffer[skip_packets:]
示例4: send
def send(cloudstream, data, timeout = 0.25):
sock = cloudstream.socket
#print data.shape
data = data.reshape(-1, 1)
#print data.shape
sock.send("upload")
alive = time.time()
while time.time() - alive < timeout:
try:
msg = sock.recv_multipart(flags = zmq.DONTWAIT)
pstart, size = msg
pstart = np.fromstring(pstart, "int32", 1)[0] * 3
size = np.fromstring(size, "int32", 1)[0] * 3
pend = pstart + size
#print "sending", pstart, pend
#print("sending", pstart, pend, data.shape[0])
if pstart < 0:
return True
elif pstart <= data.shape[0]:
#print data.shape, data.dtype, pstart, pend
#print "subdata", data[pstart:min(data.shape[0], pend)].shape
chunk = data[pstart:min(data.shape[0], pend)].tostring()
#print len(chunk)
sock.send(chunk)
alive = time.time()
except zmq.error.Again:
continue
print "Connection timeout"
return False
示例5: _read_variable_data
def _read_variable_data(self, raw_data):
""" Read the raw data and set the variable values. """
if('real' in self.flags):
number_of_columns = self.number_of_variables
elif('complex' in self.flags):
number_of_columns = 2*self.number_of_variables
else:
raise NotImplementedError
if('Transient' in self.plot_name): #Tran
number_of_columns = self.number_of_variables+1
input_data = np.fromstring(raw_data, count=number_of_columns*self.number_of_points, dtype='float32')
input_data = input_data.reshape((self.number_of_points, number_of_columns))
input_data = input_data.transpose()
time = input_data [0:2]
tmpdata= time.transpose().flatten().tostring()
time=np.fromstring(tmpdata, count=self.number_of_points, dtype='float64')
time=np.absolute(time)
input_data = input_data [1:]
input_data[0]=time
else:
input_data = np.fromstring(raw_data, count=number_of_columns*self.number_of_points, dtype='float64')
input_data = input_data.reshape((self.number_of_points, number_of_columns))
input_data = input_data.transpose()
#input_data = input_data [1:]
#np.savetxt('raw.txt', input_data)
if 'complex' in self.flags:
raw_data = input_data
input_data = np.array(raw_data[0::2], dtype='complex64')
input_data.imag = raw_data[1::2]
for variable in self.variables.values():
variable.data = input_data[variable.index]
示例6: decode_req
def decode_req(metadata, data, data2):
a, b, iterations, l, w, array_dtype, l2, w2, array_dtype2 = metadata.split('|')
syn0 = np.fromstring(data, dtype=array_dtype)
syn0 = syn0.reshape(int(l), int(w))
syn1 = np.fromstring(data2, dtype=array_dtype2).reshape(int(l2), int(w2))
conf = (syn0, syn1)
return int(a), int(b), int(iterations), conf
示例7: getcorr
def getcorr(self):
self.u.write_int('corr0_ctrl',0)
self.u.write_int('corr1_ctrl',0)
self.u.write_int('corr0_ctrl',1)
self.u.write_int('corr1_ctrl',1)
done = False
while not done:
a = self.u.read_int('corr0_addr')
b = self.u.read_int('corr1_addr')
if a > 0 and b > 0:
done = True
depth = 36*3*self.nch/4
l0 = np.fromstring(self.u.read('corr0_bram_lsb',depth*4),dtype='int16').byteswap().astype('float')
l0 = l0[::2] + l0[1::2]*1j
m0 = np.fromstring(self.u.read('corr0_bram_msb',depth*4),dtype='int16').byteswap().astype('float')
m0 = m0[::2] + m0[1::2]*1j
l1 = np.fromstring(self.u.read('corr1_bram_lsb',depth*4),dtype='int16').byteswap().astype('float')
l1 = l1[::2] + l1[1::2]*1j
m1 = np.fromstring(self.u.read('corr1_bram_msb',depth*4),dtype='int16').byteswap().astype('float')
m1 = m1[::2] + m1[1::2]*1j
c = np.zeros((3,36,self.nch),dtype='complex')
for k in range(36):
s = np.zeros((3*self.nch,),dtype='complex')
s[0::4] = m0[k::36]
s[1::4] = l0[k::36]
s[2::4] = m1[k::36]
s[3::4] = l1[k::36]
for t in range(3):
c[t,k,:] = s[(t*self.nch):((t+1)*self.nch)]
return c
示例8: read_MSR_depth_ims
def read_MSR_depth_ims(depth_file, resize='VGA'):
''' Extracts depth images and masks from the MSR Daily Activites dataset
---Parameters---
depth_file : filename for set of depth images (.bin file)
'''
file_ = open(depth_file, 'rb')
''' Get header info '''
frames = np.fromstring(file_.read(4), dtype=np.int32)[0]
cols = np.fromstring(file_.read(4), dtype=np.int32)[0]
rows = np.fromstring(file_.read(4), dtype=np.int32)[0]
''' Get depth/mask image data '''
data = file_.read()
'''
Depth images and mask images are stored together per row.
Thus we need to extract each row of size n_cols+n_rows
'''
dt = np.dtype([('depth', np.int32, cols), ('mask', np.uint8, cols)])
''' raw -> usable images '''
frame_data = np.fromstring(data, dtype=dt)
depthIms = frame_data['depth'].astype(np.uint16).reshape([frames, rows, cols])
maskIms = frame_data['mask'].astype(np.uint16).reshape([frames, rows, cols])
if resize == 'VGA':
# embed()
depthIms = np.dstack([cv2.resize(depthIms[d,:,:], (640,480)) for d in xrange(len(depthIms))])
maskIms = np.dstack([cv2.resize(maskIms[d,:,:], (640,480)) for d in xrange(len(maskIms))])
return depthIms, maskIms
示例9: read_wave
def read_wave(filename='sound.wav'):
"""Reads a wave file.
filename: string
returns: Wave
"""
fp = open_wave(filename, 'r')
nchannels = fp.getnchannels()
nframes = fp.getnframes()
sampwidth = fp.getsampwidth()
framerate = fp.getframerate()
z_str = fp.readframes(nframes)
fp.close()
dtype_map = {1:numpy.int8, 2:numpy.int16, 3:'special', 4:numpy.int32}
if sampwidth not in dtype_map:
raise ValueError('sampwidth %d unknown' % sampwidth)
if sampwidth == 3:
xs = numpy.fromstring(z_str, dtype=numpy.int8).astype(numpy.int32)
ys = (xs[2::3] * 256 + xs[1::3]) * 256 + xs[0::3]
else:
ys = numpy.fromstring(z_str, dtype=dtype_map[sampwidth])
# if it's in stereo, just pull out the first channel
if nchannels == 2:
ys = ys[::2]
wave = Wave(ys, framerate)
return wave
示例10: _read_data
def _read_data(record, start, end, info):
"""Read the binary data for each signal"""
datfile = record + '.dat'
samp_to_read = end - start
# verify against first value in header
with open(datfile, 'rb') as f:
data = _arr_to_data(numpy.fromstring(f.read(3),
dtype=numpy.uint8).reshape(1,3))
if [data[0, 2], data[0, 3]] != info['first_values']:
warnings.warn(
'First value from dat file does not match value in header')
# read into an array with 3 bytes in each row
with open(datfile, 'rb') as f:
f.seek(start*3)
arr = numpy.fromstring(f.read(3*samp_to_read),
dtype=numpy.uint8).reshape((samp_to_read, 3))
data = _arr_to_data(arr)
# adjust zerovalue and gain
data[:, 2] = (data[:, 2] - info['zero_values'][0]) / info['gains'][0]
data[:, 3] = (data[:, 3] - info['zero_values'][1]) / info['gains'][1]
# time columns
data[:, 0] = numpy.arange(start, end) # elapsed time in samples
data[:, 1] = (numpy.arange(samp_to_read) + start
) / info['samp_freq'] # in sec
return data
示例11: read
def read(self, fname, dim1, dim2, offset=0, bytecode="int32", endian="<"):
"""
Read a binary image
Parameters : fname, dim1, dim2, offset, bytecode, endian
fname : file name : str
dim1,dim2 : image dimensions : int
offset : size of the : int
bytecode among : "int8","int16","int32","int64","uint8","uint16","uint32","uint64","float32","float64",...
endian among short or long endian ("<" or ">")
"""
self.filename = fname
self.dim1 = dim1
self.dim2 = dim2
self.bytecode = bytecode
f = open(self.filename, "rb")
dims = [dim2, dim1]
bpp = len(numpy.array(0, bytecode).tostring())
size = dims[0] * dims[1] * bpp
f.seek(offset)
rawData = f.read(size)
if self.swap_needed(endian):
data = numpy.fromstring(rawData, bytecode).byteswap().reshape(tuple(dims))
else:
data = numpy.fromstring(rawData, bytecode).reshape(tuple(dims))
self.data = data
return self
示例12: EMC_ReadData
def EMC_ReadData(filePath) :
""" USAGE:
reads a CSR sparse matrix from file and converts it
to a Matrix library object in a CSR format
PARAMETERS:
filePath - full/relative path to the data file
"""
# open file for reading
inFile = open(filePath, "r")
# read matrix shape
matrixShape = numpy.fromstring(inFile.readline(), dtype = 'int', sep = ',');
# read matrix data, indices and indptr
data = numpy.fromstring(inFile.readline(), dtype = 'float', sep = ',');
indices = numpy.fromstring(inFile.readline(), dtype = 'int', sep = ',');
indptr = numpy.fromstring(inFile.readline(), dtype = 'int', sep = ',');
# close file
inFile.close()
return sparse.csr_matrix((data, indices, indptr),
shape = (matrixShape[0], matrixShape[1]))
示例13: _triage_write
def _triage_write(key, value, root, comp_kw):
if isinstance(value, dict):
sub_root = _create_titled_group(root, key, 'dict')
for key, sub_value in value.items():
if not isinstance(key, string_types):
raise TypeError('All dict keys must be strings')
_triage_write('key_{0}'.format(key), sub_value, sub_root, comp_kw)
elif isinstance(value, (list, tuple)):
title = 'list' if isinstance(value, list) else 'tuple'
sub_root = _create_titled_group(root, key, title)
for vi, sub_value in enumerate(value):
_triage_write('idx_{0}'.format(vi), sub_value, sub_root, comp_kw)
elif isinstance(value, type(None)):
_create_titled_dataset(root, key, 'None', [False])
elif isinstance(value, (int, float)):
if isinstance(value, int):
title = 'int'
else: # isinstance(value, float):
title = 'float'
_create_titled_dataset(root, key, title, np.atleast_1d(value))
elif isinstance(value, string_types):
if isinstance(value, text_type): # unicode
value = np.fromstring(value.encode('utf-8'), np.uint8)
title = 'unicode'
else:
value = np.fromstring(value.encode('ASCII'), np.uint8)
title = 'ascii'
_create_titled_dataset(root, key, title, value, comp_kw)
elif isinstance(value, np.ndarray):
_create_titled_dataset(root, key, 'ndarray', value)
else:
raise TypeError('unsupported type %s' % type(value))
示例14: ReadWav
def ReadWav(filename):
wav = None
try:
wav = wave.open(filename, 'r')
channels = wav.getnchannels()
sample_bytes = wav.getsampwidth()
wav_data = wav.readframes(wav.getnframes())
if (sample_bytes == 2):
wav_array = numpy.fromstring(wav_data, dtype=numpy.int16)
elif (sample_bytes == 1):
wav_array = numpy.fromstring(wav_data, dtype=numpy.int8)
else:
raise ValueError('Sample depth of %d bytes not supported' % sample_bytes)
float_array = numpy.zeros(wav_array.shape[0] / channels)
for i in range(channels):
float_array += wav_array[i::channels]
float_array /= max(abs(float_array))
finally:
if wav:
wav.close()
return float_array
示例15: read_ermapper
def read_ermapper(self, ifile):
'''Read in a DEM file and associated .ers file'''
ers_index = ifile.find('.ers')
if ers_index > 0:
data_file = ifile[0:ers_index]
header_file = ifile
else:
data_file = ifile
header_file = ifile + '.ers'
self.header = self.read_ermapper_header(header_file)
nroflines = int(self.header['nroflines'])
nrofcellsperlines = int(self.header['nrofcellsperline'])
self.data = self.read_ermapper_data(data_file, offset=int(self.header['headeroffset']))
self.data = numpy.reshape(self.data,(nroflines,nrofcellsperlines))
longy = numpy.fromstring(self.getHeaderParam('longitude'), sep=':')
latty = numpy.fromstring(self.getHeaderParam('latitude'), sep=':')
self.deltalatitude = float(self.header['ydimension'])
self.deltalongitude = float(self.header['xdimension'])
if longy[0] < 0:
self.startlongitude = longy[0]+-((longy[1]/60)+(longy[2]/3600))
self.endlongitude = self.startlongitude - int(self.header['nrofcellsperline'])*self.deltalongitude
else:
self.startlongitude = longy[0]+(longy[1]/60)+(longy[2]/3600)
self.endlongitude = self.startlongitude + int(self.header['nrofcellsperline'])*self.deltalongitude
if latty[0] < 0:
self.startlatitude = latty[0]-((latty[1]/60)+(latty[2]/3600))
self.endlatitude = self.startlatitude - int(self.header['nroflines'])*self.deltalatitude
else:
self.startlatitude = latty[0]+(latty[1]/60)+(latty[2]/3600)
self.endlatitude = self.startlatitude + int(self.header['nroflines'])*self.deltalatitude