Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul type hints with beartype and PEP 585 compliance #559

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
54350c0
Update to PEP 585
MilesCranmer Mar 3, 2024
3c02fd8
Switch from `Optional` to `|None`
MilesCranmer Mar 3, 2024
bfd39ac
Fix incorrect type hint
MilesCranmer Mar 3, 2024
03759a8
Fix incorrect type hint in pkl filename
MilesCranmer Mar 3, 2024
cac9333
Deprecate operators-as-string
MilesCranmer Mar 3, 2024
fa3d71c
Clean up data manipulations with dataclasses
MilesCranmer Mar 3, 2024
f35026c
Rearrange documentation and fix tests
MilesCranmer Mar 3, 2024
50d2b18
Move warning to validation function
MilesCranmer Mar 3, 2024
367e3bd
Remove large number of features warning
MilesCranmer Mar 3, 2024
f6b36d4
Remove mistaken `self.feature_names_in_` call
MilesCranmer Mar 3, 2024
420edb0
Remove test for large feature warning
MilesCranmer Mar 3, 2024
e22b11c
Remove beartype from requirements
MilesCranmer Mar 3, 2024
fe4abd5
Always turn on beartype runtime type checking, but with warnings
MilesCranmer Mar 3, 2024
7a596b8
Add missing `__future__` import
MilesCranmer Mar 3, 2024
df95867
Fix import order of `__future__`
MilesCranmer Mar 12, 2024
59d9b66
Clean up various typing issues
MilesCranmer Mar 12, 2024
bc265b2
Reduce more typing errors
MilesCranmer Mar 12, 2024
f069841
Reduce more typing errors
MilesCranmer Mar 12, 2024
b9663cb
Reduce more typing errors
MilesCranmer Mar 12, 2024
6336474
Reduce more typing errors
MilesCranmer Mar 12, 2024
c7d9551
Introduce more type hints
MilesCranmer Mar 12, 2024
1c820d3
Introduce more type hints
MilesCranmer Mar 12, 2024
aed1419
Introduce more type hints
MilesCranmer Mar 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pysr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
try:
from beartype import BeartypeConf
from beartype.claw import beartype_this_package

beartype_this_package(conf=BeartypeConf(violation_type=UserWarning))
except ImportError:
import warnings

warnings.warn("beartype not installed, skipping runtime type checks.")

# This must be imported as early as possible to prevent
# library linking issues caused by numpy/pytorch/etc. importing
# old libraries:
Expand Down
18 changes: 9 additions & 9 deletions pysr/export_latex.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Functions to help export PySR equations to LaTeX."""
from typing import List, Optional, Tuple
from __future__ import annotations

import pandas as pd
import sympy
Expand Down Expand Up @@ -27,8 +27,8 @@ def sympy2latex(expr, prec=3, full_prec=True, **settings) -> str:


def generate_table_environment(
columns: List[str] = ["equation", "complexity", "loss"]
) -> Tuple[str, str]:
columns: list[str] = ["equation", "complexity", "loss"]
) -> tuple[str, str]:
margins = "c" * len(columns)
column_map = {
"complexity": "Complexity",
Expand Down Expand Up @@ -60,9 +60,9 @@ def generate_table_environment(

def sympy2latextable(
equations: pd.DataFrame,
indices: Optional[List[int]] = None,
indices: list[int] | None = None,
precision: int = 3,
columns: List[str] = ["equation", "complexity", "loss", "score"],
columns: list[str] = ["equation", "complexity", "loss", "score"],
max_equation_length: int = 50,
output_variable_name: str = "y",
) -> str:
Expand Down Expand Up @@ -127,11 +127,11 @@ def sympy2latextable(


def sympy2multilatextable(
equations: List[pd.DataFrame],
indices: Optional[List[List[int]]] = None,
equations: list[pd.DataFrame],
indices: list[list[int]] | None = None,
precision: int = 3,
columns: List[str] = ["equation", "complexity", "loss", "score"],
output_variable_names: Optional[List[str]] = None,
columns: list[str] = ["equation", "complexity", "loss", "score"],
output_variable_names: list[str] | None = None,
) -> str:
"""Generate multiple latex tables for a list of equation sets."""
# TODO: Let user specify custom output variable
Expand Down
10 changes: 6 additions & 4 deletions pysr/export_sympy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Define utilities to export to sympy"""
from typing import Callable, Dict, List, Optional
from __future__ import annotations

from collections.abc import Callable

import sympy
from sympy import sympify
Expand Down Expand Up @@ -58,13 +60,13 @@


def create_sympy_symbols(
feature_names_in: List[str],
) -> List[sympy.Symbol]:
feature_names_in: list[str],
) -> list[sympy.Symbol]:
return [sympy.Symbol(variable) for variable in feature_names_in]


def pysr2sympy(
equation: str, *, extra_sympy_mappings: Optional[Dict[str, Callable]] = None
equation: str, *, extra_sympy_mappings: dict[str, Callable] | None = None
):
local_sympy_mappings = {
**(extra_sympy_mappings if extra_sympy_mappings else {}),
Expand Down
6 changes: 2 additions & 4 deletions pysr/julia_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
jl.seval("using SymbolicRegression: plus, sub, mult, div, pow")


def _escape_filename(filename):
def _escape_filename(filename: str) -> str:
"""Turn a path into a string with correctly escaped backslashes."""
str_repr = str(filename)
str_repr = str_repr.replace("\\", "\\\\")
return str_repr
return filename.replace("\\", "\\\\")


def _load_cluster_manager(cluster_manager):
Expand Down
Loading
Loading