Skip to content

Unify ASAD and Stokes method polarization code - #620

Open
jdbuhler wants to merge 43 commits into
cositools:developfrom
McKelvey-Engineering-CSE:faster_stokes
Open

Unify ASAD and Stokes method polarization code#620
jdbuhler wants to merge 43 commits into
cositools:developfrom
McKelvey-Engineering-CSE:faster_stokes

Conversation

@jdbuhler

@jdbuhler jdbuhler commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

The ASAD and Stokes method code to determine polarization angle and fraction share a great deal of functionality, though it is implemented differently in each method. This patch extracts the common functionality from each method into a base class, PolarizationFitting, and derives PolarizationASAD and PolarizationStokes from that. Besides avoiding a great deal of code duplication, this change lets us use the fast ASAD calculations from the ASAD method in the Stokes method as well, which reduces the latter's overall running time to a fraction of a second for GRB-sized inputs and 2-3 minutes to analyze 3 months of simulated events + background from CygX1.

The common code is mainly in computing scattering angles from data/background and in computing ASADs as input to the mu100 and mdp99 calculations. The actual fitting code is, of course, quite different and is implemented separately in each subclass. The other key differences are that the Stokes code supports calculation in the absence of background data, which the ASAD code does not, and that the ASAD method supports binned input data, which the Stokes code does not. Originally, the Stokes code used a different implementation of the mu100 calculation that produced slightly different answers, but after consulting with @eneights and @nmik , I used the ASAD code's implementation for both methods. (This can easily be changed if desired.)

The Stokes fitting code has been updated to follow a newer reference (Kislat and Spooner 2023), with errata corrections as directed by @nmik, and to compute background-subtracted normalized Q and U values. Thanks to @mabp1992 for independently implementing Stokes fitting; reconciling that code with this code smoked out several issues on both sides.

The ASAD and Stokes notebooks have been updated to the API of the new implementation, which involved renaming a couple of properties and extracting some plotting code for Stokes into its own function. The notebook versions on this branch show the output from each method under the unified code.

To improve test coverage, the same two unbinned test cases used for the ASAD method have been added to the Stokes method test suite, in addition to the existing no-background test. These tests raise numerical errors from numpy due to square roots of negative numbers, but the current belief is that these errors stem from the very coarse response binning (nside 1) in the test cases, rather than any actual error.

jdbuhler and others added 15 commits May 29, 2026 01:45
* test whether we can merge the mu100 calculations
* turn the actual Stokes fitting code into a fit() function
  analogous to that in the ASAD method
* convert gratuitous printing to logging
* simplify some code in the ASAD method after comparing it
  to the Stokes method
* track down sqrt warning in test case; this seems to be
  an error in implementing error estimates in Stokes method?
* energy trimming applies to all properties of events, not just
  the Phi/PsiChi (in particular, to time)
* enable plotting in tests to exercise that code
* add explicit plotting code for Stokes parameters
* tweak tutorials to match changes in API
* drop some parameter printing from the Stokes tutorial
  where the data is not readily available
sensible results
* reformat existing no-bg Stokes test to match other tests
@jdbuhler
jdbuhler marked this pull request as draft June 4, 2026 16:01
@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.42363% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.48%. Comparing base (48ce96e) to head (2794c0c).

Files with missing lines Patch % Lines
...osipy/polarization_fitting/polarization_fitting.py 98.76% 2 Missing ⚠️
Files with missing lines Coverage Δ
cosipy/polarization_fitting/__init__.py 100.00% <100.00%> (ø)
cosipy/polarization_fitting/polarization_asad.py 98.61% <100.00%> (+13.23%) ⬆️
cosipy/polarization_fitting/polarization_stokes.py 100.00% <100.00%> (+49.78%) ⬆️
cosipy/response/FullDetectorResponse.py 91.57% <100.00%> (+0.19%) ⬆️
...osipy/polarization_fitting/polarization_fitting.py 98.76% <98.76%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jdbuhler jdbuhler changed the title Faster stokes Unify ASAD and Stokes method polarization code Jun 4, 2026
@eneights
eneights requested a review from nmik June 4, 2026 17:07
jdbuhler added 10 commits June 5, 2026 07:21
…nally

by response-based simulated ASAD generation
  spacecraft-frame fit convention (and fix a typo it uncovered)
* convert base class members exported to subclasses into properties
…hanged

explicitly in latest cosipy), and document strategies used to obtain
scattering angles
small adjustments to uncertainty estimates used only for plotting.
I think this now matches the equations in the paper, given that
the underlying Stokes parameters are 2x larger in the code vs
the paper.
* update ASAD method test vectors for recent changes
@jdbuhler
jdbuhler marked this pull request as ready for review June 24, 2026 13:47
@jdbuhler

Copy link
Copy Markdown
Contributor Author

Taking this out of draft status... I think it should be reviewed now, even if we decide some changes are needed.

@jdbuhler
jdbuhler requested a review from eneights June 24, 2026 13:49
@israelmcmc

Copy link
Copy Markdown
Collaborator

Thanks @jdbuhler! Let me find a reviewer.

@eneights

Copy link
Copy Markdown
Contributor

The ASAD code looks good to me and the notebook outputs the same results as before. The Stokes method notebook now has slightly different results, so @nmik should take a look to make sure the changes are okay

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.

4 participants