MilesCranmer commited on
Commit
363e593
Β·
unverified Β·
2 Parent(s): 9bd009b f88031e

Merge pull request #221 from MilesCranmer/test-refactor

Browse files
.github/workflows/CI.yml CHANGED
@@ -36,21 +36,15 @@ jobs:
36
  uses: julia-actions/setup-julia@v1
37
  with:
38
  version: ${{ matrix.julia-version }}
39
- - name: "Cache dependencies"
40
- uses: actions/cache@v1 # Thanks FromFile.jl
41
- env:
42
- cache-name: cache-artifacts
43
  with:
44
- path: ~/.julia/artifacts
45
- key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
46
- restore-keys: |
47
- ${{ runner.os }}-build-${{ env.cache-name }}-
48
- ${{ runner.os }}-build-
49
- ${{ runner.os }}-
50
  - name: "Set up Python"
51
  uses: actions/setup-python@v4
52
  with:
53
  python-version: ${{ matrix.python-version }}
 
54
  - name: "Install PySR"
55
  run: |
56
  python -m pip install --upgrade pip
@@ -60,17 +54,17 @@ jobs:
60
  - name: "Install Coverage tool"
61
  run: pip install coverage coveralls
62
  - name: "Run tests"
63
- run: coverage run --source=pysr --omit='*/feynman_problems.py' -m unittest test.test
64
  - name: "Install JAX"
65
  run: pip install jax jaxlib # (optional import)
66
  - name: "Run JAX tests"
67
- run: coverage run --append --source=pysr --omit='*/feynman_problems.py' -m unittest test.test_jax
68
  - name: "Install Torch"
69
  run: pip install torch # (optional import)
70
  - name: "Run Torch tests"
71
- run: coverage run --append --source=pysr --omit='*/feynman_problems.py' -m unittest test.test_torch
72
  - name: "Run custom env tests"
73
- run: coverage run --append --source=pysr --omit='*/feynman_problems.py' -m unittest test.test_env
74
  - name: Coveralls
75
  env:
76
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -91,21 +85,13 @@ jobs:
91
 
92
  steps:
93
  - uses: actions/checkout@v3
94
- - name: "Set up Julia"
95
- uses: julia-actions/setup-julia@v1
96
- with:
97
- version: ${{ matrix.julia-version }}
98
- - name: "Cache dependencies"
99
- uses: actions/cache@v1 # Thanks FromFile.jl
100
  env:
101
- cache-name: cache-artifacts
102
  with:
103
- path: ~/.julia/artifacts
104
- key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
105
- restore-keys: |
106
- ${{ runner.os }}-build-${{ env.cache-name }}-
107
- ${{ runner.os }}-build-
108
- ${{ runner.os }}-
109
  - name: "Set up Conda"
110
  uses: conda-incubator/setup-miniconda@v2
111
  with:
@@ -113,30 +99,23 @@ jobs:
113
  miniforge-version: latest
114
  auto-activate-base: true
115
  python-version: ${{ matrix.python-version }}
116
- activate-environment: test
117
  environment-file: environment.yml
 
 
 
 
118
  - name: "Install PySR"
119
  run: |
120
  python3 -m pip install .
121
  python3 -c 'import pysr; pysr.install()'
122
- - name: "Install Coverage tool"
123
- run: python3 -m pip install coverage coveralls
124
  - name: "Run tests"
125
- run: coverage run --append --source=pysr --omit='*/feynman_problems.py' -m unittest test.test
126
- - name: "Run custom env tests"
127
- run: coverage run --append --source=pysr --omit='*/feynman_problems.py' -m unittest test.test_env
128
- - name: Coveralls
129
- env:
130
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
131
- COVERALLS_FLAG_NAME: conda-test-${{ matrix.test-name }}
132
- COVERALLS_PARALLEL: true
133
- run: coveralls --service=github
134
 
135
  coveralls:
136
  name: Indicate completion to coveralls.io
137
  needs:
138
  - test
139
- - conda_test
140
  runs-on: ubuntu-latest
141
  defaults:
142
  run:
 
36
  uses: julia-actions/setup-julia@v1
37
  with:
38
  version: ${{ matrix.julia-version }}
39
+ - name: "Cache Julia"
40
+ uses: julia-actions/cache@v1
 
 
41
  with:
42
+ cache-name: ${{ matrix.os }}-test-${{ matrix.julia-version }}-${{ matrix.python-version }}
 
 
 
 
 
43
  - name: "Set up Python"
44
  uses: actions/setup-python@v4
45
  with:
46
  python-version: ${{ matrix.python-version }}
47
+ cache: pip
48
  - name: "Install PySR"
49
  run: |
50
  python -m pip install --upgrade pip
 
54
  - name: "Install Coverage tool"
55
  run: pip install coverage coveralls
56
  - name: "Run tests"
57
+ run: coverage run --source=pysr --omit='*/test/*,*/feynman_problems.py' -m pysr.test main
58
  - name: "Install JAX"
59
  run: pip install jax jaxlib # (optional import)
60
  - name: "Run JAX tests"
61
+ run: coverage run --append --source=pysr --omit='*/test/*,*/feynman_problems.py' -m pysr.test jax
62
  - name: "Install Torch"
63
  run: pip install torch # (optional import)
64
  - name: "Run Torch tests"
65
+ run: coverage run --append --source=pysr --omit='*/test/*,*/feynman_problems.py' -m pysr.test torch
66
  - name: "Run custom env tests"
67
+ run: coverage run --append --source=pysr --omit='*/test/*,*/feynman_problems.py' -m pysr.test env
68
  - name: Coveralls
69
  env:
70
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 
85
 
86
  steps:
87
  - uses: actions/checkout@v3
88
+ - name: "Cache conda"
89
+ uses: actions/cache@v2
 
 
 
 
90
  env:
91
+ CACHE_NUMBER: 0
92
  with:
93
+ path: ~/conda_pkgs_dir
94
+ key: ${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('environment.yml') }}
 
 
 
 
95
  - name: "Set up Conda"
96
  uses: conda-incubator/setup-miniconda@v2
97
  with:
 
99
  miniforge-version: latest
100
  auto-activate-base: true
101
  python-version: ${{ matrix.python-version }}
102
+ activate-environment: pysr-test
103
  environment-file: environment.yml
104
+ - name: "Cache Julia"
105
+ uses: julia-actions/cache@v1
106
+ with:
107
+ cache-name: ${{ matrix.os }}-conda-${{ matrix.julia-version }}-${{ matrix.python-version }}
108
  - name: "Install PySR"
109
  run: |
110
  python3 -m pip install .
111
  python3 -c 'import pysr; pysr.install()'
 
 
112
  - name: "Run tests"
113
+ run: cd /tmp && python -m pysr.test main
 
 
 
 
 
 
 
 
114
 
115
  coveralls:
116
  name: Indicate completion to coveralls.io
117
  needs:
118
  - test
 
119
  runs-on: ubuntu-latest
120
  defaults:
121
  run:
.github/workflows/CI_Windows.yml CHANGED
@@ -36,21 +36,15 @@ jobs:
36
  uses: julia-actions/setup-julia@v1
37
  with:
38
  version: ${{ matrix.julia-version }}
39
- - name: "Cache dependencies"
40
- uses: actions/cache@v1 # Thanks FromFile.jl
41
- env:
42
- cache-name: cache-artifacts
43
  with:
44
- path: ~/.julia/artifacts
45
- key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
46
- restore-keys: |
47
- ${{ runner.os }}-build-${{ env.cache-name }}-
48
- ${{ runner.os }}-build-
49
- ${{ runner.os }}-
50
  - name: "Set up Python"
51
  uses: actions/setup-python@v4
52
  with:
53
  python-version: ${{ matrix.python-version }}
 
54
  - name: "Install PySR"
55
  run: |
56
  python -m pip install --upgrade pip
@@ -58,10 +52,10 @@ jobs:
58
  python setup.py install
59
  python -c 'import pysr; pysr.install()'
60
  - name: "Run tests"
61
- run: python -m unittest test.test
62
  - name: "Install Torch"
63
  run: pip install torch # (optional import)
64
  - name: "Run Torch tests"
65
- run: python -m unittest test.test_torch
66
  - name: "Run custom env tests"
67
- run: python -m unittest test.test_env
 
36
  uses: julia-actions/setup-julia@v1
37
  with:
38
  version: ${{ matrix.julia-version }}
39
+ - name: "Cache Julia"
40
+ uses: julia-actions/cache@v1
 
 
41
  with:
42
+ cache-name: ${{ matrix.os }}-test-${{ matrix.julia-version }}-${{ matrix.python-version }}
 
 
 
 
 
43
  - name: "Set up Python"
44
  uses: actions/setup-python@v4
45
  with:
46
  python-version: ${{ matrix.python-version }}
47
+ cache: pip
48
  - name: "Install PySR"
49
  run: |
50
  python -m pip install --upgrade pip
 
52
  python setup.py install
53
  python -c 'import pysr; pysr.install()'
54
  - name: "Run tests"
55
+ run: python -m pysr.test main
56
  - name: "Install Torch"
57
  run: pip install torch # (optional import)
58
  - name: "Run Torch tests"
59
+ run: python -m pysr.test torch
60
  - name: "Run custom env tests"
61
+ run: python -m pysr.test env
.github/workflows/CI_conda_forge.yml CHANGED
@@ -24,7 +24,6 @@ jobs:
24
  use-mamba: [true, false]
25
 
26
  steps:
27
- - uses: actions/checkout@v3
28
  - name: "Set up Conda"
29
  uses: conda-incubator/setup-miniconda@v2
30
  with:
@@ -40,7 +39,4 @@ jobs:
40
  run: conda activate pysr-test && conda install pysr
41
  if: ${{ !matrix.use-mamba }}
42
  - name: "Run tests"
43
- run: |
44
- conda activate pysr-test
45
- cd $(python -c 'import pysr; from pathlib import Path; print(Path(pysr.__file__).parent.parent)')
46
- python -m unittest test.test
 
24
  use-mamba: [true, false]
25
 
26
  steps:
 
27
  - name: "Set up Conda"
28
  uses: conda-incubator/setup-miniconda@v2
29
  with:
 
39
  run: conda activate pysr-test && conda install pysr
40
  if: ${{ !matrix.use-mamba }}
41
  - name: "Run tests"
42
+ run: python -m pysr.test main
 
 
 
.github/workflows/CI_docker.yml CHANGED
@@ -38,4 +38,4 @@ jobs:
38
  - name: Build docker
39
  run: docker build -t pysr --build-arg ARCH=${{ matrix.arch }} --build-arg VERSION=${{ matrix.julia-version }} --build-arg PYVERSION=${{ matrix.python-version }} .
40
  - name: Test docker
41
- run: docker run --platform=${{ matrix.arch }} --rm pysr /bin/bash -c 'python3 -m unittest test.test && python3 -m unittest test.test_env'
 
38
  - name: Build docker
39
  run: docker build -t pysr --build-arg ARCH=${{ matrix.arch }} --build-arg VERSION=${{ matrix.julia-version }} --build-arg PYVERSION=${{ matrix.python-version }} .
40
  - name: Test docker
41
+ run: docker run --platform=${{ matrix.arch }} --rm pysr /bin/bash -c 'python3 -m pysr.test main && python3 -m pysr.test env'
.github/workflows/CI_docker_large_nightly.yml CHANGED
@@ -31,4 +31,4 @@ jobs:
31
  - name: Build docker
32
  run: docker build -t pysr --build-arg ARCH=${{ matrix.arch }} --build-arg VERSION=${{ matrix.julia-version }} --build-arg PYVERSION=${{ matrix.python-version }} .
33
  - name: Test docker
34
- run: docker run --platform=${{ matrix.arch }} --rm pysr /bin/bash -c 'python3 -m unittest test.test && python3 -m unittest test.test_env'
 
31
  - name: Build docker
32
  run: docker build -t pysr --build-arg ARCH=${{ matrix.arch }} --build-arg VERSION=${{ matrix.julia-version }} --build-arg PYVERSION=${{ matrix.python-version }} .
33
  - name: Test docker
34
+ run: docker run --platform=${{ matrix.arch }} --rm pysr /bin/bash -c 'python3 -m pysr.test main && python3 -m pysr.test env'
.github/workflows/CI_large_nightly.yml CHANGED
@@ -41,7 +41,7 @@ jobs:
41
  python setup.py install
42
  python -c 'import pysr; pysr.install()'
43
  - name: "Run tests"
44
- run: python -m unittest test.test
45
  - name: "Run new env test"
46
- run: python -m unittest test.test_env
47
  if: ${{ !(matrix.os == 'windows-latest' && matrix.python-version == '3.7') }}
 
41
  python setup.py install
42
  python -c 'import pysr; pysr.install()'
43
  - name: "Run tests"
44
+ run: python -m pysr.test main
45
  - name: "Run new env test"
46
+ run: python -m pysr.test env
47
  if: ${{ !(matrix.os == 'windows-latest' && matrix.python-version == '3.7') }}
.github/workflows/CI_mac.yml CHANGED
@@ -36,21 +36,15 @@ jobs:
36
  uses: julia-actions/setup-julia@v1
37
  with:
38
  version: ${{ matrix.julia-version }}
39
- - name: "Cache dependencies"
40
- uses: actions/cache@v3
41
- env:
42
- cache-name: cache-artifacts
43
  with:
44
- path: ~/.julia/artifacts
45
- key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
46
- restore-keys: |
47
- ${{ runner.os }}-build-${{ env.cache-name }}-
48
- ${{ runner.os }}-build-
49
- ${{ runner.os }}-
50
  - name: "Set up Python"
51
  uses: actions/setup-python@v4
52
  with:
53
  python-version: ${{ matrix.python-version }}
 
54
  - name: "Install PySR"
55
  run: |
56
  python -m pip install --upgrade pip
@@ -58,14 +52,14 @@ jobs:
58
  python setup.py install
59
  python -c 'import pysr; pysr.install()'
60
  - name: "Run tests"
61
- run: python -m unittest test.test
62
  - name: "Install JAX"
63
  run: pip install jax jaxlib # (optional import)
64
  - name: "Run JAX tests"
65
- run: python -m unittest test.test_jax
66
  - name: "Install Torch"
67
  run: pip install torch # (optional import)
68
  - name: "Run Torch tests"
69
- run: python -m unittest test.test_torch
70
  - name: "Run custom env tests"
71
- run: python -m unittest test.test_env
 
36
  uses: julia-actions/setup-julia@v1
37
  with:
38
  version: ${{ matrix.julia-version }}
39
+ - name: "Cache Julia"
40
+ uses: julia-actions/cache@v1
 
 
41
  with:
42
+ cache-name: ${{ matrix.os }}-test-${{ matrix.julia-version }}-${{ matrix.python-version }}
 
 
 
 
 
43
  - name: "Set up Python"
44
  uses: actions/setup-python@v4
45
  with:
46
  python-version: ${{ matrix.python-version }}
47
+ cache: pip
48
  - name: "Install PySR"
49
  run: |
50
  python -m pip install --upgrade pip
 
52
  python setup.py install
53
  python -c 'import pysr; pysr.install()'
54
  - name: "Run tests"
55
+ run: python -m pysr.test main
56
  - name: "Install JAX"
57
  run: pip install jax jaxlib # (optional import)
58
  - name: "Run JAX tests"
59
+ run: python -m pysr.test jax
60
  - name: "Install Torch"
61
  run: pip install torch # (optional import)
62
  - name: "Run Torch tests"
63
+ run: python -m pysr.test torch
64
  - name: "Run custom env tests"
65
+ run: python -m pysr.test env
.github/workflows/docs.yml CHANGED
@@ -25,6 +25,7 @@ jobs:
25
  uses: actions/setup-python@v4
26
  with:
27
  python-version: 3.9
 
28
  - name: "Install packages for docs building"
29
  run: pip install mkdocs-material mkdocs-autorefs 'mkdocstrings[python]' docstring_parser
30
  - name: "Install PySR"
 
25
  uses: actions/setup-python@v4
26
  with:
27
  python-version: 3.9
28
+ cache: pip
29
  - name: "Install packages for docs building"
30
  run: pip install mkdocs-material mkdocs-autorefs 'mkdocstrings[python]' docstring_parser
31
  - name: "Install PySR"
.gitignore CHANGED
@@ -19,3 +19,6 @@ Manifest.toml
19
  workflow
20
  docs/index.md
21
  site
 
 
 
 
19
  workflow
20
  docs/index.md
21
  site
22
+ **/.DS_Store
23
+ **/*.code-workspace
24
+ **/*.tar.gz
Dockerfile CHANGED
@@ -50,7 +50,4 @@ RUN pip3 install .
50
  # Install Julia pre-requisites:
51
  RUN python3 -c 'import pysr; pysr.install()'
52
 
53
- # Add tests
54
- ADD ./test/ /pysr/test/
55
-
56
  CMD ["bash"]
 
50
  # Install Julia pre-requisites:
51
  RUN python3 -c 'import pysr; pysr.install()'
52
 
 
 
 
53
  CMD ["bash"]
environment.yml CHANGED
@@ -1,7 +1,6 @@
1
  name: test
2
  channels:
3
  - conda-forge
4
- - defaults
5
  dependencies:
6
  - sympy
7
  - pandas
 
1
  name: test
2
  channels:
3
  - conda-forge
 
4
  dependencies:
5
  - sympy
6
  - pandas
pysr/test/__init__.py ADDED
@@ -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/__main__.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """CLI for running PySR's test suite."""
2
+ import argparse
3
+ import os
4
+
5
+ from . import *
6
+
7
+ if __name__ == "__main__":
8
+ # Get args:
9
+ parser = argparse.ArgumentParser()
10
+ parser.usage = "python -m pysr.test [tests...]"
11
+ parser.add_argument(
12
+ "test",
13
+ nargs="*",
14
+ help="Test to run. One or more of 'main', 'env', 'jax', 'torch'.",
15
+ )
16
+
17
+ # Parse args:
18
+ args = parser.parse_args()
19
+ tests = args.test
20
+
21
+ if len(tests) == 0:
22
+ # Raise help message:
23
+ parser.print_help()
24
+ raise SystemExit(1)
25
+
26
+ # Run tests:
27
+ for test in tests:
28
+ if test in {"main", "env", "jax", "torch"}:
29
+ cur_dir = os.path.dirname(os.path.abspath(__file__))
30
+ print(f"Running test from {cur_dir}")
31
+ if test == "main":
32
+ runtests()
33
+ elif test == "env":
34
+ runtests_env()
35
+ elif test == "jax":
36
+ runtests_jax()
37
+ elif test == "torch":
38
+ runtests_torch()
39
+ else:
40
+ parser.print_help()
41
+ raise SystemExit(1)
{test β†’ pysr/test}/test.py RENAMED
@@ -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)
{test β†’ pysr/test}/test_env.py RENAMED
@@ -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)
{test β†’ pysr/test}/test_jax.py RENAMED
@@ -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)
{test β†’ pysr/test}/test_torch.py RENAMED
@@ -1,22 +1,26 @@
1
  import unittest
2
  import numpy as np
3
  import pandas as pd
4
- from pysr import sympy2torch, PySRRegressor
 
 
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
- Main = init_julia()
14
- import torch
15
- else:
16
- # Import Torch, then PyJulia
17
- # https://github.com/pytorch/pytorch/issues/78829
18
- import torch
19
- import sympy
 
 
 
 
 
 
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)
test/__init__.py DELETED
File without changes