<commit_before># coding: utf-8

from __future__ import division, print_function

__author__ = "adrn <<EMAIL>>"

# Standard library
import os, sys
from collections import OrderedDict

# Third-party
import numpy as np
import astropy.units as u
from astropy import log as logger

# Project
from .parameter import ModelParameter
from .prior import *

__all__ = ["EmceeModel"]

class EmceeModel(object):

    def __init__(self, ln_likelihood, ln_prior=None, args=()):
        """ """

        self.parameters = OrderedDict()
        self.nparameters = 0

        if ln_prior is not None:
            self.ln_prior = ln_prior

        self.ln_likelihood = ln_likelihood
        self.args = args

        if not hasattr(self.ln_likelihood, "__call__"):
            raise TypeError("ln_likelihood must be callable.")
        elif not hasattr(self.ln_prior, "__call__"):
            raise TypeError("ln_prior must be callable.")

    def add_parameter(self, param, group=None):
        """ Add a parameter to the model.

            Parameters
            ----------
            param : ModelParameter
                The parameter instance.
        """

        if not isinstance(param, ModelParameter):
            raise TypeError("Invalid parameter type '{}'".format(type(param)))

        if group is not None and group not in self.parameters.keys():
            self.parameters[group] = OrderedDict()
            self.parameters[group][param.name] = param.copy()
        else:
            self.parameters[param.name] = param.copy()

        self.nparameters += param.size

    def _walk(self):
        """ Walk through a dictionary tree with maximum depth=2 """
        group = None
        for name,param in self.parameters.items():
            if hasattr(param,"items"):
                group = name
                for grp,name,param in walk_dict(param):
                    if param.frozen: # skip frozen parameters
                        continue
                    else:
                        yield group, name, param
            else:
                yield group, name, param

    def ln_prior(self, parameters, value_dict, *args):
        """ Default prior -- if none specified, evaluates the priors
            over each parameter at the specified dictionary of values
        """
        ln_prior = 0.
        for group_name,param_name,param in self._walk():
            if group_name is None:
                v = value_dict[param_name]
            else:
                v = value_dict[group_name][param_name]
            ln_prior += param.prior(v)

        return ln_prior

    @property
    def truth_vector(self):
        """ Returns an array of the true values of all parameters in the model """

        true_p = np.array([])<commit_msg>fix walk<commit_after># coding: utf-8

from __future__ import division, print_function

__author__ = "adrn <<EMAIL>>"

# Standard library
import os, sys
from collections import OrderedDict

# Third-party
import numpy as np
import astropy.units as u
from astropy import log as logger

# Project
from .parameter import ModelParameter
from .prior import *

__all__ = ["EmceeModel"]

def walk_dict(d):
    group = None
    for name,param in d.items():
        if hasattr(param,"items"):
            group = name
            for grp,name,param in walk_dict(param):
                if param.frozen: # skip frozen parameters
                    continue
                else:
                    yield group, name, param
        else:
            yield group, name, param

class EmceeModel(object):

    def __init__(self, ln_likelihood, ln_prior=None, args=()):
        """ """

        self.parameters = OrderedDict()
        self.nparameters = 0

        if ln_prior is not None:
            self.ln_prior = ln_prior

        self.ln_likelihood = ln_likelihood
        self.args = args

        if not hasattr(self.ln_likelihood, "__call__"):
            raise TypeError("ln_likelihood must be callable.")
        elif not hasattr(self.ln_prior, "__call__"):
            raise TypeError("ln_prior must be callable.")

    def add_parameter(self, param, group=None):
        """ Add a parameter to the model.

            Parameters
            ----------
            param : ModelParameter
                The parameter instance.
        """

        if not isinstance(param, ModelParameter):
            raise TypeError("Invalid parameter type '{}'".format(type(param)))

        if group is not None and group not in self.parameters.keys():
            self.parameters[group] = OrderedDict()
            self.parameters[group][param.name] = param.copy()
        else:
            self.parameters[param.name] = param.copy()

        self.nparameters += param.size

    def _walk(self):
        """ Walk through a dictionary tree with maximum depth=2 """
        for tup in walk_dict(self.parameters):
            yield tup

    def ln_prior(self, parameters, value_dict, *args):
        """ Default prior -- if none specified, evaluates the priors
            over each parameter at the specified dictionary of values
        """
        ln_prior = 0.
        for group_name,param_name,param in self._walk():
            if group_name is None:
                v = value_dict[param_name]
            else:
                v = value_dict[group_name][param_name]
            ln_prior += param.prior(v)

        return ln_prior

    @property
    def truth_vector(self):
        """ Returns an array of the true values of all parameters in the model """

        true_p = np.array([])
{'fraction_non_alphanumeric': 0.09260264062217399, 'fraction_numerical': 0.0014469162597214686, 'mean_word_length': 2.0136239782016347, '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': '3075380', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1449, 'n_words': 464}