CHIMEFRB / fitburst

An open-source package of utilities for direct modeling of radio dynamic spectra.
https://chimefrb.github.io/fitburst/
MIT License
11 stars 2 forks source link

ValueError: array must not contain infs or NaNs #48

Closed ketansand closed 1 year ago

ketansand commented 2 years ago

This error comes up when we do spectral fitting


`ValueError                                Traceback (most recent call last)
/tmp/ipykernel_11914/3411376667.py in <module>
     27            save = 'pm_', # Prefix for saving MCMC results
     28            diagnostic_plots=True,
---> 29            output = fit_file #prefix for fitburst npz input file
     30           )

/usr/local/lib/python3.7/site-packages/fitburst/pipelines/make_input_file.py in make_input(path, peaks, nwalkers, nchain, ncores, show_chains, DM, fit_DM, logl, save, diagnostic_plots, downsample2, downsample, spectrum_lim, fill_missing_time, ref_freq, time_range, output)
     71                    downsample = downsample, DM = DM, fit_DM = fit_DM, logl = logl, peaks = peaks,
     72                    fill_missing_time = fill_missing_time, save = save, spectrum_lim = spectrum_lim,
---> 73                    time_range = time_range, show_chains=show_chains, diagnostic_plots=diagnostic_plots)
     74 
     75     params, scattering_t = np.array(params[:-1]), params[-1]

/usr/local/lib/python3.7/site-packages/fitburst/backend/baseband.py in fit_profile(path, nwalkers, nchain, ncores, time_range, downsample, downsample2, DM, fit_DM, logl, save, spectrum_lim, fill_missing_time, show_chains, peaks, diagnostic_plots)
    314         spectrum_pars, spectrum_errs = fit_rpl_mcmc(spectrum, freq, peaks=spect_peaks, res = 400. / 1024., nwalkers=100, 
    315                  nchain=5000, event_id=event_id, ncores=ncores, show_chains = show_chains,
--> 316                  diagnostic_plots=diagnostic_plots
    317                  )
    318         amps[i] = spectrum_pars[0]

/usr/local/lib/python3.7/site-packages/fitburst/analysis/mcmc_fitter.py in fit_rpl_mcmc(profile, xvals, peaks, res, nwalkers, nchain, ncores, ICs, event_id, show_chains, diagnostic_plots)
    363     for i in range(len(profile)):
    364         print(profile[i])
--> 365     fit, popt, popt_e,pcov = fitter.fit_LS(profile, xvals, peaks, event_id, ICs = ICs, model='rpl', res=res)
    366     tau, flat_samples, final_pars, final_errs = mcmc(popt, 
    367         profile, xvals, res,event_id = event_id, nwalkers=nwalkers, nchain=nchain,

/usr/local/lib/python3.7/site-packages/fitburst/analysis/fitter.py in fit_LS(profile, xvals, peaks, event_id, model, res, ICs, tight, bounds, diagnostic_plots)
    402     plt.plot(x,f(x,ICs),color = 'orange')
    403     plt.show()
--> 404     popt,pcov = curve_fit(f,x, y, p0=ICs, bounds = (lb, ub),maxfev = 100000000)
    405     if diagnostic_plots:
    406         print('Curve_fit params: ' + str(popt))

~/.local/lib/python3.7/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
    799 
    800         res = least_squares(func, p0, jac=jac, bounds=bounds, method=method,
--> 801                             **kwargs)
    802 
    803         if not res.success:

~/.local/lib/python3.7/site-packages/scipy/optimize/_lsq/least_squares.py in least_squares(fun, x0, jac, bounds, method, ftol, xtol, gtol, x_scale, loss, f_scale, diff_step, tr_solver, tr_options, jac_sparsity, max_nfev, verbose, args, kwargs)
    928         result = trf(fun_wrapped, jac_wrapped, x0, f0, J0, lb, ub, ftol, xtol,
    929                      gtol, max_nfev, x_scale, loss_function, tr_solver,
--> 930                      tr_options.copy(), verbose)
    931 
    932     elif method == 'dogbox':

~/.local/lib/python3.7/site-packages/scipy/optimize/_lsq/trf.py in trf(fun, jac, x0, f0, J0, lb, ub, ftol, xtol, gtol, max_nfev, x_scale, loss_function, tr_solver, tr_options, verbose)
    123         return trf_bounds(
    124             fun, jac, x0, f0, J0, lb, ub, ftol, xtol, gtol, max_nfev, x_scale,
--> 125             loss_function, tr_solver, tr_options, verbose)
    126 
    127 

~/.local/lib/python3.7/site-packages/scipy/optimize/_lsq/trf.py in trf_bounds(fun, jac, x0, f0, J0, lb, ub, ftol, xtol, gtol, max_nfev, x_scale, loss_function, tr_solver, tr_options, verbose)
    299             J_h = J_augmented[:m]  # Memory view.
    300             J_augmented[m:] = np.diag(diag_h**0.5)
--> 301             U, s, V = svd(J_augmented, full_matrices=False)
    302             V = V.T
    303             uf = U.T.dot(f_augmented)

~/.local/lib/python3.7/site-packages/scipy/linalg/decomp_svd.py in svd(a, full_matrices, compute_uv, overwrite_a, check_finite, lapack_driver)
    106 
    107     """
--> 108     a1 = _asarray_validated(a, check_finite=check_finite)
    109     if len(a1.shape) != 2:
    110         raise ValueError('expected matrix')

~/.local/lib/python3.7/site-packages/scipy/_lib/_util.py in _asarray_validated(a, check_finite, sparse_ok, objects_ok, mask_ok, as_inexact)
    291             raise ValueError('masked arrays are not supported')
    292     toarray = np.asarray_chkfinite if check_finite else np.asarray
--> 293     a = toarray(a)
    294     if not objects_ok:
    295         if a.dtype is np.dtype('O'):

/usr/local/lib/python3.7/site-packages/numpy/lib/function_base.py in asarray_chkfinite(a, dtype, order)
    487     if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all():
    488         raise ValueError(
--> 489             "array must not contain infs or NaNs")
    490     return a
    491 

ValueError: array must not contain infs or NaNs```
ketansand commented 2 years ago

This can be due to faulty initial guesses during the curve fit routine

ketansand commented 2 years ago

This can be moved to done because we have removed spectral fitting

emmanuelfonseca commented 1 year ago

i believe this has been resolved.