MilesCranmer commited on
Commit
51a6b05
1 Parent(s): 25f8cac

Make tests non-random

Browse files
Files changed (3) hide show
  1. test/test.py +9 -6
  2. test/test_jax.py +5 -1
  3. test/test_torch.py +6 -2
test/test.py CHANGED
@@ -53,10 +53,12 @@ class TestPipeline(unittest.TestCase):
53
 
54
  np.testing.assert_almost_equal(
55
  best_callable()[0](self.X),
56
- self.X[:, 0]**2)
 
57
  np.testing.assert_almost_equal(
58
  best_callable()[1](self.X),
59
- self.X[:, 1]**2)
 
60
 
61
  def test_empty_operators_single_input(self):
62
  X = np.random.randn(100, 1)
@@ -96,19 +98,20 @@ class TestBest(unittest.TestCase):
96
  X = np.random.randn(10, 2)
97
  y = np.cos(X[:, 0])**2
98
  for f in [best_callable(), best_callable(self.equations)]:
99
- np.testing.assert_almost_equal(f(X), y)
100
 
101
 
102
  class TestFeatureSelection(unittest.TestCase):
103
- def test_feature_selection(self):
104
  np.random.seed(0)
105
- X = np.random.randn(20001, 5)
 
 
106
  y = X[:, 2]**2 + X[:, 3]**2
107
  selected = run_feature_selection(X, y, select_k_features=2)
108
  self.assertEqual(sorted(selected), [2, 3])
109
 
110
  def test_feature_selection_handler(self):
111
- np.random.seed(0)
112
  X = np.random.randn(20000, 5)
113
  y = X[:, 2]**2 + X[:, 3]**2
114
  var_names = [f'x{i}' for i in range(5)]
 
53
 
54
  np.testing.assert_almost_equal(
55
  best_callable()[0](self.X),
56
+ self.X[:, 0]**2,
57
+ decimal=4)
58
  np.testing.assert_almost_equal(
59
  best_callable()[1](self.X),
60
+ self.X[:, 1]**2,
61
+ decimal=4)
62
 
63
  def test_empty_operators_single_input(self):
64
  X = np.random.randn(100, 1)
 
98
  X = np.random.randn(10, 2)
99
  y = np.cos(X[:, 0])**2
100
  for f in [best_callable(), best_callable(self.equations)]:
101
+ np.testing.assert_almost_equal(f(X), y, decimal=4)
102
 
103
 
104
  class TestFeatureSelection(unittest.TestCase):
105
+ def setUp(self):
106
  np.random.seed(0)
107
+
108
+ def test_feature_selection(self):
109
+ X = np.random.randn(20000, 5)
110
  y = X[:, 2]**2 + X[:, 3]**2
111
  selected = run_feature_selection(X, y, select_k_features=2)
112
  self.assertEqual(sorted(selected), [2, 3])
113
 
114
  def test_feature_selection_handler(self):
 
115
  X = np.random.randn(20000, 5)
116
  y = X[:, 2]**2 + X[:, 3]**2
117
  var_names = [f'x{i}' for i in range(5)]
test/test_jax.py CHANGED
@@ -8,6 +8,9 @@ from jax import grad
8
  import sympy
9
 
10
  class TestJAX(unittest.TestCase):
 
 
 
11
  def test_sympy2jax(self):
12
  x, y, z = sympy.symbols('x y z')
13
  cosx = 1.0 * sympy.cos(x) + y
@@ -35,5 +38,6 @@ class TestJAX(unittest.TestCase):
35
  jformat = equations.iloc[-1].jax_format
36
  np.testing.assert_almost_equal(
37
  np.array(jformat['callable'](jnp.array(X), jformat['parameters'])),
38
- np.square(np.cos(X[:, 1])) # Select feature 1
 
39
  )
 
8
  import sympy
9
 
10
  class TestJAX(unittest.TestCase):
11
+ def setUp(self):
12
+ np.random.seed(0)
13
+
14
  def test_sympy2jax(self):
15
  x, y, z = sympy.symbols('x y z')
16
  cosx = 1.0 * sympy.cos(x) + y
 
38
  jformat = equations.iloc[-1].jax_format
39
  np.testing.assert_almost_equal(
40
  np.array(jformat['callable'](jnp.array(X), jformat['parameters'])),
41
+ np.square(np.cos(X[:, 1])), # Select feature 1
42
+ decimal=4
43
  )
test/test_torch.py CHANGED
@@ -6,10 +6,13 @@ import torch
6
  import sympy
7
 
8
  class TestTorch(unittest.TestCase):
 
 
 
9
  def test_sympy2torch(self):
10
  x, y, z = sympy.symbols('x y z')
11
  cosx = 1.0 * sympy.cos(x) + y
12
- X = torch.randn((1000, 3))
13
  true = 1.0 * torch.cos(X[:, 0]) + X[:, 1]
14
  torch_module = sympy2torch(cosx, [x, y, z])
15
  self.assertTrue(
@@ -34,5 +37,6 @@ class TestTorch(unittest.TestCase):
34
  tformat = equations.iloc[-1].torch_format
35
  np.testing.assert_almost_equal(
36
  tformat(torch.tensor(X)).detach().numpy(),
37
- np.square(np.cos(X[:, 1])) #Selection 1st feature
 
38
  )
 
6
  import sympy
7
 
8
  class TestTorch(unittest.TestCase):
9
+ def setUp(self):
10
+ np.random.seed(0)
11
+
12
  def test_sympy2torch(self):
13
  x, y, z = sympy.symbols('x y z')
14
  cosx = 1.0 * sympy.cos(x) + y
15
+ X = torch.tensor(np.random.randn(1000, 3))
16
  true = 1.0 * torch.cos(X[:, 0]) + X[:, 1]
17
  torch_module = sympy2torch(cosx, [x, y, z])
18
  self.assertTrue(
 
37
  tformat = equations.iloc[-1].torch_format
38
  np.testing.assert_almost_equal(
39
  tformat(torch.tensor(X)).detach().numpy(),
40
+ np.square(np.cos(X[:, 1])), #Selection 1st feature
41
+ decimal=4
42
  )