| Conditions | 25 |
| Total Lines | 69 |
| 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:
Complex classes like annif.eval.EvaluationBatch._evaluate_samples() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Evaluation metrics for Annif""" |
||
| 102 | def _evaluate_samples(self, y_true, y_pred, metrics=[]): |
||
| 103 | y_pred_binary = y_pred > 0.0 |
||
| 104 | |||
| 105 | # define the available metrics as lazy lambda functions |
||
| 106 | # so we can execute only the ones actually requested |
||
| 107 | all_metrics = { |
||
| 108 | "Precision (doc avg)": lambda: precision_score( |
||
| 109 | y_true, y_pred_binary, average="samples" |
||
| 110 | ), |
||
| 111 | "Recall (doc avg)": lambda: recall_score( |
||
| 112 | y_true, y_pred_binary, average="samples" |
||
| 113 | ), |
||
| 114 | "F1 score (doc avg)": lambda: f1_score( |
||
| 115 | y_true, y_pred_binary, average="samples" |
||
| 116 | ), |
||
| 117 | "Precision (subj avg)": lambda: precision_score( |
||
| 118 | y_true, y_pred_binary, average="macro" |
||
| 119 | ), |
||
| 120 | "Recall (subj avg)": lambda: recall_score( |
||
| 121 | y_true, y_pred_binary, average="macro" |
||
| 122 | ), |
||
| 123 | "F1 score (subj avg)": lambda: f1_score( |
||
| 124 | y_true, y_pred_binary, average="macro" |
||
| 125 | ), |
||
| 126 | "Precision (weighted subj avg)": lambda: precision_score( |
||
| 127 | y_true, y_pred_binary, average="weighted" |
||
| 128 | ), |
||
| 129 | "Recall (weighted subj avg)": lambda: recall_score( |
||
| 130 | y_true, y_pred_binary, average="weighted" |
||
| 131 | ), |
||
| 132 | "F1 score (weighted subj avg)": lambda: f1_score( |
||
| 133 | y_true, y_pred_binary, average="weighted" |
||
| 134 | ), |
||
| 135 | "Precision (microavg)": lambda: precision_score( |
||
| 136 | y_true, y_pred_binary, average="micro" |
||
| 137 | ), |
||
| 138 | "Recall (microavg)": lambda: recall_score( |
||
| 139 | y_true, y_pred_binary, average="micro" |
||
| 140 | ), |
||
| 141 | "F1 score (microavg)": lambda: f1_score( |
||
| 142 | y_true, y_pred_binary, average="micro" |
||
| 143 | ), |
||
| 144 | "F1@5": lambda: f1_score( |
||
| 145 | y_true, filter_pred_top_k(y_pred, 5) > 0.0, average="samples" |
||
| 146 | ), |
||
| 147 | "NDCG": lambda: ndcg_score(y_true, y_pred), |
||
| 148 | "NDCG@5": lambda: ndcg_score(y_true, y_pred, limit=5), |
||
| 149 | "NDCG@10": lambda: ndcg_score(y_true, y_pred, limit=10), |
||
| 150 | "Precision@1": lambda: precision_score( |
||
| 151 | y_true, filter_pred_top_k(y_pred, 1) > 0.0, average="samples" |
||
| 152 | ), |
||
| 153 | "Precision@3": lambda: precision_score( |
||
| 154 | y_true, filter_pred_top_k(y_pred, 3) > 0.0, average="samples" |
||
| 155 | ), |
||
| 156 | "Precision@5": lambda: precision_score( |
||
| 157 | y_true, filter_pred_top_k(y_pred, 5) > 0.0, average="samples" |
||
| 158 | ), |
||
| 159 | "True positives": lambda: true_positives(y_true, y_pred_binary), |
||
| 160 | "False positives": lambda: false_positives(y_true, y_pred_binary), |
||
| 161 | "False negatives": lambda: false_negatives(y_true, y_pred_binary), |
||
| 162 | } |
||
| 163 | |||
| 164 | if not metrics: |
||
| 165 | metrics = all_metrics.keys() |
||
| 166 | |||
| 167 | with warnings.catch_warnings(): |
||
| 168 | warnings.simplefilter("ignore") |
||
| 169 | |||
| 170 | return {metric: all_metrics[metric]() for metric in metrics} |
||
| 171 | |||
| 240 |