<commit_before>__author__ = 'giacomov'
# DMFitFunction and DMSpectra add by <NAME> (<EMAIL>) Oct 26, 2016

import exceptions
import math

import astropy.units as astropy_units
import numpy as np
import warnings
from scipy.special import gammaincc, gamma, erfcinv

from astromodels.core.units import get_units
from astromodels.functions.function import Function1D, FunctionMeta, ModelAssertionViolation


class GSLNotAvailable(ImportWarning):
    pass


class NaimaNotAvailable(ImportWarning):
    pass


class InvalidUsageForFunction(exceptions.Exception):
    pass


# Now let's try and import optional dependencies

try:

    # Naima is for numerical computation of Synch. and Inverse compton spectra in randomly oriented
    # magnetic fields

    import naima
    import astropy.units as u

except ImportError:

    warnings.warn("The naima package is not available. Models that depend on it will not be available",
                  NaimaNotAvailable)

    has_naima = False

else:

    has_naima = True

try:

    # GSL is the GNU Scientific Library. Pygsl is the python wrapper for it. It is used by some
    # functions for faster computation

    from pygsl.testing.sf import gamma_inc

except ImportError:

    warnings.warn("The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it will not be "
                  "available.", GSLNotAvailable)

    has_gsl = False

else:

    has_gsl = True


# noinspection PyPep8Naming
class Powerlaw(Function1D):
    r"""
    description :

        A simple power-law

    latex : $ K~\frac{x}{piv}^{index} $

    parameters :

        K :

            desc : Normalization (differential flux at the pivot value)
            initial value : 1.0
            is_normalization : True

        piv :

            desc : Pivot value
            initial value : 1
            fix : yes

        index :

            desc : Photon index
            initial value : -2
            min : -10
            max : 10

    tests :
        - { x : 10, function value: 0.01, tolerance: 1e-20}
        - { x : 100, function value: 0.0001, tolerance: 1e-20}

    """

    __metaclass__ = FunctionMeta

    def _set_units(self, x_unit, y_unit):
        # The index is always dimensionless
        self.index.unit = astropy_units.dimensionless_unscaled

        # The pivot energy has always the same dimension as the x variable
        self.piv.unit = x_unit

        # The normalization has the same units as the y

        self.K.unit = y_unit

    # noinspection PyPep8Naming
    def evaluate(self, x, K, piv, index):
        xx = np.divide(x, piv)

        return K * np.power(xx, index)


# noinspection PyPep8Naming
class Powerlaw_flux(Function1D):
    r"""
        description :

            A simple power-law with the photon flux in a band used as normalization. This will reduce the correlation
            between the index and the normalization.

        latex : $ \frac{F(\gamma+1)} {b^{\gamma+1} - a^{\gamma+1}} (x)^{\gamma}$

        parameters :

            F :

                desc : Integral between a and b
                initial value : 1
                is_normalization : True

            index :

                desc : Photon index
                initial value : -2
                min : -10<commit_msg>Added the Powerlaw_lognorm function<commit_after>__author__ = 'giacomov'
# DMFitFunction and DMSpectra add by <NAME> (<EMAIL>) Oct 26, 2016

import exceptions
import math

import astropy.units as astropy_units
import numpy as np
import warnings
from scipy.special import gammaincc, gamma, erfcinv

from astromodels.core.units import get_units
from astromodels.functions.function import Function1D, FunctionMeta, ModelAssertionViolation


class GSLNotAvailable(ImportWarning):
    pass


class NaimaNotAvailable(ImportWarning):
    pass


class InvalidUsageForFunction(exceptions.Exception):
    pass


# Now let's try and import optional dependencies

try:

    # Naima is for numerical computation of Synch. and Inverse compton spectra in randomly oriented
    # magnetic fields

    import naima
    import astropy.units as u

except ImportError:

    warnings.warn("The naima package is not available. Models that depend on it will not be available",
                  NaimaNotAvailable)

    has_naima = False

else:

    has_naima = True

try:

    # GSL is the GNU Scientific Library. Pygsl is the python wrapper for it. It is used by some
    # functions for faster computation

    from pygsl.testing.sf import gamma_inc

except ImportError:

    warnings.warn("The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it will not be "
                  "available.", GSLNotAvailable)

    has_gsl = False

else:

    has_gsl = True


# noinspection PyPep8Naming
class Powerlaw_lognorm(Function1D):
    r"""
    description :

        A simple power-law

    latex : $ K~\frac{x}{piv}^{index} $

    parameters :

        K :

            desc : Normalization (log of differential flux at the pivot value)
            initial value : -6.0
            is_normalization : True

        piv :

            desc : Pivot value
            initial value : 1
            fix : yes

        index :

            desc : Photon index
            initial value : -2
            min : -10
            max : 10

    """

    __metaclass__ = FunctionMeta

    def _set_units(self, x_unit, y_unit):
        # The index is always dimensionless
        self.index.unit = astropy_units.dimensionless_unscaled

        # The pivot energy has always the same dimension as the x variable
        self.piv.unit = x_unit

        # The normalization has the same units as the y

        self.K.unit = astropy_units.dex(y_unit)

    # noinspection PyPep8Naming
    def evaluate(self, x, K, piv, index):

        xx = np.divide(x, piv)

        try:

            return 10**K * np.power(xx, index)

        except AttributeError:

            # If units are used, the former will fail because astropy does not support 10**K
            # by using .physical we get the value in y_unit
            return K.physical * np.power(xx, index)


class Powerlaw(Function1D):
        r"""
        description :

            A simple power-law

        latex : $ K~\frac{x}{piv}^{index} $

        parameters :

            K :

                desc : Normalization (differential flux at the pivot value)
                initial value : 1.0
                is_normalization : True

            piv :

                desc : Pivot value
                initial value : 1
                fix : yes

            index :

                desc : Photon index
                initial value : -2
                min : -10
                max : 10

        tests :
            - { x : 10, function value: 0.01, tolerance: 1e-20}
            - { x : 100, function value: 0.0001, tolerance: 1e-20}

        """

        __metaclass__ = FunctionMeta

        def _set_units(self, x_unit, y_unit):
            # The index is always dimensionless
            self.index.unit = astropy_units.dimensionless_unscaled

            # The pivot energy has always the same dimension as the x variable
            self.piv.unit = x_unit

            # The normalization has the same units as the y

            self.K.unit = y_unit

        # noinspection PyPep8Naming
        def evaluate(self, x, K, piv, index):
            xx = np.divide(x, piv)

            return K * np.power(xx, index)



# noinspection PyPep8Naming
class Powerlaw_flux(Function1D):
    r"""
        description :

            A simple power-law with the photon flux in a band used as normalization. This will reduce the correlation
            between the index and the normalization.

        latex : $ \frac{F(\gamma+1)} {b^{\gamma+1} - a^{\gamma+1}} (x)^{\gamma}$

        parameters :

            F :

                desc : Integral between a and b
                initial value : 1
                is_normalization : True

            index :

                desc : Photon index
                initial value : -2
                min : -10
{'fraction_non_alphanumeric': 0.06795395469736354, 'fraction_numerical': 0.013120435697487312, 'mean_word_length': 1.9575402635431918, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '909299', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2210, 'n_words': 791}