fermiPy / fermipy

Fermi-LAT Python Analysis Framework
http://fermipy.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
52 stars 53 forks source link

Lightcurve giving issues for daily bins #586

Open Pablogh292 opened 6 months ago

Pablogh292 commented 6 months ago

Describe the error When using fermipy to generate lightcurves of Mrk 501 with daily bins I noticed that sometimes the code would fail when it would encounter a bin with "infinite values", this would cause the entire code to crash and not generate the lightcurve fits file.

To Reproduce

import sys
from fermipy import utils
utils.init_matplotlib_backend()
from fermipy.gtanalysis import GTAnalysis
from fermipy.utils import *
import yaml
import pprint
import numpy
import argparse

from fermipy.gtanalysis import GTAnalysis

def main():

    usage = "usage: %(prog)s [config file]"
    description = "Run fermipy analysis chain."
    parser = argparse.ArgumentParser(usage=usage,description=description)

    parser.add_argument('--config', default = 'config3.yaml')
    parser.add_argument('--source', default = None)

    args = parser.parse_args()
    gta = GTAnalysis(args.config)

    if args.source is None:
        src_name = gta.roi.sources[0].name

    gta.setup()
    gta.optimize()

#    loc = gta.localize(src_name, free_radius=1.0, update=True, make_plots=True)

    model = {'SpectrumType':'EblAtten::LogParabola', 'alpha' : 2.5 , 'beta' : 0, 'SpatialModel' : 'PointSource' , 'redshift' : 0.034 , 'ebl_model':4}
    srcs = gta.find_sources(model=model, sqrt_ts_threshold=5.0,
                            min_separation=0.5)

 #   sed = gta.sed(src_name, free_radius=1.0, make_plots=True)
 #   gta.tsmap(make_plots=True)
    gta.write_roi('fit0')  
    vec=[]
    timer=544924803.
    while timer < 545702403:#602294403.:
       vec.append(timer)
       timer+=86400.
    vec.append(545702403.) 
    lc = gta.lightcurve(src_name, time_bins=vec, free_radius=3.0, use_scaled_srcmap=True,multithread=False)#True,nthread=6)

if __name__ == "__main__":
    main()`

Expected behavior It was my understanding that the code should be able to simply ignore this bin and continue onwards to the next, but for some reason when it calls for gtanalyisis.py, if it finds NaN values it raises an error and crashes the entire code instead.

Log files

2024-05-02 03:20:18 INFO    GTAnalysis.lightcurve(): Computing Lightcurve for 4FGL J1653.8+3945
2024-05-02 03:20:19 INFO    lightcurve_545702403_545788803 GTAnalysis._process_lc_bin(): Fitting time range 545702403 545788803
WARNING: FITSFixedWarning: RADECSYS= 'FK5 ' 
the RADECSYS keyword is deprecated, use RADESYSa. [astropy.wcs.wcs]
2024-05-02 03:20:32 INFO    lightcurve_545702403_545788803 GTBinnedAnalysis.write_xml(): Writing /media/pablo/datadisk/Mkn501/ResultsDAILY3/lightcurve_545702403_545788803/base_00.xml...
Joint fit  ['4FGL J1635.2+3808']
2024-05-02 03:20:32 WARNING lightcurve_545702403_545788803 GTAnalysis.profile_norm(): Source model for source 4FGL J1648.2+4232 has no counts in ROI
2024-05-02 03:20:32 WARNING lightcurve_545702403_545788803 GTAnalysis.profile_norm(): Upper limit not found.  Refitting normalization.
Traceback (most recent call last):
  File "/media/pablo/datadisk/Mkn501/runLCDailyP3-3.py", line 49, in <module>
    main()
  File "/media/pablo/datadisk/Mkn501/runLCDailyP3-3.py", line 46, in main
    lc = gta.lightcurve(src_name, time_bins=vec, free_radius=3.0, use_scaled_srcmap=True,multithread=False)#True,nthread=6)
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/lightcurve.py", line 289, in lightcurve
    o = self._make_lc(name, **config)
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/lightcurve.py", line 450, in _make_lc
    next_fit = next(mapo)
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/lightcurve.py", line 180, in _process_lc_bin
    fit_results = _fit_lc(gta, name, **kwargs)
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/lightcurve.py", line 59, in _fit_lc
    gta.fit()
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 3074, in fit
    self.update_source(name, reoptimize=config['reoptimize'])
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 4106, in update_source
    sd = self.get_src_model(name, paramsonly, reoptimize, npts,
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 4264, in get_src_model
    lnlp = self.profile_norm(name, savestate=True,
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 2472, in profile_norm
    o = self.profile(name, parName,
  File "/home/pablo/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 2684, in profile
    raise RuntimeError(
RuntimeError: Parameter scan points for 4FGL J1648.2+4232::norm include infinite value.

Environment (please complete the following information):

Additional context It should be noted that this issue happens even in the most recent version of fermipy.

Pablogh292 commented 6 months ago

Doing some further digging it would seem that the problem is with error exception inside lightcurve.py because when gtanalysis.py raises a RuntimeError, lightcurve.py somehow doesn't catch it and just crashes because of the Error. Also tried with the newest version of fermipy and the issue persists.

omodei commented 5 months ago

I posted a comment in your PR

navaneethnpk commented 5 months ago

Hi, I am getting similar issue while making lightcurve of a fermi source [7day binning]. How do I account for this? I have attached the error, code and config files.

ERROR

2024-05-24 06:03:34 INFO    lightcurve_542505601_543110401 GTBinnedAnalysis.write_xml(): Writing /home/navaneeth/Works/mrk421/mrk421_LC_batch/outdir_lc/lightcurve_542505601_543110401/base_00.xml...
Joint fit  ['4FGL J1104.4+3812', '4FGL J1131.0+3815', '4FGL J1112.5+3448', '4FGL J1127.8+3618', '4FGL J1106.7+3623']
Fitting shape 4FGL J1104.4+3812 TS:    142.949
2024-05-24 06:03:35 INFO    lightcurve_542505601_543110401 GTBinnedAnalysis.write_xml(): Writing /home/navaneeth/Works/mrk421/mrk421_LC_batch/outdir_lc/lightcurve_542505601_543110401/fit_model_final_00.xml...
2024-05-24 06:03:36 INFO    lightcurve_542505601_543110401 GTAnalysis._process_lc_bin(): Finished time range 542505601 543110401
2024-05-24 06:03:38 INFO    lightcurve_543110401_543715201 GTAnalysis._process_lc_bin(): Fitting time range 543110401 543715201
2024-05-24 06:03:39 INFO    lightcurve_543110401_543715201 GTBinnedAnalysis._create_ltcube(): Generating local LT cube.
Analysis failed in time range 543110401 543715201
(<class 'RuntimeError'>, RuntimeError('irfs.py(569): Zero exposure!'), <traceback object at 0x7fbd72e36780>)
2024-05-24 06:03:41 ERROR   GTAnalysis._make_lc(): Fit failed in bin 498 in range 543110401 543715201.
2024-05-24 06:03:43 INFO    lightcurve_543715201_544320001 GTAnalysis._process_lc_bin(): Fitting time range 543715201 544320001
2024-05-24 06:03:44 INFO    lightcurve_543715201_544320001 GTBinnedAnalysis._create_ltcube(): Generating local LT cube.
Analysis failed in time range 543715201 544320001
(<class 'RuntimeError'>, RuntimeError('irfs.py(569): Zero exposure!'), <traceback object at 0x7fbd7269efc0>)
2024-05-24 06:03:45 ERROR   GTAnalysis._make_lc(): Fit failed in bin 499 in range 543715201 544320001.
2024-05-24 06:03:47 INFO    lightcurve_544320001_544924801 GTAnalysis._process_lc_bin(): Fitting time range 544320001 544924801
2024-05-24 06:03:48 INFO    lightcurve_544320001_544924801 GTBinnedAnalysis._create_ltcube(): Generating local LT cube.
WARNING: FITSFixedWarning: RADECSYS= 'FK5 '
the RADECSYS keyword is deprecated, use RADESYSa. [astropy.wcs.wcs]
2024-05-24 06:06:01 INFO    lightcurve_544320001_544924801 GTBinnedAnalysis.write_xml(): Writing /home/navaneeth/Works/mrk421/mrk421_LC_batch/outdir_lc/lightcurve_544320001_544924801/base_00.xml...
Joint fit  ['4FGL J1104.4+3812']
2024-05-24 06:06:01 WARNING lightcurve_544320001_544924801 GTAnalysis.profile_norm(): Source model for source 4FGL J1051.4+3942 has no counts in ROI
2024-05-24 06:06:01 WARNING lightcurve_544320001_544924801 GTAnalysis.profile_norm(): Upper limit not found.  Refitting normalization.
Traceback (most recent call last):
  File "/home/navaneeth/Works/mrk421/mrk421_LC_batch/run_lc.py", line 9, in <module>
    lc = gta.lightcurve('4FGL J1104.4+3812', binsz=86400.*7.0, free_radius=3.0, prefix='Mrk421_FullLC', outdir='outdir_lc')
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/lightcurve.py", line 290, in lightcurve
    o = self._make_lc(name, **config)
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/lightcurve.py", line 451, in _make_lc
    next_fit = next(mapo)
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/lightcurve.py", line 181, in _process_lc_bin
    fit_results = _fit_lc(gta, name, **kwargs)
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/lightcurve.py", line 59, in _fit_lc
    gta.fit()
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 3074, in fit
    self.update_source(name, reoptimize=config['reoptimize'])
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 4106, in update_source
    sd = self.get_src_model(name, paramsonly, reoptimize, npts,
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 4264, in get_src_model
    lnlp = self.profile_norm(name, savestate=True,
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 2472, in profile_norm
    o = self.profile(name, parName,
  File "/home/navaneeth/miniconda3/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 2684, in profile
    raise RuntimeError(
RuntimeError: Parameter scan points for 4FGL J1051.4+3942::norm include infinite value.

CODE

import numpy as np
from fermipy.gtanalysis import GTAnalysis

gta = GTAnalysis('config.yaml', logging={'verbosity': 3})
gta.setup()
gta.optimize()

lc = gta.lightcurve('4FGL J1104.4+3812', binsz=86400.*7.0, free_radius=3.0, prefix='Mrk421_FullLC', outdir='outdir_lc')

CONFIG

data:
  evfile : pfiles.dat
  scfile : sfiles.dat

binning:
  roiwidth   : 10.0
  binsz      : 0.1
  binsperdec : 8

selection :
  emin : 100
  emax : 500000
  zmax    : 90
  evclass : 128
  evtype  : 3
  tmin    : 241920001
  tmax    : 730771205
  target : '4FGL J1104.4+3812'

gtlike:
  edisp : True
  irfs : 'P8R3_SOURCE_V3'
  edisp_disable : ['isodiff','galdiff']

model:
  src_roiwidth : 15.0
  galdiff  : '/home/navaneeth/Works/MY_FERMI_DIR/gll_iem_v07.fits'
  isodiff  : '/home/navaneeth/Works/MY_FERMI_DIR/iso_P8R3_SOURCE_V3_v1.txt'
  catalogs : '/home/navaneeth/Works/MY_FERMI_DIR/gll_psc_v34.fit'