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

Issue #93 constant handling #94

Merged
merged 1 commit into from
Apr 11, 2020
Merged
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
25 changes: 15 additions & 10 deletions fanova/visualizer.py
Original file line number Diff line number Diff line change
@@ -94,9 +94,12 @@ def generate_pairwise_marginal(self, param_list, resolution=20):
grid_fanova, grid_orig = [], []

for p in params:
if isinstance(p, (CategoricalHyperparameter)):
if isinstance(p, CategoricalHyperparameter):
grid_orig.append(p.choices)
grid_fanova.append(np.arange(len(p.choices)))
elif isinstance(p, Constant):
grid_orig.append((p.value,))
grid_fanova.append(np.arange(1))
else:
if p.log:
base = np.e # assuming ConfigSpace uses the natural logarithm
@@ -108,14 +111,16 @@ def generate_pairwise_marginal(self, param_list, resolution=20):
grid_orig.append(grid)
grid_fanova.append(grid)

# Turn into arrays
param_indices = np.array(param_indices).squeeze()
grid_fanova = np.array(grid_fanova).squeeze()
# Turn into arrays, squeeze all but the first two dimensions (to avoid squeezing away the dimension for Constants)
param_indices = np.array(param_indices)
param_indices = param_indices.reshape([s for i, s in enumerate(param_indices.shape) if i in [0, 1] or s != 1])
grid_fanova = np.array(grid_fanova)
grid_fanova = grid_fanova.reshape([s for i, s in enumerate(grid_fanova.shape) if i in [0, 1] or s != 1])

# The swap-parameter is here because this was how I found this code and without understanding of the
# in-detail implementation of fANOVA I assume there is a reason for the first element of the fanova
# method marginal_mean_variance_for_values to expect the element with less elements as the first parameter
swap = len(grid_fanova[1] > len(grid_fanova[0]))
swap = len(grid_fanova[1]) > len(grid_fanova[0])

# Populating the result
zz = np.zeros((len(grid_fanova[0]), len(grid_fanova[1])))
@@ -154,8 +159,8 @@ def plot_pairwise_marginal(self, param_list, resolution=20, show=False, three_d=

params, param_names, param_indices = self._get_parameter(param_list)

first_is_cat = isinstance(params[0], CategoricalHyperparameter)
second_is_cat = isinstance(params[1], CategoricalHyperparameter)
first_is_cat = isinstance(params[0], (CategoricalHyperparameter, Constant))
second_is_cat = isinstance(params[1], (CategoricalHyperparameter, Constant))

plt.close()
fig = plt.figure()
@@ -181,7 +186,7 @@ def plot_pairwise_marginal(self, param_list, resolution=20, show=False, three_d=

for i, cat in enumerate(choices[0]):
if params[1].log:
plt.semilogx(choices[1], zz[i],label='%s' % str(cat))
plt.semilogx(choices[1], zz[i], label='%s' % str(cat))
else:
plt.plot(choices[1], zz[i], label='%s' % str(cat))

@@ -315,7 +320,7 @@ def plot_marginal(self, param, resolution=100, log_scale=None, show=True, incumb
except AttributeError:
labels = str(param)
categorical_size = 1
indices = np.arange(1, categorical_size+1, 1)
indices = np.arange(1, categorical_size + 1, 1)
mean, std = self.generate_marginal(param_idx)
min_y = mean[0]
max_y = mean[0]
@@ -408,7 +413,7 @@ def create_most_important_pairwise_marginal_plots(self, params=None, n=20, three
for param1, param2 in most_important_pairwise_marginals:
params, param_names, param_indices = self._get_parameter([param1, param2])
param_names_str = re.sub('[!,@#\'\n$\[\]]', '', str(param_names))
outfile_name = os.path.join(self.directory, str(param_names_str).replace(" ","_") + ".png")
outfile_name = os.path.join(self.directory, str(param_names_str).replace(" ", "_") + ".png")
self.logger.info("creating %s" % outfile_name)
self.plot_pairwise_marginal((param1, param2), show=False, three_d=three_d, resolution=resolution)
plt.savefig(outfile_name)