Rewrite of polarization ASAD analysis#424
Merged
israelmcmc merged 19 commits intocositools:developfrom Oct 23, 2025
Merged
Conversation
added 15 commits
September 16, 2025 10:55
accidental changes to other code
* add test cases for vectorized polarization_angle functions
* change PolarizationASAD API for binned input to take the Histogram, not the BinnedData object that contains it
* mark FIXME in case we can stop truncating the scaled bg
threeML internal dependency breakage bug
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
israelmcmc
reviewed
Oct 23, 2025
Collaborator
israelmcmc
left a comment
There was a problem hiding this comment.
Thanks @jdbuhler. This is what I have so far. I still need to run it.
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.
The primary goal of this PR is to rewrite polarization ASAD analysis for better computational performance. It includes the following optimizations:
The existing ASAD calculations from unbinned data, binned data, or a response plus a scatt_map or dwell map have been vectorized. Methods of PolarizationAngle used by these operations have also been vectorized.
The computation of ASADs has been reorganized to remove redundant operations. In particular, computing the polarized and unpolarized ASADs from the response no longer repeatedly computes the same set of scattering directions.
The source spectrum, which does not change during the computation, is integrated once before constructing the ASADs, rather than repeatedly each time the integrated flux is needed. This change required extending PointSourceResponse.get_expectation() to take an optional pre-integrated flux array, rather than always doing the spectrum integration itself.
A further optimization was performed on the Band_Eflux spectral model used with polarization analysis in the units tests and the tutorial notebook. Evaluating this model at a given energy x and set of spectral parameters numerically integrates a Band spectrum between specified limits [a,b] to compute a normalizing constant. Evaluation for each new x recomputes this integral, even if the spectral parameters have not changed since the previous evaluation. We now cache the spectral parameters and integral in the model so that successive evaluations with the same parameters can simply reuse the cached integral. This change is transparent to the user and should not impact the model's use in, e.g., spectral fitting.
In the course of vectorizing the PolarizationAngle code, it was necessary to ensure that the source direction was always a scalar rather than a vector SkyCoord. This prompted a change to the point source response calculation in FullDetectorResponse, which assumes that the source direction is a vector SkyCoord. Passing a vector with multiple source directions was done as an optimization in the past, but it is no longer effective after a recent rewrite of FullDetectorResponse, nor is it even possible in the physically relevant case that earth occultation is considered in the scatt_map. This patch therefore requires that get_point_source_response() takes a scalar source, which created an opportunity to simplify its code. The DetectorResponse.ipynb notebook was updated to remove references to vectorized sources.
There are also some other minor optimizations in the response directory.
One optimization that was not done was modifying the dwell-time map construction to use a preloaded FullDetectorResponse rather than reopening the response file each time. This change is part of Israel's pending PR #369.
PERFORMANCE IMPACT
For the ASAD analysis on unbinned inputs in the galactic frame that is done in the tutorial notebook, the new code runs roughly 136x faster than the version on the development branch.
More than half the time spent in the new code is in the single required integration of the Band_Eflux spectrum.
There is just one change that meaningfully impacts the result of the analysis. The scaled background ASAD was previously truncated to integer values rather than being left as floating-point after scaling. Removing this truncation (the reason for which is unclear) changes the predicted PA, PF, and MDP in the tutorial notebook by at most one part in a thousand versus the old code. Without this change, the old and new code agree to within floating-point precision (around one part in 10^7).
TEST SUITE
The test suite has been extended to exercise the vectorized PolarizationAngle operations and to check the output of the ASAD computation, including for the case of binned data (which was previously not covered and was in fact broken).