Skip to content

Commit

Permalink
Add CallableEventWithFilter support to GarbageCollector (#6663)
Browse files Browse the repository at this point in the history
Fixes # .

### Description

Adds support for CallableEventWithFilter which is useful for not calling
the GarbageCollector every iteration but e.g. every 10 iterations.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [x] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Matthias Hadlich <[email protected]>
  • Loading branch information
matt3o authored Jun 28, 2023
1 parent 60620e4 commit 59470e4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions monai/handlers/garbage_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

if TYPE_CHECKING:
from ignite.engine import Engine, Events
from ignite.engine.events import CallableEventWithFilter
else:
CallableEventWithFilter, _ = optional_import(
"ignite.engine.events", IgniteInfo.OPT_IMPORT_VERSION, min_version, "CallableEventWithFilter"
)
Engine, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Engine")
Events, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Events")

Expand All @@ -43,9 +47,9 @@ class GarbageCollector:
- 0 (NOTSET)
"""

def __init__(self, trigger_event: str = "epoch", log_level: int = 10):
self.trigger_event: Events
if isinstance(trigger_event, Events):
def __init__(self, trigger_event: str | Events | CallableEventWithFilter = "epoch", log_level: int = 10):
self.trigger_event: Events | CallableEventWithFilter
if isinstance(trigger_event, Events) or isinstance(trigger_event, CallableEventWithFilter):
self.trigger_event = trigger_event
elif trigger_event.lower() == "epoch":
self.trigger_event = Events.EPOCH_COMPLETED
Expand Down

0 comments on commit 59470e4

Please sign in to comment.