From 9a55920b2d6d1776d220d6a959e1164cabf06220 Mon Sep 17 00:00:00 2001 From: Charbie Date: Wed, 15 Nov 2023 17:59:11 -0500 Subject: [PATCH 1/4] added the info + tested --- bioptim/examples/getting_started/pendulum.py | 5 +++++ bioptim/optimization/solution/solution.py | 20 ++++++++++++++++++++ tests/shard3/test_global_getting_started.py | 10 ++++++++++ 3 files changed, 35 insertions(+) diff --git a/bioptim/examples/getting_started/pendulum.py b/bioptim/examples/getting_started/pendulum.py index b57aae1e1..a61e92846 100644 --- a/bioptim/examples/getting_started/pendulum.py +++ b/bioptim/examples/getting_started/pendulum.py @@ -146,6 +146,11 @@ 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..1ada0aecb 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,25 @@ 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/tests/shard3/test_global_getting_started.py b/tests/shard3/test_global_getting_started.py index 13d303c8b..95be29784 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)) From 259169ae439b29e2b4a335829e82562f41b61b1d Mon Sep 17 00:00:00 2001 From: Charbie Date: Wed, 15 Nov 2023 17:59:46 -0500 Subject: [PATCH 2/4] blacked --- bioptim/examples/getting_started/pendulum.py | 4 +++- bioptim/optimization/solution/solution.py | 13 +++++++------ tests/shard3/test_global_getting_started.py | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/bioptim/examples/getting_started/pendulum.py b/bioptim/examples/getting_started/pendulum.py index a61e92846..c991da3ca 100644 --- a/bioptim/examples/getting_started/pendulum.py +++ b/bioptim/examples/getting_started/pendulum.py @@ -148,9 +148,11 @@ def main(): # --- Save the solution --- # import pickle - with open("pendulum.pkl", 'wb') as file: + + 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 1ada0aecb..ff1c2fa51 100644 --- a/bioptim/optimization/solution/solution.py +++ b/bioptim/optimization/solution/solution.py @@ -245,12 +245,13 @@ def bioptim_version_used(self) -> dict: 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, - } + version_dic = { + "commit_id": commit_id, + "date": date, + "branch": branch, + "tag": tag, + "bioptim_version": bioptim_version, + } return version_dic @classmethod diff --git a/tests/shard3/test_global_getting_started.py b/tests/shard3/test_global_getting_started.py index 95be29784..26b6c5b3e 100644 --- a/tests/shard3/test_global_getting_started.py +++ b/tests/shard3/test_global_getting_started.py @@ -19,7 +19,7 @@ Node, ControlType, PhaseDynamics, - __version__ + __version__, ) from tests.utils import TestUtils @@ -122,7 +122,7 @@ def test_pendulum(ode_solver, use_sx, n_threads, phase_dynamics): 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__}") + np.testing.assert_equal(version_dic["tag"].split("-")[0], f"Release_{__version__}") print(version_dic["bioptim_version"]) print(sol.bioptim_version_used) From 3656d6e74f3210794329ed625e4d82e9490b47ad Mon Sep 17 00:00:00 2001 From: Charbie Date: Wed, 15 Nov 2023 18:13:50 -0500 Subject: [PATCH 3/4] install env on github --- environment.yml | 1 + 1 file changed, 1 insertion(+) 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 From 3c1ce242fa569a3ebe2489edc5e37d28457ac501 Mon Sep 17 00:00:00 2001 From: Charbie Date: Thu, 16 Nov 2023 11:23:54 -0500 Subject: [PATCH 4/4] commented pickle to avoid adding a new dependnecy --- bioptim/examples/getting_started/pendulum.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/bioptim/examples/getting_started/pendulum.py b/bioptim/examples/getting_started/pendulum.py index c991da3ca..08892e0f3 100644 --- a/bioptim/examples/getting_started/pendulum.py +++ b/bioptim/examples/getting_started/pendulum.py @@ -146,12 +146,11 @@ 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) + # # --- Save the solution --- # + # import pickle + # with open("pendulum.pkl", "wb") as file: + # del sol.ocp + # pickle.dump(sol, file) if __name__ == "__main__":