def test_substitute_helpers(self): backend = NumbaBackend() a = se.Symbol("a") b = se.Symbol("b") y = se.Symbol("y") HELPERS = [(a, se.exp(-12 * y))] DERIVATIVES = [-b * a + y, y**2] result = backend._substitute_helpers(DERIVATIVES, HELPERS) self.assertListEqual(result, [-b * se.exp(-12 * y) + y, y**2])
def test_init(self): backend = NumbaBackend() self.assertTrue(isinstance(backend, NumbaBackend)) self.assertTrue(isinstance(backend, BaseBackend)) self.assertTrue(hasattr(backend, "_replace_current_ys")) self.assertTrue(hasattr(backend, "_replace_past_ys")) self.assertTrue(hasattr(backend, "_replace_inputs")) self.assertTrue(hasattr(backend, "_substitute_helpers"))
def test_get_numba_function_params(self): backend = NumbaBackend() symbol_params = { "node1.mass1.a": sp.Symbol("a"), "node1.mass1.noise.a": sp.Symbol("noise_a"), "node1.connectivity": np.array([[sp.Symbol("conn11"), sp.Symbol("conn12")]]), } should_be = set( [sp.Symbol("conn11"), sp.Symbol("conn12"), sp.Symbol("a")]) result = backend._get_numba_function_params(symbol_params) self.assertSetEqual(set(result), should_be)
def test_create_symbol_to_float_dict(self): backend = NumbaBackend() symbol_params = { "node1.mass1.a": sp.Symbol("a"), "node1.mass1.noise.a": sp.Symbol("noise_a"), "node1.connectivity": np.array([[sp.Symbol("conn11"), sp.Symbol("conn12")]]), } float_params = { "node1.mass1.a": 12.0, "node1.mass1.noise.a": 19 / 7.0, "node1.connectivity": np.array([[4.3, 3.4]]), } should_be = {"a": 12.0, "conn11": 4.3, "conn12": 3.4} result = backend._create_symbol_to_float_dict(symbol_params, float_params) self.assertDictEqual(result, should_be)
def test_replace_inputs(self): backend = NumbaBackend() STRING_IN = "12.4*past_y(-external_input + t, 3 + input_base_n, " "anchors(-external_input + t))" EXPECTED = "12.4*input_y[3, i]" result = backend._replace_inputs(STRING_IN) self.assertEqual(result, EXPECTED)
def test_replace_past_ys(self): backend = NumbaBackend() STRING_IN = "0.06*past_y(-3.21 + t, 2, anchors(-3.21 + t)) + 0.23*past_y(-0.5 + t, 0, anchors(-0.5 + t))" EXPECTED = "0.06*y[2, max_delay + i - 1 - 32] + 0.23*y[0, max_delay + i - 1 - 5]" result = backend._replace_past_ys(STRING_IN, dt=0.1) self.assertEqual(result, EXPECTED)
def test_replace_current_ys(self): backend = NumbaBackend() STRING_IN = "0.4*(1.0*(1.0 - current_y(0))" EXPECTED = "0.4*(1.0*(1.0 - y[0, max_delay + i - 1])" result = backend._replace_current_ys(STRING_IN) self.assertEqual(result, EXPECTED)