# Path: src/naima/utils.py
# def sed_conversion(energy, model_unit, sed):
#     """
#     Manage conversion between differential spectrum and SED
#     """
# 
#     model_pt = model_unit.physical_type
#     is_integral = model_pt in {
#         u.get_physical_type("power"),
#         u.get_physical_type("energy"),
#         u.get_physical_type("energy flux"),
#     }
#     is_differential = model_pt in {
#         u.get_physical_type("differential flux"),
#         u.get_physical_type("differential power"),
#         u.get_physical_type("differential energy"),
#         u.get_physical_type("differential number density"),
#     }
# 
#     ones = np.ones(energy.shape)
# 
#     if (sed and is_integral) or (not sed and is_differential):
#         sedf = ones
#     elif sed and is_differential:
#         sedf = energy ** 2
#     elif not sed and is_integral:
#         sedf = 1 / (energy ** 2)
#     else:
#         raise u.UnitsError(
#             "Model physical type ({0}) is not supported".format(model_pt),
#             "Supported physical types are: power, flux, differential"
#             " power, differential flux",
#         )
# 
#     is_energy_flux = model_pt in {
#         u.get_physical_type("energy"),
#         u.get_physical_type("differential energy"),
#     }
#     is_particle_flux = model_pt in {
#         u.get_physical_type("energy flux"),
#         u.get_physical_type("differential flux"),
#     }
# 
#     if sed:
#         if is_energy_flux:
#             f_unit = u.erg
#         elif is_particle_flux:
#             f_unit = u.erg / u.s / u.cm ** 2
#         else:
#             f_unit = u.erg / u.s
#     else:
#         if is_energy_flux:
#             f_unit = u.Unit("1/TeV")
#         elif is_particle_flux:
#             f_unit = u.Unit("1/(s TeV cm2)")
#         else:
#             f_unit = u.Unit("1/(s TeV)")
# 
#     log.debug(
#         "Converted from {0} ({1}) into {2} ({3}) for sed={4}".format(
#             model_unit, model_pt, f_unit, f_unit.physical_type, sed
#         )
#     )
# 
#     return f_unit, sedf
# 
# def validate_data_table(data_table, sed=None):
#     """
#     Validate all columns of a data table. If a list of tables is passed, all
#     tables will be validated and then concatenated
# 
#     Parameters
#     ----------
# 
#     data_table : `astropy.table.Table` or list of `astropy.table.Table`.
# 
#     sed : bool, optional
#         Whether to convert the fluxes to SED. If unset, all data tables are
#         converted to the format of the first data table.
#     """
#     if isinstance(data_table, Table) or isinstance(data_table, QTable):
#         data_table = [data_table]
# 
#     try:
#         for dt in data_table:
#             if not isinstance(dt, Table) and not isinstance(dt, QTable):
#                 raise TypeError(
#                     "An object passed as data_table is not an astropy Table!"
#                 )
#     except TypeError:
#         raise TypeError(
#             "Argument passed to validate_data_table is not a table and "
#             "not a list"
#         )
# 
#     def dt_sed_conversion(dt, sed):
#         f_unit, sedf = sed_conversion(dt["energy"], dt["flux"].unit, sed)
#         # roundtrip to Table to change the units
#         t = Table(dt)
#         for col in ["flux", "flux_error_lo", "flux_error_hi"]:
#             t[col].unit = f_unit
#         ndt = QTable(t)
# 
#         ndt["flux"] = (dt["flux"] * sedf).to(f_unit)
#         ndt["flux_error_lo"] = (dt["flux_error_lo"] * sedf).to(f_unit)
#         ndt["flux_error_hi"] = (dt["flux_error_hi"] * sedf).to(f_unit)
# 
#         return ndt
# 
#     data_list = []
#     for group, dt in enumerate(data_table):
#         dt_val = _validate_single_data_table(dt, group=group)
#         data_list.append(dt_val)
# 
#     # concatenate input data tables
#     data_new = data_list[0].copy()
#     f_pt = data_new["flux"].unit.physical_type
#     if sed is None:
#         sed = f_pt in ["flux", "power"]
# 
#     data_new = dt_sed_conversion(data_new, sed)
# 
#     for dt in data_list[1:]:
#         nf_pt = dt["flux"].unit.physical_type
#         if ("flux" in nf_pt and "power" in f_pt) or (
#             "power" in nf_pt and "flux" in f_pt
#         ):
#             raise TypeError(
#                 "The physical types of the data tables could not be "
#                 "matched: Some are in flux and others in luminosity units"
#             )
# 
#         dt = dt_sed_conversion(dt, sed)
# 
#         for row in dt:
#             data_new.add_row(row)
# 
#     return data_new


import multiprocessing
import warnings
import astropy.units as u
import emcee
import numpy as np
            import matplotlib.pyplot as plt
from collections.abc import Iterable
from astropy import log
from .utils import sed_conversion, validate_data_table
    from .extern.minimize import minimize
            from .model_fitter import InteractiveModelFitter
# -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst



__all__ = [
    "normal_prior",
    "uniform_prior",
    "log_uniform_prior",
    "get_sampler",
    "run_sampler",
]

# Define phsyical types used in plot and utils.validate_data_table
u.def_physical_type(u.erg / u.cm ** 2 / u.s, "flux")

# Path: src/naima/utils.py
# def sed_conversion(energy, model_unit, sed):
#     """
#     Manage conversion between differential spectrum and SED
#     """
# 
#     model_pt = model_unit.physical_type
#     is_integral = model_pt in {
#         u.get_physical_type("power"),
#         u.get_physical_type("energy"),
#         u.get_physical_type("energy flux"),
#     }
#     is_differential = model_pt in {
#         u.get_physical_type("differential flux"),
#         u.get_physical_type("differential power"),
#         u.get_physical_type("differential energy"),
#         u.get_physical_type("differential number density"),
#     }
# 
#     ones = np.ones(energy.shape)
# 
#     if (sed and is_integral) or (not sed and is_differential):
#         sedf = ones
#     elif sed and is_differential:
#         sedf = energy ** 2
#     elif not sed and is_integral:
#         sedf = 1 / (energy ** 2)
#     else:
#         raise u.UnitsError(
#             "Model physical type ({0}) is not supported".format(model_pt),
#             "Supported physical types are: power, flux, differential"
#             " power, differential flux",
#         )
# 
#     is_energy_flux = model_pt in {
#         u.get_physical_type("energy"),
#         u.get_physical_type("differential energy"),
#     }
#     is_particle_flux = model_pt in {
#         u.get_physical_type("energy flux"),
#         u.get_physical_type("differential flux"),
#     }
# 
#     if sed:
#         if is_energy_flux:
#             f_unit = u.erg
#         elif is_particle_flux:
#             f_unit = u.erg / u.s / u.cm ** 2
#         else:
#             f_unit = u.erg / u.s
#     else:
#         if is_energy_flux:
#             f_unit = u.Unit("1/TeV")
#         elif is_particle_flux:
#             f_unit = u.Unit("1/(s TeV cm2)")
#         else:
#             f_unit = u.Unit("1/(s TeV)")
# 
#     log.debug(
#         "Converted from {0} ({1}) into {2} ({3}) for sed={4}".format(
#             model_unit, model_pt, f_unit, f_unit.physical_type, sed
#         )
#     )
# 
#     return f_unit, sedf
# 
# def validate_data_table(data_table, sed=None):
#     """
#     Validate all columns of a data table. If a list of tables is passed, all
#     tables will be validated and then concatenated
# 
#     Parameters
#     ----------
# 
#     data_table : `astropy.table.Table` or list of `astropy.table.Table`.
# 
#     sed : bool, optional
#         Whether to convert the fluxes to SED. If unset, all data tables are
#         converted to the format of the first data table.
#     """
#     if isinstance(data_table, Table) or isinstance(data_table, QTable):
#         data_table = [data_table]
# 
#     try:
#         for dt in data_table:
#             if not isinstance(dt, Table) and not isinstance(dt, QTable):
#                 raise TypeError(
#                     "An object passed as data_table is not an astropy Table!"
#                 )
#     except TypeError:
#         raise TypeError(
#             "Argument passed to validate_data_table is not a table and "
#             "not a list"
#         )
# 
#     def dt_sed_conversion(dt, sed):
#         f_unit, sedf = sed_conversion(dt["energy"], dt["flux"].unit, sed)
#         # roundtrip to Table to change the units
#         t = Table(dt)
#         for col in ["flux", "flux_error_lo", "flux_error_hi"]:
#             t[col].unit = f_unit
#         ndt = QTable(t)
# 
#         ndt["flux"] = (dt["flux"] * sedf).to(f_unit)
#         ndt["flux_error_lo"] = (dt["flux_error_lo"] * sedf).to(f_unit)
#         ndt["flux_error_hi"] = (dt["flux_error_hi"] * sedf).to(f_unit)
# 
#         return ndt
# 
#     data_list = []
#     for group, dt in enumerate(data_table):
#         dt_val = _validate_single_data_table(dt, group=group)
#         data_list.append(dt_val)
# 
#     # concatenate input data tables
#     data_new = data_list[0].copy()
#     f_pt = data_new["flux"].unit.physical_type
#     if sed is None:
#         sed = f_pt in ["flux", "power"]
# 
#     data_new = dt_sed_conversion(data_new, sed)
# 
#     for dt in data_list[1:]:
#         nf_pt = dt["flux"].unit.physical_type
#         if ("flux" in nf_pt and "power" in f_pt) or (
#             "power" in nf_pt and "flux" in f_pt
#         ):
#             raise TypeError(
#                 "The physical types of the data tables could not be "
#                 "matched: Some are in flux and others in luminosity units"
#             )
# 
#         dt = dt_sed_conversion(dt, sed)
# 
#         for row in dt:
#             data_new.add_row(row)
# 
#     return data_new


# Path: src/naima/core.py
import multiprocessing
import warnings
import astropy.units as u
import emcee
import numpy as np
            import matplotlib.pyplot as plt
from collections.abc import Iterable
from astropy import log
from .utils import sed_conversion, validate_data_table
    from .extern.minimize import minimize
            from .model_fitter import InteractiveModelFitter

# -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst



__all__ = [
    "normal_prior",
    "uniform_prior",
    "log_uniform_prior",
    "get_sampler",
    "run_sampler",
]

# Define phsyical types used in plot and utils.validate_data_table
u.def_physical_type(u.erg / u.cm ** 2 / u.s, "flux")

u.def_physical_type(u.Unit("1/(s cm2 erg)"), "differential flux")