Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename CorrCoef #710

Merged
merged 3 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Renamed IoU -> Jaccard Index ([#662](https://github.com/PyTorchLightning/metrics/pull/662))


- Renamed correlation coefficient classes: ([#710](https://github.com/PyTorchLightning/metrics/pull/710))
* `MatthewsCorrcoef` -> `MatthewsCorrCoef`
* `PearsonCorrcoef` -> `PearsonCorrCoef`
* `SpearmanCorrcoef` -> `SpearmanCorrCoef`


### Removed

- Removed `embedding_similarity` metric ([#638](https://github.com/PyTorchLightning/metrics/pull/638))
Expand Down
12 changes: 6 additions & 6 deletions docs/source/references/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ KLDivergence
.. autoclass:: torchmetrics.KLDivergence
:noindex:

MatthewsCorrcoef
MatthewsCorrCoef
~~~~~~~~~~~~~~~~

.. autoclass:: torchmetrics.MatthewsCorrcoef
.. autoclass:: torchmetrics.MatthewsCorrCoef
:noindex:

Precision
Expand Down Expand Up @@ -460,10 +460,10 @@ MeanSquaredLogError
:noindex:


PearsonCorrcoef
PearsonCorrCoef
~~~~~~~~~~~~~~~

.. autoclass:: torchmetrics.PearsonCorrcoef
.. autoclass:: torchmetrics.PearsonCorrCoef
:noindex:


Expand All @@ -474,10 +474,10 @@ R2Score
:noindex:


SpearmanCorrcoef
SpearmanCorrCoef
~~~~~~~~~~~~~~~~

.. autoclass:: torchmetrics.SpearmanCorrcoef
.. autoclass:: torchmetrics.SpearmanCorrCoef
:noindex:

SymmetricMeanAbsolutePercentageError
Expand Down
6 changes: 3 additions & 3 deletions tests/classification/test_matthews_corrcoef.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from tests.classification.inputs import _input_multilabel_prob as _input_mlb_prob
from tests.helpers import seed_all
from tests.helpers.testers import NUM_CLASSES, THRESHOLD, MetricTester
from torchmetrics.classification.matthews_corrcoef import MatthewsCorrcoef
from torchmetrics.classification.matthews_corrcoef import MatthewsCorrCoef
from torchmetrics.functional.classification.matthews_corrcoef import matthews_corrcoef

seed_all(42)
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_matthews_corrcoef(self, preds, target, sk_metric, num_classes, ddp, dis
ddp=ddp,
preds=preds,
target=target,
metric_class=MatthewsCorrcoef,
metric_class=MatthewsCorrCoef,
sk_metric=sk_metric,
dist_sync_on_step=dist_sync_on_step,
metric_args={
Expand All @@ -133,7 +133,7 @@ def test_matthews_corrcoef_differentiability(self, preds, target, sk_metric, num
self.run_differentiability_test(
preds=preds,
target=target,
metric_module=MatthewsCorrcoef,
metric_module=MatthewsCorrCoef,
metric_functional=matthews_corrcoef,
metric_args={
"num_classes": num_classes,
Expand Down
12 changes: 6 additions & 6 deletions tests/regression/test_pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from tests.helpers import seed_all
from tests.helpers.testers import BATCH_SIZE, NUM_BATCHES, MetricTester
from torchmetrics.functional.regression.pearson import pearson_corrcoef
from torchmetrics.regression.pearson import PearsonCorrcoef
from torchmetrics.regression.pearson import PearsonCorrCoef

seed_all(42)

Expand Down Expand Up @@ -59,7 +59,7 @@ def test_pearson_corrcoef(self, preds, target, ddp):
ddp=ddp,
preds=preds,
target=target,
metric_class=PearsonCorrcoef,
metric_class=PearsonCorrCoef,
sk_metric=_sk_pearsonr,
dist_sync_on_step=False,
)
Expand All @@ -71,21 +71,21 @@ def test_pearson_corrcoef_functional(self, preds, target):

def test_pearson_corrcoef_differentiability(self, preds, target):
self.run_differentiability_test(
preds=preds, target=target, metric_module=PearsonCorrcoef, metric_functional=pearson_corrcoef
preds=preds, target=target, metric_module=PearsonCorrCoef, 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):
self.run_precision_test_cpu(preds, target, PearsonCorrcoef, pearson_corrcoef)
self.run_precision_test_cpu(preds, target, PearsonCorrCoef, pearson_corrcoef)

@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires cuda")
def test_pearson_corrcoef_half_gpu(self, preds, target):
self.run_precision_test_gpu(preds, target, PearsonCorrcoef, pearson_corrcoef)
self.run_precision_test_gpu(preds, target, PearsonCorrCoef, pearson_corrcoef)


def test_error_on_different_shape():
metric = PearsonCorrcoef()
metric = PearsonCorrCoef()
with pytest.raises(RuntimeError, match="Predictions and targets are expected to have the same shape"):
metric(torch.randn(100), torch.randn(50))

Expand Down
12 changes: 6 additions & 6 deletions tests/regression/test_spearman.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from tests.helpers import seed_all
from tests.helpers.testers import BATCH_SIZE, NUM_BATCHES, MetricTester
from torchmetrics.functional.regression.spearman import _rank_data, spearman_corrcoef
from torchmetrics.regression.spearman import SpearmanCorrcoef
from torchmetrics.regression.spearman import SpearmanCorrCoef

seed_all(42)

Expand Down Expand Up @@ -83,7 +83,7 @@ def test_spearman_corrcoef(self, preds, target, ddp, dist_sync_on_step):
ddp,
preds,
target,
SpearmanCorrcoef,
SpearmanCorrCoef,
_sk_metric,
dist_sync_on_step,
)
Expand All @@ -93,21 +93,21 @@ def test_spearman_corrcoef_functional(self, preds, target):

def test_spearman_corrcoef_differentiability(self, preds, target):
self.run_differentiability_test(
preds=preds, target=target, metric_module=SpearmanCorrcoef, metric_functional=spearman_corrcoef
preds=preds, target=target, metric_module=SpearmanCorrCoef, metric_functional=spearman_corrcoef
)

# Spearman half + cpu does not work due to missing support in torch.arange
@pytest.mark.xfail(reason="Spearman metric does not support cpu + half precision")
def test_spearman_corrcoef_half_cpu(self, preds, target):
self.run_precision_test_cpu(preds, target, SpearmanCorrcoef, spearman_corrcoef)
self.run_precision_test_cpu(preds, target, SpearmanCorrCoef, spearman_corrcoef)

@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires cuda")
def test_spearman_corrcoef_half_gpu(self, preds, target):
self.run_precision_test_gpu(preds, target, SpearmanCorrcoef, spearman_corrcoef)
self.run_precision_test_gpu(preds, target, SpearmanCorrCoef, spearman_corrcoef)


def test_error_on_different_shape():
metric = SpearmanCorrcoef()
metric = SpearmanCorrCoef()
with pytest.raises(RuntimeError, match="Predictions and targets are expected to have the same shape"):
metric(torch.randn(100), torch.randn(50))

Expand Down
6 changes: 6 additions & 0 deletions torchmetrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
JaccardIndex,
KLDivergence,
MatthewsCorrcoef,
MatthewsCorrCoef,
Precision,
PrecisionRecallCurve,
Recall,
Expand All @@ -51,8 +52,10 @@
MeanSquaredError,
MeanSquaredLogError,
PearsonCorrcoef,
PearsonCorrCoef,
R2Score,
SpearmanCorrcoef,
SpearmanCorrCoef,
SymmetricMeanAbsolutePercentageError,
TweedieDevianceScore,
)
Expand Down Expand Up @@ -106,6 +109,7 @@
"JaccardIndex",
"KLDivergence",
"MatthewsCorrcoef",
"MatthewsCorrCoef",
"MaxMetric",
"MeanAbsoluteError",
"MeanAbsolutePercentageError",
Expand All @@ -119,6 +123,7 @@
"MinMetric",
"MultioutputWrapper",
"PearsonCorrcoef",
"PearsonCorrCoef",
"PIT",
"Precision",
"PrecisionRecallCurve",
Expand All @@ -140,6 +145,7 @@
"SI_SNR",
"SNR",
"SpearmanCorrcoef",
"SpearmanCorrCoef",
"Specificity",
"SQuAD",
"SSIM",
Expand Down
2 changes: 1 addition & 1 deletion torchmetrics/classification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from torchmetrics.classification.iou import IoU # noqa: F401
from torchmetrics.classification.jaccard import JaccardIndex # noqa: F401
from torchmetrics.classification.kl_divergence import KLDivergence # noqa: F401
from torchmetrics.classification.matthews_corrcoef import MatthewsCorrcoef # noqa: F401
from torchmetrics.classification.matthews_corrcoef import MatthewsCorrCoef, MatthewsCorrcoef # noqa: F401
from torchmetrics.classification.precision_recall import Precision, Recall # noqa: F401
from torchmetrics.classification.precision_recall_curve import PrecisionRecallCurve # noqa: F401
from torchmetrics.classification.roc import ROC # noqa: F401
Expand Down
36 changes: 33 additions & 3 deletions torchmetrics/classification/matthews_corrcoef.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Callable, Optional
from warnings import warn

import torch
from torch import Tensor
Expand All @@ -23,7 +24,7 @@
from torchmetrics.metric import Metric


class MatthewsCorrcoef(Metric):
class MatthewsCorrCoef(Metric):
r"""
Calculates `Matthews correlation coefficient`_ that measures
the general correlation or quality of a classification. In the binary case it
Expand Down Expand Up @@ -65,10 +66,10 @@ class MatthewsCorrcoef(Metric):
will be used to perform the allgather

Example:
>>> from torchmetrics import MatthewsCorrcoef
>>> from torchmetrics import MatthewsCorrCoef
>>> target = torch.tensor([1, 1, 0, 0])
>>> preds = torch.tensor([0, 1, 0, 0])
>>> matthews_corrcoef = MatthewsCorrcoef(num_classes=2)
>>> matthews_corrcoef = MatthewsCorrCoef(num_classes=2)
>>> matthews_corrcoef(preds, target)
tensor(0.5774)

Expand Down Expand Up @@ -110,3 +111,32 @@ def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
def compute(self) -> Tensor:
"""Computes matthews correlation coefficient."""
return _matthews_corrcoef_compute(self.confmat)


class MatthewsCorrcoef(MatthewsCorrCoef):
"""Calculates `Matthews correlation coefficient`_ that measures the general correlation or quality of a
classification.

Example:
>>> matthews_corrcoef = MatthewsCorrcoef(num_classes=2)
>>> matthews_corrcoef(torch.tensor([0, 1, 0, 0]), torch.tensor([1, 1, 0, 0]))
tensor(0.5774)

.. deprecated:: v0.7
Renamed in favor of :class:`torchmetrics.MatthewsCorrCoef`. Will be removed in v0.8.
"""

def __init__(
self,
num_classes: int,
threshold: float = 0.5,
compute_on_step: bool = True,
dist_sync_on_step: bool = False,
process_group: Optional[Any] = None,
dist_sync_fn: Callable = None,
) -> None:
warn(
"`MatthewsCorrcoef` was renamed to `MatthewsCorrCoef` in v0.7 and it will be removed in v0.8",
DeprecationWarning,
)
super().__init__(num_classes, threshold, compute_on_step, dist_sync_on_step, process_group, dist_sync_fn)
4 changes: 2 additions & 2 deletions torchmetrics/regression/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from torchmetrics.regression.mean_absolute_percentage_error import MeanAbsolutePercentageError # noqa: F401
from torchmetrics.regression.mean_squared_error import MeanSquaredError # noqa: F401
from torchmetrics.regression.mean_squared_log_error import MeanSquaredLogError # noqa: F401
from torchmetrics.regression.pearson import PearsonCorrcoef # noqa: F401
from torchmetrics.regression.pearson import PearsonCorrCoef, PearsonCorrcoef # noqa: F401
from torchmetrics.regression.r2 import R2Score # noqa: F401
from torchmetrics.regression.spearman import SpearmanCorrcoef # noqa: F401
from torchmetrics.regression.spearman import SpearmanCorrCoef, SpearmanCorrcoef # noqa: F401
from torchmetrics.regression.symmetric_mean_absolute_percentage_error import ( # noqa: F401
SymmetricMeanAbsolutePercentageError,
)
Expand Down
35 changes: 32 additions & 3 deletions torchmetrics/regression/pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, List, Optional, Tuple
from warnings import warn

import torch
from torch import Tensor
Expand Down Expand Up @@ -52,7 +53,7 @@ def _final_aggregation(
return var_x, var_y, corr_xy, nb


class PearsonCorrcoef(Metric):
class PearsonCorrCoef(Metric):
r"""
Computes `Pearson Correlation Coefficient`_:

Expand All @@ -77,10 +78,10 @@ class PearsonCorrcoef(Metric):
Specify the process group on which synchronization is called.

Example:
>>> from torchmetrics import PearsonCorrcoef
>>> from torchmetrics import PearsonCorrCoef
>>> target = torch.tensor([3, -0.5, 2, 7])
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> pearson = PearsonCorrcoef()
>>> pearson = PearsonCorrCoef()
>>> pearson(preds, target)
tensor(0.9849)

Expand Down Expand Up @@ -139,3 +140,31 @@ def compute(self) -> Tensor:
n_total = self.n_total

return _pearson_corrcoef_compute(var_x, var_y, corr_xy, n_total)


class PearsonCorrcoef(PearsonCorrCoef):
r"""
Computes `Pearson Correlation Coefficient`_:

Example:
>>> pearson = PearsonCorrcoef()
>>> pearson(torch.tensor([2.5, 0.0, 2, 8]), torch.tensor([3, -0.5, 2, 7]))
tensor(0.9849)

.. deprecated:: v0.7
Renamed in favor of :class:`torchmetrics.PearsonCorrCoef`. Will be removed in v0.8.

"""

def __init__(
self,
compute_on_step: bool = True,
dist_sync_on_step: bool = False,
process_group: Optional[Any] = None,
) -> None:
warn(
"`PearsonCorrcoef` was renamed to `PearsonCorrCoef` in v0.7 and it will be removed in v0.8",
DeprecationWarning,
)

super().__init__(compute_on_step, dist_sync_on_step, process_group)
Loading