diff --git a/bioptim/examples/getting_started/pendulum.py b/bioptim/examples/getting_started/pendulum.py index b57aae1e1..08892e0f3 100644 --- a/bioptim/examples/getting_started/pendulum.py +++ b/bioptim/examples/getting_started/pendulum.py @@ -146,6 +146,12 @@ def main(): sol.print_cost() sol.animate(n_frames=100) + # # --- Save the solution --- # + # import pickle + # with open("pendulum.pkl", "wb") as file: + # del sol.ocp + # pickle.dump(sol, file) + if __name__ == "__main__": main() diff --git a/bioptim/optimization/solution/solution.py b/bioptim/optimization/solution/solution.py index 557f3accc..ff1c2fa51 100644 --- a/bioptim/optimization/solution/solution.py +++ b/bioptim/optimization/solution/solution.py @@ -6,6 +6,7 @@ from scipy.interpolate import interp1d from casadi import vertcat, DM, Function from matplotlib import pyplot as plt +import git from ...limits.objective_functions import ObjectiveFcn from ...limits.path_conditions import InitialGuess, InitialGuessList @@ -233,6 +234,26 @@ def __init__( self._stochastic_variables["unscaled"], ) + @property + def bioptim_version_used(self) -> dict: + """ + Returns info on the bioptim version used to generate the results for future reference. + """ + repo = git.Repo(search_parent_directories=True) + commit_id = str(repo.commit()) + branch = str(repo.active_branch) + tag = repo.git.describe("--tags") + bioptim_version = repo.git.version_info + date = repo.git.log("-1", "--format=%cd") + version_dic = { + "commit_id": commit_id, + "date": date, + "branch": branch, + "tag": tag, + "bioptim_version": bioptim_version, + } + return version_dic + @classmethod def from_dict(cls, ocp, _sol: dict): """ diff --git a/environment.yml b/environment.yml index 6314607d5..7b356fde4 100644 --- a/environment.yml +++ b/environment.yml @@ -7,3 +7,4 @@ dependencies: - bioviz - python-graphviz - pyqtgraph +- gitpython diff --git a/tests/shard3/test_global_getting_started.py b/tests/shard3/test_global_getting_started.py index 13d303c8b..26b6c5b3e 100644 --- a/tests/shard3/test_global_getting_started.py +++ b/tests/shard3/test_global_getting_started.py @@ -19,6 +19,7 @@ Node, ControlType, PhaseDynamics, + __version__, ) from tests.utils import TestUtils @@ -116,6 +117,15 @@ def test_pendulum(ode_solver, use_sx, n_threads, phase_dynamics): sol = ocp.solve() + # Test the bioptim version feature (this is the only test) + version_dic = sol.bioptim_version_used + print(version_dic["commit_id"]) + print(version_dic["date"]) + print(version_dic["branch"]) + np.testing.assert_equal(version_dic["tag"].split("-")[0], f"Release_{__version__}") + print(version_dic["bioptim_version"]) + print(sol.bioptim_version_used) + # Check objective function value f = np.array(sol.cost) np.testing.assert_equal(f.shape, (1, 1))