# Path: minos/train/utils.py
# class SimpleBatchIterator(object):
# 
#     def __init__(self, X, y, batch_size,
#                  X_transform=None, y_transform=None,
#                  autoloop=False, autorestart=False, preload=False):
#         self.X = X
#         self.y = y
#         self.batch_size = batch_size
#         self.X_transform = X_transform
#         self.y_transform = y_transform
#         self.autoloop = autoloop
#         self.autorestart = autorestart
#         self.index = 0
#         self.preload = preload
#         self.batch_size = batch_size
#         self.sample_count = len(X)
#         self.samples_per_epoch = self.sample_count
#         self.X, self.y = create_batches(X, y, batch_size)
# 
#     def _transform_data(self, X, y):
#         if self.X_transform:
#             X = self.X_transform.fit_transform(X)
#         if self.y_transform:
#             y = self.y_transform.fit_transform(y)
#         return numpy.asarray(X), numpy.asarray(y)
# 
#     def __iter__(self):
#         return self
# 
#     def __next__(self):
#         try:
#             if self.index >= len(self.X):
#                 shuffle_batch(self.X, self.y)
#                 if self.autorestart or self.autoloop:
#                     self.index = 0
#                 if self.autorestart or not self.autoloop:
#                     return None
#             X, y = self._transform_data(
#                 self.X[self.index],
#                 self.y[self.index])
#             shuffle_batch(X, y)
#             self.index += 1
#             return X, y
#         except Exception as ex:
#             logging.error('Error while iterating %s' % str(ex))
#             try:
#                 logging.error(traceback.format_exc())
#             finally:
#                 pass
#             raise ex


import numpy
from keras import activations, backend
from keras.datasets import reuters
from keras.engine.topology import Layer
from keras.preprocessing.text import Tokenizer
from keras.utils import np_utils
from minos.train.utils import SimpleBatchIterator
'''
Created on Feb 18, 2017

@author: julien
'''




def get_reuters_dataset(batch_size, max_words):
    (X_train, y_train), (X_test, y_test) = reuters.load_data(nb_words=max_words, test_split=0.2)

    tokenizer = Tokenizer(nb_words=max_words)
    X_train = tokenizer.sequences_to_matrix(X_train, mode='binary')
    X_test = tokenizer.sequences_to_matrix(X_test, mode='binary')

    nb_classes = numpy.max(y_train) + 1
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)

    batch_iterator = SimpleBatchIterator(
        X_train,
        y_train,
        batch_size,
        autoloop=True)

# Path: minos/train/utils.py
# class SimpleBatchIterator(object):
# 
#     def __init__(self, X, y, batch_size,
#                  X_transform=None, y_transform=None,
#                  autoloop=False, autorestart=False, preload=False):
#         self.X = X
#         self.y = y
#         self.batch_size = batch_size
#         self.X_transform = X_transform
#         self.y_transform = y_transform
#         self.autoloop = autoloop
#         self.autorestart = autorestart
#         self.index = 0
#         self.preload = preload
#         self.batch_size = batch_size
#         self.sample_count = len(X)
#         self.samples_per_epoch = self.sample_count
#         self.X, self.y = create_batches(X, y, batch_size)
# 
#     def _transform_data(self, X, y):
#         if self.X_transform:
#             X = self.X_transform.fit_transform(X)
#         if self.y_transform:
#             y = self.y_transform.fit_transform(y)
#         return numpy.asarray(X), numpy.asarray(y)
# 
#     def __iter__(self):
#         return self
# 
#     def __next__(self):
#         try:
#             if self.index >= len(self.X):
#                 shuffle_batch(self.X, self.y)
#                 if self.autorestart or self.autoloop:
#                     self.index = 0
#                 if self.autorestart or not self.autoloop:
#                     return None
#             X, y = self._transform_data(
#                 self.X[self.index],
#                 self.y[self.index])
#             shuffle_batch(X, y)
#             self.index += 1
#             return X, y
#         except Exception as ex:
#             logging.error('Error while iterating %s' % str(ex))
#             try:
#                 logging.error(traceback.format_exc())
#             finally:
#                 pass
#             raise ex


# Path: tests/fixtures.py
import numpy
from keras import activations, backend
from keras.datasets import reuters
from keras.engine.topology import Layer
from keras.preprocessing.text import Tokenizer
from keras.utils import np_utils
from minos.train.utils import SimpleBatchIterator

'''
Created on Feb 18, 2017

@author: julien
'''




def get_reuters_dataset(batch_size, max_words):
    (X_train, y_train), (X_test, y_test) = reuters.load_data(nb_words=max_words, test_split=0.2)

    tokenizer = Tokenizer(nb_words=max_words)
    X_train = tokenizer.sequences_to_matrix(X_train, mode='binary')
    X_test = tokenizer.sequences_to_matrix(X_test, mode='binary')

    nb_classes = numpy.max(y_train) + 1
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)

    batch_iterator = SimpleBatchIterator(
        X_train,
        y_train,
        batch_size,
        autoloop=True)

    test_batch_iterator = SimpleBatchIterator(