| 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""" |
||
| 89 | def _evaluate_samples(self, y_true, y_pred, metrics=[]): |
||
| 90 | y_pred_binary = y_pred > 0.0 |
||
| 91 | |||
| 92 | # define the available metrics as lazy lambda functions |
||
| 93 | # so we can execute only the ones actually requested |
||
| 94 | all_metrics = { |
||
| 95 | "Precision (doc avg)": lambda: precision_score( |
||
| 96 | y_true, y_pred_binary, average="samples" |
||
| 97 | ), |
||
| 98 | "Recall (doc avg)": lambda: recall_score( |
||
| 99 | y_true, y_pred_binary, average="samples" |
||
| 100 | ), |
||
| 101 | "F1 score (doc avg)": lambda: f1_score( |
||
| 102 | y_true, y_pred_binary, average="samples" |
||
| 103 | ), |
||
| 104 | "Precision (subj avg)": lambda: precision_score( |
||
| 105 | y_true, y_pred_binary, average="macro" |
||
| 106 | ), |
||
| 107 | "Recall (subj avg)": lambda: recall_score( |
||
| 108 | y_true, y_pred_binary, average="macro" |
||
| 109 | ), |
||
| 110 | "F1 score (subj avg)": lambda: f1_score( |
||
| 111 | y_true, y_pred_binary, average="macro" |
||
| 112 | ), |
||
| 113 | "Precision (weighted subj avg)": lambda: precision_score( |
||
| 114 | y_true, y_pred_binary, average="weighted" |
||
| 115 | ), |
||
| 116 | "Recall (weighted subj avg)": lambda: recall_score( |
||
| 117 | y_true, y_pred_binary, average="weighted" |
||
| 118 | ), |
||
| 119 | "F1 score (weighted subj avg)": lambda: f1_score( |
||
| 120 | y_true, y_pred_binary, average="weighted" |
||
| 121 | ), |
||
| 122 | "Precision (microavg)": lambda: precision_score( |
||
| 123 | y_true, y_pred_binary, average="micro" |
||
| 124 | ), |
||
| 125 | "Recall (microavg)": lambda: recall_score( |
||
| 126 | y_true, y_pred_binary, average="micro" |
||
| 127 | ), |
||
| 128 | "F1 score (microavg)": lambda: f1_score( |
||
| 129 | y_true, y_pred_binary, average="micro" |
||
| 130 | ), |
||
| 131 | "F1@5": lambda: f1_score( |
||
| 132 | y_true, filter_suggestion(y_pred, 5) > 0.0, average="samples" |
||
| 133 | ), |
||
| 134 | "NDCG": lambda: ndcg_score(y_true, y_pred), |
||
| 135 | "NDCG@5": lambda: ndcg_score(y_true, y_pred, limit=5), |
||
| 136 | "NDCG@10": lambda: ndcg_score(y_true, y_pred, limit=10), |
||
| 137 | "Precision@1": lambda: precision_score( |
||
| 138 | y_true, filter_suggestion(y_pred, 1) > 0.0, average="samples" |
||
| 139 | ), |
||
| 140 | "Precision@3": lambda: precision_score( |
||
| 141 | y_true, filter_suggestion(y_pred, 3) > 0.0, average="samples" |
||
| 142 | ), |
||
| 143 | "Precision@5": lambda: precision_score( |
||
| 144 | y_true, filter_suggestion(y_pred, 5) > 0.0, average="samples" |
||
| 145 | ), |
||
| 146 | "True positives": lambda: true_positives(y_true, y_pred_binary), |
||
| 147 | "False positives": lambda: false_positives(y_true, y_pred_binary), |
||
| 148 | "False negatives": lambda: false_negatives(y_true, y_pred_binary), |
||
| 149 | } |
||
| 150 | |||
| 151 | if not metrics: |
||
| 152 | metrics = all_metrics.keys() |
||
| 153 | |||
| 154 | with warnings.catch_warnings(): |
||
| 155 | warnings.simplefilter("ignore") |
||
| 156 | |||
| 157 | return {metric: all_metrics[metric]() for metric in metrics} |
||
| 158 | |||
| 227 |