<commit_before>from __future__ import division, print_function
import unittest
import sys
import warnings

import numpy as np
import pickle
import multiprocessing as mp

from symfit import (
    Variable, Parameter, Eq, Ge, Le, Lt, Gt, Ne, parameters, ModelError, Fit,
    Model, FitResults, variables, CallableNumericalModel, Constraint
)
from symfit.core.minimizers import *
from symfit.core.support import partial

# Defined at the global level because local functions can't be pickled.
def f(x, a, b):
    return a * x + b

def chi_squared(x, y, a, b, sum=True):
    if sum:
        return np.sum((y - f(x, a, b)) ** 2)
    else:
        return (y - f(x, a, b)) ** 2

def worker(fit_obj):
    return fit_obj.execute()


def subclasses(object, all_subs=None):
    """
    Recursively create a set of subclasses of ``object``. Returns only
    the leaves in the subclass tree.

    :param object: Class
    :param all_subs: set of subclasses so far. Will be build internally.
    :return: All leaves of the subclass tree.
    """
    if all_subs is None:
        all_subs = set()
    object_subs = set(object.__subclasses__())
    all_subs.update(object_subs)
    for sub in object_subs:
        sub_subs = subclasses(sub, all_subs=all_subs)
        # Only keep the leaves of the tree
        if sub_subs and object in all_subs:
            all_subs.remove(object)
        all_subs.update(sub_subs)
    return all_subs


class TestMinimize(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        np.random.seed(0)

    def test_custom_objective(self):
        """
        Compare the result of a custom objective with the symbolic result.
        :return:
        """
        # Create test data
        xdata = np.linspace(0, 100, 25)  # From 0 to 100 in 100 steps
        a_vec = np.random.normal(15.0, scale=2.0, size=xdata.shape)
        b_vec = np.random.normal(100, scale=2.0, size=xdata.shape)
        ydata = a_vec * xdata + b_vec  # Point scattered around the line 5 * x + 105

        # Normal symbolic fit
        a = Parameter('a', value=0, min=0.0, max=1000)
        b = Parameter('b', value=0, min=0.0, max=1000)
        x = Variable()
        model = a * x + b
<commit_msg>Improved subclasses function as suggested<commit_after>from __future__ import division, print_function
import unittest
import sys
import warnings

import numpy as np
import pickle
import multiprocessing as mp

from symfit import (
    Variable, Parameter, Eq, Ge, Le, Lt, Gt, Ne, parameters, ModelError, Fit,
    Model, FitResults, variables, CallableNumericalModel, Constraint
)
from symfit.core.minimizers import *
from symfit.core.support import partial

# Defined at the global level because local functions can't be pickled.
def f(x, a, b):
    return a * x + b

def chi_squared(x, y, a, b, sum=True):
    if sum:
        return np.sum((y - f(x, a, b)) ** 2)
    else:
        return (y - f(x, a, b)) ** 2

def worker(fit_obj):
    return fit_obj.execute()

def subclasses(base, leaves_only=True):
    """
    Recursively create a set of subclasses of ``object``.

    :param object: Class
    :param leaves_only: If ``True``, return only the leaves of the subclass tree
    :return: (All leaves of) the subclass tree.
    """
    base_subs = set(base.__subclasses__())
    if not base_subs or not leaves_only:
        all_subs = {base}
    else:
        all_subs = set()
    for sub in list(base_subs):
        sub_subs = subclasses(sub, leaves_only=leaves_only)
        all_subs.update(sub_subs)
    return all_subs

class TestMinimize(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        np.random.seed(0)

    def test_custom_objective(self):
        """
        Compare the result of a custom objective with the symbolic result.
        :return:
        """
        # Create test data
        xdata = np.linspace(0, 100, 25)  # From 0 to 100 in 100 steps
        a_vec = np.random.normal(15.0, scale=2.0, size=xdata.shape)
        b_vec = np.random.normal(100, scale=2.0, size=xdata.shape)
        ydata = a_vec * xdata + b_vec  # Point scattered around the line 5 * x + 105

        # Normal symbolic fit
        a = Parameter('a', value=0, min=0.0, max=1000)
        b = Parameter('b', value=0, min=0.0, max=1000)
        x = Variable()
        model = a * x + b

{'fraction_non_alphanumeric': 0.1019790454016298, 'fraction_numerical': 0.02048894062863795, 'mean_word_length': 2.758530183727034, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1648870', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1399, 'n_words': 461}