mlcommons / GaNDLF

A generalizable application framework for segmentation, regression, and classification using PyTorch
https://gandlf.org
Apache License 2.0
154 stars 78 forks source link

[FEATURE] How to ensure that `assert` messages are logged? #913

Closed sarthakpati closed 4 days ago

sarthakpati commented 1 month ago

Is your feature request related to a problem? Please describe.

Currently, all messages from the assert conditions flush to the console, which is fine but it does not present a unified logging interface.

Describe the solution you'd like

It would be great if those messages could get flushed to the log file (perhaps in addition to the console).

Describe alternatives you've considered

N.A.

Additional context

Perhaps this could be a solution: https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertLogs

benmalef commented 1 month ago

Assign to me 😁

benmalef commented 1 month ago

@sarthakpati I think that we can use the sys.excepthook and create a customized function that handles the exceptions.

For example

def gandlf_excepthook(exctype, value, tb):
   if exctype is AssertionError:
     logging.exception(value)
  else:
    sys.__excepthook__(exctype, value, tb)

sys.excepthook = gandlf_excepthook

I am not sure if it is a good approach. Any thoughts ??

sarthakpati commented 1 month ago

In this case, do all the assertions need to be of the type AssertionError? What would happen with other types of assertions and the simple assert message?

Any thoughts, @VukW?

benmalef commented 1 month ago

This PR is false. Sorry for that..!