| Conditions | 2 |
| Total Lines | 91 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import numpy as np |
||
| 58 | def plot_search_path( |
||
| 59 | optimizer_key, |
||
| 60 | n_iter, |
||
| 61 | objective_function, |
||
| 62 | objective_function_np, |
||
| 63 | search_space, |
||
| 64 | ): |
||
| 65 | opt_class, n_inits, random_state = optimizer_dict[optimizer_key] |
||
| 66 | opt = opt_class(search_space, rand_rest_p=0) |
||
| 67 | |||
| 68 | opt.search( |
||
| 69 | objective_function, |
||
| 70 | n_iter=n_iter, |
||
| 71 | random_state=random_state, |
||
| 72 | memory=False, |
||
| 73 | verbosity=False, |
||
| 74 | initialize={"vertices": n_inits}, |
||
| 75 | ) |
||
| 76 | |||
| 77 | conv = Converter(search_space) |
||
| 78 | |||
| 79 | plt.figure(figsize=(10, 8)) |
||
| 80 | plt.set_cmap("jet_r") |
||
| 81 | |||
| 82 | x_all, y_all = search_space["x"], search_space["y"] |
||
| 83 | xi, yi = np.meshgrid(x_all, y_all) |
||
| 84 | zi = objective_function_np(xi, yi) |
||
| 85 | |||
| 86 | plt.imshow( |
||
| 87 | zi, |
||
| 88 | alpha=0.15, |
||
| 89 | # vmin=z.min(), |
||
| 90 | # vmax=z.max(), |
||
| 91 | # origin="lower", |
||
| 92 | extent=[x_all.min(), x_all.max(), y_all.min(), y_all.max()], |
||
| 93 | ) |
||
| 94 | |||
| 95 | # print("\n Results \n", opt.results) |
||
| 96 | |||
| 97 | for n, opt_ in enumerate(tqdm(opt.optimizers)): |
||
| 98 | pos_list = np.array(opt_.pos_new_list) |
||
| 99 | score_list = np.array(opt_.score_new_list) |
||
| 100 | |||
| 101 | values_list = conv.positions2values(pos_list) |
||
| 102 | values_list = np.array(values_list) |
||
| 103 | |||
| 104 | plt.plot( |
||
| 105 | values_list[:, 0], |
||
| 106 | values_list[:, 1], |
||
| 107 | linestyle="--", |
||
| 108 | marker=",", |
||
| 109 | color="black", |
||
| 110 | alpha=0.33, |
||
| 111 | label=n, |
||
| 112 | linewidth=0.5, |
||
| 113 | ) |
||
| 114 | plt.scatter( |
||
| 115 | values_list[:, 0], |
||
| 116 | values_list[:, 1], |
||
| 117 | c=score_list, |
||
| 118 | marker="H", |
||
| 119 | s=15, |
||
| 120 | vmin=-20000, |
||
| 121 | vmax=0, |
||
| 122 | label=n, |
||
| 123 | edgecolors="black", |
||
| 124 | linewidth=0.3, |
||
| 125 | ) |
||
| 126 | |||
| 127 | plt.xlabel("x") |
||
| 128 | plt.ylabel("y") |
||
| 129 | |||
| 130 | nth_iteration = "\n\nnth Iteration: " + str(n_iter) |
||
| 131 | |||
| 132 | plt.title(optimizer_key + nth_iteration) |
||
| 133 | |||
| 134 | plt.xlim((-101, 101)) |
||
| 135 | plt.ylim((-101, 101)) |
||
| 136 | plt.colorbar() |
||
| 137 | # plt.legend(loc="upper left", bbox_to_anchor=(-0.10, 1.2)) |
||
| 138 | |||
| 139 | plt.tight_layout() |
||
| 140 | plt.savefig( |
||
| 141 | "./_plots/" |
||
| 142 | + str(opt.__class__.__name__) |
||
| 143 | + "_" |
||
| 144 | + "{0:0=2d}".format(n_iter) |
||
| 145 | + ".jpg", |
||
| 146 | dpi=300, |
||
| 147 | ) |
||
| 148 | plt.close() |
||
| 149 | |||
| 179 |