Skip to content

Commit

Permalink
style: remove unused imports left in #1725 (#1817)
Browse files Browse the repository at this point in the history
* style: remove unused imports left in #1725

* check sklearn exist when initializing contrib metrics

Co-authored-by: Sylvain Desroziers <[email protected]>
Co-authored-by: vfdev <[email protected]>
  • Loading branch information
3 people authored Mar 21, 2021
1 parent 2d37596 commit 38c88ae
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 24 deletions.
2 changes: 0 additions & 2 deletions ignite/contrib/handlers/clearml_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,12 +746,10 @@ def post_callback(self, action: str, model_info: Any) -> Any:

def __call__(self, checkpoint: Mapping, filename: str, metadata: Optional[Mapping] = None) -> None:
try:
from clearml import Model
from clearml.binding.frameworks import WeightsFileHandler
except ImportError:
try:
# Backwards-compatibility for legacy Trains SDK
from trains import Model
from trains.binding.frameworks import WeightsFileHandler
except ImportError:
raise RuntimeError(
Expand Down
4 changes: 1 addition & 3 deletions ignite/contrib/handlers/visdom_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def __init__(
# requires also `futures` to be installed.
# Let's check anyway if we can import it.
try:
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor
except ImportError:
raise RuntimeError(
"This contrib module requires concurrent.futures module"
Expand Down Expand Up @@ -191,8 +191,6 @@ def __init__(

self.executor = _DummyExecutor() # type: Union[_DummyExecutor, "ThreadPoolExecutor"]
if num_workers > 0:
from concurrent.futures import ThreadPoolExecutor

self.executor = ThreadPoolExecutor(max_workers=num_workers)

def _save(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions ignite/contrib/metrics/average_precision.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Optional, Union
from typing import Callable, Union

import torch

Expand Down Expand Up @@ -51,7 +51,7 @@ def __init__(
):

try:
from sklearn.metrics import average_precision_score
from sklearn.metrics import average_precision_score # noqa: F401
except ImportError:
raise RuntimeError("This contrib module requires sklearn to be installed.")

Expand Down
3 changes: 1 addition & 2 deletions ignite/contrib/metrics/cohen_kappa.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ def __init__(
):

try:
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import cohen_kappa_score # noqa: F401
except ImportError:
raise RuntimeError("This contrib module requires sklearn to be installed.")

if weights not in (None, "linear", "quadratic"):
raise ValueError("Kappa Weighting type must be None or linear or quadratic.")

Expand Down
4 changes: 1 addition & 3 deletions ignite/contrib/metrics/gpu_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GpuInfo(Metric):

def __init__(self) -> None:
try:
import pynvml
from pynvml.smi import nvidia_smi
except ImportError:
raise RuntimeError(
"This contrib module requires pynvml to be installed. "
Expand All @@ -46,8 +46,6 @@ def __init__(self) -> None:
if not torch.cuda.is_available():
raise RuntimeError("This contrib module requires available GPU")

from pynvml.smi import nvidia_smi

# Let it fail if no libnvidia drivers or NMVL library found
self.nvsmi = nvidia_smi.getInstance()
super(GpuInfo, self).__init__()
Expand Down
2 changes: 1 addition & 1 deletion ignite/contrib/metrics/regression/_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import abstractmethod
from typing import Callable, Tuple
from typing import Tuple

import torch

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Union
from typing import Tuple

import torch

Expand Down
13 changes: 8 additions & 5 deletions ignite/contrib/metrics/roc_auc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ def roc_auc_compute_fn(y_preds: torch.Tensor, y_targets: torch.Tensor) -> float:


def roc_auc_curve_compute_fn(y_preds: torch.Tensor, y_targets: torch.Tensor) -> Tuple[Any, Any, Any]:
try:
from sklearn.metrics import roc_curve
except ImportError:
raise RuntimeError("This contrib module requires sklearn to be installed.")
from sklearn.metrics import roc_curve

y_true = y_targets.numpy()
y_pred = y_preds.numpy()
Expand Down Expand Up @@ -63,7 +60,7 @@ def __init__(
):

try:
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_auc_score # noqa: F401
except ImportError:
raise RuntimeError("This contrib module requires sklearn to be installed.")

Expand Down Expand Up @@ -103,6 +100,12 @@ def activated_output_transform(output):
"""

def __init__(self, output_transform: Callable = lambda x: x, check_compute_fn: bool = False) -> None:

try:
from sklearn.metrics import roc_curve # noqa: F401
except ImportError:
raise RuntimeError("This contrib module requires sklearn to be installed.")

super(RocCurve, self).__init__(
roc_auc_curve_compute_fn, output_transform=output_transform, check_compute_fn=check_compute_fn
)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ profile=black

[flake8]
max-line-length = 120
ignore = E722,F401,E203,E231,F841,W503,F403,E402
ignore = E722,E203,E231,F841,W503,F403,E402
per-file-ignores = __init__.py: F401

[tool:pytest]
Expand Down
1 change: 0 additions & 1 deletion tests/ignite/contrib/metrics/test_average_precision.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

import numpy as np
import pytest
import torch
from sklearn.metrics import average_precision_score
Expand Down
1 change: 0 additions & 1 deletion tests/ignite/contrib/metrics/test_cohen_kappa.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

import numpy as np
import pytest
import torch
from sklearn.metrics import cohen_kappa_score
Expand Down
1 change: 0 additions & 1 deletion tests/ignite/contrib/metrics/test_roc_auc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

import numpy as np
import pytest
import torch
from sklearn.metrics import roc_auc_score
Expand Down
2 changes: 1 addition & 1 deletion tests/ignite/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,4 @@ def func_with_everything():


def test_smoke__utils():
from ignite._utils import apply_to_tensor, apply_to_type, convert_tensor, to_onehot
from ignite._utils import apply_to_tensor, apply_to_type, convert_tensor, to_onehot # noqa: F401

0 comments on commit 38c88ae

Please sign in to comment.