Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
n_mvs = 100
generator_list = [random_point_pair, random_line, random_circle, \
random_sphere, random_plane]
for generator in generator_list:
mv_a_array = [generator() for i in range(n_mvs)]
mv_b_array = [generator() for i in range(n_mvs)]
print(mv_a_array)
print('Starting kernel')
t = time.time()
mv_c_array = object_set_cost_cuda_mvs(mv_a_array, mv_b_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
t = time.time()
mv_d_array = object_set_cost_matrix(mv_a_array, mv_b_array, object_type='generic')
print(time.time() - t)
try:
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
except:
print(mv_c_array[0,:])
print(mv_d_array[0,:])
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
def test_line_set_cost(self):
n_mvs = 50
mv_a_array = [random_line() for i in range(n_mvs)]
mv_b_array = [random_line() for i in range(n_mvs)]
print(mv_a_array)
print('Starting kernel')
t = time.time()
mv_c_array = line_set_cost_cuda_mvs(mv_a_array, mv_b_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
t = time.time()
mv_d_array = object_set_cost_matrix(mv_a_array, mv_b_array, object_type='generic')
print(time.time() - t)
try:
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
except:
print(mv_c_array[0,:])
print(mv_d_array[0,:])
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
def assign_measurements_to_objects_matrix(objects, objects_measurements, object_type='generic',
cuda=False, symmetric=False):
"""
Assigns each object in objects_measurements to one in objects based on minimum cost
"""
if cuda:
matrix = object_set_cost_cuda_mvs(objects, objects_measurements)
else:
matrix = object_set_cost_matrix(objects, objects_measurements,
object_type=object_type,
symmetric=symmetric)
labels = np.nanargmin(matrix, axis=0)
costs = np.array([matrix[l, i] for i, l in enumerate(labels)])
return [labels, costs]
def simplify_scene_recursive(objects, threshold):
# Calculate the cost between this object and every other object
cost_mat = object_set_cost_matrix(objects, objects)
# Set the diagonals to threshold*2
np.fill_diagonal(cost_mat, np.finfo(float).max)
# Take the smallest value
min_index = np.unravel_index(np.nanargmin(cost_mat), cost_mat.shape)
min_value = cost_mat[min_index]
# If the smallest value is above the value then return the current object set
if min_value >= threshold:
return objects
# If it is less than the threshold then mean the two objects and recurse
else:
object_a = objects[min_index[0]]
object_b = objects[min_index[1]]
objects[min_index[1]] = average_objects([object_a, object_b])
del objects[min_index[0]]
return simplify_scene_recursive(objects, threshold)
def simplify_scene(objects, threshold, symmetric=False):
o_copy = [o for o in objects]
cost_matrix = object_set_cost_matrix(o_copy, o_copy, symmetric=symmetric)
np.fill_diagonal(cost_matrix, np.finfo(float).max)
while True:
# Take the smallest value
min_index = np.unravel_index(np.nanargmin(cost_matrix), cost_matrix.shape)
min_value = cost_matrix[min_index]
# If the smallest value is above the value then return the current object set
if min_value >= threshold:
return [o for o in objects if not (o == 0 * e1)]
# If it is less than the threshold then mean the two objects and recurse
else:
a_ind = min_index[0]
b_ind = min_index[1]
object_a = objects[a_ind]
object_b = objects[b_ind]
if symmetric:
objects[b_ind] = average_symmetric(object_a, object_b)
# print('Checking other starting possibilities')
min_cost = object_set_cost_matrix_sum([object_start], objects)
for a in objects:
this_cost = object_set_cost_matrix_sum([a], objects)
if this_cost < min_cost:
min_cost = this_cost
object_start = a
# print('Beginning optimisation')
rotor, cost = estimate_rotor_objects(objects, [object_start])
final_object = (rotor * object_start * ~rotor).normal()
elif averaging_method == 'unweighted':
final_object = average_objects(objects)
elif averaging_method == 'weighted':
final_object = 1.0*object_start
for i in range(10):
weights = (1.0 / (object_set_cost_matrix([final_object], objects) + 0.0001)).flatten()
final_object = average_objects(objects, weights=weights)
else:
raise ValueError('No averaging method defined')
else:
return 1.0*object_start
return final_object
if min_value >= threshold:
return [o for o in objects if not (o == 0 * e1)]
# If it is less than the threshold then mean the two objects and recurse
else:
a_ind = min_index[0]
b_ind = min_index[1]
object_a = objects[a_ind]
object_b = objects[b_ind]
if symmetric:
objects[b_ind] = average_symmetric(object_a, object_b)
else:
objects[b_ind] = average_objects([object_a, object_b])
objects[a_ind] = 0*e1
cost_matrix[:, a_ind] = np.finfo(float).max
cost_matrix[a_ind, :] = np.finfo(float).max
cost_to_others = object_set_cost_matrix([objects[b_ind]], objects, symmetric=symmetric)
cost_matrix[:, b_ind] = cost_to_others
cost_matrix[b_ind, :] = cost_to_others
np.fill_diagonal(cost_matrix, np.finfo(float).max)