# Copyright (c) 2012 Leif Johnson <leif@leifjohnson.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

'''This file contains a class for handling batched datasets.'''

import lmj.cli
import numpy.random as rng

logging = lmj.cli.get_logger(__name__)


class SequenceDataset(object):
    '''This class handles batching and shuffling a dataset.

    It's mostly copied from the dataset class from hf.py, except that the
    constructor has slightly different semantics.
    '''

    def __init__(self, *data, **kwargs):
        '''Create a minibatch dataset from a number of different data arrays.

        Positional arguments:

        There should be one unnamed keyword argument for each input in the
        neural network that will be processing this dataset. For instance, if
        you are dealing with a classifier network, you'll need one argument for
        the inputs (e.g., mnist digit pixels), and another argument for the
        target outputs (e.g., digit class labels). The order of the arguments
        should be the same as the order of inputs in the network. All arguments
        are expected to have the same number of elements along the first axis.

        Alternatively, if there is only one positional arg, and it is callable,
        then that callable will be invoked repeatedly at training and test time.
        Each invocation of the callable should return a tuple containing one
        minibatch of data. The callable will be passed one argument---the size
        of the minibatch to generate.

        Keyword arguments:

        size or batch_size: The size of the mini-batches to create from the
          data sequences. Defaults to 32.
        batches: The number of batches to yield for each call to iterate().
          Defaults to the length of the data divided by batch_size.
        label: A string that is used to describe this dataset. Usually something
          like 'test' or 'train'.
        '''
        self.label = kwargs.get('label', 'dataset')
        self.batch_size = kwargs.get('size', kwargs.get('batch_size', 32))
        self.number_batches = kwargs.get('batches')
        self.batch = 0

        batch = None
        cardinality = None
        self.callable = None
        self.batches = None
        if len(data) == 1 and callable(data[0]):
            self.callable = data[0]
            cardinality = '->'
            batch = self.callable(self.batch_size)
            if not self.number_batches:
                self.number_batches = self.batch_size
        else:
            self.batches = [
                [d[i:i + self.batch_size] for d in data]
                for i in xrange(0, len(data[0]), self.batch_size)]
            self.shuffle()
            cardinality = len(self.batches)
            batch = self.batches[0]
            if not self.number_batches:
                self.number_batches = cardinality

        logging.info('data %s: %s mini-batches of %s',
            self.label, cardinality, ', '.join(str(x.shape) for x in batch))

    def __iter__(self):
        return self.iterate(True)

    def shuffle(self):
        rng.shuffle(self.batches)

    def iterate(self, update=True):
        if self.callable:
            return self._iter_callable()
        return self._iter_batches(update)

    def _iter_batches(self, update=True):
        k = len(self.batches)
        for b in xrange(self.number_batches):
            yield self.batches[(self.batch + b) % k]
        if update:
            self.update()

    def _iter_callable(self):
        for b in xrange(self.number_batches):
            yield self.callable(self.batch_size)

    def update(self):
        if self.callable:
            return
        self.batch += self.number_batches
        if self.batch >= len(self.batches):
            self.shuffle()
            self.batch = 0

@@ -2459,24 +2459,28 @@
 llable will 
+not 
 be passed on
@@ -2477,19 +2477,19 @@
  passed 
-one
+any
  argumen
@@ -2493,56 +2493,9 @@
 ment
----the size%0A        of the minibatch to generate
+s
 .%0A%0A 
@@ -2966,83 +2966,8 @@
 t')%0A
-        self.batch_size = kwargs.get('size', kwargs.get('batch_size', 32))%0A
     
@@ -3038,16 +3038,80 @@
 ch = 0%0A%0A
+        size = kwargs.get('size', kwargs.get('batch_size', 32))%0A
         
@@ -3357,31 +3357,16 @@
 allable(
-self.batch_size
 )%0A      
@@ -3437,27 +3437,16 @@
 tches = 
-self.batch_
 size%0A   
@@ -3510,27 +3510,16 @@
 d%5Bi:i + 
-self.batch_
 size%5D fo
@@ -3580,27 +3580,16 @@
 ta%5B0%5D), 
-self.batch_
 size)%5D%0A 
@@ -4510,23 +4510,8 @@
 ble(
-self.batch_size
 )%0A%0A 

Remove .batch_size instance attribute. Call data callable without any arguments.