Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request rmcantin#11 from ericfrederich/python3
Browse files Browse the repository at this point in the history
work with Python 3
  • Loading branch information
rmcantin authored Mar 29, 2017
2 parents 2dadf53 + e82aa17 commit e4c416e
Show file tree
Hide file tree
Showing 9 changed files with 3,555 additions and 2,604 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ IF(BAYESOPT_PYTHON_INTERFACE)

#Find where to install Python libs
execute_process ( COMMAND
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)

INSTALL(
Expand Down
6 changes: 3 additions & 3 deletions cmake/PythonMagic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ endif()
if( PYTHON_EXECUTABLE )
# architecture independent
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(0)"
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(0))"
OUTPUT_VARIABLE _python_sitepackage OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE _python_failed0)
# architexture dependent
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)"
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))"
OUTPUT_VARIABLE _python_distpackage OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE _python_failed1)
# execute_process(
# COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; from os.path import relpath; print relpath(get_python_lib(1,prefix='${CMAKE_INSTALL_PREFIX}'),'${CMAKE_INSTALL_PREFIX}')"
# COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; from os.path import relpath; print(relpath(get_python_lib(1,prefix='${CMAKE_INSTALL_PREFIX}'),'${CMAKE_INSTALL_PREFIX}'))"
# OUTPUT_VARIABLE OPENRAVE_PYTHON_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE
# RESULT_VARIABLE _python_failed2)

Expand Down
6,087 changes: 3,513 additions & 2,574 deletions python/bayesopt.cpp

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions python/bayesopt.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ cdef extern from "bayesopt/bayesopt.h":
###########################################################################
cdef bopt_params dict2structparams(dict dparams):

# be nice and allow Python 3 code to pass strings or bytes
dparams = {k: v.encode('latin-1') if isinstance(v, str) else v for k, v in dparams.items()}

params = initialize_parameters_to_default()

params.n_iterations = dparams.get('n_iterations',params.n_iterations)
Expand Down
2 changes: 1 addition & 1 deletion python/bayesoptmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ def optimize(self):
if __name__ == "__main__":
BO = BayesOptContinuous()
__value__, __x__, __err__ = BO.optimize()
print "Result", __x__
print("Result", __x__)
7 changes: 6 additions & 1 deletion python/demo_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
# along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------

import sys
from SimpleCV import Camera
import numpy as np
import bayesopt
from time import sleep

# Python3 compat
if sys.version_info[0] == 3:
raw_input = input

# Initialize the camera
cam = Camera()
cost = np.zeros(256)
Expand All @@ -50,7 +55,7 @@ def costImage(i):
valid_values, params)

x_out = int(x_out)
print x_out
print(x_out)
img1 = img.binarize(x_out)

img1 = img.sideBySide(img1).sideBySide(img2)
Expand Down
8 changes: 4 additions & 4 deletions python/demo_dimscaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def func(x):

mvalue, x_out, error = bayesopt.optimize(func, dim, lb, ub, params)

print "Result", mvalue, x_out
print("Result", mvalue, x_out)

print "Global optimal", 0, np.arange(1,1+dim)
print("Global optimal", 0, np.arange(1,1+dim))

print "Y Gap", mvalue
print "X Gap", math.sqrt(mvalue*dim)
print("Y Gap", mvalue)
print("X Gap", math.sqrt(mvalue*dim))
20 changes: 10 additions & 10 deletions python/demo_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def evaluateSample(self,Xin):
params['crit_name'] = "cSum(cEI,cDistance)"
params['crit_params'] = [1, 0.5]
params['kernel_name'] = "kMaternISO3"
print "Callback implementation"
print("Callback implementation")

n = 2 # n dimensions
lb = np.zeros((n,))
Expand All @@ -59,11 +59,11 @@ def evaluateSample(self,Xin):

mvalue, x_out, error = bayesopt.optimize(testfunc, n, lb, ub, params)

print "Result", x_out
print "Seconds", clock() - start
print("Result", x_out)
print("Seconds", clock() - start)


print "OO implementation"
print("OO implementation")
bo_test = BayesOptTest(n)
bo_test.parameters = params
bo_test.lower_bound = lb
Expand All @@ -72,18 +72,18 @@ def evaluateSample(self,Xin):
start = clock()
mvalue, x_out, error = bo_test.optimize()

print "Result", x_out
print "Seconds", clock() - start
print("Result", x_out)
print("Seconds", clock() - start)


print "Callback discrete implementation"
print("Callback discrete implementation")
x_set = np.random.rand(100,n)
start = clock()

mvalue, x_out, error = bayesopt.optimize_discrete(testfunc, x_set, params)

print "Result", x_out
print "Seconds", clock() - start
print("Result", x_out)
print("Seconds", clock() - start)

value = np.array([testfunc(i) for i in x_set])
print "Optimun", x_set[value.argmin()]
print("Optimun", x_set[value.argmin()])
24 changes: 14 additions & 10 deletions python/demo_quad.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

from time import clock

# Python3 compat
if sys.version_info[0] == 3:
raw_input = input

# Function for testing.
def testfunc(Xin):
total = 5.0
Expand All @@ -48,7 +52,7 @@ def evaluateSample(self,Xin):
params['n_iter_relearn'] = 5
params['n_init_samples'] = 2

print "Callback implementation"
print("Callback implementation")

n = 5 # n dimensions
lb = np.zeros((n,))
Expand All @@ -57,11 +61,11 @@ def evaluateSample(self,Xin):
start = clock()
mvalue, x_out, error = bayesopt.optimize(testfunc, n, lb, ub, params)

print "Result", mvalue, "at", x_out
print "Running time:", clock() - start, "seconds"
print("Result", mvalue, "at", x_out)
print("Running time:", clock() - start, "seconds")
raw_input('Press INTRO to continue')

print "OO implementation"
print("OO implementation")
bo_test = BayesOptTest(n)
bo_test.parameters = params
bo_test.lower_bound = lb
Expand All @@ -70,18 +74,18 @@ def evaluateSample(self,Xin):
start = clock()
mvalue, x_out, error = bo_test.optimize()

print "Result", mvalue, "at", x_out
print "Running time:", clock() - start, "seconds"
print("Result", mvalue, "at", x_out)
print("Running time:", clock() - start, "seconds")
raw_input('Press INTRO to continue')

print "Callback discrete implementation"
print("Callback discrete implementation")
x_set = np.random.rand(100,n)
start = clock()

mvalue, x_out, error = bayesopt.optimize_discrete(testfunc, x_set, params)

print "Result", mvalue, "at", x_out
print "Running time:", clock() - start, "seconds"
print("Result", mvalue, "at", x_out)
print("Running time:", clock() - start, "seconds")

value = np.array([testfunc(i) for i in x_set])
print "Optimum", value.min(), "at", x_set[value.argmin()]
print("Optimum", value.min(), "at", x_set[value.argmin()])

0 comments on commit e4c416e

Please sign in to comment.