Skip to content

Make lal errors more user friendly

James Kennington requested to merge feature-error-classifier into main

Motivation

Errors raised by lal c libs were less-than-illuminating in many cases and often not handled explicitly. For example, the following was common:

try:
    lal.do_something(...)
except RuntimeError as e:
    pass

Error Classification

This adds a new context manager capable of capturing and reclassifying errors specifically. A simple example mirroring the above would be:

from manifold.utilities import error

try:
    with error.classify():
        lal.do_something(...)
except error.LalSimInputDomainError:
    ...
except error.LalSimError:
    ...

Classification Rules

The errors are classified using a sequence of rules, each of which defines whether or not it "applies" to a given exception, e.g.:

e = Exception(...)

class MyRule(error.ClassificationRule):
    def applies_to_exception(e):
        ...

One of the more common cases is using regex to match error messages, which is defined using a error.RegexClassificationRule

Capturing StdErr from c Library

The Classifier object makes use of the library wurlitzer to capture c-level stdout and stderr from lalsuite. This information is logged if present before the newly classified error is raised, but may also be accessed afterwards (assuming the error is caught), for example:

try:
    with error.classify(rules=[error.LAL_RULE_SIM_INPUT_DOMAIN]) as c:
        _ = lalsim.SimInspiralFD(**parameters)
except error.LalSimInputDomainError:
    # Do something with the stderr from c
    do_something(c.stderr)
Edited by James Kennington

Merge request reports