Skip to content

polygon: Fix bug in astMask when using very large polygons on the sky - #57

Merged
timj merged 10 commits into
masterfrom
56-astmask-and-asttrann-disagree-for-a-skyframe-polygon-whose-interior-is-the-larger-lobe
Jun 19, 2026
Merged

polygon: Fix bug in astMask when using very large polygons on the sky#57
timj merged 10 commits into
masterfrom
56-astmask-and-asttrann-disagree-for-a-skyframe-polygon-whose-interior-is-the-larger-lobe

Conversation

@dsberry

@dsberry dsberry commented Jun 18, 2026

Copy link
Copy Markdown
Member

Fix bug that caused some Polygons defined on the sky to be misinterpreted by astMask. Previously, a Polygon that was supplied such that the unnegated Region represent more than half the sky was effectively inverted by astMask (but not by astTranN). The fix was to reverse the order of the supplied vertices and then set the Invert attribute when constructing the Polygon, if the supplied Polygon represents more than half the sky.

Fixes #56

Fix bug that caused some Polygons defined on the sky to be misinterpreted
by astMask. Previously, a Polygon that was supplied such that the
unnegated Region represent more than half the sky was effectively
inverted by astMask (but not by astTranN). The fix was to reverse the
order of the supplied vertices and then set the Invert attribute when
constructing the Polygon, if the supplied Polygon represents more than
half the sky.
@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.00000% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.66%. Comparing base (16a2b27) to head (d955561).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
ast_tester/testregions.c 61.17% 11 Missing and 22 partials ⚠️
ast_tester/testthreads.c 60.00% 2 Missing and 2 partials ⚠️
src/polygon.c 90.32% 1 Missing and 2 partials ⚠️
src/yamlchan.c 50.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #57      +/-   ##
==========================================
+ Coverage   54.88%   57.66%   +2.77%     
==========================================
  Files         372      371       -1     
  Lines      116841   116921      +80     
  Branches    35679    35706      +27     
==========================================
+ Hits        64126    67420    +3294     
+ Misses      35601    31212    -4389     
- Partials    17114    18289    +1175     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@dsberry

dsberry commented Jun 18, 2026

Copy link
Copy Markdown
Member Author

@timj Not sure what else needs doing on this one. The test program created by claude now passes, as do all the other tests. Can I leave you to merge the PR and close the issue?

@timj

timj commented Jun 18, 2026

Copy link
Copy Markdown
Member

Thank you. I will take a look and merge.

Claude has found a few other bugs as well that I will try to do PRs for.

timj and others added 9 commits June 19, 2026 08:32
Previously tests were failing and nothing noticed.
…t arrays

yamlchan: to be on the safe side mxdim should just be 2 here

(cherry picked from commit e6c169d; the
prologue History entry was adapted to drop the asdf/transform/divide line,
as that change is not present on this branch.)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This is technically UB since it's setting it outside the range of the
object str points to, even though it's never read from and ASAN
complains rightly here, even if it's unlikely to produce a runtime bug.

Had to change this into a do/while loop to retain the correct logic.

(cherry picked from commit eb018e2)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
ctest decides pass/fail purely from the program exit code (there is no
pass/fail regular expression), so a test that always returns 0 reports
success even when its own checks fail. These programs ran off the end of
main() with no return statement (an implicit "return 0" in C99) and so had
been passing silently regardless of their results. Return astOK ? 0 : 1
instead.

testthreads also had this defect but needed a functional rewrite, so it is
handled in a separate commit.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
testregions.c was produced by automatic translation of testregions.f and
carried several defects that its (previously ignored) exit status was now
reporting:

- Every checkX function printed its "X tests failed" message
  unconditionally; the Fortran "if status != OK" guard had been dropped.
- checkBox, checkCircle and checkEllipse each used the wrong FITS header: a
  single unrelated card block had been copied into all three, where the
  Fortran uses a different header per function (checkEllipse needs the
  pole-centred WCS, otherwise the near-pole test points transform to BAD and
  astEllipse rejects them).
- checkdump compared the dump strings with strcmp; the Fortran instead checks
  astOverlap()==5, which tolerates the legitimate textual variation seen
  after a Region dump/restore round trip. Added the overlap fallback.
- Four "frm1 = astFrame(...)" statements had been absorbed into // comments
  and so never executed, leaving frm1 with a stale value.

The astGetRegionDisc check in checkCmpRegion uses tolerances relaxed to 1e-7
(from the Fortran's 1e-9/1e-8): that disc is fitted to a subsampled boundary
mesh, so it varies at the ~1e-8 level with build/optimisation settings. The
Fortran test built against the autoconf AST passes the tight tolerance on the
same machine, confirming this is a build difference rather than a logic error.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
testthreads relied on the Starlink EMS calls errStat/errAnnul to recover the
AST__LCKERR status raised in a worker thread. In the standalone CMake build
those are no-op stubs (errStat just returns astOK ? 0 : 1), so the expected
lock error could never be detected and the test could not pass once it
returned a real exit status.

Rework it so each worker watches its own thread-local status (AST keeps a
separate status per thread) and reports the outcome back through the shared
MyData struct, which the main thread then checks. Also adds the missing
exit-status return.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
testyamlchan reads imaging_wcs.asdf, tanSipWcs.txt and lsst_wcs.txt as
inputs. They exist in the source tree but were never configured into the
build directory, so the test failed at run time with "No such file" (masked
until the program started returning a real exit status). Add the missing
configure_file calls.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Record the exit-status audit, the testregions.c defects fixed against the
Fortran original, and the testthreads EMS rework.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@timj

timj commented Jun 19, 2026

Copy link
Copy Markdown
Member

I added the failing test and then realized that it wasn't failing. Turned out some C tests weren't returning bad status. Fixing that led to some tests failing because of bad Fortran to C conversion. Fixing those led to me needing the overrun fixes from #39 (@embray this will cause a rebase issue for you with the prolog -- sorry).

@timj
timj merged commit 227cfae into master Jun 19, 2026
11 checks passed
@timj
timj deleted the 56-astmask-and-asttrann-disagree-for-a-skyframe-polygon-whose-interior-is-the-larger-lobe branch June 19, 2026 17:10
@embray

embray commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

@timj Not a problem. See also my comment here: #55 (comment) I could maybe also pull that change out to a separate PR but since that one is ready as far as I'm concerned maybe it can stay there.

A few other tests have suspiciously low line coverage. A lot of that is just due to failure conditions that should be excluded from the coverage analysis. Though a few, especially testregions.c, don't seem to be running properly at all. I'm going to take a look at that.

@timj

timj commented Jun 22, 2026

Copy link
Copy Markdown
Member

Didn't this PR turn on the regions tests that were all failing previously?

@dsberry

dsberry commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

You've lost me there I'm afraid. Which region tests? And I'm not sure what you mean by turning them on.

@timj

timj commented Jun 22, 2026

Copy link
Copy Markdown
Member

I am talking about testregions.c (The port of the fortran) which was not checking status properly and so a test was failing early on (because of a porting error) and then no other tests were running with the test returning good status overall.

@timj

timj commented Jun 22, 2026

Copy link
Copy Markdown
Member

I also only just noticed that testpolygonmask.c was added on this PR but was not added to the CMake test build.

@dsberry

dsberry commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

Sorry Tim, I'm not sure what's going on here. As far as I can remember all I did on this PR was to commit the original fix in polygon.c.

@timj

timj commented Jun 23, 2026

Copy link
Copy Markdown
Member

Sorry. My fault

  • I committed testpolygonmask.c by mistake and didn't wire it up to any tests. I had forgotten it was there.
  • I committed the test code to testregions.c
  • I realized that testregions.c was broken because it was passing the test on master when it should be failing.
  • It turned out that status wasn't being returned to shell.
  • Then I realized that status wasn't being handled at all and an earlier region test was failing.
  • This led to me finding other tests that weren't setting status properly.
  • Everything then worked but the testpolygonmask.c was orphaned. I will try to remember to remove it on a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

astMask and astTranN disagree for a SkyFrame Polygon whose interior is the larger lobe

3 participants