def _create_result( problem: Problem, ls_result: OptimizeResult | None, free_parameter_labels: list[str], termination_reason: str, ) -> Result: success = ls_result is not None number_of_function_evaluation = (ls_result.nfev if ls_result is not None else len(problem.parameter_history)) number_of_jacobian_evaluation = ls_result.njev if success else None optimality = ls_result.optimality if success else None number_of_data_points = ls_result.fun.size if success else None number_of_variables = ls_result.x.size if success else None degrees_of_freedom = number_of_data_points - number_of_variables if success else None chi_square = np.sum(ls_result.fun**2) if success else None reduced_chi_square = chi_square / degrees_of_freedom if success else None root_mean_square_error = np.sqrt(reduced_chi_square) if success else None jacobian = ls_result.jac if success else None problem.save_parameters_for_history() history_index = None if success else -2 data = problem.create_result_data(history_index=history_index) # the optimized parameters are those of the last run if the optimization has crashed parameters = problem.parameters covariance_matrix = None if success: try: covariance_matrix = np.linalg.inv(jacobian.T.dot(jacobian)) standard_errors = np.sqrt(np.diagonal(covariance_matrix)) for label, error in zip(free_parameter_labels, standard_errors): parameters.get(label).standard_error = error except np.linalg.LinAlgError: warn( "The resulting Jacobian is singular, cannot compute covariance matrix and " "standard errors.") return Result( additional_penalty=problem.additional_penalty, cost=problem.cost, data=data, free_parameter_labels=free_parameter_labels, number_of_function_evaluations=number_of_function_evaluation, initial_parameters=problem.scheme.parameters, optimized_parameters=parameters, scheme=problem.scheme, success=success, termination_reason=termination_reason, chi_square=chi_square, covariance_matrix=covariance_matrix, degrees_of_freedom=degrees_of_freedom, jacobian=jacobian, number_of_data_points=number_of_data_points, number_of_jacobian_evaluations=number_of_jacobian_evaluation, number_of_variables=number_of_variables, optimality=optimality, reduced_chi_square=reduced_chi_square, root_mean_square_error=root_mean_square_error, )
def test_problem_result_data(problem: Problem): data = problem.create_result_data() assert "dataset1" in data dataset = data["dataset1"] assert "clp_label" in dataset.coords assert np.array_equal(dataset.clp_label, ["s1", "s2", "s3", "s4"]) assert problem.model.global_dimension in dataset.coords assert np.array_equal(dataset.coords[problem.model.global_dimension], suite.e_axis) assert problem.model.model_dimension in dataset.coords assert np.array_equal(dataset.coords[problem.model.model_dimension], suite.c_axis) assert "matrix" in dataset matrix = dataset.matrix if problem.index_dependent: assert len(matrix.shape) == 3 assert matrix.shape[0] == suite.e_axis.size assert matrix.shape[1] == suite.c_axis.size assert matrix.shape[2] == 4 else: assert len(matrix.shape) == 2 assert matrix.shape[0] == suite.c_axis.size assert matrix.shape[1] == 4 assert "clp" in dataset clp = dataset.clp assert len(clp.shape) == 2 assert clp.shape[0] == suite.e_axis.size assert clp.shape[1] == 4 assert "weighted_residual" in dataset assert dataset.data.shape == dataset.weighted_residual.shape assert "residual" in dataset assert dataset.data.shape == dataset.residual.shape assert "residual_singular_values" in dataset assert "weighted_residual_singular_values" in dataset