Skip to content

Commit

Permalink
autopep8 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AutoPEP8 authored and actions-user committed May 6, 2020
1 parent a19a2e8 commit 95d4fe6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
28 changes: 14 additions & 14 deletions ignite/base/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from collections import OrderedDict
from collections.abc import Mapping
from typing import Callable, Union, Optional, Any
Expand All @@ -25,49 +24,50 @@ def load_state_dict(self, state_dict: Mapping) -> None:
if len(opts) > 0 and ((not any(opts)) or (all(opts))):
raise ValueError("state_dict should contain only one of '{}' keys".format(self._state_dict_one_of_opt_keys))


class Attachable:
"""
This class contains an internal dictionary `attach_map` associating events with the bound methods plus args/kwargs
to be triggered on those events. This dictionary can be populated by the contructor or by external clients after
construction and before `attach` is called.
This class contains an internal dictionary `attach_map` associating events with the bound methods plus args/kwargs
to be triggered on those events. This dictionary can be populated by the contructor or by external clients after
construction and before `attach` is called.
"""

def __init__(self, attach_map={}):
self.attach_map = dict(attach_map)

def _iter_handlers(self):
for event, (method, _, _) in self.attach_map.items():
yield event, method

def set_handler_mapping(self, event_name: Any, handler: Callable, *args, **kwargs) -> None:
if handler is not None:
self.attach_map[event_name] = (handler, args, kwargs)
else:
del self.attach_map[event_name]

def attach(self, engine: Any) -> None:

for event, method_args in self.attach_map.items():
method, args, kwargs = method_args

if not engine.has_event_handler(method, event):
engine.add_event_handler(event, method, *args, **kwargs)

def detach(self, engine: Any) -> None:
"""
Remove the event handlers from `engine` present in the values of `self.attach_map`.
Args:
engine (Engine): the engine to remove event handlers from
"""
for event, method in self._iter_handlers():
if engine.has_event_handler(method, event):
engine.remove_event_handler(method, event)

def is_attached(self, engine: Any) -> bool:
"""
Returns True if any handler in the values of `self.attach_map` is an event handler of `engine`.
Args:
engine (Engine): the engine checked for present event handlers
"""
Expand Down
2 changes: 1 addition & 1 deletion ignite/handlers/timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Timer(Attachable):

def __init__(self, average: bool = False):
super().__init__()

self._average = average
self._t0 = perf_counter()

Expand Down
11 changes: 5 additions & 6 deletions ignite/metrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
__all__ = ["Metric"]


class Metric(Attachable,metaclass=ABCMeta):
class Metric(Attachable, metaclass=ABCMeta):
"""
Base class for all Metrics.
Expand All @@ -36,7 +36,7 @@ class Metric(Attachable,metaclass=ABCMeta):

def __init__(self, output_transform: Callable = lambda x: x, device: Optional[Union[str, torch.device]] = None):
Attachable.__init__(self)

self._output_transform = output_transform

# Check device if distributed is initialized:
Expand All @@ -52,11 +52,11 @@ def __init__(self, output_transform: Callable = lambda x: x, device: Optional[Un
if device is None:
device = "cuda"
device = torch.device(device)

self._device = device
self._is_reduced = False
self.reset()

self.set_handler_mapping(Events.EPOCH_STARTED, self.started)
self.set_handler_mapping(Events.ITERATION_COMPLETED, self.iteration_completed)

Expand Down Expand Up @@ -177,9 +177,8 @@ def attach(self, engine: Engine, name: Optional[str] = None) -> None:
"""
if name is not None:
engine.add_event_handler(Events.EPOCH_COMPLETED, self.completed, name)

super().attach(engine)


def detach(self, engine: Engine) -> None:
"""
Expand Down
6 changes: 3 additions & 3 deletions ignite/metrics/metrics_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def __init__(self, f: Callable, *args, **kwargs):
self.kwargs = kwargs
self.engine = None
super(MetricsLambda, self).__init__(device="cpu")

self.set_handler_mapping(Events.EPOCH_STARTED, None)
self.set_handler_mapping(Events.ITERATION_COMPLETED, None)

@reinit__is_reduced
def reset(self) -> None:
for i in itertools.chain(self.args, self.kwargs.values()):
Expand Down Expand Up @@ -99,7 +99,7 @@ def _internal_attach(self, engine: Engine) -> None:
# NB : metrics is attached partially
metric.attach(engine)

def attach(self, engine: Engine, name: Optional[str]=None) -> None:
def attach(self, engine: Engine, name: Optional[str] = None) -> None:
# recursively attach all its dependencies (partially)
self._internal_attach(engine)
# attach only handler on EPOCH_COMPLETED
Expand Down

0 comments on commit 95d4fe6

Please sign in to comment.