Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add waveform error handling to Relbin (and other models) #4996

Merged
merged 6 commits into from
Dec 18, 2024

Conversation

cdcapano
Copy link
Contributor

The Relative model (and models that inherit from it) do not catch failed waveform errors like the GaussianNoise models do. This adds that functionality to those models.

To avoid repeating code, and to standardize catching these errors, I created a decorator catch_waveform_error that can be added to any method that will call a waveform generator. The decorator will catch any NoWaveformError, FailedWaveformError, and RuntimeError. It will then call the class's _nowaveform_handler method (for the latter two errors this will only happen if the class's ignore_failed_waveforms attribute is set to True). In order for this to work, I had to rename some of the class's _nowaveform_loglr and _nowaveform_logl to _nowaveform_handler.

All models that previously caught these errors have been updated to use the decorator, along with the new addition to the Relative model. I added unit tests to test that the models appropriately catch each error.

One slight issue: in order to catch waveform errors in the Relative model, I also had to catch RuntimeErrors. This is being done at the level of the loglr function. This does mean that if a RuntimeError is raised elsewhere in the loglr function (not by the waverform generator), then the _nowaveform_handler will be triggered. I figured this would be ok since it's usually the waveform generator that raise RuntimeErrors.

"""
loglr = sh_total = hh_total = -numpy.inf
if self.return_sh_hh:
results = (sh_total, hh_total)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it makes sense to catch the case where the return products are different. These should never be called in such a case and you can't be certain you are matching the return product format (e.g. is it a vector or a scalar?). This path should never be used for normal calling of the model (e.g. only reconstruction or interaction with other model which should itself probably decide what to do with its own handler rather than work out how to handle unexpected input.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just trying to match the return signature. What do you suggest doing in this case? Just return -numpy.inf?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I mean it's a bit more complicated since in many cases it would be a vector for sh_total and hh_total. I'm not sure how clear it would be to handle all such cases (nor if that's really the right thing). Indeed, I think just handling the standard case is fine for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if return_sh_hh is set to True, should I just raise the error instead of trying to catch it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that case should just be an uncaught error e.g. it probably should blow up on the user since I think they are doing something wrong to end up in that situation.

@@ -166,8 +169,181 @@ def test_brute_pol_phase_marg(self):
model.update(**self.q1)
self.assertAlmostEqual(self.a2, model.loglr, delta=0.002)


class TestWaveformErrors(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding a unittest!

I think this mostly looks great. There was one minor comment. Otherwise, once you are happy with the PR and assuming all the tests now pass, I think this is good to go.

Copy link
Member

@ahnitz ahnitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments, but otherwise good!

@cdcapano cdcapano enabled auto-merge (squash) December 18, 2024 16:21
@cdcapano cdcapano merged commit 242620a into gwastro:master Dec 18, 2024
28 of 29 checks passed
@cdcapano cdcapano deleted the relbin_wferror_handling branch December 18, 2024 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants