Skip to content

Commit

Permalink
Fixing numpy testing import ascii problem
Browse files Browse the repository at this point in the history
  • Loading branch information
pariterre committed Jun 11, 2024
1 parent ae2c06d commit da0d284
Show file tree
Hide file tree
Showing 49 changed files with 2,145 additions and 2,136 deletions.
83 changes: 42 additions & 41 deletions tests/shard1/test_acados_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sys import platform

import numpy as np
import numpy.testing as npt
from bioptim import (
BiorbdModel,
Axis,
Expand Down Expand Up @@ -80,7 +81,7 @@ def test_acados_one_mayer(cost_type):
# Check end state value
states = sol.decision_states(to_merge=SolutionMerge.NODES)
q = states["q"]
np.testing.assert_almost_equal(q[0, -1], 1.0)
npt.assert_almost_equal(q[0, -1], 1.0)

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -115,7 +116,7 @@ def test_acados_mayer_first_node(cost_type):
# Check end state value
states = sol.decision_states(to_merge=SolutionMerge.NODES)
q = states["q"]
np.testing.assert_almost_equal(q[0, 0], 0.999999948505021)
npt.assert_almost_equal(q[0, 0], 0.999999948505021)

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -150,9 +151,9 @@ def test_acados_several_mayer(cost_type):
# Check end state value
states = sol.decision_states(to_merge=SolutionMerge.NODES)
q = states["q"]
np.testing.assert_almost_equal(q[0, -1], 1.0)
np.testing.assert_almost_equal(q[1, -1], 2.0)
np.testing.assert_almost_equal(q[2, -1], 3.0)
npt.assert_almost_equal(q[0, -1], 1.0)
npt.assert_almost_equal(q[1, -1], 2.0)
npt.assert_almost_equal(q[2, -1], 3.0)

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -197,7 +198,7 @@ def test_acados_one_lagrange(cost_type):
# Check end state value
states = sol.decision_states(to_merge=SolutionMerge.NODES)
q = states["q"]
np.testing.assert_almost_equal(q[0, :], target[0, :].squeeze())
npt.assert_almost_equal(q[0, :], target[0, :].squeeze())

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -245,7 +246,7 @@ def test_acados_one_lagrange_and_one_mayer(cost_type):
# Check end state value
states = sol.decision_states(to_merge=SolutionMerge.NODES)
q = states["q"]
np.testing.assert_almost_equal(q[0, :], target[0, :].squeeze(), decimal=6)
npt.assert_almost_equal(q[0, :], target[0, :].squeeze(), decimal=6)

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -284,7 +285,7 @@ def test_acados_control_lagrange_and_state_mayer(cost_type):
# Check end state value
states = sol.decision_states(to_merge=SolutionMerge.NODES)
q = states["q"]
np.testing.assert_almost_equal(q[0, -1], target.squeeze())
npt.assert_almost_equal(q[0, -1], target.squeeze())

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -319,7 +320,7 @@ def test_acados_options(cost_type):

# Check that tol impacted convergence
for i in range(len(tols) - 1):
np.testing.assert_array_less(iter[i + 1], iter[i])
npt.assert_array_less(iter[i + 1], iter[i])

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -403,16 +404,16 @@ def test_acados_custom_dynamics(problem_type_custom):
q, qdot, tau = states["q"], states["qdot"], controls["tau"]

# initial and final position
np.testing.assert_almost_equal(q[:, 0], np.array((2, 0, 0)), decimal=6)
np.testing.assert_almost_equal(q[:, -1], np.array((2, 0, 1.57)))
npt.assert_almost_equal(q[:, 0], np.array((2, 0, 0)), decimal=6)
npt.assert_almost_equal(q[:, -1], np.array((2, 0, 1.57)))

# initial and final velocities
np.testing.assert_almost_equal(qdot[:, 0], np.array((0, 0, 0)))
np.testing.assert_almost_equal(qdot[:, -1], np.array((0, 0, 0)))
npt.assert_almost_equal(qdot[:, 0], np.array((0, 0, 0)))
npt.assert_almost_equal(qdot[:, -1], np.array((0, 0, 0)))

# initial and final controls
np.testing.assert_almost_equal(tau[:, 0], np.array((0, 9.81, 2.27903226)))
np.testing.assert_almost_equal(tau[:, -1], np.array((0, 9.81, -2.27903226)))
npt.assert_almost_equal(tau[:, 0], np.array((0, 9.81, 2.27903226)))
npt.assert_almost_equal(tau[:, -1], np.array((0, 9.81, -2.27903226)))


def test_acados_one_parameter():
Expand Down Expand Up @@ -472,15 +473,15 @@ def test_acados_one_parameter():
gravity = sol.parameters["gravity_xyz"]

# initial and final position
np.testing.assert_almost_equal(q[:, 0], np.array((0, 0)), decimal=6)
np.testing.assert_almost_equal(q[:, -1], np.array((0, 3.14)), decimal=6)
npt.assert_almost_equal(q[:, 0], np.array((0, 0)), decimal=6)
npt.assert_almost_equal(q[:, -1], np.array((0, 3.14)), decimal=6)

# initial and final velocities
np.testing.assert_almost_equal(qdot[:, 0], np.array((0, 0)), decimal=6)
np.testing.assert_almost_equal(qdot[:, -1], np.array((0, 0)), decimal=6)
npt.assert_almost_equal(qdot[:, 0], np.array((0, 0)), decimal=6)
npt.assert_almost_equal(qdot[:, -1], np.array((0, 0)), decimal=6)

# parameters
np.testing.assert_almost_equal(gravity[-1], -9.80995, decimal=4)
npt.assert_almost_equal(gravity[-1], -9.80995, decimal=4)

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -548,16 +549,16 @@ def test_acados_several_parameter():
gravity, mass = sol.parameters["gravity_xyz"], sol.parameters["mass"]

# initial and final position
np.testing.assert_almost_equal(q[:, 0], np.array((0, 0)), decimal=6)
np.testing.assert_almost_equal(q[:, -1], np.array((0, 3.14)), decimal=6)
npt.assert_almost_equal(q[:, 0], np.array((0, 0)), decimal=6)
npt.assert_almost_equal(q[:, -1], np.array((0, 3.14)), decimal=6)

# initial and final velocities
np.testing.assert_almost_equal(qdot[:, 0], np.array((0, 0)), decimal=6)
np.testing.assert_almost_equal(qdot[:, -1], np.array((0, 0)), decimal=6)
npt.assert_almost_equal(qdot[:, 0], np.array((0, 0)), decimal=6)
npt.assert_almost_equal(qdot[:, -1], np.array((0, 0)), decimal=6)

# parameters
np.testing.assert_almost_equal(gravity[-1], np.array([-9.80996]), decimal=4)
np.testing.assert_almost_equal(mass[0], 20, decimal=6)
npt.assert_almost_equal(gravity[-1], np.array([-9.80996]), decimal=4)
npt.assert_almost_equal(mass[0], 20, decimal=6)

# Clean test folder
os.remove(f"./acados_ocp.json")
Expand Down Expand Up @@ -610,11 +611,11 @@ def test_acados_one_end_constraints():
q, qdot, tau = states["q"], states["qdot"], controls["tau"]

# final position
np.testing.assert_almost_equal(q[:, -1], np.array((2, 0, 0)))
npt.assert_almost_equal(q[:, -1], np.array((2, 0, 0)))

# initial and final controls
np.testing.assert_almost_equal(tau[:, 0], np.array((2.72727272, 9.81, 0)))
np.testing.assert_almost_equal(tau[:, -1], np.array((-2.72727272, 9.81, 0)))
npt.assert_almost_equal(tau[:, 0], np.array((2.72727272, 9.81, 0)))
npt.assert_almost_equal(tau[:, -1], np.array((-2.72727272, 9.81, 0)))


def test_acados_constraints_all():
Expand Down Expand Up @@ -650,15 +651,15 @@ def test_acados_constraints_all():
q, qdot, tau = states["q"], states["qdot"], controls["tau"]

# final position
np.testing.assert_almost_equal(q[:, 0], np.array([2.28988221, 0, 0, 2.95087911e-01]), decimal=6)
np.testing.assert_almost_equal(q[:, -1], np.array((2.28215749, 0, 1.57, 6.62470772e-01)), decimal=6)
npt.assert_almost_equal(q[:, 0], np.array([2.28988221, 0, 0, 2.95087911e-01]), decimal=6)
npt.assert_almost_equal(q[:, -1], np.array((2.28215749, 0, 1.57, 6.62470772e-01)), decimal=6)

np.testing.assert_almost_equal(qdot[:, 0], np.array([0, 0, 0, 0]), decimal=6)
np.testing.assert_almost_equal(qdot[:, -1], np.array([0, 0, 0, 0]), decimal=6)
npt.assert_almost_equal(qdot[:, 0], np.array([0, 0, 0, 0]), decimal=6)
npt.assert_almost_equal(qdot[:, -1], np.array([0, 0, 0, 0]), decimal=6)

# initial and final controls
np.testing.assert_almost_equal(tau[:, 0], np.array((0.04483914, 9.90739842, 2.24951691, 0.78496612)), decimal=6)
np.testing.assert_almost_equal(tau[:, -1], np.array((0.15945561, 10.03978178, -2.36075327, 0.07267697)), decimal=6)
npt.assert_almost_equal(tau[:, 0], np.array((0.04483914, 9.90739842, 2.24951691, 0.78496612)), decimal=6)
npt.assert_almost_equal(tau[:, -1], np.array((0.15945561, 10.03978178, -2.36075327, 0.07267697)), decimal=6)


def test_acados_constraints_end_all():
Expand Down Expand Up @@ -695,15 +696,15 @@ def test_acados_constraints_end_all():
q, qdot, tau = states["q"], states["qdot"], controls["tau"]

# final position
np.testing.assert_almost_equal(q[:, 0], np.array([2.01701330, 0, 0, 3.20057865e-01]), decimal=6)
np.testing.assert_almost_equal(q[:, -1], np.array((2, 0, 1.57, 7.85398168e-01)), decimal=6)
npt.assert_almost_equal(q[:, 0], np.array([2.01701330, 0, 0, 3.20057865e-01]), decimal=6)
npt.assert_almost_equal(q[:, -1], np.array((2, 0, 1.57, 7.85398168e-01)), decimal=6)

np.testing.assert_almost_equal(qdot[:, 0], np.array([0, 0, 0, 0]), decimal=6)
np.testing.assert_almost_equal(qdot[:, -1], np.array([0, 0, 0, 0]), decimal=6)
npt.assert_almost_equal(qdot[:, 0], np.array([0, 0, 0, 0]), decimal=6)
npt.assert_almost_equal(qdot[:, -1], np.array([0, 0, 0, 0]), decimal=6)

# initial and final controls
np.testing.assert_almost_equal(tau[:, 0], np.array((0.04648408, 9.88616194, 2.24285498, 0.864213)), decimal=6)
np.testing.assert_almost_equal(tau[:, -1], np.array((0.19389194, 9.99905781, -2.37713652, -0.19858311)), decimal=6)
npt.assert_almost_equal(tau[:, 0], np.array((0.04648408, 9.88616194, 2.24285498, 0.864213)), decimal=6)
npt.assert_almost_equal(tau[:, -1], np.array((0.19389194, 9.99905781, -2.37713652, -0.19858311)), decimal=6)


def test_acados_phase_dynamics_reject():
Expand Down
9 changes: 4 additions & 5 deletions tests/shard1/test_biorbd_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import os
import pytest
import numpy as np
import numpy.testing as npt
import biorbd_casadi as biorbd
from bioptim import (
BiorbdModel,
)
from bioptim import BiorbdModel


def test_biorbd_model_import():
Expand Down Expand Up @@ -77,5 +76,5 @@ def test_bounds_from_ranges(my_keys):
raise ValueError("Wrong key")

# Check min and max have the right value
np.testing.assert_almost_equal(x_bounds.min, x_min)
np.testing.assert_almost_equal(x_bounds.max, x_max)
npt.assert_almost_equal(x_bounds.min, x_min)
npt.assert_almost_equal(x_bounds.max, x_max)
11 changes: 6 additions & 5 deletions tests/shard1/test_biorbd_model_holonomic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import numpy as np
import numpy.testing as npt
import pytest
from casadi import DM, MX, Function

Expand Down Expand Up @@ -86,9 +87,9 @@ def test_model_holonomic():
):
model.state_from_partition(MX([1]), MX([4, 5, 3]))

np.testing.assert_equal(model.nb_independent_joints, 1)
np.testing.assert_equal(model.nb_dependent_joints, 2)
np.testing.assert_equal(model.nb_holonomic_constraints, 2)
npt.assert_equal(model.nb_independent_joints, 1)
npt.assert_equal(model.nb_dependent_joints, 2)
npt.assert_equal(model.nb_holonomic_constraints, 2)

# symbolic variables
q = MX([1, 2, 3])
Expand Down Expand Up @@ -144,7 +145,7 @@ def test_model_holonomic():
TestUtils.assert_equal(model.compute_qdot_v(q, qdot_u), [23.18039172, -1.4066566], expand=False)
TestUtils.assert_equal(model.compute_qdot(q, qdot_u), [4.0, 23.18039172, -1.4066566], expand=False)

np.testing.assert_almost_equal(
npt.assert_almost_equal(
model.compute_q_v(DM([0.0]), q_v_init=DM([1.0, 1.0])).toarray().squeeze(),
np.array([2 * np.pi / 3, 2 * np.pi / 3]),
decimal=6,
Expand Down Expand Up @@ -173,7 +174,7 @@ def test_example_two_pendulums():
sol = ocp.solve(Solver.IPOPT(show_online_optim=False))
states = sol.decision_states(to_merge=SolutionMerge.NODES)

np.testing.assert_almost_equal(
npt.assert_almost_equal(
states["q_u"],
[
[1.54, 1.433706, 1.185046, 0.891157, 0.561607, 0.191792, -0.206511, -0.614976, -1.018383, -1.356253, -1.54],
Expand Down
13 changes: 7 additions & 6 deletions tests/shard1/test_biorbd_multi_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import pytest
import numpy as np
from casadi import MX, DM, vertcat, Function
import numpy.testing as npt
from casadi import DM
import biorbd_casadi as biorbd
from bioptim import (
MultiBiorbdModel,
Expand Down Expand Up @@ -376,10 +377,10 @@ def test_biorbd_model():
qddot_mapping = models._var_mapping("qddot", None, variable_mappings)
tau_mapping = models._var_mapping("tau", None, variable_mappings)

np.testing.assert_equal(q_mapping["q"].to_first.map_idx, [0, 1, 2, 3, 4, 5])
np.testing.assert_equal(qdot_mapping["qdot"].to_first.map_idx, [0, 1, 2, 3, 4, 5])
np.testing.assert_equal(qddot_mapping["qddot"].to_first.map_idx, [0, 1, 2, 3, 4, 5])
np.testing.assert_equal(tau_mapping["tau"].to_first.map_idx, [1, 2, 5])
npt.assert_equal(q_mapping["q"].to_first.map_idx, [0, 1, 2, 3, 4, 5])
npt.assert_equal(qdot_mapping["qdot"].to_first.map_idx, [0, 1, 2, 3, 4, 5])
npt.assert_equal(qddot_mapping["qddot"].to_first.map_idx, [0, 1, 2, 3, 4, 5])
npt.assert_equal(tau_mapping["tau"].to_first.map_idx, [1, 2, 5])

bounds_from_ranges = BoundsList()
bounds_from_ranges["q"] = models.bounds_from_ranges("q", variable_mappings)
Expand Down Expand Up @@ -407,7 +408,7 @@ def test_biorbd_model():
else:
raise NotImplementedError("Wrong value")

np.testing.assert_almost_equal(bounds_from_ranges[key].min, DM(np.array(expected)), decimal=5)
npt.assert_almost_equal(bounds_from_ranges[key].min, DM(np.array(expected)), decimal=5)

assert models.variable_index("q", 0) == range(0, 3)
assert models.variable_index("qdot", 1) == range(3, 6)
Expand Down
9 changes: 5 additions & 4 deletions tests/shard1/test_bounds_accessor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import numpy.testing as npt
from bioptim import BoundsList, InterpolationType


Expand All @@ -12,8 +13,8 @@ def test_accessors_on_bounds_option():
x_bounds["my_key"].max[1:3] = 10

# Check min and max have the right value
np.testing.assert_almost_equal(x_bounds["my_key"].min[:], np.array([[0], [0], [0], [-10], [-10], [-10]]))
np.testing.assert_almost_equal(x_bounds["my_key"].max[:], np.array([[0], [10], [10], [100], [100], [100]]))
npt.assert_almost_equal(x_bounds["my_key"].min[:], np.array([[0], [0], [0], [-10], [-10], [-10]]))
npt.assert_almost_equal(x_bounds["my_key"].max[:], np.array([[0], [10], [10], [100], [100], [100]]))


def test_accessors_on_bounds_option_multidimensional():
Expand All @@ -31,11 +32,11 @@ def test_accessors_on_bounds_option_multidimensional():
x_bounds["my_key"].max[1:5, 1:] = 10

# Check min and max have the right value
np.testing.assert_almost_equal(
npt.assert_almost_equal(
x_bounds["my_key"].min[:],
np.array([[0, -50, 0], [0, -10, -10], [0, -10, -10], [-100, -10, -10], [-100, -10, -10], [-100, -50, 0]]),
)
np.testing.assert_almost_equal(
npt.assert_almost_equal(
x_bounds["my_key"].max[:],
np.array([[0, 150, 200], [0, 10, 10], [0, 10, 10], [100, 10, 10], [100, 10, 10], [100, 150, 200]]),
)
25 changes: 12 additions & 13 deletions tests/shard1/test_continuity_as_objective.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import platform

import numpy as np
import numpy.testing as npt
import pytest

from bioptim import PhaseDynamics, SolutionMerge
Expand Down Expand Up @@ -47,9 +48,9 @@ def test_continuity_as_objective(phase_dynamics):
expected_controls = [[-1.47014529, -0.22059134, -18.23601047], [0.0, 0.0, 0.0]]

assert sol.iterations in expected_iterations
np.testing.assert_almost_equal(sol.decision_states(to_merge=SolutionMerge.NODES)["q"], expected_q)
np.testing.assert_almost_equal(sol.decision_states(to_merge=SolutionMerge.NODES)["qdot"], expected_qdot)
np.testing.assert_almost_equal(sol.decision_controls(to_merge=SolutionMerge.NODES)["tau"], expected_controls)
npt.assert_almost_equal(sol.decision_states(to_merge=SolutionMerge.NODES)["q"], expected_q)
npt.assert_almost_equal(sol.decision_states(to_merge=SolutionMerge.NODES)["qdot"], expected_qdot)
npt.assert_almost_equal(sol.decision_controls(to_merge=SolutionMerge.NODES)["tau"], expected_controls)

# second pass
ocp_second_pass = ocp_module.prepare_ocp_second_pass(
Expand Down Expand Up @@ -136,20 +137,18 @@ def test_continuity_as_objective(phase_dynamics):
]
expected_iterations = range(5, 35) # 20 on my laptop @ipuch

np.testing.assert_almost_equal(sol_second_pass.decision_states(to_merge=SolutionMerge.NODES)["q"], expected_q)
np.testing.assert_almost_equal(sol_second_pass.decision_states(to_merge=SolutionMerge.NODES)["qdot"], expected_qdot)
np.testing.assert_almost_equal(sol_second_pass.decision_controls(to_merge=SolutionMerge.NODES)["tau"], expected_tau)
npt.assert_almost_equal(sol_second_pass.decision_states(to_merge=SolutionMerge.NODES)["q"], expected_q)
npt.assert_almost_equal(sol_second_pass.decision_states(to_merge=SolutionMerge.NODES)["qdot"], expected_qdot)
npt.assert_almost_equal(sol_second_pass.decision_controls(to_merge=SolutionMerge.NODES)["tau"], expected_tau)

np.testing.assert_almost_equal(sol_second_pass.vector, expected_vector)
np.testing.assert_almost_equal(sol_second_pass.constraints, expected_constraints)
np.testing.assert_almost_equal(float(sol_second_pass.cost), expected_cost)
npt.assert_almost_equal(sol_second_pass.vector, expected_vector)
npt.assert_almost_equal(sol_second_pass.constraints, expected_constraints)
npt.assert_almost_equal(float(sol_second_pass.cost), expected_cost)

assert sol_second_pass.detailed_cost[0]["name"] == expected_detailed_cost[0]["name"]
np.testing.assert_almost_equal(
npt.assert_almost_equal(
sol_second_pass.detailed_cost[0]["cost_value_weighted"], expected_detailed_cost[0]["cost_value_weighted"]
)
np.testing.assert_almost_equal(
sol_second_pass.detailed_cost[0]["cost_value"], expected_detailed_cost[0]["cost_value"]
)
npt.assert_almost_equal(sol_second_pass.detailed_cost[0]["cost_value"], expected_detailed_cost[0]["cost_value"])

assert sol_second_pass.iterations in expected_iterations
5 changes: 3 additions & 2 deletions tests/shard1/test_continuity_linear_continuous.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import numpy as np
import numpy.testing as npt

from bioptim import PhaseDynamics, ControlType, QuadratureRule, Solver

Expand Down Expand Up @@ -54,5 +55,5 @@ def test_continuity_linear_continuous_global():
for expected, actual in zip(expected_costs, actual_costs):
assert expected["name"] == actual["name"]

np.testing.assert_almost_equal(expected["cost_value_weighted"], actual["cost_value_weighted"])
np.testing.assert_almost_equal(expected["cost_value"], actual["cost_value"])
npt.assert_almost_equal(expected["cost_value_weighted"], actual["cost_value_weighted"])
npt.assert_almost_equal(expected["cost_value"], actual["cost_value"])
Loading

0 comments on commit da0d284

Please sign in to comment.