Repository: ad12/meddlr (language: python)

Issue:
Add support for torchmetrics>=0.7.0
torchmetrics=0.7.0 had some breaking changes. identify these and patch them


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 51c8eabc..f8928f77 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -42,7 +42,13 @@ jobs:
       matrix:
         os: [ubuntu-latest]
         python-version: ["3.7", "3.8"]
+        torchmetrics-version: ["0.6.0", "latest"]
+    env:
+      TORCHMETRICS: ${{ matrix.torchmetrics-version }}
+    
+    name: py${{ matrix.python-version  }}, torchmetrics-${{ matrix.torchmetrics-version }}, ${{ matrix.os }}
 
+    timeout-minutes: 50
     steps:
       - uses: actions/checkout@v2
 
@@ -55,6 +61,12 @@ jobs:
         run: |
           python -m pip install --upgrade pip
           pip install torch torchvision
+          if [[ $TORCHMETRICS == "latest" || -z $TORCHMETRICS ]]; then
+            echo "Installing latest torchmetrics"
+            pip install torchmetrics
+          else
+            pip install torchmetrics==$TORCHMETRICS
+          fi
           make dev
           pip install -e '.[dev]'
 
diff --git a/meddlr/metrics/metric.py b/meddlr/metrics/metric.py
index 80e812de..03a4dbe6 100644
--- a/meddlr/metrics/metric.py
+++ b/meddlr/metrics/metric.py
@@ -65,6 +65,9 @@ def __init__(
         self.add_state("ids", default=[], dist_reduce_fx=lambda x: itertools.chain(*x))
         self.add_state("values", default=[], dist_reduce_fx="cat")
 
+        # This is only to be set and used when filtering kwargs.
+        self._func_signature = None
+
     def func(self, preds, targets, *args, **kwargs) -> torch.Tensor:
         """Computes metrics for each element in the batch.
 
@@ -136,6 +139,9 @@ def _filter_kwargs(self, **kwargs: Any) -> Dict[str, Any]:
             filtered_kwargs.update(aliases)
         else:
             filtered_kwargs = kwargs
+        if self._func_signature is None:
+            self._func_signature = inspect.signature(self.func)
+        filtered_kwargs = _filter_kwargs(self._func_signature, **filtered_kwargs)
         return super()._filter_kwargs(**filtered_kwargs)
 
     def register_update_aliases(self, **kwargs):
@@ -163,3 +169,20 @@ def display_name(self):
         """Name to use for pretty printing and display purposes."""
         name = self.name()
         return "{} ({})".format(name, self.units) if self.units else name
+
+
+def _filter_kwargs(sig, **kwargs: Any) -> Dict[str, Any]:
+    # filter all parameters based on update signature except those of
+    # type VAR_POSITIONAL (*args) and VAR_KEYWORD (**kwargs)
+    _params = (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD)
+    _sign_params = sig.parameters
+    filtered_kwargs = {
+        k: v
+        for k, v in kwargs.items()
+        if (k in _sign_params.keys() and _sign_params[k].kind not in _params)
+    }
+
+    # if no kwargs filtered, return al kwargs as default
+    if not filtered_kwargs:
+        filtered_kwargs = kwargs
+    return filtered_kwargs
diff --git a/requirements.txt b/requirements.txt
index 25c3930d..0bfd3fdc 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -11,5 +11,5 @@ tqdm
 scikit-image>=0.18.2
 pandas
 silx
-torchmetrics>=0.5.1,<0.7.0
+torchmetrics>=0.5.1
 omegaconf
\ No newline at end of file
diff --git a/setup.py b/setup.py
index ab9e915c..0259839b 100644
--- a/setup.py
+++ b/setup.py
@@ -98,7 +98,7 @@ def get_version():
         "silx",
         "tqdm",
         "omegaconf",
-        "torchmetrics>=0.5.1,<0.7.0",
+        "torchmetrics>=0.5.1",
         "iopath",
         "packaging",
     ],


Test patch:
diff --git a/tests/metrics/functional/test_image.py b/tests/metrics/functional/test_image.py
index ad46064f..cd0848e1 100644
--- a/tests/metrics/functional/test_image.py
+++ b/tests/metrics/functional/test_image.py
@@ -2,9 +2,9 @@
 import pytest
 import torch
 import torch.nn.functional as F
+from packaging.version import Version
 from skimage import data
 from skimage.metrics import structural_similarity
-from torchmetrics.functional.regression import ssim as tm_ssim
 
 from meddlr.metrics.functional.image import (
     _pad,
@@ -16,6 +16,12 @@
     ssim,
 )
 from meddlr.metrics.image import compute_mse, compute_nrmse, compute_psnr, compute_ssim
+from meddlr.utils import env
+
+if Version(env.get_package_version("torchmetrics")) < Version("0.7.0"):
+    from torchmetrics.functional.regression import ssim as tm_ssim
+else:
+    from torchmetrics.functional.image import structural_similarity_index_measure as tm_ssim
 
 
 def test_mse_legacy():