本文整理汇总了Python中multiprocessing.Array.get_obj方法的典型用法代码示例。如果您正苦于以下问题:Python Array.get_obj方法的具体用法?Python Array.get_obj怎么用?Python Array.get_obj使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Array
的用法示例。
在下文中一共展示了Array.get_obj方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: shared_np_array
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def shared_np_array(shape, data=None, integer=False):
"""Create a new numpy array that resides in shared memory.
Parameters
----------
shape : tuple of ints
The shape of the new array.
data : numpy.array
Data to copy to the new array. Has to have the same shape.
integer : boolean
Whether to use an integer array. Defaults to False which means
float array.
"""
size = np.prod(shape)
if integer:
array = Array(ctypes.c_int64, int(size))
np_array = np.frombuffer(array.get_obj(), dtype="int64")
else:
array = Array(ctypes.c_double, int(size))
np_array = np.frombuffer(array.get_obj())
np_array = np_array.reshape(shape)
if data is not None:
if len(shape) != len(data.shape):
raise ValueError("`data` must have the same dimensions"
"as the created array.")
same = all(x == y for x, y in zip(shape, data.shape))
if not same:
raise ValueError("`data` must have the same shape"
"as the created array.")
np_array[:] = data
return np_array
示例2: get_predict
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def get_predict(args, ortho, model):
xp = cuda.cupy if args.gpu >= 0 else np
args.h_limit, args.w_limit = ortho.shape[0], ortho.shape[1]
args.canvas_h = args.h_limit
args.canvas_w = args.w_limit
# to share 'canvas' between different threads
canvas_ = Array(ctypes.c_float, args.canvas_h * args.canvas_w * args.channels)
canvas = np.ctypeslib.as_array(canvas_.get_obj())
canvas = canvas.reshape((args.canvas_h, args.canvas_w, args.channels))
# prepare queues and threads
patch_queue = Queue(maxsize=5)
preds_queue = Queue()
patch_worker = Process(target=create_minibatch, args=(args, ortho, patch_queue))
canvas_worker = Process(target=tile_patches, args=(args, canvas, preds_queue))
patch_worker.start()
canvas_worker.start()
while True:
minibatch = patch_queue.get()
if minibatch is None:
break
minibatch = Variable(xp.asarray(minibatch, dtype=xp.float32), volatile=True)
preds = model(minibatch, None).data
if args.gpu >= 0:
preds = xp.asnumpy(preds)
[preds_queue.put(pred) for pred in preds]
preds_queue.put(None)
patch_worker.join()
canvas_worker.join()
return canvas
示例3: steps_multiprocessing
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def steps_multiprocessing(self, number_of_steps, plot, plot_every_n):
"""
Equal to take_steps but using multiprocesing.
Parameters
----------
number_of_steps : float
Total number of time steps.
plot : object
make_plot Object.
plot_every_n : float
Every few time steps are going on a plot.
"""
shared_array_base = Array(ctypes.c_double, len(self.bodies)*2)
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
shared_array = shared_array.reshape(2, len(self.bodies))
counter = Value(ctypes.c_int64, 0)
end_plot = Value(ctypes.c_int8, 1)
old_counter = 1
rk_fun = Process(target = self.rk_fun_task, args=(number_of_steps, plot_every_n, shared_array, end_plot, counter))
plot_fun = Process(target = self.plot_fun_task, args=(old_counter, shared_array, end_plot, counter, plot))
rk_fun.start()
plot_fun.start()
rk_fun.join()
plot_fun.join()
示例4: conv_single_image
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def conv_single_image(image):
shared_array_base = Array(ctypes.c_double, image.size)
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
shared_array = shared_array.reshape(image.shape)
shared_array[:] = image
return shared_array
示例5: calculatePearsonCorrelationMatrixMultiprocessing
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def calculatePearsonCorrelationMatrixMultiprocessing(matrix, axis=0, symmetrical=True, getpvalmat=False):
if axis == 1:
matrix = matrix.T
nRows = matrix.shape[0]
# create shared array that can be used from multiple processes
output_r_arr = Array(ctypes.c_double, matrix.shape[0] * matrix.shape[0])
# then in each new process create a new numpy array using:
output_r = np.frombuffer(output_r_arr.get_obj()) # mp_arr and arr share the same memory
# make it two-dimensional
output_r = output_r.reshape((matrix.shape[0], matrix.shape[0])) # b and arr share the same memory
# output_r = np.zeros((nRows,nRows)) # old version
output_p_arr = Array(ctypes.c_double, matrix.shape[0] * matrix.shape[0])
output_p = np.frombuffer(output_p_arr.get_obj())
output_p = output_p.reshape((matrix.shape[0], matrix.shape[0]))
print 'Calculating Pearson R for each row, multithreaded'
print mp.cpu_count(), 'processes in pool'
pool = None
try:
pool = mp.Pool(mp.cpu_count(),
initializer=_init_pool,
initargs=(matrix, output_r_arr, output_p_arr,
nRows, symmetrical))
# bar = tqdm(total=nRows*nRows/2)
# tqdm.write('Calculating Pearson R for each row, multithreaded')
for result in tqdm(pool.imap_unordered(_f, range(0, nRows)), total=nRows):
# bar.update(result)
pass
# bar.close()
finally: # To make sure processes are closed in the end, even if errors happen
pool.close()
pool.join()
print output_r
if getpvalmat:
return output_r, output_p
else:
return output_r
示例6: initialize
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def initialize(self, nChannels, nSamples, windowSize=1, nptype='float32'):
'''
Initializes the buffer with a new raw array
Parameters
----------
nChannels : int
dimensionality of a single sample
nSamples : int
the buffer capacity in samples
windowSize : int, optional
optional, the size of the window to be used for reading the
data. The pocket of the this size will be created
nptype : string, optional
the type of the data to be stored
'''
self.__initialized = True
# checking parameters
if nChannels < 1:
self.logger.warning('nChannels must be a positive integer, setting to 1')
nChannels = 1
if nSamples < 1:
self.logger.warning('nSamples must be a positive integer, setting to 1')
nSamples = 1
if windowSize < 1:
self.logger.warning('wondowSize must be a positive integer, setting to 1')
windowSize = 1
# initializing
sizeBytes = c.sizeof(BufferHeader) + \
(nSamples + windowSize) * nChannels * np.dtype(nptype).itemsize
raw = Array('c', sizeBytes)
hdr = BufferHeader.from_buffer(raw.get_obj())
hdr.bufSizeBytes = nSamples * nChannels * np.dtype(nptype).itemsize
hdr.pocketSizeBytes = windowSize * nChannels * np.dtype(nptype).itemsize
hdr.dataType = datatypes.get_code(nptype)
hdr.nChannels = nChannels
hdr.nSamplesWritten = 0
self.initialize_from_raw(raw.get_obj())
示例7: initialize
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def initialize(self, n_channels, n_samples, np_dtype='float32'):
"""Initializes the buffer with a new array."""
logger.debug('Initializing {}x{} {} buffer.'.format(n_channels, n_samples, np_dtype))
# check parameters
if n_channels < 1 or n_samples < 1:
logger.error('n_channels and n_samples must be a positive integer')
raise SharedBufferError(1)
size_bytes = ct.sizeof(SharedBufferHeader) + n_samples * n_channels * np.dtype(np_dtype).itemsize
raw = Array('c', size_bytes)
hdr = SharedBufferHeader.from_buffer(raw.get_obj())
hdr.bufSizeBytes = size_bytes - ct.sizeof(SharedBufferHeader)
hdr.dataType = DataTypes.get_code(np_dtype)
hdr.nChannels = n_channels
hdr.nSamples = n_samples
hdr.position = 0
self.initialize_from_raw(raw.get_obj())
示例8: load_data
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def load_data(args, input_q, minibatch_q):
c = args.channel
s = args.size
d = args.joint_num * 2
input_data_base = Array(ctypes.c_float, args.batchsize * c * s * s)
input_data = np.ctypeslib.as_array(input_data_base.get_obj())
input_data = input_data.reshape((args.batchsize, c, s, s))
label_base = Array(ctypes.c_float, args.batchsize * d)
label = np.ctypeslib.as_array(label_base.get_obj())
label = label.reshape((args.batchsize, d))
x_queue, o_queue = Queue(), Queue()
workers = [Process(target=transform,
args=(args, x_queue, args.datadir, args.fname_index,
args.joint_index, o_queue))
for _ in range(args.batchsize)]
for w in workers:
w.start()
while True:
x_batch = input_q.get()
if x_batch is None:
break
# data augmentation
for x in x_batch:
x_queue.put(x)
j = 0
while j != len(x_batch):
a, b = o_queue.get()
input_data[j] = a
label[j] = b
j += 1
minibatch_q.put([input_data, label])
for _ in range(args.batchsize):
x_queue.put(None)
for w in workers:
w.join()
示例9: CameraStreamer
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
class CameraStreamer(Process):
def __init__(self, stayAliveObj=None, frameCountObj=None, imageObj=None, imageShapeObj=None):
Process.__init__(self)
print "CameraStreamer.__init__(): [pid: {}, OS pid: {}]".format(self.pid, os.getpid())
# * Store references to and/or create shared objects
self.stayAliveObj = stayAliveObj if stayAliveObj is not None else Value(c_bool, True)
self.frameCountObj = frameCountObj if frameCountObj is not None else Value('i', 0)
self.imageShapeObj = imageShapeObj if imageShapeObj is not None else Array('i', (camera_frame_height, camera_frame_width, camera_frame_depth))
if imageObj is not None:
# ** Use supplied shared image object
self.imageObj = imageObj
else:
# ** Create shared image object
image = np.zeros((camera_frame_height, camera_frame_width, camera_frame_depth), dtype=np.uint8) # create an image
imageShape = image.shape # store original shape
imageSize = image.size # store original size (in bytes)
image.shape = imageSize # flatten numpy array
self.imageObj = Array(c_ubyte, image) # create a synchronized shared array object
def run(self):
print "CameraStreamer.run(): [pid: {}, OS pid: {}]".format(self.pid, os.getpid())
# * Interpret shared objects properly (NOTE this needs to happen in the child process)
self.image = ctypeslib.as_array(self.imageObj.get_obj()) # get flattened image array
self.image.shape = ctypeslib.as_array(self.imageShapeObj.get_obj()) # restore original shape
# * Open camera and set desired capture properties
self.camera = cv2.VideoCapture(0)
if self.camera.isOpened():
result_width = self.camera.set(cv.CV_CAP_PROP_FRAME_WIDTH, camera_frame_width)
result_height = self.camera.set(cv.CV_CAP_PROP_FRAME_HEIGHT, camera_frame_height)
print "CameraStreamer.run(): Camera frame size set to {width}x{height} (result: {result_width}, {result_height})".format(width=camera_frame_width, height=camera_frame_height, result_width=result_width, result_height=result_height)
else:
print "CameraStreamer.run(): Unable to open camera; aborting..."
self.stayAliveObj.value = False
return
# * Keep reading frames into shared image until stopped or read error occurs
while self.stayAliveObj.value:
try:
#print "CameraStreamer.run(): Frame # {}, stay alive? {}".format(self.frameCountObj.value, self.stayAliveObj.value) # [debug]
isOkay, frame = self.camera.read()
if not isOkay:
self.stayAliveObj.value = False
self.frameCountObj.value = self.frameCountObj.value + 1
self.image[:] = frame
except KeyboardInterrupt:
self.stayAliveObj.value = False
# * Clean-up
self.camera.release()
示例10: _calculate_phi
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def _calculate_phi(self, x):
C = self.workers
neurons = self.neurons
mu = self.mu
sigmas = self.sigmas
phi = self.phi = None
n = self.n
def heavy_lifting(c, phi):
s = jobs[c][1] - jobs[c][0]
for k, i in enumerate(xrange(jobs[c][0], jobs[c][1])):
for j in xrange(neurons):
# phi[i, j] = metrics(x[i,:], mu[j])**3)
# phi[i, j] = plateSpine(x[i,:], mu[j]))
# phi[i, j] = invMultiQuadric(x[i,:], mu[j], sigmas[j]))
phi[i, j] = multiQuadric(x[i,:], mu[j], sigmas[j])
# phi[i, j] = gaussian(x[i,:], mu[j], sigmas[j]))
if k % 1000 == 0:
percent = true_divide(k, s)*100
print c, ': {:2.2f}%'.format(percent)
print c, ': Done'
# distributing the work between 4 workers
shared_array = Array(c_double, n * neurons)
phi = frombuffer(shared_array.get_obj())
phi = phi.reshape((n, neurons))
jobs = []
workers = []
p = n / C
m = n % C
for c in range(C):
jobs.append((c*p, (c+1)*p + (m if c == C-1 else 0)))
worker = Process(target = heavy_lifting, args = (c, phi))
workers.append(worker)
worker.start()
for worker in workers:
worker.join()
return phi
示例11: Buffer
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
class Buffer(object):
def __init__(self, size=1000, data_type="int32"):
self.data_type = data_type
self.head = Value("i", 0)
self.ring_buffer = Array(data_type[0], range(size))
self.size = size
for i in range(size):
self.ring_buffer[i] = 0 # probably really slow but not done often
def get_head_value(self):
return self.ring_buffer[self.head.value]
def get_buffer(self):
buf = np.frombuffer(self.ring_buffer.get_obj(), dtype=self.data_type)
return np.concatenate((buf[self.head.value + 1 :], buf[0 : self.head.value]))
def push(self, v):
self.head.value = self.head.value + 1
if self.head.value == self.size:
self.head.value = 0
self.ring_buffer[self.head.value] = v # randint(0,10)
示例12: test_continuous_send_dialog
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def test_continuous_send_dialog(self):
self.add_signal_to_form("esaver.coco")
self.__add_first_signal_to_generator()
port = self.get_free_port()
gframe = self.form.generator_tab_controller
expected = np.zeros(gframe.total_modulated_samples, dtype=np.complex64)
expected = gframe.modulate_data(expected)
current_index = Value("L", 0)
buffer = Array("f", 4 * len(expected))
process = Process(target=receive, args=(port, current_index, 2 * len(expected), buffer))
process.daemon = True
process.start()
time.sleep(1) # ensure server is up
ContinuousModulator.BUFFER_SIZE_MB = 10
continuous_send_dialog = self.__get_continuous_send_dialog()
continuous_send_dialog.device.set_client_port(port)
continuous_send_dialog.device_settings_widget.ui.spinBoxNRepeat.setValue(2)
continuous_send_dialog.ui.btnStart.click()
QTest.qWait(1000)
time.sleep(1)
process.join(1)
# CI sometimes swallows a sample
self.assertGreaterEqual(current_index.value, len(expected) - 1)
buffer = np.frombuffer(buffer.get_obj(), dtype=np.complex64)
for i in range(len(expected)):
self.assertEqual(buffer[i], expected[i], msg=str(i))
continuous_send_dialog.ui.btnStop.click()
continuous_send_dialog.ui.btnClear.click()
QTest.qWait(1)
continuous_send_dialog.close()
示例13: update_c
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
update_c(c1.ravel(),c2.ravel()) #########
print "Running main loop..."
while runflag:
#print uflag1.value
#print uflag2.value
dat1 = uflag1.value
dat2 = uflag2.value
frame = vs.read() #for testing
#datrecv1 = pipeul1.recv() #Blocking
#datrecv2 = pipeul2.recv() #Blocking
#if datrecv1[0] and datrecv2[0]:
if (dat1 == 2) and (dat2 ==2):
#pos3d = calc3d(datrecv2[2].ravel(),datrecv1[2].ravel()) ########
with uarray1.get_lock():
arr1 = np.frombuffer(uarray1.get_obj())
with uarray2.get_lock():
arr2 = np.frombuffer(uarray2.get_obj())
pos3d = calc3d(arr1,arr2)
#print np.asarray(pos3d)
imgpts, jac = cv2.projectPoints(np.float32([np.asarray(pos3d)]).reshape(-1,3), rvecs, tvecs, mtx, dist)
cv2.rectangle(frame,(int(imgpts[0,0,0]) - 2,int(imgpts[0,0,1]) - 2),(int(imgpts[0,0,0]) + 2 ,int(imgpts[0,0,1]) + 2),(255,0,0),1)
#elif not datrecv1[1] or not datrecv2[1]:
elif (dat1 == 0) or (dat2 ==0):
runflag = False
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print "Waiting for both processes to stop..."
#while pipeul1.poll(0.5) or pipeul2.poll(0.5):
示例14: Array
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
yend = nrows
for xstep in xstart:
if xstep + xtile < ncols:
xend = xstep + xtile
else:
xend = ncols
l.append((ystep,yend,xstep, xend))
#et = datetime.datetime.now()
#print 'get_tile2 time taken: ', et - st
return l
if __name__ == '__main__':
a = numpy.random.randint(0,101, (100,100))
b = Array('i', 100*100)
#out = numpy.zeros_like(a)
arr = numpy.frombuffer(b.get_obj())
print arr.shape
#out = numpy.frombuffer(b.get_obj()).reshape(100,100)
print 'a'
print a
print 'out'
print out
tiles = get_tile2(a, 12,12)
pool = Pool(processes=2)
#pool.apply(func, args=(a,tiles, out))
result=[pool.apply_async(func, (a, tile, out)) for tile in tiles]
print 'a'
print a
print 'out'
print out
print 'result'
示例15: findStandard
# 需要导入模块: from multiprocessing import Array [as 别名]
# 或者: from multiprocessing.Array import get_obj [as 别名]
def findStandard(img, downSample=True):
print 'Downsample'
if downSample:
s = img.shape
img = cv2.resize(img, (int(s[1] * 2560 / s[0]), 2560))
total = numpy.zeros(img.shape).astype(numpy.uint8)
labimg = cv2.cvtColor(img, cv2.COLOR_BGR2LAB).astype(numpy.float32)
thresh = 200
margin = 0.2 #black in between line on the standard is roughly 0.2 of the square width
squares = []
output = numpy.copy(img)
#gen circular mask for houghGrid
a, b = 30,30
rad = 30
y,x = numpy.ogrid[-a:61-a, -b:61-b]
mask = x*x + y*y <= rad*rad
horizontalOffsets = []
verticalOffsets = []
#for calculating orientation
if HIGH_MEMORY:
size = labimg.size
print labimg.size, labimg.shape
sharedlabimg_base = Array(ctypes.c_float, size)
p = Pool(initializer=initProcess,initargs=(sharedlabimg_base,))
sharedlabimg = numpy.frombuffer(sharedlabimg_base.get_obj(), dtype=numpy.float32)
sharedlabimg = sharedlabimg.reshape(labimg.shape)
print labimg.size, labimg.shape
print sharedlabimg.size, sharedlabimg.shape
sharedlabimg[:,:,:]=labimg[:,:,:]
'''
#test to see what sharedlabimg looks like after that
sharedlabimg = numpy.frombuffer(sharedlabimg_base.get_obj(), dtype=numpy.float32)
sharedlabimg = sharedlabimg.reshape(labimg.shape)
cv2.imshow('test2', labimg[::6,::6])
cv2.imshow('test', sharedlabimg[::6,::6])
if cv2.waitKey(0) == ord('q'):
quit()
'''
nMap = p.map(calcDistance, [(c,labimg.shape) for c in labStandardColors.reshape((labStandardColors.shape[0]*labStandardColors.shape[1],labStandardColors.shape[2]))])
#print 'Find squares of each color'
for r, row in enumerate(standardColors):
for c, color in enumerate(row):
labColor = labStandardColors[r][c]
print 'Calculating color ', labColor
#print 'Calculate lab distance'
if not HIGH_MEMORY:
n = numpy.linalg.norm(labimg-labColor, axis=2)
else:
n = nMap[r*len(row)+c]
n = n * 255 / n.max()
n = n.astype(numpy.uint8)
#print 'Threshold'
#n = cv2.adaptiveThreshold(n, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, int(n.shape[1]*0.02) | 1, 6)
ret, n = cv2.threshold(n, 50, 255, cv2.THRESH_BINARY_INV)
#print 'Morphology'
n = cv2.morphologyEx(n, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)))
#cv2.imshow(str(i*4+c), cv2.resize(n, dsize=(0,0), fx=0.2, fy=0.2))
#print 'Contours'
contours,h = cv2.findContours(n, cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE )
#sometimes findcontours doesn't reutnr numpy arrays
for i, contour in enumerate(contours):
contours[i] = numpy.array(contour)
toDraw = []
indices = []
#print 'Process contours'
for i, contour in enumerate(contours):
s = Square()
s, count = s.processContour(contour, i, contours, h, minWidth=(labimg.shape[1] / 100))
if s:
contours[i] = s
curSquares = []
for square in contours:
if isinstance(square, Square):
square.color = (int(color[0]), int(color[1]), int(color[2]))
curSquares.append(square)
labels = numpy.zeros((img.shape[0], img.shape[1])).astype(numpy.uint8)
means = []
#print 'Calculate LAB'
for i in range(0,len(curSquares)):
cv2.drawContours(labels, [curSquares[i].contour], -1, i+1, -1)
roi = cv2.boundingRect(curSquares[i].contour)
mean = cv2.mean(labimg[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2]] , numpy.array(labels[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2]] == i+1).astype(numpy.uint8))
curSquares[i].labColor = (mean[0], mean[1], mean[2])
##print curSquares[i].labColor, numpy.linalg.norm(curSquares[i].labColor-labColor)
#cv2.drawContours(total, [curSquares[i].contour], -1, (255,255,255), -1)
means.append((numpy.linalg.norm(curSquares[i].labColor-labColor), curSquares[i]))
means.sort(key=itemgetter(0), reverse=True)
colorYield[r][c] += len(means)
#.........这里部分代码省略.........