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