Skip to content

Commit

Permalink
[DLMED] fix flake8 issue
Browse files Browse the repository at this point in the history
Signed-off-by: Nic Ma <nma@nvidia.com>
  • Loading branch information
Nic-Ma committed Jan 25, 2021
1 parent ae89aa3 commit 645daa0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
13 changes: 4 additions & 9 deletions monai/handlers/iteration_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@
from monai.metrics import do_metric_reduction
from monai.utils import MetricReduction, exact_version, optional_import

NotComputableError, _ = optional_import("ignite.exceptions", "0.4.2", exact_version, "NotComputableError")
idist, _ = optional_import("ignite", "0.4.2", exact_version, "distributed")
Metric, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "Metric")
reinit__is_reduced, _ = optional_import("ignite.metrics.metric", "0.4.2", exact_version, "reinit__is_reduced")
if TYPE_CHECKING:
from ignite.engine import Engine
from ignite.metrics import EpochWise, MetricUsage
else:
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")
MetricUsage, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "MetricUsage")
EpochWise, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "EpochWise")


class IterationMetric(Metric): # type: ignore[valid-type, misc] # due to optional_import
Expand Down Expand Up @@ -113,20 +109,19 @@ def compute(self) -> Any:
def _reduce(self, scores) -> Any:
return do_metric_reduction(scores, MetricReduction.MEAN)[0]

def attach(self, engine: Engine, name: str, usage: Union[str, MetricUsage] = EpochWise()) -> None:
def attach(self, engine: Engine, name: str) -> None:
"""
Attaches current metric to provided engine. On the end of engine's run,
`engine.state.metrics` dictionary will contain computed metric's value under provided name.
Args:
engine: the engine to which the metric must be attached.
name: the name of the metric to attach
usage: the usage of the metric.
name: the name of the metric to attach.
"""
super().attach(engine=engine, name=name, usage=usage)
super().attach(engine=engine, name=name)
# FIXME: record engine for communication, ignite will support it in the future version soon
self.engine = engine
self.name = name
if self.save_details and not hasattr(engine.state, "metric_details"):
setattr(engine.state, "metric_details", dict())
engine.state.metric_details = dict()
7 changes: 4 additions & 3 deletions monai/handlers/metrics_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from typing import TYPE_CHECKING, Callable, Optional, Sequence, Union

import numpy as np
import torch

from monai.utils import ensure_tuple, exact_version, optional_import
from monai.utils.module import get_torch_version_tuple
Expand Down Expand Up @@ -101,7 +100,7 @@ def __call__(self, engine: Engine) -> None:
with open(os.path.join(self.save_dir, "metrics.csv"), "w") as f:
for k, v in engine.state.metrics.items():
if k in self.metrics or "*" in self.metrics:
f.write(f"f{k}\t{str(v)}\n")
f.write(f"{k}\t{str(v)}\n")

if (
self.metric_details is not None
Expand Down Expand Up @@ -137,5 +136,7 @@ def __call__(self, engine: Engine) -> None:
f.write("class\tmean\tmedian\tmax\tmin\t90percent\tstd\n")
for i, d in enumerate(v.transpose()):
f.write(
f"{labels[i]}\t{np.nanmean(d):.4f}\t{np.nanmedian(d):.4f}\t{np.nanmax(d):.4f}\t{np.nanmin(d):.4f}\t{np.nanpercentile(d, 10):.4f}\t{np.nanstd(d):.4f}\n"
f"{labels[i]}\t{np.nanmean(d):.4f}\t{np.nanmedian(d):.4f}\t"
f"{np.nanmax(d):.4f}\t{np.nanmin(d):.4f}\t"
f"{np.nanpercentile(d, 10):.4f}\t{np.nanstd(d):.4f}\n"
)
1 change: 0 additions & 1 deletion monai/handlers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from typing import TYPE_CHECKING, Any, Callable

import torch
import torch.distributed as dist

from monai.utils import exact_version, optional_import

Expand Down
2 changes: 1 addition & 1 deletion tests/test_handler_mean_dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import unittest

import torch
from ignite.engine import Engine, Events
from ignite.engine import Engine
from parameterized import parameterized

from monai.handlers import MeanDice
Expand Down

0 comments on commit 645daa0

Please sign in to comment.