Skip to content

Commit

Permalink
Merge pull request #806 from EveCharbie/save_bioptim_version
Browse files Browse the repository at this point in the history
Added the commit ID + version to sol
  • Loading branch information
pariterre committed Nov 23, 2023
2 parents a3ede0e + 3c1ce24 commit aca4e6d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bioptim/examples/getting_started/pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
21 changes: 21 additions & 0 deletions bioptim/optimization/solution/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dependencies:
- bioviz
- python-graphviz
- pyqtgraph
- gitpython
10 changes: 10 additions & 0 deletions tests/shard3/test_global_getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Node,
ControlType,
PhaseDynamics,
__version__,
)

from tests.utils import TestUtils
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit aca4e6d

Please sign in to comment.