Skip to content

Simplify source classes - #285

Draft
jdbuhler wants to merge 7 commits into
threeML:devfrom
jdbuhler:source_accel
Draft

Simplify source classes#285
jdbuhler wants to merge 7 commits into
threeML:devfrom
jdbuhler:source_accel

Conversation

@jdbuhler

@jdbuhler jdbuhler commented Aug 16, 2026

Copy link
Copy Markdown

This patch cleans up the classes in the sources subdirectory. Changes include

  • don't use Numba for the sum in point_source.py -- it was just wrapping a Numpy vectorized sum call, so confers no performance advantage
  • simplify call / get_flux methods to sum over an iterator, rather than trying to convert to a list or array first. Rather than call Python sum(), which instantiates a 0 value and adds to it, we grab the first result from the iterator directly, then do in-place adds of the remaining iterator values. This pattern has proved between 3 and 10x faster for our use cases vs using sum(), should never be slower, and should be safe for both bare arrays and Quantity arrays with compatible units.
  • for point_source integration, use SciPy's integrate.quad_vec() to do integrals for all values of x in one call, rather than separately for each value
  • don't convert iterators to lists unnecessarily where the iterable can be used directly, and update type signatures to match

One point_source integration test was updated to allow the result to differ by a very small amount (1e-15) from the analytically expected 1 as a result of using quad_vec().

It is my contention that the rewritten code is not slower than the original, more complex version. But I would appreciate suggestions on appropriate test cases to convincingly verify this hypothesis.

One data point so far: while testing cosipy with the updated ExtendedSource class using a model (Gassian_on_sphere) with a 2D spatial_shape with len(brightness) = 10, we found that call runs at about the same speed for 1000 spatial points and 3x faster for 10000 points.

PreisTo and others added 3 commits July 31, 2026 20:17
  Quantities
* type functions that iterate over their argument as taking an
  Iterable rather than a List
* fix integration test to allow small numerical error in result
@PreisTo
PreisTo changed the base branch from master to dev August 17, 2026 06:45
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