Skip to content

rm['npred'] double-counted in _update_roi fallback when model_counts_wt missing #649

@fcotizelati

Description

@fcotizelati

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

  1. Run a fermipy analysis on any ROI where at least one source does not have a model_counts_wt entry (but does have model_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()
  1. Trigger ROI update/summary (e.g. write an ROI or print ROI summary) and compare:
  • rm['npred'] for sources with model_counts_wt
  • rm['npred'] for sources without model_counts_wt
  1. For sources without model_counts_wt, you will see that rm['npred'] includes the contribution from src['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_wt is explicitly available or approximated by model_counts.
  • ROI summaries and any logic depending on npred are 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 npred in the ROI.
  • Per-source npred used for diagnostics or cuts.
  • Any post-processing or scripts that assume npred is the sum of model_counts only 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.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions