Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 compatibility #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/simple/make_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@


from spearmint.utils.database.mongodb import MongoDB
from spearmint.utils.fixes import items

from spearmint.main import get_options, parse_resources_from_config, load_jobs, remove_broken_jobs, \
load_task_group, load_hypers

def print_dict(d, level=1):
if isinstance(d, dict):
if level > 1: print ""
for k, v in d.iteritems():
for k, v in items(d):
print " " * level, k,
print_dict(v, level=level+1)
else:
Expand Down
7 changes: 6 additions & 1 deletion spearmint/choosers/acquisition_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
# its Institution.


import sys
import os
import tempfile
import copy
Expand All @@ -191,10 +192,14 @@
import scipy.linalg as spla
import scipy.stats as sps
import scipy.optimize as spo
import cPickle
import multiprocessing
import ast

if sys.version < '3':
import cPickle
else:
import pickle as cPickle

def compute_ei(model, pred, ei_target=None, compute_grad=True):
# TODO: use ei_target
if pred.ndim == 1:
Expand Down
11 changes: 6 additions & 5 deletions spearmint/choosers/default_chooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@

from .acquisition_functions import compute_ei
from ..utils.grad_check import check_grad
from ..utils.fixes import items, xrange
from ..grids import sobol_grid
from ..models.abstract_model import function_over_hypers
from .. import models
Expand Down Expand Up @@ -260,7 +261,7 @@ def fit(self, task_group, hypers=None, options=None):
grid_seed=self.grid_seed)

# A useful hack: add previously visited points to the grid
for task_name, task in task_group.tasks.iteritems():
for task_name, task in items(task_group.tasks):
if task.has_valid_inputs():
self.grid = np.append(self.grid, task.valid_normalized_data_dict['inputs'], axis=0)
if task.has_pending():
Expand All @@ -274,7 +275,7 @@ def fit(self, task_group, hypers=None, options=None):

# print 'Fittings tasks: %s' % str(task_group.tasks.keys())

for task_name, task in task_group.tasks.iteritems():
for task_name, task in items(task_group.tasks):
if task.type.lower() == 'objective':
data_dict = self.objective # confusing: this is how self.objective gets populated
elif task.type.lower() == 'constraint':
Expand Down Expand Up @@ -351,7 +352,7 @@ def suggest(self):
best_grid_ei = grid_ei[best_grid_ind]

if VERBOSE:
print 'Best EI before optimization: %f' % best_grid_ei
print('Best EI before optimization: %f' % best_grid_ei)

if self.check_grad:
check_grad(lambda x: self.acq_optimize_wrapper(x, current_best, True),
Expand Down Expand Up @@ -387,8 +388,8 @@ def suggest(self):
# Optimization should always be better unless the optimization
# breaks in some way.
if VERBOSE:
print 'Best EI after optimization: %f' % best_opt_ei
print 'Suggested input %s' % cand[best_opt_ind]
print('Best EI after optimization: %f' % best_opt_ei)
print('Suggested input %s' % cand[best_opt_ind])

if best_opt_ei >= best_grid_ei:
suggestion = cand[best_opt_ind]
Expand Down
12 changes: 10 additions & 2 deletions spearmint/grids/sobol.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@
# to enter into this License and Terms of Use on behalf of itself and
# its Institution.

import sys
import numpy as np
import cPickle as pickle

from spearmint.utils.fixes import xrange

if sys.version < '3':
import cPickle as pickle
else:
import pickle

# Numba autojit might be nice. Currently asplodes.
def sobol(num_points, num_dims):
Expand Down Expand Up @@ -236,7 +243,8 @@ def sobol(num_points, num_dims):
return Z

def to_binary(X, bits):
return 1 & (X[:,np.newaxis]/2**np.arange(bits-1,-1,-1, dtype=np.uint32))
temp = X[:,np.newaxis]/2**np.arange(bits-1,-1,-1, dtype=np.uint32)
return np.ones_like(temp) & temp

# These are the parameters for the Sobol sequence.
# This is hilarious.
Expand Down
12 changes: 6 additions & 6 deletions spearmint/kernels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from matern import Matern52
from sum_kernel import SumKernel
from product_kernel import ProductKernel
from noise import Noise
from scale import Scale
from transform_kernel import TransformKernel
from spearmint.kernels.matern import Matern52
from spearmint.kernels.sum_kernel import SumKernel
from spearmint.kernels.product_kernel import ProductKernel
from spearmint.kernels.noise import Noise
from spearmint.kernels.scale import Scale
from spearmint.kernels.transform_kernel import TransformKernel

__all__ = ["Matern52", "SumKernel", "ProductKernel", "Noise", "Scale", "TransformKernel"]
8 changes: 7 additions & 1 deletion spearmint/kernels/kernel_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@


import numpy as np
import scipy.weave

try:
import scipy.weave
except ImportError:
pass

from spearmint.utils.fixes import xrange
from scipy.spatial.distance import cdist

def dist2(ls, x1, x2=None):
Expand Down
2 changes: 1 addition & 1 deletion spearmint/kernels/matern.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@


import numpy as np
import kernel_utils
from spearmint.kernels import kernel_utils

from .abstract_kernel import AbstractKernel
from ..utils import priors
Expand Down
2 changes: 2 additions & 0 deletions spearmint/kernels/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@
import logging
#warnings.filterwarnings("ignore", category=RuntimeWarning)

from spearmint.utils.fixes import xrange

class productCov:
def __init__(self, num_dimensions, **kwargs):
# The sub-covariances of which this is the elementwise product
Expand Down
7 changes: 4 additions & 3 deletions spearmint/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
import numpy as np

from spearmint.utils.database.mongodb import MongoDB
from spearmint.utils.fixes import items

def main():
parser = optparse.OptionParser(usage="usage: %prog [options]")
Expand Down Expand Up @@ -271,7 +272,7 @@ def launch(db_address, experiment_name, job_id):
import traceback
traceback.print_exc()
sys.stderr.write("Problem executing the function\n")
print sys.exc_info()
print(sys.exc_info())

end_time = time.time()

Expand Down Expand Up @@ -305,7 +306,7 @@ def python_launcher(job):

# Convert the JSON object into useful parameters.
params = {}
for name, param in job['params'].iteritems():
for name, param in items(job['params']):
vals = param['values']

if param['type'].lower() == 'float':
Expand Down Expand Up @@ -351,7 +352,7 @@ def matlab_launcher(job):
session.run("cd('%s')" % os.path.realpath(job['expt_dir']))

session.run('params = struct()')
for name, param in job['params'].iteritems():
for name, param in items(job['params']):
vals = param['values']

# sys.stderr.write('%s = %s\n' % (param['name'], str(vals)))
Expand Down
6 changes: 4 additions & 2 deletions spearmint/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@
from collections import OrderedDict

from spearmint.utils.database.mongodb import MongoDB
from spearmint.utils.fixes import items
from spearmint.tasks.task_group import TaskGroup

from spearmint.resources.resource import parse_resources_from_config
from spearmint.resources.resource import print_resources_status

from spearmint.utils.parsing import parse_db_address


def get_options():
parser = optparse.OptionParser(usage="usage: %prog [options] directory")

Expand Down Expand Up @@ -262,7 +264,7 @@ def main():

while True:

for resource_name, resource in resources.iteritems():
for resource_name, resource in items(resources):

jobs = load_jobs(db, experiment_name)
# resource.printStatus(jobs)
Expand Down Expand Up @@ -313,7 +315,7 @@ def tired(db, experiment_name, resources):
return True if no resources are accepting jobs
"""
jobs = load_jobs(db, experiment_name)
for resource_name, resource in resources.iteritems():
for resource_name, resource in items(resources):
if resource.acceptingJobs(jobs):
return False
return True
Expand Down
4 changes: 2 additions & 2 deletions spearmint/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gp import GP
from gp_classifier import GPClassifier
from spearmint.models.gp import GP
from spearmint.models.gp_classifier import GPClassifier

__all__ = ["GP", "GPClassifier"]
2 changes: 2 additions & 0 deletions spearmint/models/abstract_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@

from abc import ABCMeta, abstractmethod

from spearmint.utils.fixes import xrange

class AbstractModel(object):
__metaclass__ = ABCMeta

Expand Down
8 changes: 5 additions & 3 deletions spearmint/models/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,20 @@
import scipy.stats as sps

from .abstract_model import AbstractModel
from ..utils.fixes import items
from ..utils.param import Param as Hyperparameter
from ..kernels import Matern52, Noise, Scale, SumKernel, TransformKernel
from ..sampling.slice_sampler import SliceSampler
from ..utils import priors
from ..utils.fixes import items, xrange
from ..transformations import BetaWarp, Transformer

try:
module = sys.modules['__main__'].__file__
log = logging.getLogger(module)
except:
log = logging.getLogger()
print 'Not running from main.'
print('Not running from main.')

DEFAULT_MCMC_ITERS = 10
DEFAULT_BURNIN = 100
Expand Down Expand Up @@ -274,7 +276,7 @@ def _set_likelihood(self, options):
self.noiseless = False

def _set_params_from_dict(self, hypers_dict):
for name, hyper in self.params.iteritems():
for name, hyper in items(self.params):
self.params[name].value = hypers_dict[name]

def _reset_params(self):
Expand Down Expand Up @@ -463,7 +465,7 @@ def set_state(self, state):
def to_dict(self):
"""return a dictionary that saves the values of the hypers and the chain length"""
gp_dict = {'hypers' : {}}
for name, hyper in self.params.iteritems():
for name, hyper in items(self.params):
gp_dict['hypers'][name] = hyper.value

gp_dict['chain length'] = self.chain_length
Expand Down
6 changes: 3 additions & 3 deletions spearmint/models/gp_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@
import scipy.optimize as spo
import scipy.io as sio
import scipy.stats as sps
import scipy.weave


from .gp import GP
from ..utils.param import Param as Hyperparameter
from ..utils.fixes import items, xrange
from ..kernels import Matern52, Noise, Scale, SumKernel, TransformKernel
from ..sampling.slice_sampler import SliceSampler
from ..sampling.whitened_prior_slice_sampler import WhitenedPriorSliceSampler
Expand All @@ -208,7 +208,7 @@
log = logging.getLogger(module)
except:
log = logging.getLogger()
print 'Not running from main.'
print('Not running from main.')

class GPClassifier(GP):
def __init__(self, num_dims, **options):
Expand Down Expand Up @@ -476,7 +476,7 @@ def to_dict(self):
gp_dict = {}

gp_dict['hypers'] = {}
for name, hyper in self.params.iteritems():
for name, hyper in items(self.params):
gp_dict['hypers'][name] = hyper.value

# Save the latent values as a dict with keys as hashes of the data
Expand Down
6 changes: 4 additions & 2 deletions spearmint/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@
import numpy as np
import sys

from spearmint.utils.fixes import items

def parse_resources_from_config(config):
"""Parse the config dict and return a dictionary of resource objects keyed by resource name"""

Expand All @@ -202,7 +204,7 @@ def parse_resources_from_config(config):
# If resources are specified
else:
resources = dict()
for resource_name, resource_opts in config["resources"].iteritems():
for resource_name, resource_opts in items(config["resources"]):
task_names = parse_tasks_in_resource_from_config(config, resource_name)
resources[resource_name] = resource_factory(resource_name, task_names, resource_opts)
return resources
Expand All @@ -217,7 +219,7 @@ def parse_tasks_in_resource_from_config(config, resource_name):
return ['main']
else:
tasks = list()
for task_name, task_config in config["tasks"].iteritems():
for task_name, task_config in items(config["tasks"]):
# If the user specified tasks but not specific resources for those tasks,
# We have to assume the tasks run on all resources...
if "resources" not in task_config:
Expand Down
8 changes: 4 additions & 4 deletions spearmint/sampling/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abstract_sampler import AbstractSampler
from slice_sampler import SliceSampler
from whitened_prior_slice_sampler import WhitenedPriorSliceSampler
from elliptical_slice_sampler import EllipticalSliceSampler
from spearmint.sampling.abstract_sampler import AbstractSampler
from spearmint.sampling.slice_sampler import SliceSampler
from spearmint.sampling.whitened_prior_slice_sampler import WhitenedPriorSliceSampler
from spearmint.sampling.elliptical_slice_sampler import EllipticalSliceSampler

__all__ = ["AbstractSampler", "SliceSampler", "WhitenedPriorSliceSampler", "EllipticalSliceSampler"]
Loading