from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import cPickle as pkl
from glob import glob
import numpy as np
import tensorflow as tf


class Processor(object):
    """
        Handles the reading and  preprocessing CIFAR-10

        NOTE:
            We assume that data_dir has the unzipped CIFAR-10 dataset
            downloaded from https://www.cs.toronto.edu/~kriz/cifar.html
    """

    def __init__(self, data_dir, batch_size=100):
        self.data_dir = data_dir
        self.batch_size = batch_size

    def get_batch(self):
        """
            Loops through the dataset and keeps yielding image batches.
            
            It is possible that at the edges you get batches smaller than
            self.batch_size. Please file an issue if this is an issue.
        """
        files = glob(self.data_dir + "/data_batch_*")
        files.sort()
        assert len(files) == 5, "Expected 5 data_batch files, found {}".format(
            len(files))
        while (True):
            for file in files:
                tf.logging.info("Processing %s", file)
                data = pkl.load(open(file, "rb"))
                data = data["data"]
                data = data.reshape(-1, 3, 32, 32)
                data = data.transpose(0, 2, 3, 1)
                print(data.shape)
                print(len(data))
                data = data.astype(np.float32)
                data = (data - 127) / 127.0
                assert np.minimum(data) >= -1
                assert np.maximum(data) <= 1
                for i in range(0, len(data), self.batch_size):
                    yield data[i:i + self.batch_size]

            tf.logging.warn("Exhausted training data, looping back...")

@@ -1026,16 +1026,37 @@
 files))%0A
+        print(files)%0A
         
@@ -1346,75 +1346,8 @@
  1)%0A
-                print(data.shape)%0A                print(len(data))%0A
     
@@ -1423,16 +1423,18 @@
 ta - 127
+.0
 ) / 127.
@@ -1434,16 +1434,47 @@
  / 127.0
+  # Not 127, as (255 / 127) %3E 1
 %0A       
@@ -1495,20 +1495,16 @@
 t np.min
-imum
 (data) %3E
@@ -1507,16 +1507,50 @@
 a) %3E= -1
+, %22min is %7B%7D%22.format(np.min(data))
 %0A       
@@ -1575,12 +1575,8 @@
 .max
-imum
 (dat
@@ -1581,17 +1581,80 @@
 ata) %3C= 
-1
+255 / 127, %22max is %7B%7D%22.format(%0A                    np.max(data))
 %0A       

change range checks in data_utils to catch an edge case