# Path: ramp/features/base.py
# F = Feature
# 
# class Map(Feature):
#     """
#     Applies given function to feature. Feature *cannot*
#     be anonymous (ie lambda). Must be defined in top level
#     (and thus picklable).
#     """
#     def __init__(self, feature, function, name=None):
#         super(Map, self).__init__(feature)
#         self.function = function
#         if name is None:
#             name = function.__name__
#         self.name = name
#         self._name = name
# 
#     def _apply(self, data, fitted_feature):
#         return data.applymap(self.function)


import sys
import unittest
import numpy as np
import pandas as pd
from pandas import DataFrame, Series, Index
from pandas.util.testing import assert_frame_equal, assert_index_equal
from ramp.features.base import F, Map
from ramp.utils import *
sys.path.append('../..')




class TestUtils(unittest.TestCase):

    def test_shuffle_df(self):
        n = 100
        df = pd.DataFrame({'a':np.random.rand(n), 
                           'b':np.random.rand(n)})
        df_shuffled = shuffle_df(df)
        self.assertEqual(df.shape, df_shuffled.shape)
        self.assertNotEqual(tuple(df['a']), tuple(df_shuffled['a']))
        self.assertNotEqual(tuple(df['b']), tuple(df_shuffled['b']))
        self.assertNotEqual(tuple(df.index), tuple(df_shuffled.index))
        assert_frame_equal(df.sort(), df_shuffled.sort())

    def test_np_hashes(self):
        a = np.random.randn(20)
        h = get_np_hash(a)
        a[0] = 200000
        h2 = get_np_hash(a)
        self.assertNotEqual(h, h2)
        b = a[0]
        a[0] = b
        self.assertEqual(h2, get_np_hash(a))

    def test_stable_repr(self):
        f = F('test')

# Path: ramp/features/base.py
# F = Feature
# 
# class Map(Feature):
#     """
#     Applies given function to feature. Feature *cannot*
#     be anonymous (ie lambda). Must be defined in top level
#     (and thus picklable).
#     """
#     def __init__(self, feature, function, name=None):
#         super(Map, self).__init__(feature)
#         self.function = function
#         if name is None:
#             name = function.__name__
#         self.name = name
#         self._name = name
# 
#     def _apply(self, data, fitted_feature):
#         return data.applymap(self.function)


# Path: ramp/tests/test_utils.py
import sys
import unittest
import numpy as np
import pandas as pd
from pandas import DataFrame, Series, Index
from pandas.util.testing import assert_frame_equal, assert_index_equal
from ramp.features.base import F, Map
from ramp.utils import *

sys.path.append('../..')




class TestUtils(unittest.TestCase):

    def test_shuffle_df(self):
        n = 100
        df = pd.DataFrame({'a':np.random.rand(n), 
                           'b':np.random.rand(n)})
        df_shuffled = shuffle_df(df)
        self.assertEqual(df.shape, df_shuffled.shape)
        self.assertNotEqual(tuple(df['a']), tuple(df_shuffled['a']))
        self.assertNotEqual(tuple(df['b']), tuple(df_shuffled['b']))
        self.assertNotEqual(tuple(df.index), tuple(df_shuffled.index))
        assert_frame_equal(df.sort(), df_shuffled.sort())

    def test_np_hashes(self):
        a = np.random.randn(20)
        h = get_np_hash(a)
        a[0] = 200000
        h2 = get_np_hash(a)
        self.assertNotEqual(h, h2)
        b = a[0]
        a[0] = b
        self.assertEqual(h2, get_np_hash(a))

    def test_stable_repr(self):
        f = F('test')

        f2 = F('test')