-
Notifications
You must be signed in to change notification settings - Fork 388
/
test_pearson.py
132 lines (107 loc) · 5.17 KB
/
test_pearson.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
# Copyright The Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import namedtuple
from functools import partial
import pytest
import torch
from scipy.stats import pearsonr
from torchmetrics.functional.regression.pearson import pearson_corrcoef
from torchmetrics.regression.pearson import PearsonCorrCoef, _final_aggregation
from unittests import BATCH_SIZE, EXTRA_DIM, NUM_BATCHES
from unittests.helpers import seed_all
from unittests.helpers.testers import MetricTester
seed_all(42)
Input = namedtuple("Input", ["preds", "target"])
_single_target_inputs1 = Input(
preds=torch.rand(NUM_BATCHES, BATCH_SIZE),
target=torch.rand(NUM_BATCHES, BATCH_SIZE),
)
_single_target_inputs2 = Input(
preds=torch.randn(NUM_BATCHES, BATCH_SIZE),
target=torch.randn(NUM_BATCHES, BATCH_SIZE),
)
_multi_target_inputs1 = Input(
preds=torch.rand(NUM_BATCHES, BATCH_SIZE, EXTRA_DIM),
target=torch.rand(NUM_BATCHES, BATCH_SIZE, EXTRA_DIM),
)
_multi_target_inputs2 = Input(
preds=torch.randn(NUM_BATCHES, BATCH_SIZE, EXTRA_DIM),
target=torch.randn(NUM_BATCHES, BATCH_SIZE, EXTRA_DIM),
)
def _scipy_pearson(preds, target):
if preds.ndim == 2:
return [pearsonr(t.numpy(), p.numpy())[0] for t, p in zip(target.T, preds.T)]
return pearsonr(target.numpy(), preds.numpy())[0]
@pytest.mark.parametrize(
"preds, target",
[
(_single_target_inputs1.preds, _single_target_inputs1.target),
(_single_target_inputs2.preds, _single_target_inputs2.target),
(_multi_target_inputs1.preds, _multi_target_inputs1.target),
(_multi_target_inputs2.preds, _multi_target_inputs2.target),
],
)
class TestPearsonCorrCoef(MetricTester):
"""Test class for `PearsonCorrCoef` metric."""
atol = 1e-3
@pytest.mark.parametrize("compute_on_cpu", [True, False])
@pytest.mark.parametrize("ddp", [True, False])
def test_pearson_corrcoef(self, preds, target, compute_on_cpu, ddp):
num_outputs = EXTRA_DIM if preds.ndim == 3 else 1
self.run_class_metric_test(
ddp=ddp,
preds=preds,
target=target,
metric_class=PearsonCorrCoef,
reference_metric=_scipy_pearson,
metric_args={"num_outputs": num_outputs, "compute_on_cpu": compute_on_cpu},
)
def test_pearson_corrcoef_functional(self, preds, target):
self.run_functional_metric_test(
preds=preds, target=target, metric_functional=pearson_corrcoef, reference_metric=_scipy_pearson
)
def test_pearson_corrcoef_differentiability(self, preds, target):
num_outputs = EXTRA_DIM if preds.ndim == 3 else 1
self.run_differentiability_test(
preds=preds,
target=target,
metric_module=partial(PearsonCorrCoef, num_outputs=num_outputs),
metric_functional=pearson_corrcoef,
)
# Pearson half + cpu does not work due to missing support in torch.sqrt
@pytest.mark.xfail(reason="PearsonCorrCoef metric does not support cpu + half precision")
def test_pearson_corrcoef_half_cpu(self, preds, target):
num_outputs = EXTRA_DIM if preds.ndim == 3 else 1
self.run_precision_test_cpu(preds, target, partial(PearsonCorrCoef, num_outputs=num_outputs), pearson_corrcoef)
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires cuda")
def test_pearson_corrcoef_half_gpu(self, preds, target):
num_outputs = EXTRA_DIM if preds.ndim == 3 else 1
self.run_precision_test_gpu(preds, target, partial(PearsonCorrCoef, num_outputs=num_outputs), pearson_corrcoef)
def test_error_on_different_shape():
metric = PearsonCorrCoef(num_outputs=1)
with pytest.raises(RuntimeError, match="Predictions and targets are expected to have the same shape"):
metric(torch.randn(100), torch.randn(50))
metric = PearsonCorrCoef(num_outputs=5)
with pytest.raises(ValueError, match="Expected both predictions and target to be either 1- or 2-.*"):
metric(torch.randn(100, 2, 5), torch.randn(100, 2, 5))
metric = PearsonCorrCoef(num_outputs=2)
with pytest.raises(ValueError, match="Expected argument `num_outputs` to match the second dimension of input.*"):
metric(torch.randn(100, 5), torch.randn(100, 5))
@pytest.mark.parametrize("shapes", [(5,), (1, 5), (2, 5)])
def test_final_aggregation_function(shapes):
"""Test that final aggregation function can take various shapes of input."""
input_fn = lambda: torch.rand(shapes)
output = _final_aggregation(input_fn(), input_fn(), input_fn(), input_fn(), input_fn(), torch.randint(10, shapes))
assert all(isinstance(out, torch.Tensor) for out in output)
assert all(out.ndim == input_fn().ndim - 1 for out in output)