Welcome to Naima¶
Naima is a Python package for computation of non-thermal radiation from relativistic particle populations. It includes tools to perform MCMC fitting of radiative models to X-ray, GeV, and TeV spectra using emcee, an affine-invariant ensemble sampler for Markov Chain Monte Carlo. Naima is an Astropy affiliated package.
There are two main components of the package: a set of nonthermal Radiative Models, and a set of utility functions that make it easier to fit a given model to observed spectral data (see Model fitting).
Nonthermal radiative models are available for Synchrotron, inverse Compton, Bremsstrahlung, and neutral pion decay processes. All of the models allow the use of an arbitrary shape of the particle energy distribution, and several functional models are also available to be used as particle distribution functions. See Radiative Models for a detailed explanation of these.
Use the sidebar on the left to access the documentation.
License & Attribution¶
Naima is released under a 3-clause BSD style license - see the LICENSE.rst for details.
If you find Naima useful in your research, you can cite Zabalza (2015) (arXiv, ADS) to acknowledge its use. The BibTeX entry for the paper is:
@ARTICLE{naima,
author = {{Zabalza}, V.},
title = {naima: a Python package for inference of relativistic particle
energy distributions from observed nonthermal spectra},
year = 2015,
journal = {Proc.~of International Cosmic Ray Conference 2015},
pages = "922",
eprint = {1509.03319},
adsurl = {http://adsabs.harvard.edu/abs/2015arXiv150903319Z},
}
Contributing¶
Please report any issues with the package here.
All development of Naima is done through the github repository, and contributions to the code are welcome. The development model is similar to that of astropy, so you can check the astropy Developer Documentation if you need information on how to make a code contribution.
Changelog¶
0.11.0 (2026-11-19)¶
API changes¶
These changes can alter the results of existing code without raising an error. See the entries below for details.
Calling
run_samplerwith a sampler that has already been run withrun_samplernow continues it and appends the new steps to its chain, instead of resetting the chain and starting over.Calling an
EblAbsorptionModelinstance now returns the absorption factorexp(-tau)instead of10**tau. Code that worked around the old value must be updated.transmissionis 0 above 100 TeV instead of 0.023.The initial walkers of logarithmic parameters (labels starting with
log10orlog) are spread by 1 dex (0.05 dex at the maximum likelihood) instead of a fraction of their value, which changes the starting point of these fits.Models that return NaN or raise an
ArithmeticErrororValueErrorfor a parameter vector give it a probability of zero instead of stopping the run.save_results_tablecomputesMaxLogLikelihoodandBICwithout the prior and without counting upper limits as data points.An unknown seed photon field name in
InverseComptonraises aValueErrorinstead of aTypeError.The
naima.model_utilsmodule and the_memoizeattribute of the models have been removed.
Bug fixes and other changes¶
run_samplercan now continue a sampler that has already been run, appending the new steps to its chain (a sampler fresh fromget_samplerstill has its burn-in discarded), andn_runis set to the number of steps in the chain. The process pool created by naima is recreated when needed and is closed also if the run fails or is interrupted.get_samplerhas a newpoolargument to use a pool provided by the user, which is never closed by naima (run_samplerpasses it on when it creates the sampler).Fix the initial walkers of
get_samplerfor parameters that start at zero, which got no spread because it is relative to the value. Parameters whose label starts withlognow use an absolute spread of 1 dex (0.05 dex whenp0is already the maximum likelihood, in the same proportion as the relative spread of the other parameters), since a relative spread of a logarithmic value depends on the units and vanishes close to zero. Walkers that start in a region forbidden by the prior are drawn again in a shrinking ball, so that they remain distinct, and a warning is logged ifp0is outside the prior.Fix
InverseCompton.fluxandInverseCompton.sedwith theseedargument returning the contribution computed for the energies of a previous call. Aseedequal to the number of seed photon fields, or negative, now raises aValueErrorinstead of anIndexErroror selecting the wrong field, and numpy integers are accepted.Remove the caching of results in the radiative models and particle distributions. The cache key ignored particle distributions without
param_names(plain functions andTableModel), so changing them returned the results for the previous parameters, andPionDecayKelner06shared its cache between instances. The cache did not help when fitting, as the models are created again for each evaluation. The_memoizeattribute no longer has any effect, and thenaima.model_utilsmodule, which only contained thememoizedecorator, has been removed.Fix
EblAbsorptionModel. Calling the instance returned10**tauinstead of the absorption factorexp(-tau)described in its docstring, which amplified the flux when multiplied to a model. The code read the optical depth table as if it containedlog10(tau), when it containstau.transmissionreturned the correct factor, but a constant 0.023 above 100 TeV where the factor is below 3e-9. It now returns 0 there, 1 for redshifts below 0.01 at any energy, and accepts scalar energies. The optical depth is limited to 700, the largest value for whichexp(-tau)does not underflow, and the table is now read from disk once instead of for every instance.get_samplerno longer modifies thep0array andlabelslist given by the caller (the normalization guess was applied in place and shortlabelswere extended in place). The normalization guess is skipped, with a warning, when the ratio between the data and model fluxes is not finite and positive, instead of settingp0toinf.Models that return NaN, or raise an arithmetic error or
ValueError(e.g. for parameters outside of the domain of a radiative model), now give a probability of zero for that parameter vector instead of stopping the run with emcee’s “Probability function returned NaN”. The first failure in each process is logged as a warning, and a warning is logged if no walker has a finite probability. This also applies to the maximum likelihood prefit ofget_sampler. Other exceptions are still raised. The blobs saved for those parameter vectors are NaN, and the plots ignore them.lnprobhas a newfallback_blobargument for this. Note thatValueErroralso covers errors that are bugs in the model, such as anUnitConversionError: they are only raised if they happen for the initialp0.save_results_tablereported the parameters with the highest posterior probability, including the prior, asML_parsand their log-probability asMaxLogLikelihood, which was also used for the BIC. The metadata now hasMAP_parsandMaxLogProbfor these, andMaxLogLikelihoodandBICare computed from the log-likelihood without the prior at that point.ML_parsis kept as an alias ofMAP_pars. Upper limits are no longer counted as data points in the BIC, and the number of data points is saved asn_data. If the sampler has no model function, the log-probability is used with a warning.The process pool created by
get_sampleris terminated when the sampler is garbage collected or the interpreter exits, instead of staying open ifrun_sampleris not used. The warning about the first failure of the model is shown again in every new fit of the same session, andlabelsthat start withlogare only taken as logarithmic parameters for the initial walkers if they start withlog10or what followslogis not a letter or digit (log10(cutoff),log10cutoffandlog_cutoff, but notlogisticorlog2).fluxandsedof the radiative models (also withseedforInverseCompton) accept scalar energies and return scalars.InverseComptonaccepts tuples both forseed_photon_fieldsand for the definition of each field (e.g.("star", 5000 * u.K, 1 * u.eV / u.cm**3)), raises aValueErrorwith a message for an unknown seed photon field name instead of a bareTypeError, andseed=Trueis no longer taken as the index 1.InverseCompton.sedwithseedonly computes the requested field instead of all of them.trapz_loglogintegrates each bin from the logarithms of its values, so that bins where the ratio of theyvalues or the producty * xoverflows no longer give0ornan, and the integral is precise and continuous for a local power-law index close to -1 and for very narrow bins.Fix docstrings: the default
nEedofInverseComptonis 100, theFIRandNIRseed photon fields were swapped,EpmaxofPionDecayis a maximum energy, and a table without an energy column mentions theenergycolumn in the error. Therun_samplerdocstring describes continued runs and the**kwargs, andget_samplerdocuments theloglabels.
0.10.5 (2026-11-14)¶
Fix the upper-limit likelihood in
lnprobmodelindexing the per-row confidence level with the number of violated upper limits instead of a mask. This applied the wrong confidence level whenclvaries between rows, and raised anIndexErrorwhen every upper limit was violated. See #261.The radiative model constructors (
Synchrotron,InverseCompton,Bremsstrahlung,PionDecayandPionDecayKelner06) now raise aTypeErrorfor unknown keyword arguments instead of silently ignoring them, e.g. a misspelledEemin. See #262.Fix
trapz_loglogoverflowing toinfin bins with a very steep local power-law index, which made the inverse Compton flux jump toinffor some parameter and energy combinations. Fixes #263.
0.10.4 (2026-09-05)¶
Fix incorrect log-prior formulas in
normal_priorandlog_uniform_prior. Fixes #253.Fix a mutable default argument bug in
plot_dataandplot_fitthat could leak upper-limit styling between calls.Add a
labelkeyword toplot_datafor setting a legend label on the plotted flux points. Fixes #237.plot_fitnow recomputes the model on a fine energy grid by default instead of reusing the (possibly sparse) values stored in the sampler blobs, fixing inconsistent plots for models such as synchrotron self-Compton. Fixes #240.save_results_tablenow records the physical unit of each parameter in a newunitcolumn. Fixes #243.Dropped support for Python 3.10, following astropy.
0.10.3 (2025-08-05)¶
Add overwrite keyword to
save_results_table- @HealthyPear
0.10.2 (2025-01-17)¶
Fix bug when Eemin and Eemax where set too close. Fixes #225.
0.10.1 (2025-01-17)¶
Dropped support for Python 3.9, updated to numpy v2, updated packaging.
0.10.0 (2021-11-02)¶
Updated emcee dependency to version 3. The interface to access the chain, log-probability, and blobs has changed accordingly, see emcee’s documentation.
0.9.1 (2020-01-31)¶
Fix exceptions thrown by
np.array_equalfor some combinations of model and blob outputs.
0.9.0 (2019-11-08)¶
Dropped Python 2 support. If you need to use naima in Python 2, please use version 0.8.4.
0.8.4 (2019-08-27)¶
Updated deprecated uses of numpy and astropy.Quantity logic
Updated tests for latest versions of dependencies
0.8.3 (2018-11-27)¶
Bug fixes¶
Fixed plotting scalar blobs with units.
Fixed plotting vector blobs with same length as data but incompatible units.
0.8.2 (2018-11-26)¶
Formatted source code with black.
Bug fixes¶
Fixed deprecated use of np.all and normed argument to matplotlib’s hist.
0.8.1 (2017-09-27)¶
Bug fixes¶
Fixed deprecated negative signs on numpy booleans.
Fixed wrong sign in delta functional approximation of Kelner PionDecay.
0.8 (2016-12-21)¶
Added a
threadsparameter toplot_fitandplot_samplesthat set the number of cores to use in computing model samples for plotting.Added a new model for EBL absorption based on the tables of Dominguez et al. 2011.
Bug fixes¶
Updated to use new
emceeautocorrelation API in version 2.2.Fixed sherpa models string representation.
0.7.1 (2016-02-04)¶
Packaging bugfix
0.7 (2016-02-04)¶
The
InverseComptonclass can now compute IC on arbitrary seed photon fields passed as arrays.plot_fitandplot_datahave new options (errorbar_optsandulim_opts) to control the properties of spectral flux points and upper-limits.There is a new table model class in
naima.models.TableModel.Added
corner(formertriangle_plot) as a dependency.
0.6.1 (2015-10-29)¶
Performance improvements to the memoize logic that result in 10% or higher improvement in model execution time.
0.6 (2015-09-10)¶
Medians and associated errors are now shown with a precision corresponding to a single significant digit in the errors (except when the leading digit is 1, when two significant digits are shown). Note that they are still saved with full precision to the result tables.
There is a new GUI tool for interactive model fitting:
InteractiveModelFitter, which can be accessed directly or through theinteractiveargument ofget_samplerprior to a sampling run.Sampling run results can be saved and retrieved for later analysis or archival with the new functions
naima.save_runandnaima.read_run.The individual contributions to the total Inverse Compton spectrum of the different seed photon fields can now be accessed through the
seedargument of theInverseCompton.fluxandInverseCompton.sedfunctions.
Bug Fixes¶
save_diagnostic_plotsnow turns matplotlib interactivity off, so the plots are only saved and not shown.The
groupcolumn is now preserved if a data table is validated more than once.
0.5 (2015-08-05)¶
save_results_tablenow saves the maximum log likelihood and ML parameters of the sample.Update
astropy_helpersto 1.0.2 and require astropy >= 1.0.2 to ensure pickleable Tables.Internal data is a QTable instead of a dict.
When multiple input spectra are used, they are now plotted with different colors and markers.
Now doing the prefit with a minimizer that allows for relative tolerance termination: in general prefit will be faster.
Add
e_rangeande_npointsparameters toplot_fitto allow computing the model samples for a wider energy range that the observed spectrum (or at energies between data sets, such as X-ray and gamma-ray)Added
plot_corneras a thin wrapper aroundtriangle.cornerwith ML parameter plotting.
0.4 (2015-03-19)¶
All models have a cache of length 10 so that the output is not computed when the parameters have not changed. It can be turned off through the
_memoizeattribute of the models.
Bug Fixes¶
Fixed concatenation of UL and CL in
validate_data_table.
0.3 (2015-02-19)¶
Added an option to save the distribution properties of scalar blobs when saving results table: option
include_blobsofsave_results_table.A new method for radiative classes allows to renormalize the particle distributions to a given energy content in particles. See
set_Weandset_Wpin leptonic and hadronic classes, respectively.The default FIR and NIR photon fields for
naima.InverseComptonhave been set to the GALPROP values at a galactic radius of 6.5 kpc.Require astropy >= 1.0.
Bug Fixes¶
Fixed sed conversion of residuals [#69]
Fixed
plot_dataso it can take lists of data_tables.
API Changes¶
The name of the
table_formatparameter ofsave_results_tablehas been changed toformatfor consistency withastropy.io.ascii.
0.2 (2015-02-10)¶
Refactored sherpa models to use parent SherpaModelECPL class [#62]
Added a data_sed flag to get_sampler to select whether to convert all data tables to SED or choose format of first data tables when providing multiple data tables.
Added support for a
flux_ulcolumn in input data tables.Added a method to estimate magnetic field:
naima.estimate_B.Added the option to perform an optimization of the parameters before the MCMC run: see option
prefitinnaima.get_sampler.Convert between SED and differential fluxes automatically if the model and data physical types do not match.
Add blob_labels parameter to save_diagnostic_plots.
Bug Fixes¶
Fix sherpa models guess() for integrated datasets.
Only complain about CL when there are ULs at a different CL.
Fix parsing of string upper limit columns in Python 3.
Use old energy unit when plotting a new data set onto a figure [#64]
Show ordinate units when plotting blobs without spectral data.
API Changes¶
module sherpamod is now sherpa_modules.
0.1 (2015-02-02)¶
Initial release