Skip to content

Commit

Permalink
moving examples folder inside bioptim
Browse files Browse the repository at this point in the history
  • Loading branch information
pariterre committed Nov 10, 2021
1 parent 91fb017 commit b4c8e84
Show file tree
Hide file tree
Showing 113 changed files with 726 additions and 531 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ examples/*/biorbd_optim
examples/biorbd_optim

# Remove other development artifacts
examples/sandbox
*/examples/sandbox
*.npy
*.bo
*.bob
Expand Down
6 changes: 4 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ recursive-include examples *.py
recursive-include examples *.bioMod
recursive-include examples *.vtp
recursive-include examples *.STL
recursive-exclude examples/sandbox *.*
recursive-exclude examples c_generated_code
prune *sandbox*
prune *c_generated_code*
global-exclude *.py[cod]

2 changes: 0 additions & 2 deletions __init__.py

This file was deleted.

Empty file added bioptim/examples/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions examples/READM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Examples can be found in `{ROOT_FOLDER}/bioptim/examples/` or alternatively can be ran directly from the root (or after installing using `python setup.py install`) by running: `python -m bioptim.examples`.

51 changes: 28 additions & 23 deletions tests/test__global_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
from bioptim import OptimalControlProgram, CostType, OdeSolver, Solver
from bioptim.limits.penalty import PenaltyOption

from .utils import TestUtils

import matplotlib

matplotlib.use("Agg")


def test_plot_graphs_one_phase():
# Load graphs_one_phase
bioptim_folder = TestUtils.bioptim_folder()
graph = TestUtils.load_module(bioptim_folder + "/examples/torque_driven_ocp/track_markers_with_torque_actuators.py")
from bioptim.examples.torque_driven_ocp import track_markers_with_torque_actuators as ocp_module

bioptim_folder = os.path.dirname(ocp_module.__file__)

ocp = graph.prepare_ocp(
biorbd_model_path=bioptim_folder + "/examples/torque_driven_ocp/models/cube.bioMod",
ocp = ocp_module.prepare_ocp(
biorbd_model_path=bioptim_folder + "/models/cube.bioMod",
n_shooting=30,
final_time=2,
)
Expand All @@ -36,21 +35,22 @@ def test_plot_graphs_one_phase():

def test_plot_merged_graphs():
# Load graphs_one_phase
bioptim_folder = TestUtils.bioptim_folder()
merged_graphs = TestUtils.load_module(bioptim_folder + "/examples/muscle_driven_ocp/muscle_excitations_tracker.py")
from bioptim.examples.muscle_driven_ocp import muscle_excitations_tracker as ocp_module

bioptim_folder = os.path.dirname(ocp_module.__file__)

# Define the problem
model_path = bioptim_folder + "/examples/muscle_driven_ocp/models/arm26.bioMod"
model_path = bioptim_folder + "/models/arm26.bioMod"
biorbd_model = biorbd.Model(model_path)
final_time = 0.1
n_shooting = 5

# Generate random data to fit
np.random.seed(42)
t, markers_ref, x_ref, muscle_excitations_ref = merged_graphs.generate_data(biorbd_model, final_time, n_shooting)
t, markers_ref, x_ref, muscle_excitations_ref = ocp_module.generate_data(biorbd_model, final_time, n_shooting)

biorbd_model = biorbd.Model(model_path) # To prevent from free variable, the model must be reloaded
ocp = merged_graphs.prepare_ocp(
ocp = ocp_module.prepare_ocp(
biorbd_model,
final_time,
n_shooting,
Expand All @@ -67,21 +67,23 @@ def test_plot_merged_graphs():

def test_plot_graphs_multi_phases():
# Load graphs_one_phase
bioptim_folder = TestUtils.bioptim_folder()
graphs = TestUtils.load_module(bioptim_folder + "/examples/getting_started/example_multiphase.py")
ocp = graphs.prepare_ocp(biorbd_model_path=bioptim_folder + "/examples/getting_started/models/cube.bioMod")
from bioptim.examples.getting_started import example_multiphase as ocp_module

bioptim_folder = os.path.dirname(ocp_module.__file__)

ocp = ocp_module.prepare_ocp(biorbd_model_path=bioptim_folder + "/models/cube.bioMod")
sol = ocp.solve()
sol.graphs(automatically_organize=False)


def test_add_new_plot():
# Load graphs_one_phase
bioptim_folder = TestUtils.bioptim_folder()
graphs = TestUtils.load_module(
bioptim_folder + "/examples/torque_driven_ocp/track_markers_with_torque_actuators.py"
)
ocp = graphs.prepare_ocp(
biorbd_model_path=bioptim_folder + "/examples/torque_driven_ocp/models/cube.bioMod",
from bioptim.examples.torque_driven_ocp import track_markers_with_torque_actuators as ocp_module

bioptim_folder = os.path.dirname(ocp_module.__file__)

ocp = ocp_module.prepare_ocp(
biorbd_model_path=bioptim_folder + "/models/cube.bioMod",
n_shooting=20,
final_time=0.5,
)
Expand Down Expand Up @@ -124,9 +126,11 @@ def test_add_new_plot():

def test_console_objective_functions():
# Load graphs_one_phase
bioptim_folder = TestUtils.bioptim_folder()
graphs = TestUtils.load_module(bioptim_folder + "/examples/getting_started/example_multiphase.py")
ocp = graphs.prepare_ocp(biorbd_model_path=bioptim_folder + "/examples/getting_started/models/cube.bioMod")
from bioptim.examples.getting_started import example_multiphase as ocp_module

bioptim_folder = os.path.dirname(ocp_module.__file__)

ocp = ocp_module.prepare_ocp(biorbd_model_path=bioptim_folder + "/models/cube.bioMod")
sol = ocp.solve()
ocp = sol.ocp # We will override ocp with known and controlled values for the test

Expand Down Expand Up @@ -206,3 +210,4 @@ def override_penalty(pen: list[PenaltyOption]):

sys.stdout = sys.__stdout__ # Reset redirect.
assert captured_output.getvalue() == expected_output

Loading

0 comments on commit b4c8e84

Please sign in to comment.