Skip to content

Rewrite of polarization ASAD analysis#424

Merged
israelmcmc merged 19 commits intocositools:developfrom
McKelvey-Engineering-CSE:new_polarization
Oct 23, 2025
Merged

Rewrite of polarization ASAD analysis#424
israelmcmc merged 19 commits intocositools:developfrom
McKelvey-Engineering-CSE:new_polarization

Conversation

@jdbuhler
Copy link
Copy Markdown
Contributor

The primary goal of this PR is to rewrite polarization ASAD analysis for better computational performance. It includes the following optimizations:

  1. 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.

  2. 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.

  3. 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.

  • old code: 158.96s
  • new code: 1.164s

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).

@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 23, 2025

Codecov Report

❌ Patch coverage is 87.88927% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.13%. Comparing base (efd5a28) to head (05415be).
⚠️ Report is 20 commits behind head on develop.

Files with missing lines Patch % Lines
cosipy/polarization/polarization_asad.py 86.52% 26 Missing ⚠️
cosipy/polarization/polarization_angle.py 87.87% 4 Missing ⚠️
cosipy/response/functions.py 88.00% 3 Missing ⚠️
cosipy/response/FullDetectorResponse.py 91.30% 2 Missing ⚠️
Files with missing lines Coverage Δ
cosipy/response/PointSourceResponse.py 100.00% <100.00%> (ø)
cosipy/threeml/custom_functions.py 62.70% <100.00%> (+0.82%) ⬆️
cosipy/response/FullDetectorResponse.py 90.85% <91.30%> (+0.02%) ⬆️
cosipy/response/functions.py 91.48% <88.00%> (+0.37%) ⬆️
cosipy/polarization/polarization_angle.py 92.53% <87.87%> (-2.78%) ⬇️
cosipy/polarization/polarization_asad.py 85.91% <86.52%> (+14.43%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@israelmcmc israelmcmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jdbuhler. This is what I have so far. I still need to run it.

Comment thread cosipy/response/PointSourceResponse.py Outdated
Comment thread cosipy/polarization/polarization_angle.py Outdated
@israelmcmc israelmcmc merged commit aa0bc2e into cositools:develop Oct 23, 2025
4 checks passed
@jdbuhler jdbuhler deleted the new_polarization branch October 24, 2025 03:33
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.

2 participants