Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Performs a sequential rotor update based on the rotors between individual objects
Exits when a full rotation through all objects produces a very small update of rotor
"""
grade_list = [grade_obj(ob) for ob in query_model]
R_total = 1.0 + 0.0*e1
for j in range(n_iterations):
r_set = 1.0 + 0.0*e1
if random_sequence:
indices = random.sample(range(len(query_model)), len(query_model))
else:
indices = range(len(query_model))
for i in indices:
grade = grade_list[i]
new_obj = normalised(apply_rotor(query_model[i], R_total)(grade))
C1 = normalised(new_obj)
C2 = normalised(reference_model[i])
if abs(C1 + C2) < 0.0001:
C1 = -C1
if object_type == 'lines':
rroot = normalised(square_roots_of_rotor((rotor_between_objects(C1, C2)))[0])
else:
if motor:
if grade_obj(C1, 0.00001) == 4:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(motor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
r_set = normalised((rroot*r_set)(0, 2, 4))
R_total = normalised((rroot * R_total)(0, 2, 4))
def minimisation_func(x):
R = rotorconversion(x)
query_model_remapped = [normalised((apply_rotor(l, R))(grade_list[i])) for i, l in enumerate(query_model)]
return object_set_cost_sum(reference_model, query_model_remapped,
object_type=object_type, motor=motor,
symmetric=symmetric)
object_type=object_type, motor=motor,
symmetric=symmetric)
res = minimize(minimisation_func, x0, method='SLSQP', options={'ftol': 10.0 ** (-16),
'maxiter': 1000,
'disp': False})
if print_res:
print(res)
res = minimize(minimisation_func, res.x, method='L-BFGS-B', options={'ftol': 10.0**(-16),
'maxiter': 1000,
'disp': False,
'maxls': 40})
if print_res:
print(res)
rotor = rotorconversion(res.x)
query_model_remapped = [normalised((apply_rotor(l, rotor))(grade_list[i])) for i, l in enumerate(query_model)]
cost = object_set_cost_sum(reference_model, query_model_remapped, object_type=object_type, motor=motor,
symmetric=symmetric)
return rotor, cost
n_repeats = int(len(query_model)/2)
if objects_per_sample is None:
objects_per_sample = max(int(len(query_model)/10), 5)
min_cost = np.finfo(float).max
min_rotor = 1.0 + 0.0 * e1
for i in range(n_repeats):
indices = random.sample(range(len(reference_model)), objects_per_sample)
object_sample_reference = [reference_model[j] for j in indices]
object_sample_query = [query_model[j] for j in indices]
try:
rotor, e_flag = sequential_object_rotor_estimation(object_sample_reference, object_sample_query,
object_type=object_type)
except Exception:
rotor = 1.0 + 0*e1
print('ERROR')
query_model_remapped = [normalised(apply_rotor(l, rotor)) for l in object_sample_query]
new_cost = object_set_cost_sum(reference_model, query_model_remapped, object_type=object_type)
if new_cost < min_cost:
min_cost = new_cost
min_rotor = rotor
# print('SAMPLE: ', i, ' cost ', min_cost)
return min_rotor, min_cost
new_obj = normalised(apply_rotor(query_model[i], R_total)(grade))
C1 = normalised(new_obj)
C2 = normalised(reference_model[i])
if abs(C1 + C2) < 0.0001:
C1 = -C1
if object_type == 'lines':
rroot = normalised(square_roots_of_rotor((rotor_between_objects(C1, C2)))[0])
else:
if motor:
if grade_obj(C1, 0.00001) == 4:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(motor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
r_set = normalised((rroot*r_set)(0, 2, 4))
R_total = normalised((rroot * R_total)(0, 2, 4))
if rotor_cost(r_set) < cost_tolerance:
exit_flag = 0
return R_total, exit_flag
exit_flag = 1
return R_total, exit_flag
indices = range(len(query_model))
for i in indices:
grade = grade_list[i]
new_obj = normalised(apply_rotor(query_model[i], R_total)(grade))
C1 = normalised(new_obj)
C2 = normalised(reference_model[i])
if abs(C1 + C2) < 0.0001:
C1 = -C1
if object_type == 'lines':
rroot = normalised(square_roots_of_rotor((rotor_between_objects(C1, C2)))[0])
else:
if motor:
if grade_obj(C1, 0.00001) == 4:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(motor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
r_set = normalised((rroot*r_set)(0, 2, 4))
R_total = normalised((rroot * R_total)(0, 2, 4))
if rotor_cost(r_set) < cost_tolerance:
exit_flag = 0
return R_total, exit_flag
exit_flag = 1
return R_total, exit_flag
C1 = normalised(new_obj)
C2 = normalised(reference_model[i])
if abs(C1 + C2) < 0.0001:
C1 = -C1
if object_type == 'lines':
rroot = normalised(square_roots_of_rotor((rotor_between_objects(C1, C2)))[0])
else:
if motor:
if grade_obj(C1, 0.00001) == 4:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(motor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
r_set = normalised((rroot*r_set)(0, 2, 4))
R_total = normalised((rroot * R_total)(0, 2, 4))
if rotor_cost(r_set) < cost_tolerance:
exit_flag = 0
return R_total, exit_flag
exit_flag = 1
return R_total, exit_flag
Performs a sequential rotor update based on the rotors between individual objects
Exits when a full rotation through all objects produces a very small update of rotor
"""
grade_list = [grade_obj(ob) for ob in query_model]
R_total = 1.0 + 0.0*e1
for j in range(n_iterations):
r_set = 1.0 + 0.0*e1
if random_sequence:
indices = random.sample(range(len(query_model)), len(query_model))
else:
indices = range(len(query_model))
for i in indices:
grade = grade_list[i]
new_obj = normalised(apply_rotor(query_model[i], R_total)(grade))
C1 = normalised(new_obj)
C2 = normalised(reference_model[i])
if abs(C1 + C2) < 0.0001:
C1 = -C1
if object_type == 'lines':
rroot = normalised(square_roots_of_rotor((rotor_between_objects(C1, C2)))[0])
else:
if motor:
if grade_obj(C1, 0.00001) == 4:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(motor_between_objects(C1, C2))[0])
else:
rroot = normalised(square_roots_of_rotor(rotor_between_objects(C1, C2))[0])
r_set = normalised((rroot*r_set)(0, 2, 4))
R_total = normalised((rroot * R_total)(0, 2, 4))
if rotor_cost(r_set) < cost_tolerance: