Skip to content

Commit

Permalink
fixed a few type annotation problems
Browse files Browse the repository at this point in the history
  • Loading branch information
KrissiHub committed Oct 11, 2023
1 parent 8baf7fc commit e053e92
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 65 deletions.
15 changes: 8 additions & 7 deletions deepcave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
from functools import wraps
from pathlib import Path

from dash_extensions.enrich import (
DashProxy,
MultiplexerTransform,
NoOutputTransform,
TriggerTransform,
)

name = "DeepCAVE"
package_name = "deepcave"
author = "R. Sass and E. Bergman and A. Biedenkapp and F. Hutter and M. Lindauer"
Expand All @@ -39,7 +46,7 @@
ROOT_DIR = Path(__file__).parent


def get_app(title: str):
def get_app(title: str) -> DashProxy:
"""
Create the Dash application.
Expand All @@ -54,12 +61,6 @@ def get_app(title: str):
The dash application.
"""
import dash_bootstrap_components as dbc
from dash_extensions.enrich import (
DashProxy,
MultiplexerTransform,
NoOutputTransform,
TriggerTransform,
)

app = DashProxy(
__name__,
Expand Down
1 change: 1 addition & 0 deletions deepcave/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def PLUGINS(self) -> Dict[str, List["Plugin"]]:
from deepcave.plugins.summary.footprint import FootPrint
from deepcave.plugins.summary.overview import Overview

plugins: Dict[str, List["Plugin"]] = {}
plugins = {
"Summary": [
Overview(),
Expand Down
59 changes: 36 additions & 23 deletions deepcave/evaluators/epm/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ConfigSpace import ConfigurationSpace
from random_forest import RandomForest
import deepcave.utils.styled_plot as styled_plot
from deepcave.runs.converters.smac3v1 import SMAC3v1Run
from deepcave.runs.run import AbstractRun
from deepcave.utils import layout
Expand All @@ -26,8 +27,8 @@
# print(type(test_run.get_trial_key(test_config.config_id, 10)))
For testing the get leaf values, but it returns nothing?
It returns a Tuple but of what exactly?
# For testing the get leaf values, but it returns nothing?
# It returns a Tuple but of what exactly?
x = np.array(test_config.get_array())
print(test_forest.get_leaf_values(x))
Expand Down Expand Up @@ -55,36 +56,40 @@
# print(layout.get_slider_marks(config_dict_))
yaxis = "hallo"
layout_kwargs = {
"margin": 1,
"xaxis": {"title": "Budget", "domain": 5},
}
"margin": 1,
"xaxis": {"title": "Budget", "domain": 5},
}
print(layout_kwargs)
layout_kwargs[yaxis] = {
# "title": objective.name,
"titlefont": {"color": 0},
"tickfont": {"color": 0},
"range": [3, 2],
}
# "title": objective.name,
"titlefont": {"color": 0},
"tickfont": {"color": 0},
"range": [3, 2],
}
print(layout_kwargs)
layout_kwargs[yaxis] |= {
"anchor": "free",
"overlaying": "y",
"side": "left",
"position": 0,
}
"anchor": "free",
"overlaying": "y",
"side": "left",
"position": 0,
}
print(layout_kwargs)"""
print(layout_kwargs)
"""# Consider two lists
languages1=['Python','PHP','Java',]
languages2=['C','C++','C#']
print("Language List 1: ",languages1)
print("Language List 2: ",languages2)
# Consider two lists
languages1 = [
"Python",
"PHP",
"Java",
]
languages2 = ["C", "C++", "C#"]
print("Language List 1: ", languages1)
print("Language List 2: ", languages2)
# Append list into another list
languages1.append([language for language in languages2])
print("Result: ",languages1)
print("Result: ", languages1)
tuple_test = ("arg2", "arg3", "arg4")
print(tuple_test)
Expand All @@ -94,4 +99,12 @@
pal = sns.color_palette()
hex_codes = pal.as_hex()
print(type(hex_codes[5 % len(hex_codes)]))"""
print(type(hex_codes[5 % len(hex_codes)]))
styled_plot = styled_plot.StyledPlot()
plt = styled_plot.plt
print(type(plt.render()))"""

list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list_comp = [1.5 for i in list]
10 changes: 4 additions & 6 deletions deepcave/evaluators/footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, run: AbstractRun):
self._depth = np.array(depth) # type: ignore

# Global variables
self._distances = None
self._distances: Optional[np.ndarray] = None
self._trained = False
self._reset()

Expand Down Expand Up @@ -142,6 +142,7 @@ def calculate(

# Init distances
self._init_distances(X, config_ids, exclude_configs=exclude_configs)
assert self._distances is not None

border_generator = sample_border_config(self.cs)
random_generator = sample_random_config(self.cs, d=support_discretization)
Expand Down Expand Up @@ -173,8 +174,8 @@ def calculate(
continue

# Encode config
config = np.array(self.run.encode_config(config))
rejected = self._update_distances(config, config_id, rejection_threshold)
config_array = np.array(self.run.encode_config(config))
rejected = self._update_distances(config_array, config_id, rejection_threshold)
if not rejected:
# Count
if config_id == BORDER_CONFIG_ID:
Expand All @@ -196,15 +197,12 @@ def calculate(
break

# Or if we reach more than 4000 (otherwise it takes too long)
assert self._distances is not None

if self._distances.shape[0] % 100 == 0:
logger.info(f"Found {self._distances.shape[0]} configurations...")

if self._distances.shape[0] > 4000:
break

assert self._distances is not None
logger.info(f"Added {count_border} border configs and {count_random} random configs.")
logger.info(f"Total configurations: {self._distances.shape[0]}.")
logger.info("Getting MDS data...")
Expand Down
27 changes: 15 additions & 12 deletions deepcave/evaluators/lpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _get_neighborhood(self) -> Dict[str, List[Union[np.ndarray, List[np.ndarray]
"""
hp_names = self.cs.get_hyperparameter_names()

neighborhood = {}
neighborhood: Dict[str, List[Union[np.ndarray, List[np.ndarray]]]] = {}
for hp_idx, hp_name in enumerate(hp_names):
# Check if hyperparameter is active
if not np.isfinite(self.incumbent_array[hp_idx]):
Expand All @@ -301,16 +301,16 @@ def _get_neighborhood(self) -> Dict[str, List[Union[np.ndarray, List[np.ndarray]
base = np.e
log_lower = np.log(hp.lower) / np.log(base)
log_upper = np.log(hp.upper) / np.log(base)
neighbors = np.logspace(
neighbors_range = np.logspace(
start=log_lower,
stop=log_upper,
num=self.continous_neighbors,
endpoint=True,
base=base,
)
else:
neighbors = np.linspace(hp.lower, hp.upper, self.continous_neighbors)
neighbors = list(map(lambda x: hp._inverse_transform(x), neighbors))
neighbors_range = np.linspace(hp.lower, hp.upper, self.continous_neighbors)
neighbors = list(map(lambda x: hp._inverse_transform(x), neighbors_range))
else:
neighbors = hp.get_neighbors(self.incumbent_array[hp_idx], self.rs)

Expand All @@ -336,19 +336,22 @@ def _get_neighborhood(self) -> Dict[str, List[Union[np.ndarray, List[np.ndarray]
map(lambda x: x[0], sorted(enumerate(checked_neighbors), key=lambda y: y[1]))
)
if isinstance(self.cs.get_hyperparameter(hp_name), CategoricalHyperparameter):
checked_neighbors_non_unit_cube = list(
checked_neighbors_non_unit_cube_categorical = list(
np.array(checked_neighbors_non_unit_cube)[sort_idx]
)
neighborhood[hp_name] = [
np.array(checked_neighbors)[sort_idx],
checked_neighbors_non_unit_cube_categorical,
]
else:
checked_neighbors_non_unit_cube = np.array(checked_neighbors_non_unit_cube)[
sort_idx
checked_neighbors_non_unit_cube_non_categorical = np.array(
checked_neighbors_non_unit_cube
)[sort_idx]
neighborhood[hp_name] = [
np.array(checked_neighbors)[sort_idx],
checked_neighbors_non_unit_cube_non_categorical,
]

neighborhood[hp_name] = [
np.array(checked_neighbors)[sort_idx],
checked_neighbors_non_unit_cube,
]

return neighborhood

def _predict_mean_var(self, config: Configuration) -> Tuple[np.ndarray, np.ndarray]:
Expand Down
3 changes: 2 additions & 1 deletion deepcave/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,9 @@ def toggle_help_modal(n: Optional[int], is_open: bool) -> Tuple[bool, str]:
A tuple containing the is open information and the code.
"""
if n:
# Wait for meeting
return not is_open

# Wait for meeting
return is_open

# Register callback to click on configurations
Expand Down
3 changes: 2 additions & 1 deletion deepcave/plugins/hyperparameter/importances.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ def load_mpl_outputs(run, inputs: Dict[str, Any], outputs) -> str:
x_values,
y,
yerr=y_err,
# plt is matplotlib object and has no function get_color, Issue opened
color=plt.get_color(budget_id),
label=budget,
error_kw=dict(lw=1, capsize=2, capthick=1),
Expand All @@ -515,5 +516,5 @@ def load_mpl_outputs(run, inputs: Dict[str, Any], outputs) -> str:
# Rotate x ticks
plt.xticks(x_values, x_labels, rotation=90)
plt.ylabel("Importance")

# plt is matplotlib object and has no function render, Issue opened
return plt.render()
4 changes: 2 additions & 2 deletions deepcave/plugins/objective/cost_over_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def load_outputs(runs, inputs, outputs):
y_err = np.array(outputs[run.id]["costs_std"])
y_upper = list(y + y_err)
y_lower = list(y - y_err)
y = list(y)
y_list = list(y)

hovertext = None
hoverinfo = "skip"
Expand All @@ -307,7 +307,7 @@ def load_outputs(runs, inputs, outputs):
traces.append(
go.Scatter(
x=x,
y=y,
y=y_list,
name=run.name,
line_shape="hv",
line=dict(color=get_color(idx)),
Expand Down
7 changes: 5 additions & 2 deletions deepcave/plugins/objective/pareto_front.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- ParetoFront: Generate an interactive Pareto Front visualization.
"""

from typing import Any, Callable, Dict, List, Union
from typing import Any, Callable, Dict, List, Literal, Union

import dash_bootstrap_components as dbc
import numpy as np
Expand Down Expand Up @@ -470,7 +470,9 @@ def load_mpl_outputs(runs, inputs, outputs):
x += [points[point_idx][0]]
y += [points[point_idx][1]]

# Issue opened
color = plt.get_color(idx) # , alpha=0.1)
# Issue opened
color_pareto = plt.get_color(idx)

if show_all:
Expand All @@ -482,6 +484,7 @@ def load_mpl_outputs(runs, inputs, outputs):
optimize1 = objective_1.optimize
optimize2 = objective_2.optimize

line_shape: Union[Literal["post"], Literal["pre"], Literal["mid"]]
if optimize1 == optimize2:
if objective_1.optimize == "lower":
line_shape = "post"
Expand All @@ -504,5 +507,5 @@ def load_mpl_outputs(runs, inputs, outputs):
plt.ylabel(objective_2.name)

plt.legend()

# Issue already opened
return plt.render()
4 changes: 2 additions & 2 deletions deepcave/plugins/summary/footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,13 @@ def load_mpl_outputs(run, inputs, outputs):
if points == "incumbent_points":
size = 10
marker_symbol = "^"

# Issue opened
color = plt.get_color(color_id)
plt.scatter(x, y, marker=marker_symbol, s=size, label=name, c=color)

plt.axis("off")
plt.legend(loc="lower right")

# Issue opened
images += [plt.render()]

return images
6 changes: 4 additions & 2 deletions deepcave/runs/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ def update_groups(self, groups: Optional[Dict[str, List[str]]] = None) -> None:
instantiated_groups = {}
if groups is None:
groups = self.c.get("groups")

# This check is necessary because groups could still be None
if groups is None:
raise TypeError("Groups can not be None.")
# Add grouped runs
for group_name, run_paths in groups.items():
runs = []
Expand Down Expand Up @@ -433,7 +435,7 @@ def get_groups(self) -> List[Group]:
self.update()
return list(self.groups.values())

def get_runs(self, include_groups=False) -> List[AbstractRun]:
def get_runs(self, include_groups: bool = False) -> List[AbstractRun]:
"""
Return the runs from the internal cache.
Expand Down
19 changes: 12 additions & 7 deletions deepcave/utils/styled_plotty.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def get_color(id_: int, alpha: float = 1) -> Union[str, Tuple[float, float, floa
return f"rgba({r}, {g}, {b}, {alpha})"


def get_discrete_heatmap(x, y, values: List[Any], labels: List[Any]):
def get_discrete_heatmap(
x: List[Union[float, int]], y: List[int], values: List[Any], labels: List[Any]
) -> go.Heatmap:
"""
Generate a discrete colorscale from a (nested) list or numpy array of values.
Expand Down Expand Up @@ -150,8 +152,11 @@ def get_discrete_heatmap(x, y, values: List[Any], labels: List[Any]):
for i2, v2 in enumerate(v1):
z[i1][i2] = mapping[v2]

n_intervals = v + [len(v)]
n_intervals = [(i - n_intervals[0]) / (n_intervals[-1] - n_intervals[0]) for i in n_intervals]
n_intervals_int = v + [len(v)]
n_intervals = [
(i - n_intervals_int[0]) / (n_intervals_int[-1] - n_intervals_int[0])
for i in n_intervals_int
]
colors = [get_color(i) for i in range(len(n_intervals))]

discrete_colorscale = []
Expand All @@ -161,12 +166,12 @@ def get_discrete_heatmap(x, y, values: List[Any], labels: List[Any]):
tickvals = [np.mean(n_intervals[k : k + 2]) for k in range(len(n_intervals) - 1)]
ticktext = unique_sorted_labels

x = [str(i) for i in x]
y = [str(i) for i in y]
x_str = [str(i) for i in x]
y_str = [str(i) for i in y]

return go.Heatmap(
x=x,
y=y,
x=x_str,
y=y_str,
z=z,
showscale=True,
colorscale=discrete_colorscale,
Expand Down

0 comments on commit e053e92

Please sign in to comment.