Skip to content

Commit

Permalink
Merge pull request #298 from w2ll2am/master
Browse files Browse the repository at this point in the history
Added CLI to run pysr.install() to install Julia dependencies
  • Loading branch information
MilesCranmer authored May 20, 2023
2 parents e66aef0 + 4726b4a commit d6f734d
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 25 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
python setup.py install
python -c 'import pysr; pysr.install()'
python -m pysr install
- name: "Install Coverage tool"
run: pip install coverage coveralls
- name: "Run tests"
run: coverage run --source=pysr --omit='*/test/*,*/feynman_problems.py' -m pysr.test main
run: |
coverage run --source=pysr --omit='*/test/*,*/feynman_problems.py' -m pysr.test main
coverage run --append --source=pysr --omit='*/test/*,*/feynman_problems.py' -m pysr.test cli
- name: "Install JAX"
run: pip install jax jaxlib # (optional import)
- name: "Run JAX tests"
Expand Down Expand Up @@ -112,7 +114,7 @@ jobs:
- name: "Install PySR"
run: |
python3 -m pip install .
python3 -c 'import pysr; pysr.install()'
python3 -m pysr install
- name: "Run tests"
run: cd /tmp && python -m pysr.test main

Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/CI_Windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
python setup.py install
python -c 'import pysr; pysr.install()'
python -m pysr install
- name: "Run tests"
run: python -m pysr.test main
run: |
python -m pysr.test main
python -m pysr.test cli
- name: "Install Torch"
run: pip install torch # (optional import)
- name: "Run Torch tests"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/CI_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
- name: Build docker
run: docker build --platform=${{ matrix.arch }} -t pysr --build-arg JLVERSION=${{ matrix.julia-version }} --build-arg PYVERSION=${{ matrix.python-version }} .
- name: Test docker
run: docker run --platform=${{ matrix.arch }} --rm pysr /bin/bash -c 'python3 -m pysr.test main && python3 -m pysr.test env'
run: docker run --platform=${{ matrix.arch }} --rm pysr /bin/bash -c 'python3 -m pysr.test main && python3 -m pysr.test cli && python3 -m pysr.test env'
2 changes: 1 addition & 1 deletion .github/workflows/CI_docker_large_nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
- name: Build docker
run: docker build --platform=${{ matrix.arch }} -t pysr --build-arg JLVERSION=${{ matrix.julia-version }} .
- name: Test docker
run: docker run --platform=${{ matrix.arch }} --rm pysr /bin/bash -c 'python3 -m pysr.test main && python3 -m pysr.test env'
run: docker run --platform=${{ matrix.arch }} --rm pysr /bin/bash -c 'python3 -m pysr.test main && python3 -m pysr.test cli && python3 -m pysr.test env'
6 changes: 4 additions & 2 deletions .github/workflows/CI_large_nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
python setup.py install
python -c 'import pysr; pysr.install()'
python -m pysr install
- name: "Run tests"
run: python -m pysr.test main
run: |
python -m pysr.test main
python -m pysr.test cli
- name: "Run new env test"
run: python -m pysr.test env
if: ${{ !(matrix.os == 'windows-latest' && matrix.python-version == '3.7') }}
6 changes: 4 additions & 2 deletions .github/workflows/CI_mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
python setup.py install
python -c 'import pysr; pysr.install()'
python -m pysr install
- name: "Run tests"
run: python -m pysr.test main
run: |
python -m pysr.test main
python -m pysr.test cli
- name: "Install JAX"
run: pip install jax jaxlib # (optional import)
- name: "Run JAX tests"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ADD ./pysr/ /pysr/pysr/
RUN pip3 install .

# Install Julia pre-requisites:
RUN python3 -c 'import pysr; pysr.install()'
RUN python3 -m pysr install

# metainformation
LABEL org.opencontainers.image.authors = "Miles Cranmer"
Expand Down
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dependencies:
- setuptools
- pyjulia
- openlibm
- openspecfun
- openspecfun
- click
4 changes: 4 additions & 0 deletions pysr/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pysr._cli.main import pysr as _cli

if __name__ == "__main__":
_cli(prog_name="pysr")
Empty file added pysr/_cli/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions pysr/_cli/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import click
from ..julia_helpers import install


@click.group("pysr")
@click.pass_context
def pysr(context):
ctx = context


@pysr.command("install", help="Install Julia dependencies for PySR.")
@click.option(
"-p",
"julia_project",
"--project",
default=None,
type=str,
help="Install in a specific Julia project (e.g., a local copy of SymbolicRegression.jl).",
metavar="PROJECT_DIRECTORY",
)
@click.option("-q", "--quiet", is_flag=True, default=False, help="Disable logging.")
@click.option(
"--precompile",
"precompile",
flag_value=True,
default=None,
help="Force precompilation of Julia libraries.",
)
@click.option(
"--no-precompile",
"precompile",
flag_value=False,
default=None,
help="Disable precompilation.",
)
def _install(julia_project, quiet, precompile):
install(julia_project, quiet, precompile)
1 change: 0 additions & 1 deletion pysr/export_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def generate_single_table(
"$" + output_variable_name + " = " + latex_equation + "$"
)
else:

broken_latex_equation = " ".join(
[
r"\begin{minipage}{0.8\linewidth}",
Expand Down
10 changes: 4 additions & 6 deletions pysr/julia_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ def install(julia_project=None, quiet=False, precompile=None): # pragma: no cov

def _import_error():
return """
Required dependencies are not installed or built. Run the following code in the Python REPL:
>>> import pysr
>>> pysr.install()
Required dependencies are not installed or built. Run the following command in your terminal:
python3 -m pysr install
"""


Expand Down Expand Up @@ -286,14 +284,14 @@ def _backend_version_assertion(Main):
f"does not match expected version {expected_backend_version}. "
"Things may break. "
"Please update your PySR installation with "
"`python -c 'import pysr; pysr.install()'`."
"`python3 -m pysr install`."
)
except JuliaError: # pragma: no cover
warnings.warn(
"You seem to have an outdated version of SymbolicRegression.jl. "
"Things may break. "
"Please update your PySR installation with "
"`python -c 'import pysr; pysr.install()'`."
"`python3 -m pysr install`."
)


Expand Down
1 change: 1 addition & 0 deletions pysr/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .test_env import runtests as runtests_env
from .test_jax import runtests as runtests_jax
from .test_torch import runtests as runtests_torch
from .test_cli import runtests as runtests_cli
6 changes: 4 additions & 2 deletions pysr/test/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
parser.add_argument(
"test",
nargs="*",
help="Test to run. One or more of 'main', 'env', 'jax', 'torch'.",
help="Test to run. One or more of 'main', 'env', 'jax', 'torch', 'cli'.",
)

# Parse args:
Expand All @@ -25,7 +25,7 @@

# Run tests:
for test in tests:
if test in {"main", "env", "jax", "torch"}:
if test in {"main", "env", "jax", "torch", "cli"}:
cur_dir = os.path.dirname(os.path.abspath(__file__))
print(f"Running test from {cur_dir}")
if test == "main":
Expand All @@ -36,6 +36,8 @@
runtests_jax()
elif test == "torch":
runtests_torch()
elif test == "cli":
runtests_cli()
else:
parser.print_help()
raise SystemExit(1)
57 changes: 57 additions & 0 deletions pysr/test/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import unittest
from click import testing as click_testing
from .._cli.main import pysr


class TestCli(unittest.TestCase):
# TODO: Include test for custom project here.
def setUp(self):
self.cli_runner = click_testing.CliRunner()

def test_help_on_all_commands(self):
expected = "\n".join(
[
"Usage: pysr [OPTIONS] COMMAND [ARGS]...",
"",
"Options:",
" --help Show this message and exit.",
"",
"Commands:",
" install Install Julia dependencies for PySR.",
"",
]
)
result = self.cli_runner.invoke(pysr, ["--help"])
self.assertEqual(expected, result.output)
self.assertEqual(0, result.exit_code)

def test_help_on_install(self):
expected = "\n".join(
[
"Usage: pysr install [OPTIONS]",
"",
" Install Julia dependencies for PySR.",
"",
"Options:",
" -p, --project PROJECT_DIRECTORY",
" Install in a specific Julia project (e.g., a",
" local copy of SymbolicRegression.jl).",
" -q, --quiet Disable logging.",
" --precompile Force precompilation of Julia libraries.",
" --no-precompile Disable precompilation.",
" --help Show this message and exit.",
"",
]
)
result = self.cli_runner.invoke(pysr, ["install", "--help"])
self.assertEqual(expected, result.output)
self.assertEqual(0, result.exit_code)


def runtests():
"""Run all tests in cliTest.py."""
loader = unittest.TestLoader()
suite = unittest.TestSuite()
suite.addTests(loader.loadTestsFromTestCase(TestCli))
runner = unittest.TextTestRunner()
return runner.run(suite)
2 changes: 1 addition & 1 deletion pysr/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.13.0"
__version__ = "0.14.0"
__symbolic_regression_jl_version__ = "0.18.0"
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
sympy
pandas
numpy
scikit_learn >= 1.0.0
julia >= 0.6.0
scikit_learn>=1.0.0
julia>=0.6.0
click>=7.0.0
setuptools>=50.0.0

0 comments on commit d6f734d

Please sign in to comment.