Skip to content

Commit e4c416e

Browse files
authored
Merge pull request rmcantin#11 from ericfrederich/python3
work with Python 3
2 parents 2dadf53 + e82aa17 commit e4c416e

File tree

9 files changed

+3555
-2604
lines changed

9 files changed

+3555
-2604
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ IF(BAYESOPT_PYTHON_INTERFACE)
223223

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

229229
INSTALL(

cmake/PythonMagic.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ endif()
1313
if( PYTHON_EXECUTABLE )
1414
# architecture independent
1515
execute_process(
16-
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(0)"
16+
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(0))"
1717
OUTPUT_VARIABLE _python_sitepackage OUTPUT_STRIP_TRAILING_WHITESPACE
1818
RESULT_VARIABLE _python_failed0)
1919
# architexture dependent
2020
execute_process(
21-
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)"
21+
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))"
2222
OUTPUT_VARIABLE _python_distpackage OUTPUT_STRIP_TRAILING_WHITESPACE
2323
RESULT_VARIABLE _python_failed1)
2424
# execute_process(
25-
# 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}')"
25+
# 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}'))"
2626
# OUTPUT_VARIABLE OPENRAVE_PYTHON_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE
2727
# RESULT_VARIABLE _python_failed2)
2828

python/bayesopt.cpp

Lines changed: 3513 additions & 2574 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/bayesopt.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ cdef extern from "bayesopt/bayesopt.h":
121121
###########################################################################
122122
cdef bopt_params dict2structparams(dict dparams):
123123

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

126129
params.n_iterations = dparams.get('n_iterations',params.n_iterations)

python/bayesoptmodule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@ def optimize(self):
177177
if __name__ == "__main__":
178178
BO = BayesOptContinuous()
179179
__value__, __x__, __err__ = BO.optimize()
180-
print "Result", __x__
180+
print("Result", __x__)

python/demo_cam.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@
1919
# along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
2020
# ------------------------------------------------------------------------
2121

22+
import sys
2223
from SimpleCV import Camera
2324
import numpy as np
2425
import bayesopt
2526
from time import sleep
2627

28+
# Python3 compat
29+
if sys.version_info[0] == 3:
30+
raw_input = input
31+
2732
# Initialize the camera
2833
cam = Camera()
2934
cost = np.zeros(256)
@@ -50,7 +55,7 @@ def costImage(i):
5055
valid_values, params)
5156

5257
x_out = int(x_out)
53-
print x_out
58+
print(x_out)
5459
img1 = img.binarize(x_out)
5560

5661
img1 = img.sideBySide(img1).sideBySide(img2)

python/demo_dimscaling.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def func(x):
5757

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

60-
print "Result", mvalue, x_out
60+
print("Result", mvalue, x_out)
6161

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

64-
print "Y Gap", mvalue
65-
print "X Gap", math.sqrt(mvalue*dim)
64+
print("Y Gap", mvalue)
65+
print("X Gap", math.sqrt(mvalue*dim))

python/demo_distance.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def evaluateSample(self,Xin):
4949
params['crit_name'] = "cSum(cEI,cDistance)"
5050
params['crit_params'] = [1, 0.5]
5151
params['kernel_name'] = "kMaternISO3"
52-
print "Callback implementation"
52+
print("Callback implementation")
5353

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

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

62-
print "Result", x_out
63-
print "Seconds", clock() - start
62+
print("Result", x_out)
63+
print("Seconds", clock() - start)
6464

6565

66-
print "OO implementation"
66+
print("OO implementation")
6767
bo_test = BayesOptTest(n)
6868
bo_test.parameters = params
6969
bo_test.lower_bound = lb
@@ -72,18 +72,18 @@ def evaluateSample(self,Xin):
7272
start = clock()
7373
mvalue, x_out, error = bo_test.optimize()
7474

75-
print "Result", x_out
76-
print "Seconds", clock() - start
75+
print("Result", x_out)
76+
print("Seconds", clock() - start)
7777

7878

79-
print "Callback discrete implementation"
79+
print("Callback discrete implementation")
8080
x_set = np.random.rand(100,n)
8181
start = clock()
8282

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

85-
print "Result", x_out
86-
print "Seconds", clock() - start
85+
print("Result", x_out)
86+
print("Seconds", clock() - start)
8787

8888
value = np.array([testfunc(i) for i in x_set])
89-
print "Optimun", x_set[value.argmin()]
89+
print("Optimun", x_set[value.argmin()])

python/demo_quad.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
from time import clock
2727

28+
# Python3 compat
29+
if sys.version_info[0] == 3:
30+
raw_input = input
31+
2832
# Function for testing.
2933
def testfunc(Xin):
3034
total = 5.0
@@ -48,7 +52,7 @@ def evaluateSample(self,Xin):
4852
params['n_iter_relearn'] = 5
4953
params['n_init_samples'] = 2
5054

51-
print "Callback implementation"
55+
print("Callback implementation")
5256

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

60-
print "Result", mvalue, "at", x_out
61-
print "Running time:", clock() - start, "seconds"
64+
print("Result", mvalue, "at", x_out)
65+
print("Running time:", clock() - start, "seconds")
6266
raw_input('Press INTRO to continue')
6367

64-
print "OO implementation"
68+
print("OO implementation")
6569
bo_test = BayesOptTest(n)
6670
bo_test.parameters = params
6771
bo_test.lower_bound = lb
@@ -70,18 +74,18 @@ def evaluateSample(self,Xin):
7074
start = clock()
7175
mvalue, x_out, error = bo_test.optimize()
7276

73-
print "Result", mvalue, "at", x_out
74-
print "Running time:", clock() - start, "seconds"
77+
print("Result", mvalue, "at", x_out)
78+
print("Running time:", clock() - start, "seconds")
7579
raw_input('Press INTRO to continue')
7680

77-
print "Callback discrete implementation"
81+
print("Callback discrete implementation")
7882
x_set = np.random.rand(100,n)
7983
start = clock()
8084

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

83-
print "Result", mvalue, "at", x_out
84-
print "Running time:", clock() - start, "seconds"
87+
print("Result", mvalue, "at", x_out)
88+
print("Running time:", clock() - start, "seconds")
8589

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

0 commit comments

Comments
 (0)