File size: 1,891 Bytes
ad955c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1300ad
 
 
 
 
ad955c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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("--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

    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,
            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)
        df.to_csv("pysr_output.csv", index=False)
    except Exception as e:
        error_message = tb.format_exc()
        # Dump to file:
        empty_df.to_csv("pysr_output.csv", index=False)
        with open("error.log", "w") as f:
            f.write(error_message)