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

RelativeDifferencePrior gives wrong results due to use of abs() #1409

Closed
KrisThielemans opened this issue Apr 26, 2024 · 4 comments · Fixed by #1410
Closed

RelativeDifferencePrior gives wrong results due to use of abs() #1409

KrisThielemans opened this issue Apr 26, 2024 · 4 comments · Fixed by #1410
Labels
Milestone

Comments

@KrisThielemans
Copy link
Collaborator

We use the abs() function, e.g.

* abs(current_image_estimate[z][y][x] - current_image_estimate[z + dz][y + dy][x + dx])

It turns out that with g++-8 (and presumably any version), this falls back to the C version of abs, which will convert its argument to an integer type.

To fix this, we have to explicitly use std::abs (which has an overload for float and double in <cmath>).

Impact` of this is rather dramatic. When running test_priors.cxx, I get the following for the gradient of the RDP potential:

RDP old 0.590537 RDP new 0.287209
RDP old 0.893955 RDP new 0.326926
RDP old 0.319332 RDP new 0.20883
RDP old 0.00598219 RDP new 0.00592888
RDP old 0.373463 RDP new 0.229309
RDP old 0.980504 RDP new 0.332455
RDP old -0.00389029 RDP new -0.00386772
RDP old 0.348276 RDP new 0.220149
RDP old 0 RDP new 0
RDP old 0.153586 RDP new 0.123653
RDP old 0.893202 RDP new 0.326867
RDP old 0.493659 RDP new 0.265453
RDP old 0.773317 RDP new 0.315314
RDP old 0.18338 RDP new 0.141967
RDP old -0.159962 RDP new -0.129018
RDP old 0.731704 RDP new 0.31015
RDP old 0.973774 RDP new 0.332113

This is despite the tests all working, including a test on numerical vs analytic gradient which is surprising. Possibly this is because we use abs() consistently...

@KrisThielemans KrisThielemans added this to the v6.1 milestone Apr 26, 2024
@KrisThielemans
Copy link
Collaborator Author

@Imraj-Singh, you faithfully reproduce this bug in #1408

@KrisThielemans
Copy link
Collaborator Author

KrisThielemans commented Apr 26, 2024

Plots for derivative of the RDP
image
image

@KrisThielemans
Copy link
Collaborator Author

Looks like this was introduced in the PR for the RDP #335. @robbietuk, making you aware.

It is possible that this bug only transpired once we moved to C++-11, or certain compiler versions. I don't know.

@KrisThielemans
Copy link
Collaborator Author

KrisThielemans commented Apr 26, 2024

Other places that use abs for floats/doubles that could be affected:

Library: (worrying!)

Utilities:

Relatively harmless as error checking:

Test code:

Commented out code, hence harmless (but confusing):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant