<commit_before>import time, sys, os
import numpy as np
from scipy.linalg import LinAlgError

__all__ = ["lnlike_spec", "lnlike_phot", "chi_spec", "chi_phot", "write_log"]


def lnlike_spec(spec_mu, obs=None, spec_noise=None, f_outlier_spec=0.0, **vectors):
        """Calculate the likelihood of the spectroscopic data given the
        spectroscopic model.  Allows for the use of a gaussian process
        covariance matrix for multiplicative residuals.

        :param spec_mu:
            The mean model spectrum, in linear or logarithmic units, including
            e.g. calibration and sky emission.

        :param obs: (optional)
            A dictionary of the observational data, including the keys
            *``spectrum`` a numpy array of the observed spectrum, in linear or
             logarithmic units (same as ``spec_mu``).
            *``unc`` the uncertainty of same length as ``spectrum``
            *``mask`` optional boolean array of same length as ``spectrum``
            *``wavelength`` if using a GP, the metric that is used in the
             kernel generation, of same length as ``spectrum`` and typically
             giving the wavelength array.

        :param spec_noise: (optional)
            A NoiseModel object with the methods `compute` and `lnlikelihood`.
            If ``spec_noise`` is supplied, the `wavelength` entry in the obs
            dictionary must exist.

        :param vectors: (optional)
            A dictionary of vectors of same length as ``wavelength`` giving
            possible weghting functions for the kernels

        :returns lnlikelhood:
            The natural logarithm of the likelihood of the data given the mean
            model spectrum.
        """
        if obs['spectrum'] is None:
            return 0.0

        mask = obs.get('mask', slice(None))
        vectors['mask'] = mask
        vectors['wavelength'] = obs['wavelength']

        delta = (obs['spectrum'] - spec_mu)[mask]
        var = (obs['unc'][mask])**2

        if spec_noise is not None:
            try:
                spec_noise.compute(**vectors)
                if (f_outlier_spec == 0.0):
                    return spec_noise.lnlikelihood(delta)

                # disallow (correlated noise model + mixture model)
                assert spec_noise.Sigma.ndim == 1

                # redefine errors, scaling sigma_bad by (jittered sigma / original sigma)
                var_bad = spec_noise.Sigma * (vectors["nsigma_bad"]**2)
                var = spec_noise.Sigma
            except(LinAlgError):
                return np.nan_to_num(-np.inf)

        lnp = -0.5*( (delta**2/var) + np.log(2*np.pi*var) )
        if (f_outlier_spec == 0.0):
            return lnp.sum()
        else:
            lnp_bad = -0.5*( (delta**2/var_bad) + np.log(2*np.pi*var_bad) )
            lnp_tot = np.logaddexp(lnp + np.log(1-f_outlier_spec), lnp_bad + np.log(f_outlier_spec))

            return lnp_tot.sum()

def lnlike_phot(phot_mu, obs=None, phot_noise=None, **vectors):
    """Calculate the likelihood of the photometric data given the spectroscopic
    model.  Allows for the use of a gaussian process covariance matrix.

    :param phot_mu:<commit_msg>allow mixture model without noise model<commit_after>import time, sys, os
import numpy as np
from scipy.linalg import LinAlgError

__all__ = ["lnlike_spec", "lnlike_phot", "chi_spec", "chi_phot", "write_log"]


def lnlike_spec(spec_mu, obs=None, spec_noise=None, f_outlier_spec=0.0, **vectors):
        """Calculate the likelihood of the spectroscopic data given the
        spectroscopic model.  Allows for the use of a gaussian process
        covariance matrix for multiplicative residuals.

        :param spec_mu:
            The mean model spectrum, in linear or logarithmic units, including
            e.g. calibration and sky emission.

        :param obs: (optional)
            A dictionary of the observational data, including the keys
            *``spectrum`` a numpy array of the observed spectrum, in linear or
             logarithmic units (same as ``spec_mu``).
            *``unc`` the uncertainty of same length as ``spectrum``
            *``mask`` optional boolean array of same length as ``spectrum``
            *``wavelength`` if using a GP, the metric that is used in the
             kernel generation, of same length as ``spectrum`` and typically
             giving the wavelength array.

        :param spec_noise: (optional)
            A NoiseModel object with the methods `compute` and `lnlikelihood`.
            If ``spec_noise`` is supplied, the `wavelength` entry in the obs
            dictionary must exist.

        :param vectors: (optional)
            A dictionary of vectors of same length as ``wavelength`` giving
            possible weghting functions for the kernels

        :returns lnlikelhood:
            The natural logarithm of the likelihood of the data given the mean
            model spectrum.
        """
        if obs['spectrum'] is None:
            return 0.0

        mask = obs.get('mask', slice(None))
        vectors['mask'] = mask
        vectors['wavelength'] = obs['wavelength']

        delta = (obs['spectrum'] - spec_mu)[mask]
        var = (obs['unc'][mask])**2

        if spec_noise is not None:
            try:
                spec_noise.compute(**vectors)
                if (f_outlier_spec == 0.0):
                    return spec_noise.lnlikelihood(delta)

                # disallow (correlated noise model + mixture model)
                # and redefine errors
                assert spec_noise.Sigma.ndim == 1
                var = spec_noise.Sigma

            except(LinAlgError):
                return np.nan_to_num(-np.inf)

        lnp = -0.5*( (delta**2/var) + np.log(2*np.pi*var) )
        if (f_outlier_spec == 0.0):
            return lnp.sum()
        else:
            var_bad = var * (vectors["nsigma_bad"]**2)
            lnp_bad = -0.5*( (delta**2/var_bad) + np.log(2*np.pi*var_bad) )
            lnp_tot = np.logaddexp(lnp + np.log(1-f_outlier_spec), lnp_bad + np.log(f_outlier_spec))

            return lnp_tot.sum()

def lnlike_phot(phot_mu, obs=None, phot_noise=None, **vectors):
    """Calculate the likelihood of the photometric data given the spectroscopic
    model.  Allows for the use of a gaussian process covariance matrix.

    :param phot_mu:
{'fraction_non_alphanumeric': 0.09576977512187451, 'fraction_numerical': 0.006290297216543481, 'mean_word_length': 2.213744315310763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7466222', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1729, 'n_words': 598}