Spaces:
Running
Running
Commit
·
a2fd8f3
1
Parent(s):
08fbf08
Force tests to import locally
Browse files- pysr/test/__init__.py +4 -0
- pysr/test/test.py +27 -8
- pysr/test/test_env.py +11 -1
- pysr/test/test_jax.py +18 -3
- pysr/test/test_torch.py +31 -12
pysr/test/__init__.py
CHANGED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .test import runtests
|
| 2 |
+
from .test_env import runtests as runtests_env
|
| 3 |
+
from .test_jax import runtests as runtests_jax
|
| 4 |
+
from .test_torch import runtests as runtests_torch
|
pysr/test/test.py
CHANGED
|
@@ -4,14 +4,6 @@ import inspect
|
|
| 4 |
import unittest
|
| 5 |
import numpy as np
|
| 6 |
from sklearn import model_selection
|
| 7 |
-
from pysr import PySRRegressor
|
| 8 |
-
from pysr.sr import (
|
| 9 |
-
run_feature_selection,
|
| 10 |
-
_handle_feature_selection,
|
| 11 |
-
_csv_filename_to_pkl_filename,
|
| 12 |
-
idx_model_selection,
|
| 13 |
-
)
|
| 14 |
-
from pysr.export_latex import to_latex
|
| 15 |
from sklearn.utils.estimator_checks import check_estimator
|
| 16 |
import sympy
|
| 17 |
import pandas as pd
|
|
@@ -20,6 +12,15 @@ import pickle as pkl
|
|
| 20 |
import tempfile
|
| 21 |
from pathlib import Path
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
DEFAULT_PARAMS = inspect.signature(PySRRegressor.__init__).parameters
|
| 24 |
DEFAULT_NITERATIONS = DEFAULT_PARAMS["niterations"].default
|
| 25 |
DEFAULT_POPULATIONS = DEFAULT_PARAMS["populations"].default
|
|
@@ -856,3 +857,21 @@ class TestLaTeXTable(unittest.TestCase):
|
|
| 856 |
+ self.create_true_latex(middle_part, include_score=True)
|
| 857 |
)
|
| 858 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import unittest
|
| 5 |
import numpy as np
|
| 6 |
from sklearn import model_selection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from sklearn.utils.estimator_checks import check_estimator
|
| 8 |
import sympy
|
| 9 |
import pandas as pd
|
|
|
|
| 12 |
import tempfile
|
| 13 |
from pathlib import Path
|
| 14 |
|
| 15 |
+
from .. import PySRRegressor
|
| 16 |
+
from ..sr import (
|
| 17 |
+
run_feature_selection,
|
| 18 |
+
_handle_feature_selection,
|
| 19 |
+
_csv_filename_to_pkl_filename,
|
| 20 |
+
idx_model_selection,
|
| 21 |
+
)
|
| 22 |
+
from ..export_latex import to_latex
|
| 23 |
+
|
| 24 |
DEFAULT_PARAMS = inspect.signature(PySRRegressor.__init__).parameters
|
| 25 |
DEFAULT_NITERATIONS = DEFAULT_PARAMS["niterations"].default
|
| 26 |
DEFAULT_POPULATIONS = DEFAULT_PARAMS["populations"].default
|
|
|
|
| 857 |
+ self.create_true_latex(middle_part, include_score=True)
|
| 858 |
)
|
| 859 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
| 860 |
+
|
| 861 |
+
|
| 862 |
+
def runtests():
|
| 863 |
+
"""Run all tests in test.py."""
|
| 864 |
+
suite = unittest.TestSuite()
|
| 865 |
+
loader = unittest.TestLoader()
|
| 866 |
+
test_cases = [
|
| 867 |
+
TestPipeline,
|
| 868 |
+
TestBest,
|
| 869 |
+
TestFeatureSelection,
|
| 870 |
+
TestMiscellaneous,
|
| 871 |
+
TestLaTeXTable,
|
| 872 |
+
]
|
| 873 |
+
for test_case in test_cases:
|
| 874 |
+
tests = loader.loadTestsFromTestCase(test_case)
|
| 875 |
+
suite.addTests(tests)
|
| 876 |
+
runner = unittest.TextTestRunner()
|
| 877 |
+
return runner.run(suite)
|
pysr/test/test_env.py
CHANGED
|
@@ -2,9 +2,10 @@
|
|
| 2 |
|
| 3 |
import unittest
|
| 4 |
import os
|
| 5 |
-
from pysr import julia_helpers
|
| 6 |
from tempfile import TemporaryDirectory
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
class TestJuliaProject(unittest.TestCase):
|
| 10 |
"""Various tests for working with Julia projects."""
|
|
@@ -46,3 +47,12 @@ class TestJuliaProject(unittest.TestCase):
|
|
| 46 |
del os.environ["JULIA_DEPOT_PATH"]
|
| 47 |
else:
|
| 48 |
os.environ["JULIA_DEPOT_PATH"] = old_env
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import unittest
|
| 4 |
import os
|
|
|
|
| 5 |
from tempfile import TemporaryDirectory
|
| 6 |
|
| 7 |
+
from .. import julia_helpers
|
| 8 |
+
|
| 9 |
|
| 10 |
class TestJuliaProject(unittest.TestCase):
|
| 11 |
"""Various tests for working with Julia projects."""
|
|
|
|
| 47 |
del os.environ["JULIA_DEPOT_PATH"]
|
| 48 |
else:
|
| 49 |
os.environ["JULIA_DEPOT_PATH"] = old_env
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def runtests():
|
| 53 |
+
"""Run all tests in test_env.py."""
|
| 54 |
+
loader = unittest.TestLoader()
|
| 55 |
+
suite = unittest.TestSuite()
|
| 56 |
+
suite.addTests(loader.loadTestsFromTestCase(TestJuliaProject))
|
| 57 |
+
runner = unittest.TextTestRunner()
|
| 58 |
+
return runner.run(suite)
|
pysr/test/test_jax.py
CHANGED
|
@@ -1,18 +1,20 @@
|
|
| 1 |
import unittest
|
| 2 |
import numpy as np
|
| 3 |
-
from pysr import sympy2jax, PySRRegressor
|
| 4 |
import pandas as pd
|
| 5 |
-
from jax import numpy as jnp
|
| 6 |
-
from jax import random
|
| 7 |
import sympy
|
| 8 |
from functools import partial
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
| 11 |
class TestJAX(unittest.TestCase):
|
| 12 |
def setUp(self):
|
| 13 |
np.random.seed(0)
|
| 14 |
|
| 15 |
def test_sympy2jax(self):
|
|
|
|
|
|
|
|
|
|
| 16 |
x, y, z = sympy.symbols("x y z")
|
| 17 |
cosx = 1.0 * sympy.cos(x) + y
|
| 18 |
key = random.PRNGKey(0)
|
|
@@ -22,6 +24,8 @@ class TestJAX(unittest.TestCase):
|
|
| 22 |
self.assertTrue(jnp.all(jnp.isclose(f(X, params), true)).item())
|
| 23 |
|
| 24 |
def test_pipeline_pandas(self):
|
|
|
|
|
|
|
| 25 |
X = pd.DataFrame(np.random.randn(100, 10))
|
| 26 |
y = np.ones(X.shape[0])
|
| 27 |
model = PySRRegressor(
|
|
@@ -53,6 +57,8 @@ class TestJAX(unittest.TestCase):
|
|
| 53 |
)
|
| 54 |
|
| 55 |
def test_pipeline(self):
|
|
|
|
|
|
|
| 56 |
X = np.random.randn(100, 10)
|
| 57 |
y = np.ones(X.shape[0])
|
| 58 |
model = PySRRegressor(progress=False, max_evals=10000, output_jax_format=True)
|
|
@@ -112,3 +118,12 @@ class TestJAX(unittest.TestCase):
|
|
| 112 |
|
| 113 |
np.testing.assert_almost_equal(y.values, np_output, decimal=3)
|
| 114 |
np.testing.assert_almost_equal(y.values, jax_output, decimal=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import unittest
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
| 4 |
import sympy
|
| 5 |
from functools import partial
|
| 6 |
|
| 7 |
+
from .. import sympy2jax, PySRRegressor
|
| 8 |
+
|
| 9 |
|
| 10 |
class TestJAX(unittest.TestCase):
|
| 11 |
def setUp(self):
|
| 12 |
np.random.seed(0)
|
| 13 |
|
| 14 |
def test_sympy2jax(self):
|
| 15 |
+
from jax import numpy as jnp
|
| 16 |
+
from jax import random
|
| 17 |
+
|
| 18 |
x, y, z = sympy.symbols("x y z")
|
| 19 |
cosx = 1.0 * sympy.cos(x) + y
|
| 20 |
key = random.PRNGKey(0)
|
|
|
|
| 24 |
self.assertTrue(jnp.all(jnp.isclose(f(X, params), true)).item())
|
| 25 |
|
| 26 |
def test_pipeline_pandas(self):
|
| 27 |
+
from jax import numpy as jnp
|
| 28 |
+
|
| 29 |
X = pd.DataFrame(np.random.randn(100, 10))
|
| 30 |
y = np.ones(X.shape[0])
|
| 31 |
model = PySRRegressor(
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
def test_pipeline(self):
|
| 60 |
+
from jax import numpy as jnp
|
| 61 |
+
|
| 62 |
X = np.random.randn(100, 10)
|
| 63 |
y = np.ones(X.shape[0])
|
| 64 |
model = PySRRegressor(progress=False, max_evals=10000, output_jax_format=True)
|
|
|
|
| 118 |
|
| 119 |
np.testing.assert_almost_equal(y.values, np_output, decimal=3)
|
| 120 |
np.testing.assert_almost_equal(y.values, jax_output, decimal=3)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def runtests():
|
| 124 |
+
"""Run all tests in test_jax.py."""
|
| 125 |
+
loader = unittest.TestLoader()
|
| 126 |
+
suite = unittest.TestSuite()
|
| 127 |
+
suite.addTests(loader.loadTestsFromTestCase(TestJAX))
|
| 128 |
+
runner = unittest.TextTestRunner()
|
| 129 |
+
return runner.run(suite)
|
pysr/test/test_torch.py
CHANGED
|
@@ -1,22 +1,26 @@
|
|
| 1 |
import unittest
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Need to initialize Julia before importing torch...
|
| 7 |
-
import platform
|
| 8 |
|
| 9 |
-
if platform.system() == "Darwin":
|
| 10 |
-
# Import PyJulia, then Torch
|
| 11 |
-
from pysr.julia_helpers import init_julia
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
class TestTorch(unittest.TestCase):
|
|
@@ -24,6 +28,7 @@ class TestTorch(unittest.TestCase):
|
|
| 24 |
np.random.seed(0)
|
| 25 |
|
| 26 |
def test_sympy2torch(self):
|
|
|
|
| 27 |
x, y, z = sympy.symbols("x y z")
|
| 28 |
cosx = 1.0 * sympy.cos(x) + y
|
| 29 |
|
|
@@ -35,6 +40,7 @@ class TestTorch(unittest.TestCase):
|
|
| 35 |
)
|
| 36 |
|
| 37 |
def test_pipeline_pandas(self):
|
|
|
|
| 38 |
X = pd.DataFrame(np.random.randn(100, 10))
|
| 39 |
y = np.ones(X.shape[0])
|
| 40 |
model = PySRRegressor(
|
|
@@ -69,6 +75,7 @@ class TestTorch(unittest.TestCase):
|
|
| 69 |
)
|
| 70 |
|
| 71 |
def test_pipeline(self):
|
|
|
|
| 72 |
X = np.random.randn(100, 10)
|
| 73 |
y = np.ones(X.shape[0])
|
| 74 |
model = PySRRegressor(
|
|
@@ -103,6 +110,7 @@ class TestTorch(unittest.TestCase):
|
|
| 103 |
)
|
| 104 |
|
| 105 |
def test_mod_mapping(self):
|
|
|
|
| 106 |
x, y, z = sympy.symbols("x y z")
|
| 107 |
expression = x**2 + sympy.atanh(sympy.Mod(y + 1, 2) - 1) * 3.2 * z
|
| 108 |
|
|
@@ -120,6 +128,7 @@ class TestTorch(unittest.TestCase):
|
|
| 120 |
)
|
| 121 |
|
| 122 |
def test_custom_operator(self):
|
|
|
|
| 123 |
X = np.random.randn(100, 3)
|
| 124 |
y = np.ones(X.shape[0])
|
| 125 |
model = PySRRegressor(
|
|
@@ -160,6 +169,7 @@ class TestTorch(unittest.TestCase):
|
|
| 160 |
)
|
| 161 |
|
| 162 |
def test_feature_selection_custom_operators(self):
|
|
|
|
| 163 |
rstate = np.random.RandomState(0)
|
| 164 |
X = pd.DataFrame({f"k{i}": rstate.randn(2000) for i in range(10, 21)})
|
| 165 |
cos_approx = lambda x: 1 - (x**2) / 2 + (x**4) / 24 + (x**6) / 720
|
|
@@ -188,3 +198,12 @@ class TestTorch(unittest.TestCase):
|
|
| 188 |
|
| 189 |
np.testing.assert_almost_equal(y.values, np_output, decimal=3)
|
| 190 |
np.testing.assert_almost_equal(y.values, torch_output, decimal=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import unittest
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
+
import platform
|
| 5 |
+
import sympy
|
| 6 |
+
from .. import sympy2torch, PySRRegressor
|
| 7 |
|
| 8 |
# Need to initialize Julia before importing torch...
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def _import_torch():
|
| 12 |
+
if platform.system() == "Darwin":
|
| 13 |
+
# Import PyJulia, then Torch
|
| 14 |
+
from ..julia_helpers import init_julia
|
| 15 |
+
|
| 16 |
+
init_julia()
|
| 17 |
+
|
| 18 |
+
import torch
|
| 19 |
+
else:
|
| 20 |
+
# Import Torch, then PyJulia
|
| 21 |
+
# https://github.com/pytorch/pytorch/issues/78829
|
| 22 |
+
import torch
|
| 23 |
+
return torch
|
| 24 |
|
| 25 |
|
| 26 |
class TestTorch(unittest.TestCase):
|
|
|
|
| 28 |
np.random.seed(0)
|
| 29 |
|
| 30 |
def test_sympy2torch(self):
|
| 31 |
+
torch = _import_torch()
|
| 32 |
x, y, z = sympy.symbols("x y z")
|
| 33 |
cosx = 1.0 * sympy.cos(x) + y
|
| 34 |
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
def test_pipeline_pandas(self):
|
| 43 |
+
torch = _import_torch()
|
| 44 |
X = pd.DataFrame(np.random.randn(100, 10))
|
| 45 |
y = np.ones(X.shape[0])
|
| 46 |
model = PySRRegressor(
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
def test_pipeline(self):
|
| 78 |
+
torch = _import_torch()
|
| 79 |
X = np.random.randn(100, 10)
|
| 80 |
y = np.ones(X.shape[0])
|
| 81 |
model = PySRRegressor(
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
def test_mod_mapping(self):
|
| 113 |
+
torch = _import_torch()
|
| 114 |
x, y, z = sympy.symbols("x y z")
|
| 115 |
expression = x**2 + sympy.atanh(sympy.Mod(y + 1, 2) - 1) * 3.2 * z
|
| 116 |
|
|
|
|
| 128 |
)
|
| 129 |
|
| 130 |
def test_custom_operator(self):
|
| 131 |
+
torch = _import_torch()
|
| 132 |
X = np.random.randn(100, 3)
|
| 133 |
y = np.ones(X.shape[0])
|
| 134 |
model = PySRRegressor(
|
|
|
|
| 169 |
)
|
| 170 |
|
| 171 |
def test_feature_selection_custom_operators(self):
|
| 172 |
+
torch = _import_torch()
|
| 173 |
rstate = np.random.RandomState(0)
|
| 174 |
X = pd.DataFrame({f"k{i}": rstate.randn(2000) for i in range(10, 21)})
|
| 175 |
cos_approx = lambda x: 1 - (x**2) / 2 + (x**4) / 24 + (x**6) / 720
|
|
|
|
| 198 |
|
| 199 |
np.testing.assert_almost_equal(y.values, np_output, decimal=3)
|
| 200 |
np.testing.assert_almost_equal(y.values, torch_output, decimal=3)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def runtests():
|
| 204 |
+
"""Run all tests in test_torch.py."""
|
| 205 |
+
loader = unittest.TestLoader()
|
| 206 |
+
suite = unittest.TestSuite()
|
| 207 |
+
suite.addTests(loader.loadTestsFromTestCase(TestTorch))
|
| 208 |
+
runner = unittest.TextTestRunner()
|
| 209 |
+
return runner.run(suite)
|