Conditions | 26 |
Total Lines | 65 |
Code Lines | 45 |
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""" |
||
110 | def _evaluate_samples(self, y_true, y_pred, metrics=[]): |
||
111 | y_pred_binary = y_pred > 0.0 |
||
112 | y_true_sparse = csr_matrix(y_true) |
||
113 | |||
114 | # define the available metrics as lazy lambda functions |
||
115 | # so we can execute only the ones actually requested |
||
116 | all_metrics = { |
||
117 | "Precision (doc avg)": lambda: precision_score( |
||
118 | y_true_sparse, y_pred_binary, average="samples" |
||
119 | ), |
||
120 | "Recall (doc avg)": lambda: recall_score( |
||
121 | y_true_sparse, y_pred_binary, average="samples" |
||
122 | ), |
||
123 | "F1 score (doc avg)": lambda: f1_score( |
||
124 | y_true_sparse, y_pred_binary, average="samples" |
||
125 | ), |
||
126 | "Precision (subj avg)": lambda: precision_score( |
||
127 | y_true_sparse, y_pred_binary, average="macro" |
||
128 | ), |
||
129 | "Recall (subj avg)": lambda: recall_score( |
||
130 | y_true_sparse, y_pred_binary, average="macro" |
||
131 | ), |
||
132 | "F1 score (subj avg)": lambda: f1_score( |
||
133 | y_true_sparse, y_pred_binary, average="macro" |
||
134 | ), |
||
135 | "Precision (weighted subj avg)": lambda: precision_score( |
||
136 | y_true_sparse, y_pred_binary, average="weighted" |
||
137 | ), |
||
138 | "Recall (weighted subj avg)": lambda: recall_score( |
||
139 | y_true_sparse, y_pred_binary, average="weighted" |
||
140 | ), |
||
141 | "F1 score (weighted subj avg)": lambda: f1_score( |
||
142 | y_true_sparse, y_pred_binary, average="weighted" |
||
143 | ), |
||
144 | "Precision (microavg)": lambda: precision_score( |
||
145 | y_true_sparse, y_pred_binary, average="micro" |
||
146 | ), |
||
147 | "Recall (microavg)": lambda: recall_score( |
||
148 | y_true_sparse, y_pred_binary, average="micro" |
||
149 | ), |
||
150 | "F1 score (microavg)": lambda: f1_score( |
||
151 | y_true_sparse, y_pred_binary, average="micro" |
||
152 | ), |
||
153 | "F1@5": lambda: f1_score( |
||
154 | y_true_sparse, filter_pred_top_k(y_pred, 5) > 0.0, average="samples" |
||
155 | ), |
||
156 | "NDCG": lambda: ndcg_score(y_true, y_pred), |
||
157 | "NDCG@5": lambda: ndcg_score(y_true, y_pred, limit=5), |
||
158 | "NDCG@10": lambda: ndcg_score(y_true, y_pred, limit=10), |
||
159 | "Precision@1": lambda: precision_at_k_score(y_true, y_pred, limit=1), |
||
160 | "Precision@3": lambda: precision_at_k_score(y_true, y_pred, limit=3), |
||
161 | "Precision@5": lambda: precision_at_k_score(y_true, y_pred, limit=5), |
||
162 | "LRAP": lambda: label_ranking_average_precision_score(y_true, y_pred), |
||
163 | "True positives": lambda: true_positives(y_true, y_pred_binary), |
||
164 | "False positives": lambda: false_positives(y_true, y_pred_binary), |
||
165 | "False negatives": lambda: false_negatives(y_true, y_pred_binary), |
||
166 | } |
||
167 | |||
168 | if not metrics: |
||
169 | metrics = all_metrics.keys() |
||
170 | |||
171 | with warnings.catch_warnings(): |
||
172 | warnings.simplefilter("ignore") |
||
173 | |||
174 | return {metric: all_metrics[metric]() for metric in metrics} |
||
175 | |||
252 |