File size: 2,165 Bytes
745ec89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b007552
2b58e9e
b007552
 
 
 
745ec89
 
 
 
 
 
2b58e9e
745ec89
 
 
 
 
 
6ab7b97
745ec89
 
6ab7b97
745ec89
 
 
 
 
 
 
 
 
 
cfec30d
2b58e9e
745ec89
 
 
 
 
 
 
 
 
6ab7b97
 
 
 
 
745ec89
 
 
19997c8
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import pandas as pd
import traceback as tb
import numpy as np
from argparse import ArgumentParser

# Args:
# niterations
# binary_operators
# unary_operators
# col_to_fit

empty_df = pd.DataFrame(
    {
        "equation": [],
        "loss": [],
        "complexity": [],
    }
)

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("--niterations", type=int)
    parser.add_argument("--maxsize", type=int)
    parser.add_argument("--binary_operators", type=str)
    parser.add_argument("--unary_operators", type=str)
    parser.add_argument("--col_to_fit", type=str)
    parser.add_argument("--filename", type=str)
    args = parser.parse_args()
    niterations = args.niterations
    binary_operators = eval(args.binary_operators)
    unary_operators = eval(args.unary_operators)
    col_to_fit = args.col_to_fit
    filename = args.filename
    maxsize = args.maxsize

    os.environ["PATH"] += ":/home/user/.local/bin/"

    try:
        import pysr
        from julia.api import JuliaInfo

        info = JuliaInfo.load(julia="/home/user/.local/bin/julia")
        from julia import Main as _Main

        pysr.sr.Main = _Main

        from pysr import PySRRegressor

        df = pd.read_csv(filename)
        y = np.array(df[col_to_fit])
        X = df.drop([col_to_fit], axis=1)

        model = PySRRegressor(
            update=False,
            progress=False,
            maxsize=maxsize,
            niterations=niterations,
            binary_operators=binary_operators,
            unary_operators=unary_operators,
        )
        model.fit(X, y)

        df = model.equations_[["equation", "loss", "complexity"]]
        # Convert all columns to string type:
        df = df.astype(str)
        error_message = (
            "Success!\n"
            f"You may run the model locally (faster) with "
            f"the following parameters:\n" + str(model.get_params())
        )
    except Exception as e:
        error_message = tb.format_exc()
        # Dump to file:
        df = empty_df

    df.to_csv("pysr_output.csv", index=False)
    with open("error.log", "w") as f:
        f.write(error_message)