def test_getobj_getlock_obj(self):
arr1 = self.Array('i', list(range(10)))
lock1 = arr1.get_lock()
obj1 = arr1.get_obj()
arr2 = self.Array('i', list(range(10)), lock=None)
lock2 = arr2.get_lock()
obj2 = arr2.get_obj()
lock = self.Lock()
arr3 = self.Array('i', list(range(10)), lock=lock)
lock3 = arr3.get_lock()
obj3 = arr3.get_obj()
self.assertEqual(lock, lock3)
arr4 = self.Array('i', range(10), lock=False)
self.assertFalse(hasattr(arr4, 'get_lock'))
self.assertFalse(hasattr(arr4, 'get_obj'))
self.assertRaises(AttributeError,
self.Array, 'i', range(10), lock='notalock')
arr5 = self.RawArray('i', range(10))
self.assertFalse(hasattr(arr5, 'get_lock'))
self.assertFalse(hasattr(arr5, 'get_obj'))
#
#
#
python类Array()的实例源码
def _create_xs_shared(xs):
"""Create shared variable for data (xs).
"""
n = len(xs.reshape(-1))
xs_shared_base = multiprocessing.Array(ctypes.c_double, n)
shape = xs.shape
view = np.ctypeslib.as_array(xs_shared_base.get_obj())
view = view.reshape(shape)
view[:] = xs[:]
del view
xs_shared = (xs_shared_base, shape)
return xs_shared
def init_net(size, words_num, contexts_num):
tmp = np.random.uniform(low=-0.5/size, high=0.5/size, size=(words_num, size))
syn0 = np.ctypeslib.as_ctypes(tmp)
syn0 = Array(syn0._type_, syn0, lock=False)
tmp = np.zeros(shape=(contexts_num, size))
syn1 = np.ctypeslib.as_ctypes(tmp)
syn1 = Array(syn1._type_, syn1, lock=False)
return (syn0, syn1)
def run(args):
# create dummy environment to be able to create model
env = gym.make(args.environment)
assert isinstance(env.observation_space, Box)
assert isinstance(env.action_space, Discrete)
print("Observation space:", env.observation_space)
print("Action space:", env.action_space)
# create main model
model = create_model(env, args)
model.summary()
env.close()
# force runner processes to use cpu
os.environ["CUDA_VISIBLE_DEVICES"] = ""
# for better compatibility with Theano and Tensorflow
multiprocessing.set_start_method('spawn')
# create shared buffer for sharing weights
blob = pickle.dumps(model.get_weights(), pickle.HIGHEST_PROTOCOL)
shared_buffer = Array('c', len(blob))
shared_buffer.raw = blob
# create fifos and threads for all runners
fifos = []
for i in range(args.num_runners):
fifo = Queue(args.queue_length)
fifos.append(fifo)
process = Process(target=runner, args=(shared_buffer, fifo, args))
process.start()
# start trainer in main thread
trainer(model, fifos, shared_buffer, args)
def test_continuous_send_dialog(self):
self.add_signal_to_form("esaver.complex")
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(0.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.ui.spinBoxNRepeat.setValue(2)
continuous_send_dialog.ui.btnStart.click()
QTest.qWait(100)
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)
self.__close_dialog(continuous_send_dialog)
def __init__(self, size: int):
self.__data = Array("f", 2*size)
self.size = size
self.__left_index = Value("L", 0)
self.__right_index = Value("L", 0)
self.__length = Value("L", 0)
def perform_filter(result_array: Array, data, f_low, f_high, filter_bw):
result_array = np.frombuffer(result_array.get_obj(), dtype=np.complex64)
result_array[:] = Filter.apply_bandpass_filter(data, f_low, f_high, filter_bw=filter_bw)
def on_bandpass_filter_triggered(self, f_low: float, f_high: float):
self.filter_abort_wanted = False
QApplication.instance().setOverrideCursor(Qt.WaitCursor)
filter_bw = Filter.read_configured_filter_bw()
filtered = Array("f", 2 * self.signal.num_samples)
p = Process(target=perform_filter, args=(filtered, self.signal.data, f_low, f_high, filter_bw))
p.daemon = True
p.start()
while p.is_alive():
QApplication.instance().processEvents()
if self.filter_abort_wanted:
p.terminate()
p.join()
QApplication.instance().restoreOverrideCursor()
return
time.sleep(0.1)
filtered = np.frombuffer(filtered.get_obj(), dtype=np.complex64)
signal = self.signal.create_new(new_data=filtered.astype(np.complex64))
signal.name = self.signal.name + " filtered with f_low={0:.4n} f_high={1:.4n} bw={2:.4n}".format(f_low, f_high,
filter_bw)
self.signal_created.emit(signal)
QApplication.instance().restoreOverrideCursor()
def SetRhoUpdateFunc(Func=None):
global rho_update_func
rho_update_func = Func if Func else __default_rho_update_func
# Tuple of indices to identify the information package for each node. Actual
# length of specific package (list) may vary depending on node degree.
# X_NID: Node ID
# X_OBJ: CVXPY Objective
# X_VARS: CVXPY Variables (entry from node_variables structure)
# X_CON: CVXPY Constraints
# X_IND: Starting index into shared node_vals Array
# X_LEN: Total length (sum of dimensions) of all variables
# X_DEG: Number of neighbors
# X_NEIGHBORS: Placeholder for information about each neighbors
# Information for each neighbor is two entries, appended in order.
# Starting index of the corresponding z-value in edge_z_vals. Then for u.
def getValue(arr, index, length):
return numpy.array(arr[index:(index + length)])
# Write value of numpy array nparr (with given length) to a shared Array at
# the given starting index.
def writeValue(sharedarr, index, nparr, length):
if length == 1:
nparr = [nparr]
sharedarr[index:(index + length)] = nparr
# Write the values for all of the Variables involved in a given Objective to
# the given shared Array.
# variables should be an entry from the node_values structure.
def SetRhoUpdateFunc(Func=None):
global rho_update_func
rho_update_func = Func if Func else __default_rho_update_func
# Tuple of indices to identify the information package for each node. Actual
# length of specific package (list) may vary depending on node degree.
# X_NID: Node ID
# X_OBJ: CVXPY Objective
# X_VARS: CVXPY Variables (entry from node_variables structure)
# X_CON: CVXPY Constraints
# X_IND: Starting index into shared node_vals Array
# X_LEN: Total length (sum of dimensions) of all variables
# X_DEG: Number of neighbors
# X_NEIGHBORS: Placeholder for information about each neighbors
# Information for each neighbor is two entries, appended in order.
# Starting index of the corresponding z-value in edge_z_vals. Then for u.
def getValue(arr, index, length):
return numpy.array(arr[index:(index + length)])
# Write value of numpy array nparr (with given length) to a shared Array at
# the given starting index.
def SetRhoUpdateFunc(Func=None):
global rho_update_func
rho_update_func = Func if Func else __default_rho_update_func
# Tuple of indices to identify the information package for each node. Actual
# length of specific package (list) may vary depending on node degree.
# X_NID: Node ID
# X_OBJ: CVXPY Objective
# X_VARS: CVXPY Variables (entry from node_variables structure)
# X_CON: CVXPY Constraints
# X_IND: Starting index into shared node_vals Array
# X_LEN: Total length (sum of dimensions) of all variables
# X_DEG: Number of neighbors
# X_NEIGHBORS: Placeholder for information about each neighbors
# Information for each neighbor is two entries, appended in order.
# Starting index of the corresponding z-value in edge_z_vals. Then for u.
def getValue(arr, index, length):
return numpy.array(arr[index:(index + length)])
# Write value of numpy array nparr (with given length) to a shared Array at
# the given starting index.
def writeValue(sharedarr, index, nparr, length):
if length == 1:
nparr = [nparr]
sharedarr[index:(index + length)] = nparr
# Write the values for all of the Variables involved in a given Objective to
# the given shared Array.
# variables should be an entry from the node_values structure.
def SetRhoUpdateFunc(Func=None):
global rho_update_func
rho_update_func = Func if Func else __default_rho_update_func
# Tuple of indices to identify the information package for each node. Actual
# length of specific package (list) may vary depending on node degree.
# X_NID: Node ID
# X_OBJ: CVXPY Objective
# X_VARS: CVXPY Variables (entry from node_variables structure)
# X_CON: CVXPY Constraints
# X_IND: Starting index into shared node_vals Array
# X_LEN: Total length (sum of dimensions) of all variables
# X_DEG: Number of neighbors
# X_NEIGHBORS: Placeholder for information about each neighbors
# Information for each neighbor is two entries, appended in order.
# Starting index of the corresponding z-value in edge_z_vals. Then for u.
def getValue(arr, index, length):
return numpy.array(arr[index:(index + length)])
# Write value of numpy array nparr (with given length) to a shared Array at
# the given starting index.
def SetRhoUpdateFunc(Func=None):
global rho_update_func
rho_update_func = Func if Func else __default_rho_update_func
# Tuple of indices to identify the information package for each node. Actual
# length of specific package (list) may vary depending on node degree.
# X_NID: Node ID
# X_OBJ: CVXPY Objective
# X_VARS: CVXPY Variables (entry from node_variables structure)
# X_CON: CVXPY Constraints
# X_IND: Starting index into shared node_vals Array
# X_LEN: Total length (sum of dimensions) of all variables
# X_DEG: Number of neighbors
# X_NEIGHBORS: Placeholder for information about each neighbors
# Information for each neighbor is two entries, appended in order.
# Starting index of the corresponding z-value in edge_z_vals. Then for u.
def getValue(arr, index, length):
return numpy.array(arr[index:(index + length)])
# Write value of numpy array nparr (with given length) to a shared Array at
# the given starting index.