Skip to content

Commit

Permalink
Merge branch 'master' into python3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer authored Dec 30, 2023
2 parents 2186ad8 + d366b04 commit f0a34d7
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 130 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
- id: check-added-large-files
# General formatting
- repo: https://github.com/psf/black
rev: 23.9.1
rev: 23.12.1
hooks:
- id: black
- id: black-jupyter
Expand All @@ -20,12 +20,12 @@ repos:
- id: nbstripout
# Unused imports
- repo: https://github.com/hadialqattan/pycln
rev: "v2.3.0"
rev: "v2.4.0"
hooks:
- id: pycln
# Sorted imports
- repo: https://github.com/PyCQA/isort
rev: "5.12.0"
rev: "5.13.2"
hooks:
- id: isort
additional_dependencies: [toml]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ You may load the model from the `pkl` file with:
model = PySRRegressor.from_file("hall_of_fame.2022-08-10_100832.281.pkl")
```

There are several other useful features such as denoising (e.g., `denoising=True`),
There are several other useful features such as denoising (e.g., `denoise=True`),
feature selection (e.g., `select_k_features=3`).
For examples of these and other features, see the [examples page](https://astroautomata.com/PySR/examples).
For a detailed look at more options, see the [options page](https://astroautomata.com/PySR/options).
Expand Down
100 changes: 63 additions & 37 deletions docs/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,77 @@

## Pre-defined

All Base julia operators that take 1 or 2 scalars as input,
and output a scalar as output, are available. A selection
of these and other valid operators are stated below.
First, note that pretty much any valid Julia function which
takes one or two scalars as input, and returns on scalar as output,
is likely to be a valid operator[^1].
A selection of these and other valid operators are stated below.

**Binary**

`+`, `-`, `*`, `/`, `^`, `greater`, `mod`, `logical_or`,
`logical_and`
- `+`
- `-`
- `*`
- `/`
- `^`
- `max`
- `min`
- `mod`
- `cond`
- Equal to `(x, y) -> x > 0 ? y : 0`
- `greater`
- Equal to `(x, y) -> x > y ? 1 : 0`
- `logical_or`
- Equal to `(x, y) -> (x > 0 || y > 0) ? 1 : 0`
- `logical_and`
- Equal to `(x, y) -> (x > 0 && y > 0) ? 1 : 0`

**Unary**

`neg`,
`square`,
`cube`,
`exp`,
`abs`,
`log`,
`log10`,
`log2`,
`log1p`,
`sqrt`,
`sin`,
`cos`,
`tan`,
`sinh`,
`cosh`,
`tanh`,
`atan`,
`asinh`,
`acosh`,
`atanh_clip` (=atanh((x+1)%2 - 1)),
`erf`,
`erfc`,
`gamma`,
`relu`,
`round`,
`floor`,
`ceil`,
`round`,
`sign`.
- `neg`
- `square`
- `cube`
- `exp`
- `abs`
- `log`
- `log10`
- `log2`
- `log1p`
- `sqrt`
- `sin`
- `cos`
- `tan`
- `sinh`
- `cosh`
- `tanh`
- `atan`
- `asinh`
- `acosh`
- `atanh_clip`
- Equal to `atanh(mod(x + 1, 2) - 1)`
- `erf`
- `erfc`
- `gamma`
- `relu`
- `round`
- `floor`
- `ceil`
- `round`
- `sign`

## Custom

Instead of passing a predefined operator as a string,
you can define with by passing it to the `pysr` function, with, e.g.,
you can just define a custom function as Julia code. For example:

```python
PySRRegressor(
...,
unary_operators=["myfunction(x) = x^2"],
binary_operators=["myotherfunction(x, y) = x^2*y"]
binary_operators=["myotherfunction(x, y) = x^2*y"],
extra_sympy_mappings={
"myfunction": lambda x: x**2,
"myotherfunction": lambda x, y: x**2 * y,
},
)
```

Expand All @@ -62,7 +82,7 @@ Make sure that it works with
instead of `1.5e3`, if you write any constant numbers, or simply convert a result to `Float64(...)`.

PySR expects that operators not throw an error for any input value over the entire real line from `-3.4e38` to `+3.4e38`.
Thus, for "invalid" inputs, such as negative numbers to a `sqrt` function, you may simply return a `NaN` of the same type as the input. For example,
Thus, for invalid inputs, such as negative numbers to a `sqrt` function, you may simply return a `NaN` of the same type as the input. For example,

```julia
my_sqrt(x) = x >= 0 ? sqrt(x) : convert(typeof(x), NaN)
Expand All @@ -71,3 +91,9 @@ my_sqrt(x) = x >= 0 ? sqrt(x) : convert(typeof(x), NaN)
would be a valid operator. The genetic algorithm
will preferentially selection expressions which avoid
any invalid values over the training dataset.


<!-- Footnote for 1: -->
<!-- (Will say "However, you may need to define a `extra_sympy_mapping`":) -->

[^1]: However, you will need to define a sympy equivalent in `extra_sympy_mapping` if you want to use a function not in the above list.
7 changes: 7 additions & 0 deletions pysr/export_sympy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
"ceil": sympy.ceiling,
"sign": sympy.sign,
"gamma": sympy.gamma,
"round": lambda x: sympy.ceiling(x - 0.5),
"max": lambda x, y: sympy.Piecewise((y, x < y), (x, True)),
"min": lambda x, y: sympy.Piecewise((x, x < y), (y, True)),
"cond": lambda x, y: sympy.Piecewise((y, x > 0), (0.0, True)),
"logical_or": lambda x, y: sympy.Piecewise((1.0, (x > 0) | (y > 0)), (0.0, True)),
"logical_and": lambda x, y: sympy.Piecewise((1.0, (x > 0) & (y > 0)), (0.0, True)),
"relu": lambda x: sympy.Piecewise((0.0, x < 0), (x, True)),
}


Expand Down
10 changes: 9 additions & 1 deletion pysr/julia_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ def install(julia_project=None, quiet=False, precompile=None): # pragma: no cov
],
)
# Try installing again:
julia.install(quiet=quiet)
try:
julia.install(quiet=quiet)
except julia.tools.PyCallInstallError:
warnings.warn(
"PyCall.jl failed to install on second attempt. "
+ "Please consult the GitHub issue "
+ "https://github.com/MilesCranmer/PySR/issues/257 "
+ "for advice on fixing this."
)

Main, init_log = init_julia(julia_project, quiet=quiet, return_aux=True)
io_arg = _get_io_arg(quiet)
Expand Down
1 change: 1 addition & 0 deletions pysr/param_groupings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
- procs
- multithreading
- cluster_manager
- heap_size_hint_in_bytes
- batching
- batch_size
- precision
Expand Down
Loading

0 comments on commit f0a34d7

Please sign in to comment.