Explicit 1D Benchmarks

This file demonstrates how to generate, plot, and output data for 1d benchmarks

Choose from:

  1. Koza_01
  2. Koza_02
  3. Koza_03
  4. Lipson_01
  5. Lipson_02
  6. Lipson_03
  7. Nguyen_01
  8. Nguyen_02
  9. Nguyen_03
  10. Nguyen_04
  11. Nguyen_05
  12. Nguyen_06
  13. Nguyen_07
  14. Nguyen_08

Imports


In [1]:
from pypge.benchmarks import explicit

import numpy as np

# visualization libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# plot the visuals in ipython
%matplotlib inline

Generate the data with noise


In [2]:
# Set your output directories
img_dir = "../img/benchmarks/explicit/"
data_dir = "../data/benchmarks/explicit/"

# choose your problem here
prob = explicit.Nguyen_04(noise=0.01,npts=1000)

# you can also specify the following params as keyword arguments
#
# params = {
#     'name': "Koza_01",
#     'xs_str': ["x"],
#     'eqn_str': "x**4 + x**3 + x**2 + x",
#     'xs_params': [ (-4.0,4.0) ],
#     'npts': 200,
#     'noise': 1.0
# }

# or make your own with the following
#
# explicit.Explicit_1D(params):


{   'eqn_str': 'x**6 + x**5 - x**4 + x**3 + x**2 + x',
    'name': 'Nguyen_04',
    'noise': 0.01,
    'npts': 1000,
    'xs': [x],
    'xs_params': [(-5.0, 5.0)],
    'xs_str': ['x']}

Plot inline and save image


In [3]:
print prob['name'], prob['eqn']
print prob['xpts'].shape

fig = plt.figure()
fig.set_size_inches(16, 12)

plt.plot(prob['xpts'][0], prob['ypure'], 'r.')
plt.legend(loc='center left', bbox_to_anchor=(0.67, 0.12))

plt.title(prob['name'] + " Clean", fontsize=36)
plt.savefig(img_dir + prob['name'].lower() + "_clean.png", dpi=200)
# plt.show()

### You can only do one of 'savefig()' or 'show()'

fig = plt.figure()
fig.set_size_inches(16, 12)

plt.plot(prob['xpts'][0], prob['ypts'], 'b.')
plt.legend(loc='center left', bbox_to_anchor=(0.67, 0.12))

plt.title(prob['name'] + " Noisy", fontsize=36)
plt.savefig(img_dir + prob['name'].lower() + "_noisy.png", dpi=200)
# plt.show()


Nguyen_04 x**6 + x**5 - x**4 + x**3 + x**2 + x
(1, 1000)
/Users/tony/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.py:475: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
  warnings.warn("No labelled objects found. "

Output json and csv data


In [4]:
data = np.array([prob['xpts'][0], prob['ypts']]).T
print data.shape

cols = [['x', 'out']]
out_data = cols + data.tolist()
import json
json_out = json.dumps( out_data, indent=4)
# print json_out

f_json = open(data_dir + prob['name'].lower() + ".json", 'w')
f_json.write(json_out)
f_json.close()

f_csv = open(data_dir + prob['name'].lower() + ".csv", 'w')
for row in out_data:
    line = ", ".join([str(col) for col in row]) + "\n"
    f_csv.write(line)
f_csv.close()


(1000, 2)

Output clean json and csv data


In [5]:
data = np.array([prob['xpts'][0], prob['ypure']]).T
print data.shape

cols = [['x', 'out']]
out_data = cols + data.tolist()
import json
json_out = json.dumps( out_data, indent=4)
# print json_out

f_json = open(data_dir + prob['name'].lower() + "_clean.json", 'w')
f_json.write(json_out)
f_json.close()

f_csv = open(data_dir + prob['name'].lower() + "_clean.csv", 'w')
for row in out_data:
    line = ", ".join([str(col) for col in row]) + "\n"
    f_csv.write(line)
f_csv.close()


(1000, 2)