# Path: src/naima/utils.py
# def trapz_loglog(y, x, axis=-1, intervals=False):
#     """
#     Integrate along the given axis using the composite trapezoidal rule in
#     loglog space.
# 
#     Integrate `y` (`x`) along given axis in loglog space.
# 
#     Parameters
#     ----------
#     y : array_like
#         Input array to integrate.
#     x : array_like, optional
#         Independent variable to integrate over.
#     axis : int, optional
#         Specify the axis.
# 
#     Returns
#     -------
#     trapz : float
#         Definite integral as approximated by trapezoidal rule in loglog space.
#     """
#     try:
#         y_unit = y.unit
#         y = y.value
#     except AttributeError:
#         y_unit = 1.0
#     try:
#         x_unit = x.unit
#         x = x.value
#     except AttributeError:
#         x_unit = 1.0
# 
#     y = np.asanyarray(y)
#     x = np.asanyarray(x)
# 
#     slice1 = [slice(None)] * y.ndim
#     slice2 = [slice(None)] * y.ndim
#     slice1[axis] = slice(None, -1)
#     slice2[axis] = slice(1, None)
# 
#     slice1 = tuple(slice1)
#     slice2 = tuple(slice2)
# 
#     if x.ndim == 1:
#         shape = [1] * y.ndim
#         shape[axis] = x.shape[0]
#         x = x.reshape(shape)
# 
#     with warnings.catch_warnings():
#         warnings.simplefilter("ignore")
#         # Compute the power law indices in each integration bin
#         b = np.log10(y[slice2] / y[slice1]) / np.log10(x[slice2] / x[slice1])
# 
#         # if local powerlaw index is -1, use \int 1/x = log(x); otherwise use
#         # normal powerlaw integration
#         trapzs = np.where(
#             np.abs(b + 1.0) > 1e-10,
#             (
#                 y[slice1]
#                 * (x[slice2] * (x[slice2] / x[slice1]) ** b - x[slice1])
#             )
#             / (b + 1),
#             x[slice1] * y[slice1] * np.log(x[slice2] / x[slice1]),
#         )
# 
#     tozero = (y[slice1] == 0.0) + (y[slice2] == 0.0) + (x[slice1] == x[slice2])
#     trapzs[tozero] = 0.0
# 
#     if intervals:
#         return trapzs * x_unit * y_unit
# 
#     ret = np.add.reduce(trapzs, axis) * x_unit * y_unit
# 
#     return ret


import numpy as np
from astropy.tests.helper import pytest
from ..utils import trapz_loglog
    from sherpa import ui
    from ..sherpa_models import Bremsstrahlung, InverseCompton, Synchrotron
    from ..sherpa_models import PionDecay
# Licensed under a 3-clause BSD style license - see LICENSE.rst


try:

    HAS_SHERPA = True
except ImportError:
    HAS_SHERPA = False

energies = np.logspace(8, 10, 10)  # 0.1 to 10 TeV in keV
test_spec_points = (
    1e-20
    * (energies / 1e9) ** -0.7
    * (1 + 0.2 * np.random.randn(energies.size))
)
test_err_points = 0.2 * test_spec_points

elo = energies[:-1]
ehi = energies[1:]

# Path: src/naima/utils.py
# def trapz_loglog(y, x, axis=-1, intervals=False):
#     """
#     Integrate along the given axis using the composite trapezoidal rule in
#     loglog space.
# 
#     Integrate `y` (`x`) along given axis in loglog space.
# 
#     Parameters
#     ----------
#     y : array_like
#         Input array to integrate.
#     x : array_like, optional
#         Independent variable to integrate over.
#     axis : int, optional
#         Specify the axis.
# 
#     Returns
#     -------
#     trapz : float
#         Definite integral as approximated by trapezoidal rule in loglog space.
#     """
#     try:
#         y_unit = y.unit
#         y = y.value
#     except AttributeError:
#         y_unit = 1.0
#     try:
#         x_unit = x.unit
#         x = x.value
#     except AttributeError:
#         x_unit = 1.0
# 
#     y = np.asanyarray(y)
#     x = np.asanyarray(x)
# 
#     slice1 = [slice(None)] * y.ndim
#     slice2 = [slice(None)] * y.ndim
#     slice1[axis] = slice(None, -1)
#     slice2[axis] = slice(1, None)
# 
#     slice1 = tuple(slice1)
#     slice2 = tuple(slice2)
# 
#     if x.ndim == 1:
#         shape = [1] * y.ndim
#         shape[axis] = x.shape[0]
#         x = x.reshape(shape)
# 
#     with warnings.catch_warnings():
#         warnings.simplefilter("ignore")
#         # Compute the power law indices in each integration bin
#         b = np.log10(y[slice2] / y[slice1]) / np.log10(x[slice2] / x[slice1])
# 
#         # if local powerlaw index is -1, use \int 1/x = log(x); otherwise use
#         # normal powerlaw integration
#         trapzs = np.where(
#             np.abs(b + 1.0) > 1e-10,
#             (
#                 y[slice1]
#                 * (x[slice2] * (x[slice2] / x[slice1]) ** b - x[slice1])
#             )
#             / (b + 1),
#             x[slice1] * y[slice1] * np.log(x[slice2] / x[slice1]),
#         )
# 
#     tozero = (y[slice1] == 0.0) + (y[slice2] == 0.0) + (x[slice1] == x[slice2])
#     trapzs[tozero] = 0.0
# 
#     if intervals:
#         return trapzs * x_unit * y_unit
# 
#     ret = np.add.reduce(trapzs, axis) * x_unit * y_unit
# 
#     return ret


# Path: src/naima/tests/test_sherpamod.py
import numpy as np
from astropy.tests.helper import pytest
from ..utils import trapz_loglog
    from sherpa import ui
    from ..sherpa_models import Bremsstrahlung, InverseCompton, Synchrotron
    from ..sherpa_models import PionDecay

# Licensed under a 3-clause BSD style license - see LICENSE.rst


try:

    HAS_SHERPA = True
except ImportError:
    HAS_SHERPA = False

energies = np.logspace(8, 10, 10)  # 0.1 to 10 TeV in keV
test_spec_points = (
    1e-20
    * (energies / 1e9) ** -0.7
    * (1 + 0.2 * np.random.randn(energies.size))
)
test_err_points = 0.2 * test_spec_points

elo = energies[:-1]
ehi = energies[1:]

test_spec_int = trapz_loglog(test_spec_points, energies, intervals=True)