Repository navigation
relativity: return zero, not NaN, for deflection along the line of sight - #1159
Open
Glupozombie wants to merge 1 commit into
Open
Glupozombie wants to merge 1 commit into
Glupozombie wants to merge 1 commit into
Conversation
_compute_deflection has two divisors that can be zero: emag, when the deflector sits exactly at the observer, and fac2 = 1 + qdote, when the target lies on the line from the observer through the deflector. The `flag` that detects the second case is meant to switch the correction off, but it cannot: NumPy performs the division anyway, 0 / 0 is NaN, and multiplying NaN by False gives NaN rather than zero. The result is NaN positions from apparent(), which become NaN times, which raise OverflowError when a caller converts them to a datetime. The failure is not confined to exact alignment. Off the line by 1e-8 au the result is still NaN; by 1e-6 au it is zero. So the whole cone that `flag` was written to cover, roughly an arcsecond wide, produced NaN. Guard both divisors with where(), keeping emag in its original position in the product so the rounding is unchanged. Substituting 1.0 where `flag` is False is safe both ways: when fac2 is zero the numerator is zero too, so the deflection genuinely is zero, and when `flag` is False but fac2 is not, False * x was already zero for finite x and NaN for the infinite x the old expression could also produce. Across 4000 randomized geometries, a third forced into near or exact alignment, the old code produced NaN in 261 and the new code in none, while every geometry where both are finite agrees bitwise. Test: test_deflection_along_the_line_of_sight_is_zero, which fails on master with "RuntimeWarning: invalid value encountered in divide" and passes with the change. The suite goes from 186 to 187 passing with unchanged failure and error counts. This is the expression behind the NaN setting times reported for Saturn on 1980-12-27 in issue skyfielders#1086. That report stopped reproducing in 1.54, when find_risings and find_settings began skipping deflection, but the defect was still reachable from any direct apparent() call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is wrong
relativity._compute_deflectionreturns NaN when the target lies on the line from the observer through a deflector, and when the deflector sits exactly at the observer:flagis meant to switch the correction off in exactly those geometries, but it cannot: NumPy performs the division anyway,0 / 0is NaN, and multiplying NaN byFalsegives NaN, not zero. The comment aboveflagsays the deflection "is set to zero", and today it is not.Measured on current
master(1.55):It is not only the mathematically exact case. Off the line by 1e-8 au, still NaN; by 1e-6 au, zero. So a whole cone around the alignment — the cone
flagwas written to cover, roughly an arcsecond — currently produces NaN instead of zero.NaN there is not a cosmetic problem: it travels through
apparent()into positions and times, and a NaN time then blows up in the caller. That is the chain reported in #1086, where Skyfield ≤ 1.53 returned a NaN setting time for Saturn on 1980-12-27 and.utc_datetime()raised:#1086 itself stopped reproducing in 1.54, because
find_risings()andfind_settings()now call.apparent(no_deflectors)and skip deflection entirely. But the defective expression is still inrelativity.py, and any directapparent()call in that geometry still gets NaN — so this is a live defect, just no longer reachable from the almanac routines.The change
plus a comment recording why the guards are needed.
emagkeeps its position in the product so the rounding is untouched. Substituting1.0whereflagis False is safe in both directions: whenfac2is zero the numerator is zero too, so the correction genuinely is zero; and whenflagis False butfac2is not,False * xwas already zero for any finitex, and was NaN for the infinitexthat the old code could also produce.Verification
No behaviour change where the old code was finite. 4000 randomized observer/target/deflector geometries, a third of them forced into near-alignment or exact anti-alignment:
Every one of the 261 geometries where the old code produced NaN now produces a finite result, and for every geometry where both are finite the two outputs are bitwise identical.
The degenerate cases now return zero, silently. With
warnings.simplefilter('error')— the same posture astest-code.sh, which exportsPYTHONWARNINGS=error— all of[-2,0,0],[-1,0,0],[-2,1e-8,0],[-2,1e-12,0]againstpe=[1,0,0], andpe=[0,0,0], return exactly[0. 0. 0.]and raise no warning.New test:
test_deflection_along_the_line_of_sight_is_zeroinskyfield/tests/test_deflection.py. On unpatchedmasterit fails withRuntimeWarning: invalid value encountered in divideatrelativity.py:162; with the change,test_deflection.pyruns 7 tests and all pass.No regressions. Running the test suite here,
187 passed, 1 failed, 165 errorsagainst a baseline of186 passed, 1 failed, 165 errorson unpatchedmaster— the extra pass is the new test, and the failure and error counts are identical. They are all environmental in this container: the failure istest_stars_tycho2needing to downloadtyc_main_head.dat, and the errors aretest_against_novas(127),test_elementslib(26),test_against_horizons(8) andtest_strs_and_reprs(4), which need the NOVAS library, Horizons access or data files I do not have. Nine further test modules were skipped because they importassay, which is not on PyPI; I could not run them.Related
Measurements for #1086 across versions, scanning Saturn rise and set for all 367 days of 1980 at the equator with
de421.bsp, are in the issue:Jun 15divide by zeroandDec 27invalid valueplus a NaNTruesetting event on 1.50;Dec 27only on 1.53; nothing on 1.54 ormaster. I could not pin the 1.53 NaN to a single instant — a 5-minute grid over both dates is clean even on 1.53 — so it came from one of the sub-second iterations inside the search, which is consistent with the cone described above.