| Conditions | 3 |
| Total Lines | 61 |
| Code Lines | 46 |
| 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 time |
||
| 54 | def create_performance_data( |
||
| 55 | study_name, objective_function, search_space, n_iter |
||
| 56 | ): |
||
| 57 | results = [] |
||
| 58 | |||
| 59 | for opt_name in tqdm(optimizer_dict.keys()): |
||
| 60 | total_time_list = [] |
||
| 61 | eval_time_list = [] |
||
| 62 | iter_time_list = [] |
||
| 63 | |||
| 64 | for random_state in tqdm(range(runs)): |
||
| 65 | |||
| 66 | c_time = time.time() |
||
| 67 | opt = optimizer_dict[opt_name](search_space) |
||
| 68 | opt.search( |
||
| 69 | objective_function, |
||
| 70 | n_iter=n_iter, |
||
| 71 | verbosity=False, |
||
| 72 | random_state=random_state, |
||
| 73 | ) |
||
| 74 | |||
| 75 | total_time = time.time() - c_time |
||
| 76 | eval_time = np.array(opt.eval_times).sum() |
||
| 77 | iter_time = np.array(opt.iter_times).sum() |
||
| 78 | |||
| 79 | total_time_list.append(total_time) |
||
| 80 | eval_time_list.append(eval_time) |
||
| 81 | iter_time_list.append(iter_time) |
||
| 82 | |||
| 83 | total_time_mean = np.array(total_time_list).mean() |
||
| 84 | eval_time_mean = np.array(eval_time_list).mean() |
||
| 85 | iter_time_mean = np.array(iter_time_list).mean() |
||
| 86 | |||
| 87 | total_time_std = np.array(total_time_list).std() |
||
| 88 | eval_time_std = np.array(eval_time_list).std() |
||
| 89 | iter_time_std = np.array(iter_time_list).std() |
||
| 90 | |||
| 91 | results.append( |
||
| 92 | [ |
||
| 93 | total_time_mean, |
||
| 94 | total_time_std, |
||
| 95 | eval_time_mean, |
||
| 96 | eval_time_std, |
||
| 97 | iter_time_mean, |
||
| 98 | iter_time_std, |
||
| 99 | ] |
||
| 100 | ) |
||
| 101 | |||
| 102 | index = [ |
||
| 103 | "total_time_mean", |
||
| 104 | "total_time_std", |
||
| 105 | "eval_time_mean", |
||
| 106 | "eval_time_std", |
||
| 107 | "iter_time_mean", |
||
| 108 | "iter_time_std", |
||
| 109 | ] |
||
| 110 | columns = list(optimizer_dict.keys()) |
||
| 111 | |||
| 112 | results = np.array(results).T |
||
| 113 | results = pd.DataFrame(results, columns=columns, index=index) |
||
| 114 | results.to_csv("./_data/" + study_name + ".csv") |
||
| 115 | |||
| 121 |