Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_profile_result():
# create a pypesto result
result = create_optimization_result()
# write some dummy results for profiling
ratio_path_1 = [0.15, 0.25, 0.7, 1., 0.8, 0.35, 0.15]
ratio_path_2 = [0.1, 0.2, 0.7, 1., 0.8, 0.3, 0.1]
x_path_1 = np.array([[2., 2.1, 2.3, 2.5, 2.7, 2.9, 3.],
[1., 1.2, 1.4, 1.5, 1.6, 1.8, 2.]])
x_path_2 = np.array([[1., 1.1, 1.3, 1.5, 1.7, 1.9, 2.1],
[2.1, 2.2, 2.4, 2.5, 2.8, 2.9, 3.1]])
fval_path_1 = [4., 3., 1., 0., 1.5, 2.5, 5.]
fval_path_2 = [4.5, 3.5, 1.5, 0., 1.3, 2.3, 4.3]
tmp_result_1 = profile.ProfilerResult(x_path_1, fval_path_1, ratio_path_1)
tmp_result_2 = profile.ProfilerResult(x_path_2, fval_path_2, ratio_path_2)
# use pypesto function to write the numeric values into the results
result.profile_result.append_empty_profile_list()
result.profile_result.append_profiler_result(tmp_result_1)
result.profile_result.append_profiler_result(tmp_result_2)
return result
def create_profile_result():
# create a pypesto result
result = create_optimization_result()
# write some dummy results for profiling
ratio_path_1 = [0.15, 0.25, 0.7, 1., 0.8, 0.35, 0.15]
ratio_path_2 = [0.1, 0.2, 0.7, 1., 0.8, 0.3, 0.1]
x_path_1 = np.array([[2., 2.1, 2.3, 2.5, 2.7, 2.9, 3.],
[1., 1.2, 1.4, 1.5, 1.6, 1.8, 2.]])
x_path_2 = np.array([[1., 1.1, 1.3, 1.5, 1.7, 1.9, 2.1],
[2.1, 2.2, 2.4, 2.5, 2.8, 2.9, 3.1]])
fval_path_1 = [4., 3., 1., 0., 1.5, 2.5, 5.]
fval_path_2 = [4.5, 3.5, 1.5, 0., 1.3, 2.3, 4.3]
tmp_result_1 = profile.ProfilerResult(x_path_1, fval_path_1, ratio_path_1)
tmp_result_2 = profile.ProfilerResult(x_path_2, fval_path_2, ratio_path_2)
# use pypesto function to write the numeric values into the results
result.profile_result.append_empty_profile_list()
result.profile_result.append_profiler_result(tmp_result_1)
result.profile_result.append_profiler_result(tmp_result_2)
return result
def test_approximate_profiles(self):
"""Test for the approximate profile function."""
n_steps = 50
assert self.result.optimize_result.list[0].hess is None
result = profile.approximate_parameter_profile(
problem=self.problem, result=self.result, profile_index=[0, 1],
n_steps=n_steps)
profile_list = result.profile_result.list[-1]
assert profile_list[0] is None
assert isinstance(profile_list[1], profile.ProfilerResult)
assert np.isclose(profile_list[1].ratio_path.max(), 1)
assert len(profile_list[1].ratio_path) == n_steps
assert profile_list[1].x_path.shape == (2, n_steps)
# with pre-defined hessian
result = deepcopy(self.result)
result.optimize_result.list[0].hess = np.array([[2, 0], [0, 1]])
profile.approximate_parameter_profile(
problem=self.problem, result=result, profile_index=[0, 1],
n_steps=n_steps)
def test_default_profiling(self):
# loop over methods for creating new initial guesses
method_list = ['fixed_step', 'adaptive_step_order_0',
'adaptive_step_order_1', 'adaptive_step_regression']
for i_run, method in enumerate(method_list):
# run profiling
result = profile.parameter_profile(
problem=self.problem,
result=self.result,
optimizer=self.optimizer,
next_guess_method=method)
# check result
self.assertTrue(
isinstance(result.profile_result.list[i_run][0],
profile.ProfilerResult))
self.assertEqual(len(result.profile_result.list), i_run+1)
self.assertEqual(len(result.profile_result.list[i_run]), 2)
# check whether profiling needed maybe too many steps
steps = result.profile_result.list[i_run][0]['ratio_path'].size
if method == 'adaptive_step_regression':
self.assertTrue(steps < 20, 'Profiling with regression based '
'proposal needed too many steps.')
self.assertTrue(steps > 1, 'Profiling with regression based '
'proposal needed not enough steps.')
elif method == 'adaptive_step_order_1':
self.assertTrue(steps < 25, 'Profiling with 1st order based '
'proposal needed too many steps.')
self.assertTrue(steps > 1, 'Profiling with 1st order based '
'proposal needed not enough steps.')
elif method == 'adaptive_step_order_0':