fermiPy/fermipy

problem with gta.optimize (Value error: function value is NaN)

Closed this issue · 1 comments

lylol commented

Dear all,

I am trying to run a fermipy analysis following: https://fermipy.readthedocs.io/en/latest/quickstart.html.
The following gta.setup function operates normally,

from fermipy.gtanalysis import GTAnalysis
gta = GTAnalysis('config.yaml',logging={'verbosity' : 3})
gta.setup()

however when I run

gta.optimize()

it gives the following error (I use the 4FGL-DR4 catalogs from https://fermi.gsfc.nasa.gov/ssc/data/access/lat/14yr_catalog/):

error report

2023-09-21 10:20:13 INFO    GTAnalysis.optimize(): Starting
Joint fit  ['galdiff', 'isodiff', '4FGL J1224.9+2122', '4FGL J1239.5+0443', '4FGL J1222.5+0414']
/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/scipy/interpolate/_fitpack2.py:313: UserWarning: 
The maximal number of iterations maxit (set to 20 by the program)
allowed for finding a smoothing spline with fp=s has been reached: s
too small.
There is an approximation returned but the corresponding weighted sum
of squared residuals does not satisfy the condition abs(fp-s)/s < tol.
  warnings.warn(message)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 2339, in optimize
    self.fit(loglevel=logging.DEBUG, **config['optimizer'])
  File "/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 3074, in fit
    self.update_source(name, reoptimize=config['reoptimize'])
  File "/Users/yonlinli/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 "/Users/yonlinli/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 "/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/gtanalysis.py", line 2439, in profile_norm
    lims = utils.get_parameter_limits(lnlp['xvals'],
  File "/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/utils.py", line 804, in get_parameter_limits
    x0 = find_function_root(sd, xval[ilo], xval[ihi])
  File "/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/fermipy/utils.py", line 700, in find_function_root
    return brentq(lambda t: fn(t) + delta, x0, xb, xtol=xtol)
  File "/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/scipy/optimize/_zeros_py.py", line 799, in brentq
    r = _zeros._brentq(f, a, b, xtol, rtol, maxiter, args, full_output, disp)
  File "/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/scipy/optimize/_zeros_py.py", line 98, in f_raise
    raise err
ValueError: The function value at x=0.0 is NaN; solver cannot continue.

Corresponding reports from fermipy.log :

2023-09-21 10:20:13 INFO    GTAnalysis.optimize(): Starting
2023-09-21 10:20:13 DEBUG   GTAnalysis.optimize(): LogLike: -129050.599035
2023-09-21 10:20:14 DEBUG   GTAnalysis.free_source(): Freeing parameters for galdiff               : ['Prefactor']
2023-09-21 10:20:14 DEBUG   GTAnalysis.free_source(): Freeing parameters for isodiff               : ['Normalization']
2023-09-21 10:20:14 DEBUG   GTAnalysis.free_source(): Freeing parameters for 4FGL J1224.9+2122     : ['norm']
2023-09-21 10:20:14 DEBUG   GTAnalysis.free_source(): Freeing parameters for 4FGL J1239.5+0443     : ['norm']
2023-09-21 10:20:14 DEBUG   GTAnalysis.free_source(): Freeing parameters for 4FGL J1222.5+0414     : ['norm']
2023-09-21 10:20:14 DEBUG   GTAnalysis.fit(): Starting fit.
2023-09-21 10:20:15 DEBUG   GTAnalysis.fit(): Fit complete.
2023-09-21 10:20:15 DEBUG   GTAnalysis.get_src_model(): Generating source dict for 4FGL J1222.5+0414
2023-09-21 10:20:15 DEBUG   GTAnalysis.profile_norm(): Profiling 4FGL J1222.5+0414
2023-09-21 10:20:16 DEBUG   GTAnalysis.profile_norm(): Finished
2023-09-21 10:20:16 DEBUG   GTAnalysis.get_src_model(): Generating source dict for 4FGL J1224.9+2122
2023-09-21 10:20:16 DEBUG   GTAnalysis.profile_norm(): Profiling 4FGL J1224.9+2122
2023-09-21 10:20:16 DEBUG   GTAnalysis.profile_norm(): Finished
2023-09-21 10:20:16 DEBUG   GTAnalysis.get_src_model(): Generating source dict for 4FGL J1239.5+0443
2023-09-21 10:20:16 DEBUG   GTAnalysis.profile_norm(): Profiling 4FGL J1239.5+0443

No more outputs, so I suppose there are some problems with the profiling procedure.

Environment :

  • MacOS
  • Darwin Kernel Version 22.4.0: Mon Mar 6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64
  • fermipy 1.2.0, Sciencetools 2.2.0

Besides I find that when I decrease the 'src_roiwidth' parameter (in the 'model' section of the configuration file) from 20.0 to 5.0 , sometimes the optimize function reports no error (sometimes it reports the same NaN error described above, confusingly):

2023-09-21 15:47:17 INFO    GTAnalysis.optimize(): Starting
Joint fit  ['galdiff', 'isodiff']
/Users/yonlinli/mambaforge/envs/fermipy/lib/python3.9/site-packages/scipy/interpolate/_fitpack2.py:313: UserWarning: 
The maximal number of iterations maxit (set to 20 by the program)
allowed for finding a smoothing spline with fp=s has been reached: s
too small.
There is an approximation returned but the corresponding weighted sum
of squared residuals does not satisfy the condition abs(fp-s)/s < tol.
  warnings.warn(message)
Fitting shape galdiff TS:    824.873
Fitting shape isodiff TS:    694.017
Fitting shape 4FGL J1230.8+1223 TS:     59.605
2023-09-21 15:47:19 INFO    GTAnalysis.optimize(): Finished
2023-09-21 15:47:19 INFO    GTAnalysis.optimize(): LogLike: -129566.256655 Delta-LogLike: 1115.703656
2023-09-21 15:47:19 INFO    GTAnalysis.optimize(): Execution time: 1.69 s
{'loglike0': -130681.96031147361, 'loglike1': -129566.25665539311, 'dloglike': 1115.703656080499, 'config': {'npred_threshold': 1.0, 'npred_frac': 0.95, 'shape_ts_threshold': 25.0, 'max_free_sources': 5, 'skip': [], 'optimizer': {'optimizer': 'MINUIT', 'tol': 0.001, 'max_iter': 100, 'init_lambda': 0.0001, 'retries': 3, 'min_fit_quality': 2, 'verbosity': 0}}}

Then I run

gta.print_roi()

the outputs are:

2023-09-21 15:47:23 INFO    GTAnalysis.print_roi(): 
name                SpatialModel   SpectrumType     offset        ts       npred
--------------------------------------------------------------------------------
4FGL J1230.8+1223   PointSource    LogParabola       0.047     87.80        71.9
4FGL J1233.0+1333   PointSource    LogParabola       1.250      0.41        12.6
4FGL J1235.9+1136   PointSource    LogParabola       1.443      1.62         8.3
4FGL J1223.3+1213   PointSource    PowerLaw          1.880      3.39        77.3
4FGL J1231.5+1421   PointSource    PowerLaw          1.951      3.44        15.1
4FGL J1223.0+1100   PointSource    LogParabola       2.393      8.95         7.8
isodiff             ConstantValue  FileFunction      -----  15837.94     22086.4
galdiff             MapCubeFunctio PowerLaw          -----  21913.07     25110.9

But I really need to perform the analysis in a wider range ('src_roiwidth'=20 deg).
I sincerely appreciate every help on this problem.

lylol commented

It seems like a matter of memory. I successfully run the same program on a different device .