| Conditions | 26 |
| Total Lines | 79 |
| Code Lines | 52 |
| 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""" |
||
| 121 | def _evaluate_samples(self, y_true, y_pred, metrics=[]): |
||
| 122 | y_pred_binary = y_pred > 0.0 |
||
| 123 | # dense versions of sparse arrays, for functions that need them |
||
| 124 | # FIXME: conversion to dense arrays should be avoided |
||
| 125 | y_pred_binary_dense = y_pred_binary.toarray() |
||
| 126 | y_pred_dense = y_pred.toarray() |
||
| 127 | y_true_dense = y_true.toarray() |
||
| 128 | |||
| 129 | # define the available metrics as lazy lambda functions |
||
| 130 | # so we can execute only the ones actually requested |
||
| 131 | all_metrics = { |
||
| 132 | "Precision (doc avg)": lambda: precision_score( |
||
| 133 | y_true, y_pred_binary, average="samples" |
||
| 134 | ), |
||
| 135 | "Recall (doc avg)": lambda: recall_score( |
||
| 136 | y_true, y_pred_binary, average="samples" |
||
| 137 | ), |
||
| 138 | "F1 score (doc avg)": lambda: f1_score( |
||
| 139 | y_true, y_pred_binary, average="samples" |
||
| 140 | ), |
||
| 141 | "Precision (subj avg)": lambda: precision_score( |
||
| 142 | y_true, y_pred_binary, average="macro" |
||
| 143 | ), |
||
| 144 | "Recall (subj avg)": lambda: recall_score( |
||
| 145 | y_true, y_pred_binary, average="macro" |
||
| 146 | ), |
||
| 147 | "F1 score (subj avg)": lambda: f1_score( |
||
| 148 | y_true, y_pred_binary, average="macro" |
||
| 149 | ), |
||
| 150 | "Precision (weighted subj avg)": lambda: precision_score( |
||
| 151 | y_true, y_pred_binary, average="weighted" |
||
| 152 | ), |
||
| 153 | "Recall (weighted subj avg)": lambda: recall_score( |
||
| 154 | y_true, y_pred_binary, average="weighted" |
||
| 155 | ), |
||
| 156 | "F1 score (weighted subj avg)": lambda: f1_score( |
||
| 157 | y_true, y_pred_binary, average="weighted" |
||
| 158 | ), |
||
| 159 | "Precision (microavg)": lambda: precision_score( |
||
| 160 | y_true, y_pred_binary, average="micro" |
||
| 161 | ), |
||
| 162 | "Recall (microavg)": lambda: recall_score( |
||
| 163 | y_true, y_pred_binary, average="micro" |
||
| 164 | ), |
||
| 165 | "F1 score (microavg)": lambda: f1_score( |
||
| 166 | y_true, y_pred_binary, average="micro" |
||
| 167 | ), |
||
| 168 | "F1@5": lambda: f1_score( |
||
| 169 | y_true, filter_pred_top_k(y_pred, 5) > 0.0, average="samples" |
||
| 170 | ), |
||
| 171 | "NDCG": lambda: ndcg_score(y_true_dense, y_pred_dense), |
||
| 172 | "NDCG@5": lambda: ndcg_score(y_true_dense, y_pred_dense, limit=5), |
||
| 173 | "NDCG@10": lambda: ndcg_score(y_true_dense, y_pred_dense, limit=10), |
||
| 174 | "Precision@1": lambda: precision_at_k_score( |
||
| 175 | y_true_dense, y_pred_dense, limit=1 |
||
| 176 | ), |
||
| 177 | "Precision@3": lambda: precision_at_k_score( |
||
| 178 | y_true_dense, y_pred_dense, limit=3 |
||
| 179 | ), |
||
| 180 | "Precision@5": lambda: precision_at_k_score( |
||
| 181 | y_true_dense, y_pred_dense, limit=5 |
||
| 182 | ), |
||
| 183 | "LRAP": lambda: label_ranking_average_precision_score(y_true, y_pred_dense), |
||
| 184 | "True positives": lambda: true_positives(y_true_dense, y_pred_binary_dense), |
||
| 185 | "False positives": lambda: false_positives( |
||
| 186 | y_true_dense, y_pred_binary_dense |
||
| 187 | ), |
||
| 188 | "False negatives": lambda: false_negatives( |
||
| 189 | y_true_dense, y_pred_binary_dense |
||
| 190 | ), |
||
| 191 | } |
||
| 192 | |||
| 193 | if not metrics: |
||
| 194 | metrics = all_metrics.keys() |
||
| 195 | |||
| 196 | with warnings.catch_warnings(): |
||
| 197 | warnings.simplefilter("ignore") |
||
| 198 | |||
| 199 | return {metric: all_metrics[metric]() for metric in metrics} |
||
| 200 | |||
| 273 |