Skip to content

Commit

Permalink
Add __repr__ and __str__ to Metrics baseclass (Project-MONAI#7487)
Browse files Browse the repository at this point in the history
### Description

When training a model using MONAI metrics for experiment tracking, I
tend to log which metrics I am using. Unfortunately, just sending the
metrics objects to Tensorboard will result in a list like
[CustomMetric1, CustomMetric2, <monai.metrics.Metric ...>, etc.]

Adding `__repr__` and `__str__` methods to the base class will solve
this small annoyance. The current implementation will only return the
class name, but if a certain metric would wish to report more data for
its `__repr__` string, this can be easily overridden in any subclass.

### 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.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] 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: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Signed-off-by: Mathijs de Boer <m.deboer-41@umcutrecht.nl>
Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Co-authored-by: Mathijs de Boer <m.deboer-41@umcutrecht.nl>
Signed-off-by: Juan Pablo de la Cruz Gutiérrez <juampatronics@gmail.com>
  • Loading branch information
3 people authored and juampatronics committed Mar 25, 2024
1 parent b0c96d8 commit 771af49
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions monai/metrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
"""
raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.")

def __str__(self):
return self.__class__.__name__


class IterationMetric(Metric):
"""
Expand Down

0 comments on commit 771af49

Please sign in to comment.