Fitting¶
- class SEDFitLM(data, p0=None, bounds_l=None, bounds_h=None, vary=None, params=None)¶
This class is a wrapper around lmfit.Minimizer class from lmfit package. It is designed to provide more friendly interface for SED Fitting purposes.
- Parameters
data (list) – Data in the form of list with nu, L_nu, L_nu_err values, each as 1-D np.ndarray
p0 (list) – Initial guess for fit [None]
bounds_l (list) – List of lower limits [None]
bounds_h (list) – List of upper limits [None]
vary (list) – List of bools, wheter or not to vary a given params [None]
params (list) – lmfit.Parameters class which alternatively overrides; p0, bounds_l, bounds_h, vary
- edist_fun_from_fit(gamma)¶
Computes the electron distribution of the best fit for a given gamma.
- Parameters
gamma (np.array) – the gamma values at which the electron distribution is evaluted
- Returns
dn/dgamma/dV – The values of the electron distribution of the best fit, given gamma
- Return type
np.array
- fit(*args, **kwargs)¶
Interface to lmfit.Minimizer.minimize. After successful execeution the result is stored in SEDFitLM.MinimizerResult with the same format as lmfit.Minimizer.MinimizerResult.
Example:
>>> kwargs_for_emcee = {"nsamples":400, "nwalkers":400, "nsteps":1000} >>> fitobject.fit(method="emcee", **kwags_for_emcee)
- Parameters
*args – args for lmfit.Minimizer.minimize
**kwargs – kwargs for lmfit.Minimizer.minimize
- Returns
- Return type
None
- fit_fun(nu, p)¶
Evaluates the model function for given nu and lmfit.Parameters.
- Parameters
nu (np.ndarray) – nu values at which to evalute model functions
p (lmfit.Parameters) – Parameters at which to evalute model functions
- Returns
luminosity – Evaluates the total SED for a set of defined models [erg s-1]
- Return type
np.ndarray
- guess(**kwargs)¶
Estimates the fit using lmfit.Minimizer.minimize with “Nelder-Mead” method and updates the
self.params
.- Parameters
**kwargs (dict) – keyword arguements pass self.fit
- Returns
- Return type
None
- plot_data(ax, kwargs_for_data={})¶
Plot of the data.
- Parameters
ax (matplotlib.pyplot.axis) – axis object to which the data plot is added
kwargs_for_data (dict) – kwargs for matplotlib.pyplot.errorbar
- Returns
- Return type
None
- plot_data(ax, kwargs_for_data={})¶
Plot of the data.
- Parameters
ax (matplotlib.pyplot.axis) – axis object to which the data plot is added
kwargs_for_data (dict) – kwargs for matplotlib.pyplot.errorbar
- Returns
- Return type
None
- plot_edist(ax=None, gamma=array([1.00000000e+00, 1.09698580e+00, 1.20337784e+00, 1.32008840e+00, 1.44811823e+00, 1.58856513e+00, 1.74263339e+00, 1.91164408e+00, 2.09704640e+00, 2.30043012e+00, 2.52353917e+00, 2.76828663e+00, 3.03677112e+00, 3.33129479e+00, 3.65438307e+00, 4.00880633e+00, 4.39760361e+00, 4.82410870e+00, 5.29197874e+00, 5.80522552e+00, 6.36824994e+00, 6.98587975e+00, 7.66341087e+00, 8.40665289e+00, 9.22197882e+00, 1.01163798e+01, 1.10975250e+01, 1.21738273e+01, 1.33545156e+01, 1.46497140e+01, 1.60705282e+01, 1.76291412e+01, 1.93389175e+01, 2.12145178e+01, 2.32720248e+01, 2.55290807e+01, 2.80050389e+01, 3.07211300e+01, 3.37006433e+01, 3.69691271e+01, 4.05546074e+01, 4.44878283e+01, 4.88025158e+01, 5.35356668e+01, 5.87278661e+01, 6.44236351e+01, 7.06718127e+01, 7.75259749e+01, 8.50448934e+01, 9.32930403e+01, 1.02341140e+02, 1.12266777e+02, 1.23155060e+02, 1.35099352e+02, 1.48202071e+02, 1.62575567e+02, 1.78343088e+02, 1.95639834e+02, 2.14614120e+02, 2.35428641e+02, 2.58261876e+02, 2.83309610e+02, 3.10786619e+02, 3.40928507e+02, 3.73993730e+02, 4.10265811e+02, 4.50055768e+02, 4.93704785e+02, 5.41587138e+02, 5.94113398e+02, 6.51733960e+02, 7.14942899e+02, 7.84282206e+02, 8.60346442e+02, 9.43787828e+02, 1.03532184e+03, 1.13573336e+03, 1.24588336e+03, 1.36671636e+03, 1.49926843e+03, 1.64467618e+03, 1.80418641e+03, 1.97916687e+03, 2.17111795e+03, 2.38168555e+03, 2.61267523e+03, 2.86606762e+03, 3.14403547e+03, 3.44896226e+03, 3.78346262e+03, 4.15040476e+03, 4.55293507e+03, 4.99450512e+03, 5.47890118e+03, 6.01027678e+03, 6.59318827e+03, 7.23263390e+03, 7.93409667e+03, 8.70359136e+03, 9.54771611e+03, 1.04737090e+04, 1.14895100e+04, 1.26038293e+04, 1.38262217e+04, 1.51671689e+04, 1.66381689e+04, 1.82518349e+04, 2.00220037e+04, 2.19638537e+04, 2.40940356e+04, 2.64308149e+04, 2.89942285e+04, 3.18062569e+04, 3.48910121e+04, 3.82749448e+04, 4.19870708e+04, 4.60592204e+04, 5.05263107e+04, 5.54266452e+04, 6.08022426e+04, 6.66991966e+04, 7.31680714e+04, 8.02643352e+04, 8.80488358e+04, 9.65883224e+04, 1.05956018e+05, 1.16232247e+05, 1.27505124e+05, 1.39871310e+05, 1.53436841e+05, 1.68318035e+05, 1.84642494e+05, 2.02550194e+05, 2.22194686e+05, 2.43744415e+05, 2.67384162e+05, 2.93316628e+05, 3.21764175e+05, 3.52970730e+05, 3.87203878e+05, 4.24757155e+05, 4.65952567e+05, 5.11143348e+05, 5.60716994e+05, 6.15098579e+05, 6.74754405e+05, 7.40196000e+05, 8.11984499e+05, 8.90735464e+05, 9.77124154e+05, 1.07189132e+06, 1.17584955e+06, 1.28989026e+06, 1.41499130e+06, 1.55222536e+06, 1.70276917e+06, 1.86791360e+06, 2.04907469e+06, 2.24780583e+06, 2.46581108e+06, 2.70495973e+06, 2.96730241e+06, 3.25508860e+06, 3.57078596e+06, 3.91710149e+06, 4.29700470e+06, 4.71375313e+06, 5.17092024e+06, 5.67242607e+06, 6.22257084e+06, 6.82607183e+06, 7.48810386e+06, 8.21434358e+06, 9.01101825e+06, 9.88495905e+06, 1.08436597e+07, 1.18953407e+07, 1.30490198e+07, 1.43145894e+07, 1.57029012e+07, 1.72258597e+07, 1.88965234e+07, 2.07292178e+07, 2.27396575e+07, 2.49450814e+07, 2.73644000e+07, 3.00183581e+07, 3.29297126e+07, 3.61234270e+07, 3.96268864e+07, 4.34701316e+07, 4.76861170e+07, 5.23109931e+07, 5.73844165e+07, 6.29498899e+07, 6.90551352e+07, 7.57525026e+07, 8.30994195e+07, 9.11588830e+07, 1.00000000e+08]), show=True, xlim=None, ylim=None)¶
Plots electron distribution of the best fit
- Parameters
ax (matplotlib.pyplot.axis, default=None) – if None, plot fit will create a new figure and axis object. Otherwise plot will be added to axis object
gamma (np.ndarray, default=np.logspace(0, 8, 200)) – Gamma at which the electron distribution is evaluated from best fit result.
show (bool, default=True) – if False plt.show is not called.
xlim (tuple, default=None) – if not None, sets the xlimits of the plot
ylim (tuple, default=None) – if not None, sets the ylimits of the plot
- Returns
- Return type
None
- plot_fit(ax=None, nu=array([1.00000000e+10, 1.59985872e+10, 2.55954792e+10, 4.09491506e+10, 6.55128557e+10, 1.04811313e+11, 1.67683294e+11, 2.68269580e+11, 4.29193426e+11, 6.86648845e+11, 1.09854114e+12, 1.75751062e+12, 2.81176870e+12, 4.49843267e+12, 7.19685673e+12, 1.15139540e+13, 1.84206997e+13, 2.94705170e+13, 4.71486636e+13, 7.54312006e+13, 1.20679264e+14, 1.93069773e+14, 3.08884360e+14, 4.94171336e+14, 7.90604321e+14, 1.26485522e+15, 2.02358965e+15, 3.23745754e+15, 5.17947468e+15, 8.28642773e+15, 1.32571137e+16, 2.12095089e+16, 3.39322177e+16, 5.42867544e+16, 8.68511374e+16, 1.38949549e+17, 2.22299648e+17, 3.55648031e+17, 5.68986603e+17, 9.10298178e+17, 1.45634848e+18, 2.32995181e+18, 3.72759372e+18, 5.96362332e+18, 9.54095476e+18, 1.52641797e+19, 2.44205309e+19, 3.90693994e+19, 6.25055193e+19, 1.00000000e+20]), show=True, xlim=None, ylim=None, kwargs_for_data={}, kwargs_for_fit={})¶
Plots fit results
- Parameters
ax (matplotlib.pyplot.axis, default=None) – if None, plot fit will create a new figure and axis object. Otherwise plot will be added to axis object
nu (np.ndarray, default=np.logspace(10, 20, 50)) – the x values for at which the best fit solution is evaluted
show (bool, default=True) – if False plt.show is not called.
xlim (tuple, default=None) – if not None, sets the xlimits of the plot
ylim (tuple, default=None) – if not None, sets the ylimits of the plot
kwargs_for_data (dict, default={}) – Keywords for plt.plot to plot data.
kwargs_for_fit (dict) – Keyword arguments passed to plt.plot for SED model plot. The plot of the combined fit of all models can be controlled by adding “model” to the keyword, e.g. kwargs_for_fit={“model_lw”:10, “lw”:5} sets the line width of the combined model to 10, where as the individual model components have line width 5. [{}]
- Returns
- Return type
None
- plot_fun(nu, p, ax, kwargs_for_fit={})¶
Creates a plot of the fit using the best fit
- Parameters
nu (np.ndarray) – Frequencies to evalutate SED model
p (lmfit.Parameters) – Parameters to evaluate the model.
ax (matplotlib.pyplot.axis) – axis object to which the data plot is added
kwargs_for_fit (dict) – keywords arguments passed to kwargs_for data. if keyword has “model” as a prefix matplotlib keyword is passed to the combined fit, else is passed to individal fits [{}]
- Returns
- Return type
None
- plot_initial_guess(ax=None, p0=None, nu=array([1.00000000e+10, 1.59985872e+10, 2.55954792e+10, 4.09491506e+10, 6.55128557e+10, 1.04811313e+11, 1.67683294e+11, 2.68269580e+11, 4.29193426e+11, 6.86648845e+11, 1.09854114e+12, 1.75751062e+12, 2.81176870e+12, 4.49843267e+12, 7.19685673e+12, 1.15139540e+13, 1.84206997e+13, 2.94705170e+13, 4.71486636e+13, 7.54312006e+13, 1.20679264e+14, 1.93069773e+14, 3.08884360e+14, 4.94171336e+14, 7.90604321e+14, 1.26485522e+15, 2.02358965e+15, 3.23745754e+15, 5.17947468e+15, 8.28642773e+15, 1.32571137e+16, 2.12095089e+16, 3.39322177e+16, 5.42867544e+16, 8.68511374e+16, 1.38949549e+17, 2.22299648e+17, 3.55648031e+17, 5.68986603e+17, 9.10298178e+17, 1.45634848e+18, 2.32995181e+18, 3.72759372e+18, 5.96362332e+18, 9.54095476e+18, 1.52641797e+19, 2.44205309e+19, 3.90693994e+19, 6.25055193e+19, 1.00000000e+20]), show=True, xlim=None, ylim=None)¶
Plots the luminosity of the inital guess provided to the fit object, or the the initial guess provided to the function
- Parameters
ax (matplotlib.pyplot.axis, default=None) – if None, plot fit will create a new figure and axis object. Otherwise plot will be added to axis object
p0 (lmfit.Parameters) – List of alternative parameters, if None uses the lmfit.Parameters after
SEDFitLM.guess()
nu (np.ndarray, default=np.logspace(10, 20, 50)) – Frequencies at which the best fit solution is evaluted
show (bool, default=True) – if False plt.show is not called.
xlim (tuple, default=None) – if not None, sets the xlimits of the plot
ylim (tuple, default=None) – if not None, sets the ylimits of the plot
- Returns
- Return type
None
- report_fit(**kwargs)¶
Wrapper around lmfit.fit_report to report fit results.
- Parameters
kwargs (dict) – kwargs for lmfit.fit_report
- Returns
- Return type
None