|
| 1 | +# Copyright 2026 DeepMind Technologies Limited |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +"""I/O utilities for saving system identification results.""" |
| 17 | + |
| 18 | +from collections.abc import Sequence |
| 19 | +import os |
| 20 | +import pathlib |
| 21 | +import pickle |
| 22 | + |
| 23 | +from absl import logging |
| 24 | +from mujoco.sysid._src import parameter |
| 25 | +from mujoco.sysid._src.optimize import calculate_intervals |
| 26 | +from mujoco.sysid._src.trajectory import ModelSequences |
| 27 | +import scipy.optimize as scipy_optimize |
| 28 | + |
| 29 | + |
| 30 | +def save_results( |
| 31 | + experiment_results_folder: str | os.PathLike[str], |
| 32 | + models_sequences: Sequence[ModelSequences], |
| 33 | + initial_params: parameter.ParameterDict, |
| 34 | + opt_params: parameter.ParameterDict, |
| 35 | + opt_result: scipy_optimize.OptimizeResult, |
| 36 | + residual_fn, |
| 37 | +): |
| 38 | + """Save optimization results and confidence intervals to disk.""" |
| 39 | + experiment_results_folder = pathlib.Path(experiment_results_folder) |
| 40 | + if not experiment_results_folder.exists(): |
| 41 | + experiment_results_folder.mkdir(parents=True, exist_ok=True) |
| 42 | + logging.info( |
| 43 | + "Experiment results will be saved to %s", experiment_results_folder |
| 44 | + ) |
| 45 | + |
| 46 | + initial_params.save_to_disk(experiment_results_folder / "params_x_0.yaml") |
| 47 | + opt_params.save_to_disk(experiment_results_folder / "params_x_hat.yaml") |
| 48 | + |
| 49 | + with open( |
| 50 | + os.path.join(experiment_results_folder, "results.pkl"), "wb" |
| 51 | + ) as handle: |
| 52 | + pickle.dump(opt_result, handle, protocol=pickle.HIGHEST_PROTOCOL) |
| 53 | + |
| 54 | + # TODO(b/0): these intervals should be part of the params object. |
| 55 | + residuals_star, _, _ = residual_fn( |
| 56 | + opt_result.x, opt_params, return_pred_all=True |
| 57 | + ) |
| 58 | + covariance, intervals = calculate_intervals(residuals_star, opt_result.jac) |
| 59 | + with open( |
| 60 | + os.path.join(experiment_results_folder, "confidence.pkl"), "wb" |
| 61 | + ) as handle: |
| 62 | + pickle.dump( |
| 63 | + {"cov": covariance, "intervals": intervals}, |
| 64 | + handle, |
| 65 | + protocol=pickle.HIGHEST_PROTOCOL, |
| 66 | + ) |
| 67 | + |
| 68 | + # Dump identified models to disk. |
| 69 | + for model_sequences in models_sequences: |
| 70 | + model_sequences.spec.to_file( |
| 71 | + (experiment_results_folder / f"{model_sequences.name}.xml").as_posix() |
| 72 | + ) |
| 73 | + |
| 74 | + # Log nominal compared to initial. |
| 75 | + x0 = initial_params.as_vector() |
| 76 | + x_nominal = initial_params.as_nominal_vector() |
| 77 | + logging.info( |
| 78 | + "Initial Parameters\n%s", |
| 79 | + initial_params.compare_parameters( |
| 80 | + x0, opt_result.x, measured_params=x_nominal |
| 81 | + ), |
| 82 | + ) |
0 commit comments