# Path: core/tests/base.py
# class ScanTest(TestCase):
# 
#     def create_app(self):
#         return app
# 
#     def setUp(self):
#         db.create_all(app=app)
# 
#     def tearDown(self):
#         db.session.remove()
#         db.drop_all()
# 
# Path: scan/log.py
# LOGGING = {
#     'version': 1,
#     'disable_existing_loggers': False,
#     'formatters': {
#         'standard': {
#             'format': '%(asctime)s %(levelname)s %(process)d '
#                       '[%(name)s] %(filename)s:%(lineno)d - %(message)s',
#             },
#         'syslog_format': {'format': syslog_format},
#         'raw': {'format': '%(message)s'},
#         },
#     'handlers': {
#         'console': {
#             'level': 'DEBUG',
#             'class': 'logging.StreamHandler',
#             'formatter': 'standard',
#             'stream': sys.stdout,
#             }
#     },
#     'loggers': {
#         'requests.packages.urllib3.connectionpool': {
#             'handlers': [],
#             'propagate': False,
#             'level': 'DEBUG',
#             },
#         '': {
#             'handlers': ['console'],
#             'level': 'DEBUG',
#             'propagate': False
#         },
#         }
# }
# 
# Path: core/tests/base.py
# class ScanTest(TestCase):
#     def create_app(self):
#     def setUp(self):
#     def tearDown(self):
# 
# Path: core/algo/scorer.py
# class Scorer(object):
#     classification_max = 4
#     cv_folds = 2
# 
#     def __init__(self, text, scores):
#         self.text = text
#         self.scores = scores
#         self.feature_generator = FeatureGenerator()
#         self.classifier = RandomForestRegressor(
#             n_estimators=100,
#             min_samples_split=4,
#             min_samples_leaf=3,
#             random_state=1
#         )
# 
#         unique_scores = set(scores)
#         if len(unique_scores) <= self.classification_max:
#             self.classifier = RandomForestClassifier(
#                 n_estimators=100,
#                 min_samples_split=4,
#                 min_samples_leaf=3,
#                 random_state=1
#             )
# 
#         self.fit_feats()
#         self.fit_done = False
# 
#     def fit_feats(self):
#         self.feature_generator.fit(self.text, self.scores)
# 
#     def get_features(self):
#         feats = []
#         for t in self.text:
#             feats.append(self.feature_generator.get_features(t))
# 
#         feat_mat = np.vstack(feats)
#         return feat_mat
# 
#     def train(self):
#         feats = self.get_features()
#         scores = np.array(self.scores)
# 
#         # Compute error metrics for the estimator.
#         self.cv_scores = cross_validation.cross_val_score(self.classifier, feats, scores)
#         self.cv_score = self.cv_scores.mean()
#         self.cv_dev = self.cv_scores.std()
# 
#         self.classifier.fit(feats, scores)
#         self.fit_done = True
# 
#     def predict(self, text):
#         feats = self.feature_generator.get_features(text)
#         return self.classifier.predict(feats)
# 
# Path: scan/test_settings.py
# DB_URL = "sqlite:///test.db"
# SQLALCHEMY_DATABASE_URI = DB_URL
# TESTING = True
# CHARACTER_LIMIT = 1000
# TRAINING_LIMIT = 50
# QUICK_TEST_LIMIT = 5


from core.tests.base import ScanTest
from scan.log import logging
from core.tests.base import db
from mock import patch
from core.algo.scorer import Scorer
from scan import test_settings as settings
import os
import json
import random
    def generic_setup(self, scores, text):
        #Shuffle to mix up the classes, set seed to make it repeatable
        random.seed(1)
        shuffled_scores = []
        shuffled_text = []
        indices = [i for i in range(0, len(scores))]
        random.shuffle(indices)
        for i in indices:
            shuffled_scores.append(scores[i])
            shuffled_text.append(text[i])

        self.text = shuffled_text[:settings.TRAINING_LIMIT]
        self.scores = shuffled_scores[:settings.TRAINING_LIMIT]

    def model_creation_and_grading(self):
        score_subset = self.scores[:settings.QUICK_TEST_LIMIT]
        text_subset = self.text[:settings.QUICK_TEST_LIMIT]
        self.model.train()
        assert self.model.fit_done

        pred = self.model.predict(text_subset[0])

    def scoring_accuracy(self):
        random.seed(1)
        self.model.train()
        assert self.model.fit_done
        assert self.model.cv_score <= self.expected_mae_max

        log.info("CV Score: {0}".format(self.model.cv_score))


# Path: core/tests/base.py
# class ScanTest(TestCase):
# 
#     def create_app(self):
#         return app
# 
#     def setUp(self):
#         db.create_all(app=app)
# 
#     def tearDown(self):
#         db.session.remove()
#         db.drop_all()
# 
# Path: scan/log.py
# LOGGING = {
#     'version': 1,
#     'disable_existing_loggers': False,
#     'formatters': {
#         'standard': {
#             'format': '%(asctime)s %(levelname)s %(process)d '
#                       '[%(name)s] %(filename)s:%(lineno)d - %(message)s',
#             },
#         'syslog_format': {'format': syslog_format},
#         'raw': {'format': '%(message)s'},
#         },
#     'handlers': {
#         'console': {
#             'level': 'DEBUG',
#             'class': 'logging.StreamHandler',
#             'formatter': 'standard',
#             'stream': sys.stdout,
#             }
#     },
#     'loggers': {
#         'requests.packages.urllib3.connectionpool': {
#             'handlers': [],
#             'propagate': False,
#             'level': 'DEBUG',
#             },
#         '': {
#             'handlers': ['console'],
#             'level': 'DEBUG',
#             'propagate': False
#         },
#         }
# }
# 
# Path: core/tests/base.py
# class ScanTest(TestCase):
#     def create_app(self):
#     def setUp(self):
#     def tearDown(self):
# 
# Path: core/algo/scorer.py
# class Scorer(object):
#     classification_max = 4
#     cv_folds = 2
# 
#     def __init__(self, text, scores):
#         self.text = text
#         self.scores = scores
#         self.feature_generator = FeatureGenerator()
#         self.classifier = RandomForestRegressor(
#             n_estimators=100,
#             min_samples_split=4,
#             min_samples_leaf=3,
#             random_state=1
#         )
# 
#         unique_scores = set(scores)
#         if len(unique_scores) <= self.classification_max:
#             self.classifier = RandomForestClassifier(
#                 n_estimators=100,
#                 min_samples_split=4,
#                 min_samples_leaf=3,
#                 random_state=1
#             )
# 
#         self.fit_feats()
#         self.fit_done = False
# 
#     def fit_feats(self):
#         self.feature_generator.fit(self.text, self.scores)
# 
#     def get_features(self):
#         feats = []
#         for t in self.text:
#             feats.append(self.feature_generator.get_features(t))
# 
#         feat_mat = np.vstack(feats)
#         return feat_mat
# 
#     def train(self):
#         feats = self.get_features()
#         scores = np.array(self.scores)
# 
#         # Compute error metrics for the estimator.
#         self.cv_scores = cross_validation.cross_val_score(self.classifier, feats, scores)
#         self.cv_score = self.cv_scores.mean()
#         self.cv_dev = self.cv_scores.std()
# 
#         self.classifier.fit(feats, scores)
#         self.fit_done = True
# 
#     def predict(self, text):
#         feats = self.feature_generator.get_features(text)
#         return self.classifier.predict(feats)
# 
# Path: scan/test_settings.py
# DB_URL = "sqlite:///test.db"
# SQLALCHEMY_DATABASE_URI = DB_URL
# TESTING = True
# CHARACTER_LIMIT = 1000
# TRAINING_LIMIT = 50
# QUICK_TEST_LIMIT = 5


# Path: core/algo/tests.py
from core.tests.base import ScanTest
from scan.log import logging
from core.tests.base import db
from mock import patch
from core.algo.scorer import Scorer
from scan import test_settings as settings
import os
import json
import random

    def generic_setup(self, scores, text):
        #Shuffle to mix up the classes, set seed to make it repeatable
        random.seed(1)
        shuffled_scores = []
        shuffled_text = []
        indices = [i for i in range(0, len(scores))]
        random.shuffle(indices)
        for i in indices:
            shuffled_scores.append(scores[i])
            shuffled_text.append(text[i])

        self.text = shuffled_text[:settings.TRAINING_LIMIT]
        self.scores = shuffled_scores[:settings.TRAINING_LIMIT]

    def model_creation_and_grading(self):
        score_subset = self.scores[:settings.QUICK_TEST_LIMIT]
        text_subset = self.text[:settings.QUICK_TEST_LIMIT]
        self.model.train()
        assert self.model.fit_done

        pred = self.model.predict(text_subset[0])

    def scoring_accuracy(self):
        random.seed(1)
        self.model.train()
        assert self.model.fit_done
        assert self.model.cv_score <= self.expected_mae_max

        log.info("CV Score: {0}".format(self.model.cv_score))


class PolarityTest(ScanTest, GenericTest):