# 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 ramp.features.base import F, Map
from ramp.folds import *
from ramp.utils import *
        ntr = 6000
        nte = 1000
        df = pd.DataFrame({'a':np.arange(n), 'y':np.hstack([np.ones(n/r), np.zeros(n/r * (r -1))])})
        balanced_folds = BootstrapFolds(n_folds, F('y'), df, seed=1,
                                        pos_train=ptr, neg_train=ntr, pos_test=pte, neg_test=nte)
        folds = list(balanced_folds)
        self.assertEqual(len(folds), n_folds)
        te = set()
        for train, test in folds:
            self.assertFalse(set(train) & set(test))
            te = te | set(train)
            train_y = df.y[train]
            test_y = df.y[test]
            # ensure sizes are correct
            self.assertEqual(len(train_y), ptr + ntr)
            self.assertEqual(len(test_y), pte + nte)
            self.assertEqual(sum(train_y), ptr)
            self.assertEqual(sum(test_y), pte)
        # ensure all instances were used in test (well, almost all)
        self.assertEqual(len(te), 9998) # seed is set

    def test_bootstrapped_folds_from_percents(self):
        n = 10000
        r = 4
        n_folds = 10
        train_pos_percent = .5
        test_pos_percent = .2
        train_percent = .8

        df = pd.DataFrame({'a':np.arange(n), 'y':np.hstack([np.ones(n/r), np.zeros(n/r * (r -1))])})

# 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_folds.py
import sys
import unittest
import numpy as np
import pandas as pd
from pandas import DataFrame, Series, Index
from ramp.features.base import F, Map
from ramp.folds import *
from ramp.utils import *

        ntr = 6000
        nte = 1000
        df = pd.DataFrame({'a':np.arange(n), 'y':np.hstack([np.ones(n/r), np.zeros(n/r * (r -1))])})
        balanced_folds = BootstrapFolds(n_folds, F('y'), df, seed=1,
                                        pos_train=ptr, neg_train=ntr, pos_test=pte, neg_test=nte)
        folds = list(balanced_folds)
        self.assertEqual(len(folds), n_folds)
        te = set()
        for train, test in folds:
            self.assertFalse(set(train) & set(test))
            te = te | set(train)
            train_y = df.y[train]
            test_y = df.y[test]
            # ensure sizes are correct
            self.assertEqual(len(train_y), ptr + ntr)
            self.assertEqual(len(test_y), pte + nte)
            self.assertEqual(sum(train_y), ptr)
            self.assertEqual(sum(test_y), pte)
        # ensure all instances were used in test (well, almost all)
        self.assertEqual(len(te), 9998) # seed is set

    def test_bootstrapped_folds_from_percents(self):
        n = 10000
        r = 4
        n_folds = 10
        train_pos_percent = .5
        test_pos_percent = .2
        train_percent = .8

        df = pd.DataFrame({'a':np.arange(n), 'y':np.hstack([np.ones(n/r), np.zeros(n/r * (r -1))])})

        balanced_folds = BootstrapFolds(n_folds, F('y'), df, seed=1,