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

Typing: overload methods #336

Closed
wants to merge 6 commits into from
Closed
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
5 changes: 3 additions & 2 deletions torchmetrics/audio/si_sdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

from torch import Tensor, tensor

Expand Down Expand Up @@ -84,7 +84,8 @@ def __init__(
self.add_state("sum_si_sdr", default=tensor(0.0), dist_reduce_fx="sum")
self.add_state("total", default=tensor(0), dist_reduce_fx="sum")

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/audio/si_snr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

from torch import Tensor, tensor

Expand Down Expand Up @@ -79,7 +79,8 @@ def __init__(
self.add_state("sum_si_snr", default=tensor(0.0), dist_reduce_fx="sum")
self.add_state("total", default=tensor(0), dist_reduce_fx="sum")

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/audio/snr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

from torch import Tensor, tensor

Expand Down Expand Up @@ -89,7 +89,8 @@ def __init__(
self.add_state("sum_snr", default=tensor(0.0), dist_reduce_fx="sum")
self.add_state("total", default=tensor(0), dist_reduce_fx="sum")

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

from torch import Tensor, tensor

Expand Down Expand Up @@ -216,7 +216,8 @@ def __init__(
self.mode: DataType = None # type: ignore
self.multiclass = multiclass

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets. See :ref:`references/modules:input types` for more information
on input types.
Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/auc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

from torch import Tensor

Expand Down Expand Up @@ -69,7 +69,8 @@ def __init__(
' For large datasets this may lead to large memory footprint.'
)

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -148,7 +148,8 @@ def __init__(
' For large datasets this may lead to large memory footprint.'
)

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, List, Optional, Union
from typing import Any, List, Optional, Union, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -98,7 +98,8 @@ def __init__(
' For large datasets this may lead to large memory footprint.'
)

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
13 changes: 9 additions & 4 deletions torchmetrics/classification/binned_precision_recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, List, Optional, Tuple, Union
from typing import Any, List, Optional, Tuple, Union, overload
from warnings import warn

import torch
Expand All @@ -22,8 +22,12 @@
from torchmetrics.utilities.data import METRIC_EPS, to_onehot


def _recall_at_precision(precision: Tensor, recall: Tensor, thresholds: Tensor,
min_precision: float) -> Tuple[Tensor, Tensor]:
def _recall_at_precision(
precision: Tensor,
recall: Tensor,
thresholds: Tensor,
min_precision: float,
) -> Tuple[Tensor, Tensor]:
try:
max_recall, _, best_threshold = max((r, p, t) for p, r, t in zip(precision, recall, thresholds)
if p >= min_precision)
Expand Down Expand Up @@ -155,7 +159,8 @@ def __init__(
dist_reduce_fx="sum",
)

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Args
preds: (n_samples, n_classes) tensor
Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/cohen_kappa.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Optional
from typing import Any, Optional, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -100,7 +100,8 @@ def __init__(

self.add_state("confmat", default=torch.zeros(num_classes, num_classes), dist_reduce_fx="sum")

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Optional
from typing import Any, Optional, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -120,7 +120,8 @@ def __init__(
default = torch.zeros(num_classes, 2, 2) if multilabel else torch.zeros(num_classes, num_classes)
self.add_state("confmat", default=default, dist_reduce_fx="sum")

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/hamming_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

import torch
from torch import Tensor, tensor
Expand Down Expand Up @@ -90,7 +90,8 @@ def __init__(
raise ValueError("The `threshold` should lie in the (0,1) interval.")
self.threshold = threshold

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets. See :ref:`references/modules:input types` for more information
on input types.
Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/hinge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional, Union
from typing import Any, Callable, Optional, Union, overload

from torch import Tensor, tensor

Expand Down Expand Up @@ -115,7 +115,8 @@ def __init__(
self.squared = squared
self.multiclass_mode = multiclass_mode

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
measure, total = _hinge_update(preds, target, squared=self.squared, multiclass_mode=self.multiclass_mode)

self.measure = measure + self.measure
Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/kldivergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -91,7 +91,8 @@ def __init__(
self.add_state('measures', [], dist_reduce_fx='cat')
self.add_state('total', torch.zeros(1), dist_reduce_fx='sum')

def update(self, p: Tensor, q: Tensor) -> None: # type: ignore
@overload
def update(self, p: Tensor, q: Tensor) -> None:
measures, total = _kld_update(p, q, self.log_prob)
if self.reduction is None or self.reduction == 'none':
self.measures.append(measures)
Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/matthews_corrcoef.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional
from typing import Any, Callable, Optional, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -95,7 +95,8 @@ def __init__(

self.add_state("confmat", default=torch.zeros(num_classes, num_classes), dist_reduce_fx="sum")

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/precision_recall_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, List, Optional, Tuple, Union
from typing import Any, List, Optional, Tuple, Union, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -109,7 +109,8 @@ def __init__(
' For large datasets this may lead to large memory footprint.'
)

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, List, Optional, Tuple, Union
from typing import Any, Callable, List, Optional, Tuple, Union, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -133,7 +133,8 @@ def __init__(
' For large datasets this may lead to large memory footprint.'
)

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets.

Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/classification/stat_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional, Tuple
from typing import Any, Callable, Optional, Tuple, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -189,7 +189,8 @@ def __init__(
for s in ("tp", "fp", "tn", "fn"):
self.add_state(s, default=default(), dist_reduce_fx=reduce_fn)

def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore
@overload
def update(self, preds: Tensor, target: Tensor) -> None:
"""
Update state with predictions and targets. See :ref:`references/modules:input types` for more information
on input types.
Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/image/fid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, List, Optional, Union
from typing import Any, Callable, List, Optional, Union, overload

import numpy as np
import torch
Expand Down Expand Up @@ -247,7 +247,8 @@ def __init__(
self.add_state("real_features", [], dist_reduce_fx=None)
self.add_state("fake_features", [], dist_reduce_fx=None)

def update(self, imgs: Tensor, real: bool) -> None: # type: ignore
@overload
def update(self, imgs: Tensor, real: bool) -> None:
""" Update the state with extracted features

Args:
Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/image/inception.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, List, Optional, Tuple, Union
from typing import Any, Callable, List, Optional, Tuple, Union, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -145,7 +145,8 @@ def __init__(
self.splits = splits
self.add_state("features", [], dist_reduce_fx=None)

def update(self, imgs: Tensor) -> None: # type: ignore
@overload
def update(self, imgs: Tensor) -> None:
""" Update the state with extracted features

Args:
Expand Down
5 changes: 3 additions & 2 deletions torchmetrics/image/kid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 typing import Any, Callable, Optional, Tuple, Union
from typing import Any, Callable, Optional, Tuple, Union, overload

import torch
from torch import Tensor
Expand Down Expand Up @@ -238,7 +238,8 @@ def __init__(
self.add_state("real_features", [], dist_reduce_fx=None)
self.add_state("fake_features", [], dist_reduce_fx=None)

def update(self, imgs: Tensor, real: bool) -> None: # type: ignore
@overload
def update(self, imgs: Tensor, real: bool) -> None:
""" Update the state with extracted features

Args:
Expand Down
Loading