Skip to content

Commit

Permalink
Add pre-commit hook for isort
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Sep 16, 2023
1 parent b933dea commit 976f8d8
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 87 deletions.
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ repos:
rev: 0.6.1
hooks:
- id: nbstripout
# Unused imports:
# Unused imports
- repo: https://github.com/hadialqattan/pycln
rev: "v2.2.2"
hooks:
- id: pycln
# Sorted imports
- repo: https://github.com/PyCQA/isort
rev: "5.12.0"
hooks:
- id: isort
additional_dependencies: [toml]
10 changes: 6 additions & 4 deletions benchmarks/hyperparamopt.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""Start a hyperoptimization from a single node"""
import sys
import numpy as np
import pickle as pkl
from pysr import PySRRegressor
import sys

import hyperopt
from hyperopt import hp, fmin, tpe, Trials
import numpy as np
from hyperopt import Trials, fmin, hp, tpe
from hyperopt.fmin import generate_trials_to_calculate
from space import *

from pysr import PySRRegressor

# Change the following code to your file
################################################################################
TRIALS_FOLDER = "trials2"
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/print_best_model.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Print the best model parameters and loss"""
import numpy as np
import pickle as pkl
import hyperopt
from hyperopt import hp, fmin, tpe, Trials
from space import space
from pprint import PrettyPrinter

import hyperopt
import numpy as np
from hyperopt import Trials, fmin, hp, tpe
from space import space

# Change the following code to your file
################################################################################
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/space.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from hyperopt import hp, fmin, tpe, Trials
from hyperopt import Trials, fmin, hp, tpe

binary_operators = ["*", "/", "+", "-"]
unary_operators = ["sin", "cos", "exp", "log"]
Expand Down
10 changes: 6 additions & 4 deletions docs/gen_param_docs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Load YAML file param_groupings.yml:
from yaml import safe_load
import re
import sys

from docstring_parser import parse
from yaml import safe_load

sys.path.append("..")


from pysr import PySRRegressor
import pysr
import re
from docstring_parser import parse

found_params = []

Expand Down
3 changes: 2 additions & 1 deletion docs/generate_papers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""This script generates the papers.md file from the papers.yml file."""
import yaml
from pathlib import Path

import yaml

data_file = "papers.yml"
papers_header = Path("stylesheets") / "papers_header.txt"
output_file = "papers.md"
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.isort]
profile = "black"
31 changes: 12 additions & 19 deletions pysr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
from . import sklearn_monkeypatch
from .version import __version__
from .sr import (
pysr,
PySRRegressor,
best,
best_tex,
best_callable,
best_row,
)
from .julia_helpers import install
from .feynman_problems import Problem, FeynmanProblem
from .export_jax import sympy2jax
from .export_torch import sympy2torch
from .feynman_problems import FeynmanProblem, Problem
from .julia_helpers import install
from .sr import PySRRegressor, best, best_callable, best_row, best_tex, pysr
from .version import __version__

__all__ = [
"sklearn_monkeypatch",
"__version__",
"pysr",
"sympy2jax",
"sympy2torch",
"FeynmanProblem",
"Problem",
"install",
"PySRRegressor",
"best",
"best_tex",
"best_callable",
"best_row",
"install",
"Problem",
"FeynmanProblem",
"sympy2jax",
"sympy2torch",
"best_tex",
"pysr",
"__version__",
]
1 change: 1 addition & 0 deletions pysr/_cli/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import click

from ..julia_helpers import install


Expand Down
5 changes: 3 additions & 2 deletions pysr/export_latex.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Functions to help export PySR equations to LaTeX."""
from typing import List

import pandas as pd
import sympy
from sympy.printing.latex import LatexPrinter
import pandas as pd
from typing import List


class PreciseLatexPrinter(LatexPrinter):
Expand Down
3 changes: 2 additions & 1 deletion pysr/export_numpy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Code for exporting discovered expressions to numpy"""
import warnings

import numpy as np
import pandas as pd
from sympy import lambdify
import warnings


class CallableEquation:
Expand Down
1 change: 1 addition & 0 deletions pysr/export_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import collections as co
import functools as ft

import sympy


Expand Down
9 changes: 6 additions & 3 deletions pysr/feynman_problems.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import numpy as np
import csv
from .sr import pysr, best
from pathlib import Path
from functools import partial
from pathlib import Path

import numpy as np

from .sr import best, pysr

PKG_DIR = Path(__file__).parents[1]
FEYNMAN_DATASET = PKG_DIR / "datasets" / "FeynmanEquations.csv"
Expand Down Expand Up @@ -118,6 +120,7 @@ def do_feynman_experiments_parallel(
data_dir=FEYNMAN_DATASET,
):
import multiprocessing as mp

from tqdm import tqdm

problems = mk_problems(first=first, gen=True, dp=dp, data_dir=data_dir)
Expand Down
7 changes: 4 additions & 3 deletions pysr/julia_helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Functions for initializing the Julia environment and installing deps."""
import sys
import os
import subprocess
import sys
import warnings
from pathlib import Path
import os

from julia.api import JuliaError

from .version import __version__, __symbolic_regression_jl_version__
from .version import __symbolic_regression_jl_version__, __version__

juliainfo = None
julia_initialized = False
Expand Down
45 changes: 21 additions & 24 deletions pysr/sr.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
"""Define the PySRRegressor scikit-learn interface."""
import copy
from io import StringIO
import os
import sys
import numpy as np
import pandas as pd
import sympy
from sympy import sympify
import pickle as pkl
import re
import tempfile
import shutil
from pathlib import Path
import pickle as pkl
from datetime import datetime
import sys
import tempfile
import warnings
from datetime import datetime
from io import StringIO
from multiprocessing import cpu_count
from sklearn.base import BaseEstimator, RegressorMixin, MultiOutputMixin
from pathlib import Path

import numpy as np
import pandas as pd
import sympy
from sklearn.base import BaseEstimator, MultiOutputMixin, RegressorMixin
from sklearn.utils import check_array, check_consistent_length, check_random_state
from sklearn.utils.validation import (
_check_feature_names_in,
check_is_fitted,
)
from sklearn.utils.validation import _check_feature_names_in, check_is_fitted
from sympy import sympify

from .deprecated import make_deprecated_kwargs_for_pysr_regressor
from .export_latex import generate_multiple_tables, generate_single_table, to_latex
from .export_numpy import CallableEquation
from .julia_helpers import (
init_julia,
_process_julia_project,
is_julia_version_greater_eq,
_escape_filename,
_load_backend,
_load_cluster_manager,
_process_julia_project,
_update_julia_project,
_load_backend,
init_julia,
is_julia_version_greater_eq,
)
from .export_numpy import CallableEquation
from .export_latex import generate_single_table, generate_multiple_tables, to_latex
from .deprecated import make_deprecated_kwargs_for_pysr_regressor


Main = None # TODO: Rename to more descriptive name like "julia_runtime"

Expand Down Expand Up @@ -2454,7 +2451,7 @@ def idx_model_selection(equations: pd.DataFrame, model_selection: str) -> int:
def _denoise(X, y, Xresampled=None, random_state=None):
"""Denoise the dataset using a Gaussian process."""
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, WhiteKernel, ConstantKernel
from sklearn.gaussian_process.kernels import RBF, ConstantKernel, WhiteKernel

gp_kernel = RBF(np.ones(X.shape[1])) + WhiteKernel(1e-1) + ConstantKernel()
gpr = GaussianProcessRegressor(
Expand Down
2 changes: 1 addition & 1 deletion pysr/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .test import runtests
from .test_cli import runtests as runtests_cli
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

__all__ = ["runtests", "runtests_env", "runtests_jax", "runtests_torch", "runtests_cli"]
28 changes: 14 additions & 14 deletions pysr/test/test.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import inspect
import os
import pickle as pkl
import tempfile
import traceback
import inspect
import unittest
import warnings
from pathlib import Path

import numpy as np
import pandas as pd
import sympy
from sklearn import model_selection
from sklearn.utils.estimator_checks import check_estimator
import sympy
import pandas as pd
import warnings
import pickle as pkl
import tempfile
from pathlib import Path

from .. import julia_helpers
from .. import PySRRegressor
from .. import PySRRegressor, julia_helpers
from ..export_latex import to_latex
from ..sr import (
run_feature_selection,
_handle_feature_selection,
_csv_filename_to_pkl_filename,
idx_model_selection,
_check_assertions,
_csv_filename_to_pkl_filename,
_handle_feature_selection,
_process_constraints,
idx_model_selection,
run_feature_selection,
)
from ..export_latex import to_latex

DEFAULT_PARAMS = inspect.signature(PySRRegressor.__init__).parameters
DEFAULT_NITERATIONS = DEFAULT_PARAMS["niterations"].default
Expand Down
2 changes: 2 additions & 0 deletions pysr/test/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest

from click import testing as click_testing

from .._cli.main import pysr


Expand Down
2 changes: 1 addition & 1 deletion pysr/test/test_env.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Contains tests for creating and initializing custom Julia projects."""

import unittest
import os
import unittest
from tempfile import TemporaryDirectory

from .. import julia_helpers
Expand Down
5 changes: 3 additions & 2 deletions pysr/test/test_jax.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import unittest
from functools import partial

import numpy as np
import pandas as pd
import sympy
from functools import partial

from .. import sympy2jax, PySRRegressor
from .. import PySRRegressor, sympy2jax


class TestJAX(unittest.TestCase):
Expand Down
6 changes: 4 additions & 2 deletions pysr/test/test_torch.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import platform
import unittest

import numpy as np
import pandas as pd
import platform
import sympy
from .. import sympy2torch, PySRRegressor

from .. import PySRRegressor, sympy2torch

# Need to initialize Julia before importing torch...

Expand Down

0 comments on commit 976f8d8

Please sign in to comment.