MilesCranmer commited on
Commit
0c0aff7
β€’
1 Parent(s): 009c407

Create PyPI package under pysr

Browse files
operators.jl β†’ julia/operators.jl RENAMED
File without changes
{src β†’ julia}/sr.jl RENAMED
File without changes
pysr/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .sr import pysr
{src β†’ pysr}/sr.py RENAMED
@@ -126,7 +126,9 @@ def pysr(X=None, y=None, threads=4,
126
  y = eval(eval_str)
127
  print("Running on", eval_str)
128
 
129
- def_hyperparams = f"""include("operators.jl")
 
 
130
  const binops = {'[' + ', '.join(binary_operators) + ']'}
131
  const unaops = {'[' + ', '.join(unary_operators) + ']'}
132
  const ns=10;
@@ -163,22 +165,18 @@ const mutationWeights = [
163
  const y = convert(Array{Float32, 1}, """f"{y_str})""""
164
  """
165
 
166
- starting_path = f'cd {pathlib.Path().absolute()}'
167
- code_path = f'cd {pathlib.Path(__file__).parent.absolute()}' #Move to filepath of code
168
-
169
- os.system(code_path)
170
-
171
- with open(f'.hyperparams_{rand_string}.jl', 'w') as f:
172
  print(def_hyperparams, file=f)
173
 
174
- with open(f'.dataset_{rand_string}.jl', 'w') as f:
175
  print(def_datasets, file=f)
176
 
 
177
  command = [
178
  'julia -O3',
179
  '--threads auto',
180
  '-e',
181
- f'\'include(".hyperparams_{rand_string}.jl"); include(".dataset_{rand_string}.jl"); include("sr.jl"); fullRun({niterations:d}, npop={npop:d}, ncyclesperiteration={ncyclesperiteration:d}, fractionReplaced={fractionReplaced:f}f0, verbosity=round(Int32, {verbosity:f}), topn={topn:d})\'',
182
  ]
183
  if timeout is not None:
184
  command = [f'timeout {timeout}'] + command
@@ -190,46 +188,5 @@ const y = convert(Array{Float32, 1}, """f"{y_str})""""
190
  except FileNotFoundError:
191
  print("Couldn't find equation file!")
192
  output = pd.DataFrame()
193
- os.system(starting_path)
194
  return output
195
 
196
-
197
-
198
- if __name__ == "__main__":
199
- parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
200
-
201
- parser.add_argument("--threads", type=int, default=4, help="Number of threads")
202
- parser.add_argument("--parsimony", type=float, default=default_parsimony, help="How much to punish complexity")
203
- parser.add_argument("--alpha", type=float, default=default_alpha, help="Scaling of temperature")
204
- parser.add_argument("--maxsize", type=int, default=20, help="Max size of equation")
205
- parser.add_argument("--niterations", type=int, default=20, help="Number of total migration periods")
206
- parser.add_argument("--npop", type=int, default=int(default_npop), help="Number of members per population")
207
- parser.add_argument("--ncyclesperiteration", type=int, default=10000, help="Number of evolutionary cycles per migration")
208
- parser.add_argument("--topn", type=int, default=int(default_topn), help="How many best species to distribute from each population")
209
- parser.add_argument("--perturbationFactor", type=float, default=default_perturbationFactor)
210
- parser.add_argument("--fractionReplacedHof", type=float, default=default_fractionReplacedHof, help="Fraction of population to replace with hall of fame")
211
- parser.add_argument("--fractionReplaced", type=float, default=default_fractionReplaced, help="Fraction of population to replace with best from other populations")
212
- parser.add_argument("--weightAddNode", type=float, default=default_weightAddNode)
213
- parser.add_argument("--weightInsertNode", type=float, default=default_weightInsertNode)
214
- parser.add_argument("--weightDeleteNode", type=float, default=default_weightDeleteNode)
215
- parser.add_argument("--weightMutateConstant", type=float, default=default_weightMutateConstant)
216
- parser.add_argument("--weightMutateOperator", type=float, default=default_weightMutateOperator)
217
- parser.add_argument("--weightRandomize", type=float, default=default_weightRandomize)
218
- parser.add_argument("--weightSimplify", type=float, default=default_weightSimplify)
219
- parser.add_argument("--weightDoNothing", type=float, default=default_weightDoNothing)
220
- parser.add_argument("--migration", type=bool, default=True, help="Whether to migrate")
221
- parser.add_argument("--hofMigration", type=bool, default=True, help="Whether to have hall of fame migration")
222
- parser.add_argument("--shouldOptimizeConstants", type=bool, default=True, help="Whether to use classical optimization on constants before every migration (doesn't impact performance that much)")
223
- parser.add_argument("--annealing", type=bool, default=True, help="Whether to use simulated annealing")
224
- parser.add_argument("--equation_file", type=str, default='hall_of_fame.csv', help="File to dump best equations to")
225
- parser.add_argument("--test", type=str, default='simple1', help="Which test to run")
226
-
227
- parser.add_argument(
228
- "--binary-operators", type=str, nargs="+", default=["plus", "mult"],
229
- help="Binary operators. Make sure they are defined in operators.jl")
230
- parser.add_argument(
231
- "--unary-operators", type=str, nargs="+", default=["exp", "sin", "cos"],
232
- help="Unary operators. Make sure they are defined in operators.jl")
233
- args = vars(parser.parse_args()) #dict
234
-
235
- pysr(**args)
 
126
  y = eval(eval_str)
127
  print("Running on", eval_str)
128
 
129
+ pkg_directory = '/'.join(__file__.split('/')[:-2] + ['/julia'])
130
+
131
+ def_hyperparams = f"""include("{pkg_directory}/operators.jl")
132
  const binops = {'[' + ', '.join(binary_operators) + ']'}
133
  const unaops = {'[' + ', '.join(unary_operators) + ']'}
134
  const ns=10;
 
165
  const y = convert(Array{Float32, 1}, """f"{y_str})""""
166
  """
167
 
168
+ with open(f'/tmp/.hyperparams_{rand_string}.jl', 'w') as f:
 
 
 
 
 
169
  print(def_hyperparams, file=f)
170
 
171
+ with open(f'/tmp/.dataset_{rand_string}.jl', 'w') as f:
172
  print(def_datasets, file=f)
173
 
174
+
175
  command = [
176
  'julia -O3',
177
  '--threads auto',
178
  '-e',
179
+ f'\'include("/tmp/.hyperparams_{rand_string}.jl"); include("/tmp/.dataset_{rand_string}.jl"); include("{pkg_directory}/sr.jl"); fullRun({niterations:d}, npop={npop:d}, ncyclesperiteration={ncyclesperiteration:d}, fractionReplaced={fractionReplaced:f}f0, verbosity=round(Int32, {verbosity:f}), topn={topn:d})\'',
180
  ]
181
  if timeout is not None:
182
  command = [f'timeout {timeout}'] + command
 
188
  except FileNotFoundError:
189
  print("Couldn't find equation file!")
190
  output = pd.DataFrame()
 
191
  return output
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
setup.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import setuptools
2
+
3
+ with open("README.md", "r") as fh:
4
+ long_description = fh.read()
5
+
6
+ setuptools.setup(
7
+ name="pysr", # Replace with your own username
8
+ version="0.0.1",
9
+ author="Miles Cranmer",
10
+ author_email="[email protected]",
11
+ description="Simple and efficient symbolic regression",
12
+ long_description=long_description,
13
+ long_description_content_type="text/markdown",
14
+ url="https://github.com/MilesCranmer/pysr",
15
+ packages=setuptools.find_packages(),
16
+ package_data={
17
+ 'pysr': ['../julia/*.jl']
18
+ },
19
+ include_package_data=False,
20
+ classifiers=[
21
+ "Programming Language :: Python :: 3",
22
+ "Operating System :: OS Independent",
23
+ ],
24
+ python_requires='>=3.3',
25
+ )