/
evaluate.py
494 lines (426 loc) · 17.1 KB
/
evaluate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
"""
Utility functions to evaluate models on datasets.
"""
import csv
import logging
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
import numpy as np
import deepchem as dc
from deepchem.metrics import Metric
logger = logging.getLogger(__name__)
Score = Dict[str, float]
Metric_Func = Callable[..., Any]
Metrics = Union[Metric, Metric_Func, List[Metric], List[Metric_Func]]
def output_statistics(scores: Score, stats_out: str) -> None:
"""Write computed stats to file.
Statistics are written to specified `stats_out` file.
Parameters
----------
scores: dict
Dictionary mapping names of metrics to scores.
stats_out: str
Name of file to write scores to.
"""
logger.warning("output_statistics is deprecated.")
with open(stats_out, "w") as statsfile:
statsfile.write(str(scores) + "\n")
def output_predictions(dataset: "dc.data.Dataset", y_preds: np.ndarray,
csv_out: str) -> None:
"""Writes predictions to file.
Writes predictions made on `dataset` to a specified file on
disk. `dataset.ids` are used to format predictions. The produce CSV file will have format as follows
| ID | Task1Name | Task2Name |
| ----------- | ------------ | ------------ |
| identifer1 | prediction11 | prediction12 |
| identifer2 | prediction21 | prediction22 |
Parameters
----------
dataset: dc.data.Dataset
Dataset on which predictions have been made.
y_preds: np.ndarray
Predictions to output
csv_out: str
Name of file to write predictions to.
"""
data_ids = dataset.ids
n_tasks = len(dataset.get_task_names())
y_preds = np.reshape(y_preds, (len(y_preds), n_tasks))
assert len(y_preds) == len(data_ids)
with open(csv_out, "w") as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(["ID"] + dataset.get_task_names())
for mol_id, y_pred in zip(data_ids, y_preds):
csvwriter.writerow([mol_id] + list(y_pred))
def _process_metric_input(metrics: Metrics) -> List[Metric]:
"""A private helper method which processes metrics correctly.
Metrics can be input as `dc.metrics.Metric` objects, lists of
`dc.metrics.Metric` objects, or as raw metric functions or lists of
raw metric functions. Metric functions are functions which accept
two arguments `y_true, y_pred` both of which must be `np.ndarray`
objects and return a float value. This functions normalizes these
different types of inputs to type `list[dc.metrics.Metric]` object
for ease of later processing.
Note that raw metric functions which don't have names attached will
simply be named "metric-#" where # is their position in the provided
metric list. For example, "metric-1" or "metric-7"
Parameters
----------
metrics: dc.metrics.Metric/list[dc.metrics.Metric]/metric function/ list[metric function]
Input metrics to process.
Returns
-------
final_metrics: list[dc.metrics.Metric]
Converts all input metrics and outputs a list of
`dc.metrics.Metric` objects.
"""
# Make sure input is a list
if not isinstance(metrics, list):
# FIXME: Incompatible types in assignment
metrics = [metrics] # type: ignore
final_metrics = []
# FIXME: Argument 1 to "enumerate" has incompatible type
for i, metric in enumerate(metrics): # type: ignore
# Ensure that metric is wrapped in a list.
if isinstance(metric, Metric):
final_metrics.append(metric)
# This case checks if input is a function then wraps a
# dc.metrics.Metric object around it
elif callable(metric):
wrap_metric = Metric(metric, name="metric-%d" % (i + 1))
final_metrics.append(wrap_metric)
else:
raise ValueError(
"metrics must be one of metric function / dc.metrics.Metric object /"
"list of dc.metrics.Metric or metric functions.")
return final_metrics
def relative_difference(x: np.ndarray, y: np.ndarray) -> np.ndarray:
"""Compute the relative difference between x and y
The two argument arrays must have the same shape.
Parameters
----------
x: np.ndarray
First input array
y: np.ndarray
Second input array
Returns
-------
z: np.ndarray
We will have `z == np.abs(x-y) / np.abs(max(x, y))`.
"""
z = np.abs(x - y) / np.abs(max(x, y))
return z
class Evaluator(object):
"""Class that evaluates a model on a given dataset.
The evaluator class is used to evaluate a `dc.models.Model` class on
a given `dc.data.Dataset` object. The evaluator is aware of
`dc.trans.Transformer` objects so will automatically undo any
transformations which have been applied.
Examples
--------
Evaluators allow for a model to be evaluated directly on a Metric
for `sklearn`. Let's do a bit of setup constructing our dataset and
model.
>>> import deepchem as dc
>>> import numpy as np
>>> X = np.random.rand(10, 5)
>>> y = np.random.rand(10, 1)
>>> dataset = dc.data.NumpyDataset(X, y)
>>> model = dc.models.MultitaskRegressor(1, 5)
>>> transformers = []
Then you can evaluate this model as follows
>>> import sklearn
>>> evaluator = Evaluator(model, dataset, transformers)
>>> multitask_scores = evaluator.compute_model_performance(
... sklearn.metrics.mean_absolute_error)
Evaluators can also be used with `dc.metrics.Metric` objects as well
in case you want to customize your metric further.
>>> evaluator = Evaluator(model, dataset, transformers)
>>> metric = dc.metrics.Metric(dc.metrics.mae_score)
>>> multitask_scores = evaluator.compute_model_performance(metric)
"""
def __init__(self, model, dataset: "dc.data.Dataset",
transformers: List["dc.trans.Transformer"]):
"""Initialize this evaluator
Parameters
----------
model: Model
Model to evaluate. Note that this must be a regression or
classification model and not a generative model.
dataset: Dataset
Dataset object to evaluate `model` on.
transformers: List[Transformer]
List of `dc.trans.Transformer` objects. These transformations
must have been applied to `dataset` previously. The dataset will
be untransformed for metric evaluation.
"""
self.model = model
self.dataset = dataset
self.output_transformers = [
transformer for transformer in transformers if transformer.transform_y
]
def output_statistics(self, scores: Score, stats_out: str):
""" Write computed stats to file.
Parameters
----------
scores: dict
Dictionary mapping names of metrics to scores.
stats_out: str
Name of file to write scores to.
"""
logger.warning(
"Evaluator.output_statistics is deprecated."
"Please use dc.utils.evaluate.output_statistics instead."
"This method will be removed in a future version of DeepChem.")
with open(stats_out, "w") as statsfile:
statsfile.write(str(scores) + "\n")
def output_predictions(self, y_preds: np.ndarray, csv_out: str):
"""Writes predictions to file.
Writes predictions made on `self.dataset` to a specified file on
disk. `self.dataset.ids` are used to format predictions.
Parameters
----------
y_preds: np.ndarray
Predictions to output
csv_out: str
Name of file to write predictions to.
"""
logger.warning(
"Evaluator.output_predictions is deprecated."
"Please use dc.utils.evaluate.output_predictions instead."
"This method will be removed in a future version of DeepChem.")
data_ids = self.dataset.ids
n_tasks = len(self.dataset.get_task_names())
y_preds = np.reshape(y_preds, (len(y_preds), n_tasks))
assert len(y_preds) == len(data_ids)
with open(csv_out, "w") as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(["ID"] + self.dataset.get_task_names())
for mol_id, y_pred in zip(data_ids, y_preds):
csvwriter.writerow([mol_id] + list(y_pred))
def compute_model_performance(
self,
metrics: Metrics,
csv_out: Optional[str] = None,
stats_out: Optional[str] = None,
per_task_metrics: bool = False,
use_sample_weights: bool = False,
n_classes: int = 2) -> Union[Score, Tuple[Score, Score]]:
"""
Computes statistics of model on test data and saves results to csv.
Parameters
----------
metrics: dc.metrics.Metric/list[dc.metrics.Metric]/function
The set of metrics provided. This class attempts to do some
intelligent handling of input. If a single `dc.metrics.Metric`
object is provided or a list is provided, it will evaluate
`self.model` on these metrics. If a function is provided, it is
assumed to be a metric function that this method will attempt to
wrap in a `dc.metrics.Metric` object. A metric function must
accept two arguments, `y_true, y_pred` both of which are
`np.ndarray` objects and return a floating point score. The
metric function may also accept a keyword argument
`sample_weight` to account for per-sample weights.
csv_out: str, optional (DEPRECATED)
Filename to write CSV of model predictions.
stats_out: str, optional (DEPRECATED)
Filename to write computed statistics.
per_task_metrics: bool, optional
If true, return computed metric for each task on multitask dataset.
use_sample_weights: bool, optional (default False)
If set, use per-sample weights `w`.
n_classes: int, optional (default None)
If specified, will use `n_classes` as the number of unique classes
in `self.dataset`. Note that this argument will be ignored for
regression metrics.
Returns
-------
multitask_scores: dict
Dictionary mapping names of metrics to metric scores.
all_task_scores: dict, optional
If `per_task_metrics == True`, then returns a second dictionary
of scores for each task separately.
"""
if csv_out is not None:
logger.warning(
"csv_out is deprecated as an argument and will be removed in a future version of DeepChem."
"Output is not written to CSV; manually write output instead.")
if stats_out is not None:
logger.warning(
"stats_out is deprecated as an argument and will be removed in a future version of DeepChem."
"Stats output is not written; please manually write output instead")
# Process input metrics
metrics = _process_metric_input(metrics)
y = self.dataset.y
y = dc.trans.undo_transforms(y, self.output_transformers)
w = self.dataset.w
y_pred = self.model.predict(self.dataset, self.output_transformers)
n_tasks = len(self.dataset.get_task_names())
multitask_scores = {}
all_task_scores = {}
# Compute multitask metrics
for metric in metrics:
results = metric.compute_metric(
y,
y_pred,
w,
per_task_metrics=per_task_metrics,
n_tasks=n_tasks,
n_classes=n_classes,
use_sample_weights=use_sample_weights)
if per_task_metrics:
multitask_scores[metric.name], computed_metrics = results
all_task_scores[metric.name] = computed_metrics
else:
multitask_scores[metric.name] = results
if not per_task_metrics:
return multitask_scores
else:
return multitask_scores, all_task_scores
class GeneratorEvaluator(object):
"""Evaluate models on a stream of data.
This class is a partner class to `Evaluator`. Instead of operating
over datasets this class operates over a generator which yields
batches of data to feed into provided model.
Examples
--------
>>> import deepchem as dc
>>> import numpy as np
>>> X = np.random.rand(10, 5)
>>> y = np.random.rand(10, 1)
>>> dataset = dc.data.NumpyDataset(X, y)
>>> model = dc.models.MultitaskRegressor(1, 5)
>>> generator = model.default_generator(dataset, pad_batches=False)
>>> transformers = []
Then you can evaluate this model as follows
>>> import sklearn
>>> evaluator = GeneratorEvaluator(model, generator, transformers)
>>> multitask_scores = evaluator.compute_model_performance(
... sklearn.metrics.mean_absolute_error)
Evaluators can also be used with `dc.metrics.Metric` objects as well
in case you want to customize your metric further. (Note that a given
generator can only be used once so we have to redefine the generator here.)
>>> generator = model.default_generator(dataset, pad_batches=False)
>>> evaluator = GeneratorEvaluator(model, generator, transformers)
>>> metric = dc.metrics.Metric(dc.metrics.mae_score)
>>> multitask_scores = evaluator.compute_model_performance(metric)
"""
def __init__(self,
model,
generator: Iterable[Tuple[Any, Any, Any]],
transformers: List["dc.trans.Transformer"],
labels: Optional[List] = None,
weights: Optional[List] = None):
"""
Parameters
----------
model: Model
Model to evaluate.
generator: generator
Generator which yields batches to feed into the model. For a
KerasModel, it should be a tuple of the form (inputs, labels,
weights). The "correct" way to create this generator is to use
`model.default_generator` as shown in the example above.
transformers: List[Transformer]
Tranformers to "undo" when applied to the models outputs
labels: list of Layer
layers which are keys in the generator to compare to outputs
weights: list of Layer
layers which are keys in the generator for weight matrices
"""
self.model = model
self.generator = generator
self.output_transformers = [
transformer for transformer in transformers if transformer.transform_y
]
self.label_keys = labels
self.weights = weights
if labels is not None and len(labels) != 1:
raise ValueError("GeneratorEvaluator currently only supports one label")
def compute_model_performance(
self,
metrics: Metrics,
per_task_metrics: bool = False,
use_sample_weights: bool = False,
n_classes: int = 2) -> Union[Score, Tuple[Score, Score]]:
"""
Computes statistics of model on test data and saves results to csv.
Parameters
----------
metrics: dc.metrics.Metric/list[dc.metrics.Metric]/function
The set of metrics provided. This class attempts to do some
intelligent handling of input. If a single `dc.metrics.Metric`
object is provided or a list is provided, it will evaluate
`self.model` on these metrics. If a function is provided, it is
assumed to be a metric function that this method will attempt to
wrap in a `dc.metrics.Metric` object. A metric function must
accept two arguments, `y_true, y_pred` both of which are
`np.ndarray` objects and return a floating point score.
per_task_metrics: bool, optional
If true, return computed metric for each task on multitask
dataset.
use_sample_weights: bool, optional (default False)
If set, use per-sample weights `w`.
n_classes: int, optional (default None)
If specified, will assume that all `metrics` are classification
metrics and will use `n_classes` as the number of unique classes
in `self.dataset`.
Returns
-------
multitask_scores: dict
Dictionary mapping names of metrics to metric scores.
all_task_scores: dict, optional
If `per_task_metrics == True`, then returns a second dictionary
of scores for each task separately.
"""
metrics = _process_metric_input(metrics)
# We use y/w to aggregate labels/weights across generator.
y = []
w = []
def generator_closure():
"""This function is used to pull true labels/weights out as we iterate over the generator."""
if self.label_keys is None:
weights = None
# This is a KerasModel.
for batch in self.generator:
# Some datasets have weights
try:
inputs, labels, weights = batch
except ValueError:
try:
inputs, labels, weights, ids = batch
except ValueError:
raise ValueError(
"Generator must yield values of form (input, labels, weights) or (input, labels, weights, ids)"
)
y.append(labels[0])
if len(weights) > 0:
w.append(weights[0])
yield (inputs, labels, weights)
# Process predictions and populate y/w lists
y_pred = self.model.predict_on_generator(generator_closure())
# Combine labels/weights
y = np.concatenate(y, axis=0)
w = np.concatenate(w, axis=0)
multitask_scores = {}
all_task_scores = {}
# Undo data transformations.
y = dc.trans.undo_transforms(y, self.output_transformers)
y_pred = dc.trans.undo_transforms(y_pred, self.output_transformers)
# Compute multitask metrics
for metric in metrics:
results = metric.compute_metric(
y,
y_pred,
w,
per_task_metrics=per_task_metrics,
n_classes=n_classes,
use_sample_weights=use_sample_weights)
if per_task_metrics:
multitask_scores[metric.name], computed_metrics = results
all_task_scores[metric.name] = computed_metrics
else:
multitask_scores[metric.name] = results
if not per_task_metrics:
return multitask_scores
else:
return multitask_scores, all_task_scores