"""Tests for told_logic.py (card 03's arithmetic): the told box, the executed command, torque equivalence, the classes.""" import unittest import numpy as np import told_logic as TL KNEE = 4 class ToldBox(unittest.TestCase): def test_knee_held_to_half_and_others_to_one(self): low, high = TL.box(6, KNEE) self.assertEqual(high[KNEE], 0.5) self.assertEqual(low[KNEE], -0.5) self.assertTrue(np.all(np.delete(high, KNEE) == 1.0) and np.all(np.delete(low, KNEE) == -1.0)) def test_projection_at_boundaries_of_both_signs_and_inside(self): low, high = TL.box(6, KNEE) x = np.array([0.9, -0.9, 0.2, -1.5, 0.7, 0.0]) y = TL.project(x, low, high) self.assertEqual(y[KNEE], 0.5) self.assertEqual(TL.project(np.array([0, 0, 0, 0, -0.8, 0.0]), low, high)[KNEE], -0.5) self.assertEqual(TL.project(np.array([0, 0, 0, 0, 0.3, 0.0]), low, high)[KNEE], 0.3) self.assertEqual(list(np.delete(y, KNEE)), [0.9, -0.9, 0.2, -1.0, 0.0]) def test_full_box_is_the_identity_on_valid_actions(self): low, high = TL.box(6, KNEE, half=1.0) x = np.linspace(-1, 1, 6) self.assertTrue(np.array_equal(TL.project(x, low, high), x)) def test_std_bounds_scale_only_the_knee(self): lo, hi = TL.std_bounds(0.05, 2.0, 6, KNEE) self.assertEqual((lo[KNEE], hi[KNEE]), (0.025, 1.0)) self.assertTrue(np.all(np.delete(lo, KNEE) == 0.05) and np.all(np.delete(hi, KNEE) == 2.0)) lo1, hi1 = TL.std_bounds(0.05, 2.0, 6, KNEE, half=1.0) self.assertTrue(np.all(lo1 == 0.05) and np.all(hi1 == 2.0)) def test_bad_box_is_refused(self): for half in (0, -0.5, 1.5, float("nan")): with self.assertRaises(ValueError): TL.box(6, KNEE, half) with self.assertRaises(ValueError): TL.box(6, 6) class ExecutedCommand(unittest.TestCase): def test_model_half_reaches_the_simulator_as_one_without_clipping(self): for m, u in ((0.5, 1.0), (-0.5, -1.0), (0.3, 0.6), (0.0, 0.0)): action = np.array([0.1, -0.2, 0.3, -0.4, m, 0.6]) command, clipped = TL.executed_command(action, KNEE) self.assertAlmostEqual(command[KNEE], u) self.assertFalse(clipped) self.assertEqual(action[KNEE], m) # the model-space action is never modified self.assertEqual(list(np.delete(command, KNEE)), [0.1, -0.2, 0.3, -0.4, 0.6]) def test_torque_equivalence_inside_the_box(self): for m in (0.5, -0.5, 0.3, -0.3, 0.0): self.assertTrue(TL.torque_equivalent(m)) self.assertFalse(TL.torque_equivalent(0.8)) # outside the box gear 25 cannot deliver it class Classes(unittest.TestCase): COMP = dict(df_bar=547.7, n=334.2, t=36.75) def test_ordered_classes(self): self.assertEqual(TL.outcome(True, 520.0, **self.COMP), TL.MATCHES) self.assertEqual(TL.outcome(True, 420.0, **self.COMP), TL.PARTIAL) self.assertEqual(TL.outcome(True, 360.0, **self.COMP), TL.NONE_SHOWN) self.assertEqual(TL.outcome(True, 200.0, **self.COMP), TL.NONE_SHOWN) def test_failed_check_makes_any_result_invalid(self): self.assertEqual(TL.outcome(False, 600.0, **self.COMP), TL.INVALID) def test_boundaries(self): self.assertEqual(TL.outcome(True, 547.7 - 36.75, **self.COMP), TL.MATCHES) self.assertEqual(TL.outcome(True, 334.2 + 36.75, **self.COMP), TL.NONE_SHOWN) def test_non_finite_mean_is_refused(self): with self.assertRaises(ValueError): TL.outcome(True, float("inf"), **self.COMP) def test_paired_differences_per_seed(self): out = TL.paired_differences([10.0, 20.0], {2: [1.0, 2.0], 3: [10.0, 10.0]}) self.assertEqual(out["2"]["differences"], [9.0, 18.0]) self.assertEqual(out["3"]["mean"], 5.0) with self.assertRaises(ValueError): TL.paired_differences([1.0], {2: [1.0, 2.0]}) def test_progress_total(self): self.assertEqual(TL.progress_total(), 21) if __name__ == "__main__": unittest.main()