Skip to content

Point Source Spectral Fitting with Normalizing Flows Approximation for Response and Background#515

Open
scipascal wants to merge 15 commits intocositools:developfrom
scipascal:interfaces
Open

Point Source Spectral Fitting with Normalizing Flows Approximation for Response and Background#515
scipascal wants to merge 15 commits intocositools:developfrom
scipascal:interfaces

Conversation

@scipascal
Copy link
Contributor

@scipascal scipascal commented Mar 13, 2026

Description

This pull request introduces a neural-network-powered response and background approximation to cosipy, which powers the unbinned Poisson likelihood analysis of continuum point sources. For more information, refer to the tutorial notebook example_crab_fit_normalizing_flows.ipynb or my thesis, "Development of an Efficient Response Description for the COSI MeV 𝜸-Ray Telescope."

Hardware Testing

The implementation has been verified on the following environment:

  • OS: Debian GNU/Linux 12 (Bookworm)
  • CPU: 2 × AMD EPYC 9454 (96 physical cores total)
  • Memory: 1.1 TiB
  • GPU: 4 × NVIDIA RTX A5000 (24 GiB VRAM each)

Important

Multiprocessing or PyTorch may encounter issues on AMD GPUs or M-Processors.

Installation

Running this code requires additional packages. Install them using the ml extra:
pip install -e ".[ml]" (includes torch, sphericart[torch] and normflows)

Additionally, you’ll need two checkpoint files to run NFResponse and NFBackground, which you can find on Wasabi (thanks @GallegoSav).

Closes #389

pjanowsk added 13 commits March 13, 2026 14:15
Adding my neural network response and psr implementation to cosipy
interfaces

Merged latest changes from Israels interfaces branch and applied my
changes: Response implementation and PSR folding with EventData and
PhotonList
…rlap with background development, fixed batchsize and compile mode setter bug.
…zing flows models contain CPU syncs, the cuda streams provide no benefit and will be removed. This commit saves the current state.
@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 0.55487% with 1613 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.80%. Comparing base (4911568) to head (dcffa20).

Files with missing lines Patch % Lines
cosipy/threeml/optimized_unbinned_folding.py 0.00% 602 Missing ⚠️
cosipy/response/NFBase.py 0.00% 355 Missing ⚠️
cosipy/response/NFResponseModels.py 0.00% 164 Missing ⚠️
cosipy/background_estimation/NFBackgroundModels.py 0.00% 142 Missing ⚠️
cosipy/response/NFResponse.py 0.00% 117 Missing ⚠️
...py/background_estimation/nf_unbinned_background.py 0.00% 84 Missing ⚠️
cosipy/response/nf_instrument_response_function.py 0.00% 53 Missing ⚠️
cosipy/background_estimation/NFBackground.py 0.00% 46 Missing ⚠️
cosipy/threeml/unbinned_model_folding.py 0.00% 45 Missing ⚠️
cosipy/response/NFWorkerState.py 0.00% 4 Missing ⚠️
... and 1 more
Files with missing lines Coverage Δ
cosipy/data_io/EmCDSUnbinnedData.py 40.57% <100.00%> (ø)
cosipy/interfaces/expectation_interface.py 53.44% <ø> (ø)
cosipy/interfaces/source_response_interface.py 100.00% <100.00%> (ø)
cosipy/response/__init__.py 100.00% <100.00%> (ø)
cosipy/threeml/psr_fixed_ei.py 0.00% <0.00%> (ø)
cosipy/response/NFWorkerState.py 0.00% <0.00%> (ø)
cosipy/threeml/unbinned_model_folding.py 0.00% <0.00%> (ø)
cosipy/background_estimation/NFBackground.py 0.00% <0.00%> (ø)
cosipy/response/nf_instrument_response_function.py 0.00% <0.00%> (ø)
...py/background_estimation/nf_unbinned_background.py 0.00% <0.00%> (ø)
... and 5 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

from importlib.util import find_spec

if find_spec("torch") is None:
raise RuntimeError("Install cosipy with [ml] optional package to use this feature.")
Copy link
Contributor

Choose a reason for hiding this comment

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

put the pip command to use explicitly. pip install -e . "[ml]"

@scipascal scipascal removed the pull-request-needs-reviewer No reviewer assigned label Mar 13, 2026
@GallegoSav
Copy link
Contributor

@israelmcmc I tried the tutorial notebook with the brightest GRB ( GRB_bn090424592 ) using GPU or CPU.

Both worked well.
CPU is slower but still affordable ~20-30 min for precomputation (which can be saved ).

@israelmcmc
Copy link
Collaborator

israelmcmc commented Mar 16, 2026

Thanks @scipascal and @GallegoSav. I'll try it out.

@GallegoSav for that GRB that lasted 20-30 min in a CPU, so you know how many event you were analyzing?

Or in general, I want to get a sense of the cost per event in both GPUs and CPU, maybe @scipascal knows too.

@scipascal
Copy link
Contributor Author

scipascal commented Mar 16, 2026

Or in general, I want to get a sense of the cost per event in both GPUs and CPU, maybe @scipascal knows too.

@israelmcmc The benchmark that I performed in my thesis was 34e4 evaluations per second for the probability density function on four NVIDIA RTX A5000. Lets assume you need 60 evaluations per event for numerical integration. Then you get 5.7e3 events per second. Thats very slow and therefore we need the caching and optimized integration schemes to use it.

@israelmcmc
Copy link
Collaborator

Thanks @scipascal. Do you have the same performance numbers of a CPU?

@israelmcmc israelmcmc moved this to Under review in cosipy Mar 16, 2026
@scipascal
Copy link
Contributor Author

@israelmcmc, for CPU only (see the hardware specifications) I get around 225 events per second compared to the 5.7e3 events per second with 4 GPUs.

@israelmcmc
Copy link
Collaborator

Thanks for checking @scipascal!

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

Projects

Status: Under review

Development

Successfully merging this pull request may close these issues.

Add NN instrument response to cosipy

3 participants