Skip to content

Commit

Permalink
Merge pull request #25 from Krxsy/master
Browse files Browse the repository at this point in the history
Ported Python 2 Code to Python 3
  • Loading branch information
sfalkner committed May 19, 2015
2 parents a7cd1a0 + 84f544b commit 4ff075c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
18 changes: 9 additions & 9 deletions pyfanova/fanova.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
def check_java_version():
import re
from subprocess import STDOUT, check_output
out = check_output(["java", "-version"], stderr=STDOUT).split("\n")
out = check_output(["java".encode("utf-8"), "-version".encode("utf-8")], stderr=STDOUT).split("\n".encode("utf-8"))
if len(out) < 1:
print "Failed checking Java version. Make sure Java version 7 or greater is installed."
print("Failed checking Java version. Make sure Java version 7 or greater is installed.")
return False
m = re.match('java version "\d+.(\d+)..*', out[0])
m = re.match('java version "\d+.(\d+)..*'.encode("utf-8"), out[0])
if m is None or len(m.groups()) < 1:
print "Failed checking Java version. Make sure Java version 7 or greater is installed."
print("Failed checking Java version. Make sure Java version 7 or greater is installed.")
return False
java_version = int(m.group(1))
if java_version < 7:
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(self, smac_output, num_trees=30,
self._config_space = ConfigSpace(self._remote)

param_names = self._config_space.get_parameter_names()
self.param_name2dmin = dict(zip(param_names, range(len(param_names))))
self.param_name2dmin = dict(list(zip(param_names, list(range(len(param_names))))))
else:
stdout, stderr = self._process.communicate()
error_msg = "Failed starting fanova. Did you start it from a SMAC state-run directory?"
Expand Down Expand Up @@ -179,7 +179,7 @@ def get_categorical_marginal_for_value(self, param, value):
"""
size = self._config_space.get_categorical_size(param)
if(value >= size):
print "Categorical value %d is out of bounds [%d, %d] for parameter %s" %(value, 0, size, param)
print("Categorical value %d is out of bounds [%d, %d] for parameter %s" %(value, 0, size, param))
return
else:
return self._get_marginal_for_value(param, value)
Expand Down Expand Up @@ -272,7 +272,7 @@ def print_all_marginals(self, max_num=30, pairwise=True):
for marginal, param_name in zip(main_marginal_performances, param_names):
labelled_performances.append((marginal, "%.2f%% due to main effect: %s" % (marginal, param_name), param_name))

print "Sum of fractions for main effects %.2f%%" % (sum(main_marginal_performances))
print("Sum of fractions for main effects %.2f%%" % (sum(main_marginal_performances)))

if pairwise:
pairwise_marginal_performance = self.get_all_pairwise_marginals()
Expand All @@ -282,14 +282,14 @@ def print_all_marginals(self, max_num=30, pairwise=True):
label = "%.2f%% due to interaction: %s x %s" % (pairwise_marginal_performance, param_name1, param_name2)
labelled_performances.append((pairwise_marginal_performance, label, param_name1 + " x " + param_name2))

print "Sum of fractions for pairwise interaction effects %.2f%%" % (sum_of_pairwise_marginals)
print("Sum of fractions for pairwise interaction effects %.2f%%" % (sum_of_pairwise_marginals))

sorted_performances = sorted(labelled_performances, reverse=True)
return_values = []
if max_num is not None:
sorted_performances = sorted_performances[:max_num]
for marginal, label, name in sorted_performances:
print label
print(label)
return_values.append((marginal, name))
return return_values

Expand Down
12 changes: 6 additions & 6 deletions pyfanova/fanova_from_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _write_runs_and_results_file(self, values):
writer.writerow(("Run Number", "Run History Configuration ID", "Instance ID", "Response Value (y)", "Censored?", "Cutoff Time Used",
"Seed", "Runtime", "Run Length", "Run Result Code", "Run Quality", "SMAC Iteration", "SMAC Cumulative Runtime", "Run Result"))

for i in xrange(0, len(values)):
for i in range(0, len(values)):
line = (i, i, 1, 0, 0, 0, 1, 0, 0, 0, values[i], 0, 0, "SAT")
writer.writerow(line)

Expand All @@ -69,7 +69,7 @@ def _write_runs_and_results_file(self, values):
def _write_param_file(self):

fh = open(os.path.join(self._scenario_dir, "param-file.txt"), "w")
for i in xrange(0, self._num_of_params):
for i in range(0, self._num_of_params):
param_string = "X" + str(i) + " " + str(self._bounds[i]) + " " + "[" + str(self._defaults[i]) + "]\n"
logging.debug(param_string)
fh.write(param_string)
Expand All @@ -79,9 +79,9 @@ def _write_param_file(self):
def _write_paramstrings_file(self, params):

fh = open(os.path.join(self._scenario_dir, "paramstrings.txt"), "w")
for i in xrange(0, params.shape[0]):
for i in range(0, params.shape[0]):
line = str(i) + ": "
for j in xrange(0, params.shape[1]):
for j in range(0, params.shape[1]):
line = line + "X" + str(j) + "='" + str(params[i][j]) + "', "
#remove the last comma and whitespace from the string again
line = line[:-2]
Expand Down Expand Up @@ -114,7 +114,7 @@ def _read_csv_file(self, filename):
fh.seek(0)
rownum = 0
for line in reader:
for param in xrange(0, self._num_of_params):
for param in range(0, self._num_of_params):
X[rownum][param] = line[param]
y[rownum] = line[-1]
rownum += 1
Expand All @@ -123,7 +123,7 @@ def _read_csv_file(self, filename):

self._bounds = []
self._defaults = []
for i in xrange(0, self._num_of_params):
for i in range(0, self._num_of_params):
#Take min and max value as bounds for smac parameter file
self._bounds.append([np.min(X[:, i]), np.max(X[:, i])])
#Set min value as default value for smac parameter file
Expand Down
10 changes: 5 additions & 5 deletions pyfanova/fanova_from_hpolib.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _load_pkl(self, pkl):

def _clean_trials(self):
trials = []
for i in xrange(0, len(self.trials)):
for i in range(0, len(self.trials)):
result = self.trials[i]["result"]
if not np.isfinite(result):
logging.warning("skipping result, that's not finite...")
Expand Down Expand Up @@ -93,7 +93,7 @@ def _write_runs_and_results_file(self):
writer.writerow(("Run Number", "Run History Configuration ID", "Instance ID", "Response Value (y)", "Censored?", "Cutoff Time Used",
"Seed", "Runtime", "Run Length", "Run Result Code", "Run Quality", "SMAC Iteration", "SMAC Cumulative Runtime", "Run Result"))

for i in xrange(0, len(self.trials)):
for i in range(0, len(self.trials)):
result = self.trials[i]["result"]
line = (i, i, 1, 0, 0, 0, 1, 0, 0, 0, result, 0, 0, "SAT")
writer.writerow(line)
Expand All @@ -108,15 +108,15 @@ def _write_param_file(self):

def _write_paramstrings_file(self):
fh = open(os.path.join(self._scenario_dir, "paramstrings.txt"), "w")
for i in xrange(0, len(self.trials)):
for i in range(0, len(self.trials)):
params = self.trials[i]["params"]
clean_params = {}
for param_name, param_value in params.iteritems():
for param_name, param_value in params.items():
#FIX of a hpolib bug, where the parameter names in the pkl contain a - infront of their name
if param_name[0] == '-':
param_name = param_name[1:]
clean_params[param_name] = param_value
param_list = ["%s='%s'" % (key, value) for key, value in clean_params.iteritems()]
param_list = ["%s='%s'" % (key, value) for key, value in clean_params.items()]
line = "%d: %s\n" % (i, ", ".join(param_list))
fh.write(line)
fh.close()
Expand Down
34 changes: 17 additions & 17 deletions pyfanova/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class Visualizer(object):
def __init__(self, fanova):
self._fanova = fanova
self._latex_template = '''\documentclass[letterpaper]{article}
\usepackage{times}
\usepackage{graphicx}
\usepackage{epsfig}
\usepackage{subfigure}
\usepackage{lscape}
\\usepackage{times}
\\usepackage{graphicx}
\\usepackage{epsfig}
\\usepackage{subfigure}
\\usepackage{lscape}
\begin{document}
\title{Functional ANOVA Analysis}
\maketitle
Expand All @@ -39,7 +39,7 @@ def create_all_plots(self, directory, **kwargs):
for param_name in self._fanova.get_config_space().get_categorical_parameters():
plt.clf()
outfile_name = os.path.join(directory, param_name.replace(os.sep, "_") + ".png")
print "creating %s" % outfile_name
print("creating %s" % outfile_name)
self.plot_categorical_marginal(param_name)
plt.savefig(outfile_name)

Expand All @@ -50,7 +50,7 @@ def create_all_plots(self, directory, **kwargs):
for param_name in params_to_plot:
plt.clf()
outfile_name = os.path.join(directory, param_name.replace(os.sep, "_") + ".png")
print "creating %s" % outfile_name
print("creating %s" % outfile_name)
self.plot_marginal(param_name, **kwargs)
plt.savefig(outfile_name)

Expand All @@ -60,11 +60,11 @@ def create_most_important_pairwise_marginal_plots(self, directory, n=20):
most_important_pairwise_marginals = self._fanova.get_most_important_pairwise_marginals(n)
for param1, param2 in most_important_pairwise_marginals:
if param1 in categorical_parameters or param2 in categorical_parameters:
print "skipping pairwise marginal plot %s x %s, because one of them is categorical" % (param1, param2)
print("skipping pairwise marginal plot %s x %s, because one of them is categorical" % (param1, param2))
continue
outfile_name = os.path.join(directory, param1.replace(os.sep, "_") + "x" + param2.replace(os.sep, "_") + ".png")
plt.clf()
print "creating %s" % outfile_name
print("creating %s" % outfile_name)
self.plot_pairwise_marginal(param1, param2)
plt.savefig(outfile_name)

Expand All @@ -74,7 +74,7 @@ def plot_categorical_marginal(self, param):
param_name = self._fanova.get_config_space().get_parameter_names()[dim]
else:
if param not in self._fanova.param_name2dmin:
print "Parameter %s not known" % param
print("Parameter %s not known" % param)
return
dim = self._fanova.param_name2dmin[param]
param_name = param
Expand All @@ -84,15 +84,15 @@ def plot_categorical_marginal(self, param):
labels = self._fanova.get_config_space().get_categorical_values(param)

if param_name not in self._fanova.get_config_space().get_categorical_parameters():
print "Parameter %s is not a categorical parameter!" % (param_name)
print("Parameter %s is not a categorical parameter!" % (param_name))

indices = np.asarray(range(categorical_size))
indices = np.asarray(list(range(categorical_size)))
width = 0.5
marginals = [self._fanova.get_categorical_marginal_for_value(param_name, i) for i in range(categorical_size)]
mean, std = zip(*marginals)
mean, std = list(zip(*marginals))
#plt.bar(indices, mean, width, color='red', yerr=std)
#plot mean
b = plt.boxplot(map(lambda x: [x], mean), 0, '', labels=labels)
b = plt.boxplot([[x] for x in mean], 0, '', labels=labels)
min_y = mean[0]
max_y = mean[0]
# blow up boxes
Expand Down Expand Up @@ -161,20 +161,20 @@ def plot_marginal(self, param, lower_bound=0, upper_bound=1, is_int=False, resol
param_name = self._fanova.get_config_space().get_parameter_names()[dim]
else:
if param not in self._fanova.param_name2dmin:
print "Parameter %s not known" % param
print("Parameter %s not known" % param)
return
dim = self._fanova.param_name2dmin[param]
param_name = param

if param_name not in self._fanova.get_config_space().get_integer_parameters() and param_name not in self._fanova.get_config_space().get_continuous_parameters():
print "Parameter %s is not a continuous or integer parameter!" % (param_name)
print("Parameter %s is not a continuous or integer parameter!" % (param_name))
return
grid = np.linspace(lower_bound, upper_bound, resolution)
display_grid = [self._fanova.get_config_space().unormalize_value(param_name, value) for value in grid]

mean = np.zeros(resolution)
std = np.zeros(resolution)
for i in xrange(0, resolution):
for i in range(0, resolution):
(m, s) = self._fanova.get_marginal_for_value(dim, grid[i])
mean[i] = m
std[i] = s
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
def check_java_version():
import re
from subprocess import STDOUT, check_output
out = check_output(["java", "-version"], stderr=STDOUT).split("\n")
out = check_output(["java".encode("utf-8"), "-version".encode("utf-8")], stderr=STDOUT).split("\n".encode("utf-8"))
if len(out) < 1:
print "failed checking Java version. Make sure Java version 7 or greater is installed."
print("failed checking Java version. Make sure Java version 7 or greater is installed.")
return False
m = re.match('java version "\d+.(\d+)..*', out[0])
m = re.match('java version "\d+.(\d+)..*'.encode("utf-8"), out[0])
if m is None or len(m.groups()) < 1:
print "failed checking Java version. Make sure Java version 7 or greater is installed."
print("failed checking Java version. Make sure Java version 7 or greater is installed.")
return False
java_version = int(m.group(1))
if java_version < 7:
Expand Down

0 comments on commit 4ff075c

Please sign in to comment.