|
| 1 | +from typing import Dict |
| 2 | +import json |
| 3 | +import pickle |
| 4 | + |
| 5 | +from fast_pareto import nondominated_rank |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from targets.hpolib.api import DatasetChoices as HPOlibChoices |
| 12 | +from targets.nmt_bench.api import DatasetChoices as NMTChoices |
| 13 | + |
| 14 | + |
| 15 | +plt.rcParams["font.family"] = "Times New Roman" |
| 16 | +plt.rcParams["font.size"] = 18 |
| 17 | +plt.rcParams["mathtext.fontset"] = "stix" # The setting of math font |
| 18 | + |
| 19 | + |
| 20 | +def get_nd_rank_for_hpolib(percentile: int) -> Dict[str, np.ndarray]: |
| 21 | + nd_rank = {} |
| 22 | + for dataset in HPOlibChoices: |
| 23 | + print(dataset.name) |
| 24 | + costs = pickle.load(open(f"targets/hpolib/metric_vals/{dataset.name}.pkl", "rb")) |
| 25 | + data = np.asarray([costs["valid_mse"], costs["runtime"]]).T |
| 26 | + nd_rank[dataset.name] = nondominated_rank(costs=data) |
| 27 | + |
| 28 | + return nd_rank |
| 29 | + |
| 30 | + |
| 31 | +def get_nd_rank_for_nmt(percentile: int) -> Dict[str, np.ndarray]: |
| 32 | + nd_rank = {} |
| 33 | + for dataset in NMTChoices: |
| 34 | + costs = json.load(open(f"nmt-bench/{dataset.value}")) |
| 35 | + data = np.asarray([costs["bleu"], costs["decoding_time"]]).T |
| 36 | + nd_rank[dataset.name] = nondominated_rank(costs=data, larger_is_better_objectives=[0]) |
| 37 | + |
| 38 | + return nd_rank |
| 39 | + |
| 40 | + |
| 41 | +def plot_cum(ax: plt.Axes, nd_rank: Dict[str, np.ndarray], percentile: int, set_ylabel: bool) -> None: |
| 42 | + colors = ["red", "blue", "green", "purple"] |
| 43 | + for i, (k, v) in enumerate(nd_rank.items()): |
| 44 | + n_configs = v.size |
| 45 | + order = np.argsort(v)[:int(n_configs * percentile / 100)] |
| 46 | + cnt = np.zeros(n_configs) |
| 47 | + cnt[np.arange(n_configs)[order]] = 1 |
| 48 | + if len(nd_rank) == 4: |
| 49 | + dataset_name = " ".join([s.capitalize() for s in k.split("_")]) |
| 50 | + else: |
| 51 | + lang = {"so": "Somali", "sw": "Swahili", "tl": "Tagalog", "en": "English"} |
| 52 | + dataset_name = " to ".join([lang[s] for s in k.split("_")]) |
| 53 | + ax.plot(np.arange(n_configs), np.cumsum(cnt), label=dataset_name, color=colors[i]) |
| 54 | + |
| 55 | + title = f"Cumulated count of Top-{percentile}% configuration" |
| 56 | + ax.set_title(title) |
| 57 | + ax.set_xlabel("Config indices") |
| 58 | + |
| 59 | + if set_ylabel: |
| 60 | + ax.set_ylabel("Cumulated count") |
| 61 | + |
| 62 | + ax.legend() |
| 63 | + ax.grid() |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + _, axes = plt.subplots( |
| 68 | + figsize=(20, 5), |
| 69 | + ncols=2, |
| 70 | + gridspec_kw={"wspace": 0.1}, |
| 71 | + ) |
| 72 | + nd_rank = get_nd_rank_for_hpolib(percentile=1) |
| 73 | + plot_cum(axes[0], nd_rank, percentile=1, set_ylabel=True) |
| 74 | + |
| 75 | + nd_rank = get_nd_rank_for_nmt(percentile=5) |
| 76 | + plot_cum(axes[1], nd_rank, percentile=5, set_ylabel=False) |
| 77 | + |
| 78 | + plt.savefig("figs/dataset-dist.pdf", bbox_inches="tight") |
0 commit comments