-
Notifications
You must be signed in to change notification settings - Fork 58
Description
Describe the bug
In fermipy/gtanalysis.py:_update_roi (around line 637), rm['npred'] is updated twice when model_counts_wt is not present for a source.
The relevant code in the fallback branch currently does:
rm['model_counts_wt'] += src['model_counts']
rm['npred'] += np.sum(src['model_counts'])
However, rm['npred'] was already incremented a few lines above, so this path double-counts the predicted counts for sources that do not have model_counts_wt.
This leads to inflated npred values in ROI summaries and any downstream logic relying on npred in cases where some sources lack weighted counts.
This was observed in a standard LAT ROI analysis where some sources in the ROI do not have model_counts_wt defined; the issue is independent of the specific dataset and occurs whenever _update_roi encounters such sources.
To Reproduce
- Run a fermipy analysis on any ROI where at least one source does not have a
model_counts_wtentry (but does havemodel_counts), e.g.:
from fermipy.gtanalysis import GTAnalysis
gta = GTAnalysis('config.yaml', logging={'verbosity': 3})
gta.setup()
gta.fit()
# Force an ROI model update (which calls _update_roi internally)
gta.roi.summary()
- Trigger ROI update/summary (e.g. write an ROI or print ROI summary) and compare:
rm['npred']for sources withmodel_counts_wtrm['npred']for sources withoutmodel_counts_wt
- For sources without
model_counts_wt, you will see thatrm['npred']includes the contribution fromsrc['model_counts']twice.
Alternatively, you can confirm the issue directly by inspecting the _update_roi logic around the fallback branch for model_counts_wt.
Expected behavior
In the fallback branch, the code should update rm['npred_wt'] instead of rm['npred'], so that unweighted npred is not double-counted. For example:
rm['model_counts_wt'] += src['model_counts']
rm['npred_wt'] += np.sum(src['model_counts'])
This way:
rm['npred']is incremented only once (earlier in_update_roi).- Weighted quantities remain consistent whether
model_counts_wtis explicitly available or approximated bymodel_counts. - ROI summaries and any logic depending on
npredare not biased when weighted counts are missing.
Log files
This is a logic bug and does not produce an explicit error. ROI summaries complete successfully but npred is overestimated for sources without model_counts_wt. There are no relevant warnings or stack traces.
Environment (please complete the following information):
- OS: macOS (ARM64, Darwin 25.2.0)
- Darwin 192.168.1.64 25.2.0 Darwin Kernel Version 25.2.0: Tue Nov 18 21:09:41 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6031 arm64
- fermipy: 1.4.0
- fermitools: 2.2.0
- fermitools-data: 0.18
Additional context
Because npred is used in ROI summaries and potentially in selection or diagnostics based on predicted counts, this double counting can subtly affect:
- Reported total
npredin the ROI. - Per-source
npredused for diagnostics or cuts. - Any post-processing or scripts that assume
npredis the sum ofmodel_countsonly once per source.
A simple fix is to replace the second line in the fallback branch:
rm['npred'] += np.sum(src['model_counts'])
with
rm['npred_wt'] += np.sum(src['model_counts'])
so that the behavior is consistent with the intended weighted/unweighted separation.