<commit_before>from math import isnan

import numpy as np
from mosfit.constants import LIKELIHOOD_FLOOR
from mosfit.modules.module import Module

CLASS_NAME = 'Likelihood'


class Likelihood(Module):
    """Calculate the maximum likelihood score for a model.
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def process(self, **kwargs):
        self._model_mags = kwargs['model_magnitudes']
        self._times = kwargs['times']
        self._mags = kwargs['magnitudes']
        self._e_mags = kwargs['e_magnitudes']
        self._upper_limits = kwargs['upperlimits']
        self._fractions = kwargs['fractions']
        if min(self._fractions) < 0.0 or max(self._fractions) > 1.0:
            return {'value': LIKELIHOOD_FLOOR}
        for mi, mag in enumerate(self._model_mags):
            if isnan(mag) or (not self._upper_limits[mi] and
                              not np.isfinite(mag)):
                return {'value': LIKELIHOOD_FLOOR}
        self._variance2 = kwargs['variance']**2
        self._e_mags = [kwargs['default_upper_limit_error']
                        if x == '' and self._upper_limits[i] else x
                        for i, x in enumerate(self._e_mags)]
        self._n_mags = len(self._mags)

        sum_members = [
            (x - y if not u or x < y else 0.0)**2 /
            (z**2 + self._variance2) + np.log(self._variance2 + z**2)
            for x, y, z, u in zip(self._model_mags, self._mags, self._e_mags,
                                  self._upper_limits)
        ]
        value = -0.5 * np.sum(sum_members)
        if isnan(value):
            return {'value': LIKELIHOOD_FLOOR}
        # if min(x) < 0.0:
        #     value = value + self._n_mags * np.sum([y for y in x if y < 0.0])
        # if max(x) > 1.0:
        #     value = value + self._n_mags * np.sum(
        #         [1.0 - y for y in x if y > 1.0])
        return {'value': value}<commit_msg>MAINT: likelihood now properly deals with NaN model mags for upp lims<commit_after>from math import isnan

import numpy as np
from mosfit.constants import LIKELIHOOD_FLOOR
from mosfit.modules.module import Module

CLASS_NAME = 'Likelihood'


class Likelihood(Module):
    """Calculate the maximum likelihood score for a model.
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def process(self, **kwargs):
        self._model_mags = kwargs['model_magnitudes']
        self._times = kwargs['times']
        self._mags = kwargs['magnitudes']
        self._e_mags = kwargs['e_magnitudes']
        self._upper_limits = kwargs['upperlimits']
        self._fractions = kwargs['fractions']
        if min(self._fractions) < 0.0 or max(self._fractions) > 1.0:
            return {'value': LIKELIHOOD_FLOOR}
        for mi, mag in enumerate(self._model_mags):
            if (not self._upper_limits[mi] and
                    (isnan(mag) or not np.isfinite(mag))):
                return {'value': LIKELIHOOD_FLOOR}
        self._variance2 = kwargs['variance']**2
        self._e_mags = [
            kwargs['default_upper_limit_error']
            if x == '' and self._upper_limits[i] else x
            for i, x in enumerate(self._e_mags)
        ]
        self._n_mags = len(self._mags)

        sum_members = [
            (x - y if not u or (x < y and not isnan(x)) else 0.0)**2 /
            (z**2 + self._variance2) + np.log(self._variance2 + z**2)
            for x, y, z, u in zip(self._model_mags, self._mags, self._e_mags,
                                  self._upper_limits)
        ]
        value = -0.5 * np.sum(sum_members)
        if isnan(value):
            return {'value': LIKELIHOOD_FLOOR}
        # if min(x) < 0.0:
        #     value = value + self._n_mags * np.sum([y for y in x if y < 0.0])
        # if max(x) > 1.0:
        #     value = value + self._n_mags * np.sum(
        #         [1.0 - y for y in x if y > 1.0])
        return {'value': value}
{'fraction_non_alphanumeric': 0.12287813529262731, 'fraction_numerical': 0.01266784899923993, 'mean_word_length': 1.9931766489764973, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3002476', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1292, 'n_words': 323}