<commit_before>'''
Created on Feb 17, 2013

@author: jasonrudy
'''

import pickle
import os

import numpy
from nose.tools import assert_true, assert_false, assert_equal

from pyearth._basis import Basis, ConstantBasisFunction, HingeBasisFunction, \
    LinearBasisFunction, SmoothedHingeBasisFunction


class BaseTestClass(object):

    def __init__(self):
        numpy.random.seed(0)
        data = numpy.genfromtxt(
            os.path.join(os.path.dirname(__file__), 'test_data.csv'),
            delimiter=',', skip_header=1)
        self.y = numpy.array(data[:, 5])
        self.X = numpy.array(data[:, 0:5])


class TestConstantBasisFunction(BaseTestClass):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.bf = ConstantBasisFunction()

    def test_apply(self):
        m, _ = self.X.shape
        B = numpy.empty(shape=(m, 10))
        assert_false(numpy.all(B[:, 0] == 1))
        self.bf.apply(self.X, B[:, 0])
        assert_true(numpy.all(B[:, 0] == 1))

    def test_apply_deriv(self):
        m, _ = self.X.shape
        b = numpy.empty(shape=m)
        j = numpy.empty(shape=m)
        self.bf.apply_deriv(self.X, b, j, 1)
        assert_true(numpy.all(b == 1))
        assert_true(numpy.all(j == 0))

    def test_pickle_compatibility(self):
        bf_copy = pickle.loads(pickle.dumps(self.bf))
        assert_true(self.bf == bf_copy)

    def test_smoothed_version(self):
        smoothed = self.bf._smoothed_version(None, {}, {})
        assert_true(type(smoothed) is ConstantBasisFunction)


class TestSmoothedHingeBasisFunction(BaseTestClass):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.parent = ConstantBasisFunction()
        self.bf1 = SmoothedHingeBasisFunction(self.parent,
                                              1.0, 0.0, 3.0, 10, 1,
                                              False)
        self.bf2 = SmoothedHingeBasisFunction(self.parent,
                                              1.0, 0.0, 3.0, 10, 1,
                                              True)

    def test_getters(self):
        assert not self.bf1.get_reverse()
        assert self.bf2.get_reverse()
        assert self.bf1.get_knot() == 1.0
        assert self.bf1.get_variable() == 1
        assert self.bf1.get_knot_idx() == 10
        assert self.bf1.get_parent() == self.parent
        assert self.bf1.get_knot_minus() == 0.0
        assert self.bf1.get_knot_plus() == 3.0

    def test_pickle_compatibility(self):
        bf_copy = pickle.loads(pickle.dumps(self.bf1))
        assert_equal(self.bf1, bf_copy)

    def test_smoothed_version(self):
        translation = {self.parent: self.parent._smoothed_version(None, {}, {})}
        smoothed = self.bf1._smoothed_version(self.parent, {}, translation)
        assert_equal(self.bf1, smoothed)

    def test_degree(self):
        assert_equal(self.bf1.degree(), 1)
        assert_equal(self.bf2.degree(), 1)

    def test_p_r(self):
        pplus = (2*3.0 + 0.0 - 3*1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        assert_equal(self.bf1.get_p(), pplus)
        assert_equal(self.bf1.get_r(), rplus)
        assert_equal(self.bf2.get_p(), pminus)
        assert_equal(self.bf2.get_r(), rminus)

    def test_apply(self):
        m, _ = self.X.shape
        B = numpy.ones(shape=(m, 10))
        self.bf1.apply(self.X, B[:, 0])
        self.bf2.apply(self.X, B[:, 1])
        pplus = (2 * 3.0 + 0.0 - 3 * 1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        c1 = numpy.ones(m)
        c1[self.X[:, 1] <= 0.0] = 0.0
        c1[(self.X[:, 1] > 0.0) &
           (self.X[:, 1] < 3.0)] = (pplus*((self.X[(self.X[:, 1] > 0.0) &
                                           (self.X[:, 1] < 3.0), 1] - 0.0)**2) +
                                    rplus*((self.X[(self.X[:, 1] > 0.0) &
                                           (self.X[:, 1] < 3.0), 1] - 0.0)**3))
        c1[self.X[:, 1] >= 3.0] = self.X[self.X[:, 1] >= 3.0, 1] - 1.0
        c2 = numpy.ones(m)
        c2[self.X[:, 1] >= 3.0] = 0.0
        c2[self.X[:, 1] <= 0.0] = -1 * (self.X[self.X[:, 1] <= 0.0] - 1.0)
        c2[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**2) +
            rminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**3)
        )
        numpy.testing.assert_almost_equal(B[:, 0], c1)
        numpy.testing.assert_almost_equal(B[:, 1], c2)

    def test_apply_deriv(self):
        m, _ = self.X.shape
        pplus = (2*3.0 + 0.0 - 3*1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        c1 = numpy.ones(m)
        c1[self.X[:, 1] <= 0.0] = 0.0
        c1[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pplus*((self.X[(self.X[:, 1] > 0.0) &
                   (self.X[:, 1] < 3.0), 1] - 0.0)**2) +
            rplus*((self.X[(self.X[:, 1] > 0.0) &
                   (self.X[:, 1] < 3.0), 1] - 0.0)**3))
        c1[self.X[:, 1] >= 3.0] = self.X[self.X[:, 1] >= 3.0, 1] - 1.0
        c2 = numpy.ones(m)
        c2[self.X[:, 1] >= 3.0] = 0.0
        c2[self.X[:, 1] <= 0.0] = -1 * (self.X[self.X[:, 1] <= 0.0] - 1.0)
        c2[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**2) +
            rminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**3)
        )
        b1 = numpy.empty(shape=m)<commit_msg>fix unit test warnings<commit_after>'''
Created on Feb 17, 2013

@author: jasonrudy
'''

import pickle
import os

import numpy
from nose.tools import assert_true, assert_false, assert_equal

from pyearth._basis import Basis, ConstantBasisFunction, HingeBasisFunction, \
    LinearBasisFunction, SmoothedHingeBasisFunction


class BaseTestClass(object):

    def __init__(self):
        numpy.random.seed(0)
        data = numpy.genfromtxt(
            os.path.join(os.path.dirname(__file__), 'test_data.csv'),
            delimiter=',', skip_header=1)
        self.y = numpy.array(data[:, 5])
        self.X = numpy.array(data[:, 0:5])


class TestConstantBasisFunction(BaseTestClass):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.bf = ConstantBasisFunction()

    def test_apply(self):
        m, _ = self.X.shape
        B = numpy.empty(shape=(m, 10))
        assert_false(numpy.all(B[:, 0] == 1))
        self.bf.apply(self.X, B[:, 0])
        assert_true(numpy.all(B[:, 0] == 1))

    def test_apply_deriv(self):
        m, _ = self.X.shape
        b = numpy.empty(shape=m)
        j = numpy.empty(shape=m)
        self.bf.apply_deriv(self.X, b, j, 1)
        assert_true(numpy.all(b == 1))
        assert_true(numpy.all(j == 0))

    def test_pickle_compatibility(self):
        bf_copy = pickle.loads(pickle.dumps(self.bf))
        assert_true(self.bf == bf_copy)

    def test_smoothed_version(self):
        smoothed = self.bf._smoothed_version(None, {}, {})
        assert_true(type(smoothed) is ConstantBasisFunction)


class TestSmoothedHingeBasisFunction(BaseTestClass):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.parent = ConstantBasisFunction()
        self.bf1 = SmoothedHingeBasisFunction(self.parent,
                                              1.0, 0.0, 3.0, 10, 1,
                                              False)
        self.bf2 = SmoothedHingeBasisFunction(self.parent,
                                              1.0, 0.0, 3.0, 10, 1,
                                              True)

    def test_getters(self):
        assert not self.bf1.get_reverse()
        assert self.bf2.get_reverse()
        assert self.bf1.get_knot() == 1.0
        assert self.bf1.get_variable() == 1
        assert self.bf1.get_knot_idx() == 10
        assert self.bf1.get_parent() == self.parent
        assert self.bf1.get_knot_minus() == 0.0
        assert self.bf1.get_knot_plus() == 3.0

    def test_pickle_compatibility(self):
        bf_copy = pickle.loads(pickle.dumps(self.bf1))
        assert_equal(self.bf1, bf_copy)

    def test_smoothed_version(self):
        translation = {self.parent: self.parent._smoothed_version(None, {}, {})}
        smoothed = self.bf1._smoothed_version(self.parent, {}, translation)
        assert_equal(self.bf1, smoothed)

    def test_degree(self):
        assert_equal(self.bf1.degree(), 1)
        assert_equal(self.bf2.degree(), 1)

    def test_p_r(self):
        pplus = (2*3.0 + 0.0 - 3*1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        assert_equal(self.bf1.get_p(), pplus)
        assert_equal(self.bf1.get_r(), rplus)
        assert_equal(self.bf2.get_p(), pminus)
        assert_equal(self.bf2.get_r(), rminus)

    def test_apply(self):
        m, _ = self.X.shape
        B = numpy.ones(shape=(m, 10))
        self.bf1.apply(self.X, B[:, 0])
        self.bf2.apply(self.X, B[:, 1])
        pplus = (2 * 3.0 + 0.0 - 3 * 1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        c1 = numpy.ones(m)
        c1[self.X[:, 1] <= 0.0] = 0.0
        c1[(self.X[:, 1] > 0.0) &
           (self.X[:, 1] < 3.0)] = (pplus*((self.X[(self.X[:, 1] > 0.0) &
                                           (self.X[:, 1] < 3.0), 1] - 0.0)**2) +
                                    rplus*((self.X[(self.X[:, 1] > 0.0) &
                                           (self.X[:, 1] < 3.0), 1] - 0.0)**3))
        c1[self.X[:, 1] >= 3.0] = self.X[self.X[:, 1] >= 3.0, 1] - 1.0
        c2 = numpy.ones(m)
        c2[self.X[:, 1] >= 3.0] = 0.0
        c2.flat[self.X[:, 1] <= 0.0] = -1 * (self.X[self.X[:, 1] <= 0.0] - 1.0)
        c2[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**2) +
            rminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**3)
        )
        numpy.testing.assert_almost_equal(B[:, 0], c1)
        numpy.testing.assert_almost_equal(B[:, 1], c2)

    def test_apply_deriv(self):
        m, _ = self.X.shape
        pplus = (2*3.0 + 0.0 - 3*1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        c1 = numpy.ones(m)
        c1[self.X[:, 1] <= 0.0] = 0.0
        c1[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pplus*((self.X[(self.X[:, 1] > 0.0) &
                   (self.X[:, 1] < 3.0), 1] - 0.0)**2) +
            rplus*((self.X[(self.X[:, 1] > 0.0) &
                   (self.X[:, 1] < 3.0), 1] - 0.0)**3))
        c1[self.X[:, 1] >= 3.0] = self.X[self.X[:, 1] >= 3.0, 1] - 1.0
        c2 = numpy.ones(m)
        c2[self.X[:, 1] >= 3.0] = 0.0
        c2.flat[self.X[:, 1] <= 0.0] = -1 * (self.X[self.X[:, 1] <= 0.0] - 1.0)
        c2[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**2) +
            rminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**3)
        )
        b1 = numpy.empty(shape=m)
{'fraction_non_alphanumeric': 0.20502965995488345, 'fraction_numerical': 0.06800902331021806, 'mean_word_length': 1.695945945945946, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236505', 'n_tokens_mistral': 5144, 'n_tokens_neox': 4808, 'n_words': 846}