# Path: batchup/image/utils.py
# class ImageArrayUInt8ToFloat32 (object):
#     """
#     An array wrapper class that converts `uint8` image values in the range
#     0:255 to `float32` with the range being (optionally) provided to the
#     constructor. The default range is 0:1.
# 
#     Note: read-only
#     """
#     def __init__(self, arr_u8, val_lower=0.0, val_upper=1.0):
#         """
#         Constructor
# 
#         Parameters
#         ----------
#         arr_8: array-like object e.g. a NumPy array
#             An object that supports `__len__` and `__getitem__` and
#             optionally has a shape attribute
#         val_lower: float
#             The lower end of the output value range
#         val_upper: float
#             The upper end of the output value range
#         """
#         self.arr_u8 = arr_u8
#         self.offset = np.float32(val_lower)
#         self.scale = np.float32((val_upper - val_lower) / 255.0)
#         self.dtype = np.float32
# 
#     @property
#     def shape(self):
#         """
#         Shape attribute accessor
# 
#         Returns
#         -------
#         The shape of the underlying array if available, otherwise
#         raises `AttributeError`
#         """
#         try:
#             return self.arr_u8.shape
#         except AttributeError:
#             raise AttributeError('Underlying array has no `shape` attribute')
# 
#     def __len__(self):
#         """
#         Length accessor
# 
#         Returns
#         -------
#         The length of the underlying array.
#         """
#         return len(self.arr_u8)
# 
#     def __getitem__(self, *indices):
#         """
#         Item accessor
# 
#         Accesses elements from the underlying array and converts them to
#         `float32` type.
# 
#         Parameters
#         ----------
#         indices
#             Indices into the underlying array
# 
#         Returns
#         -------
#         Items from underlying array converted to `float32`
#         """
#         x = self.arr_u8.__getitem__(*indices)
#         return x.astype(np.float32) * self.scale + self.offset


import os
import numpy as np
import tables
from scipy.io import loadmat
from .. import config
from ..image.utils import ImageArrayUInt8ToFloat32
from . import dataset
def delete_cache():  # pragma: no cover
    dataset.delete_dataset_cache(_H5_SVHN_FILENAME, _H5_EXTRA_FILENAME)


class SVHN (object):
    def __init__(self, n_val=10000, val_lower=0.0, val_upper=1.0):
        h5_path = fetch_svhn_train_test()
        if h5_path is not None:
            f = tables.open_file(h5_path, mode='r')

            train_X_u8 = f.root.svhn.train_X_u8
            train_y = f.root.svhn.train_y
            self.test_X_u8 = f.root.svhn.test_X_u8
            self.test_y = f.root.svhn.test_y

            if n_val == 0 or n_val is None:
                self.train_X_u8 = train_X_u8
                self.train_y = train_y
                self.val_X_u8 = np.zeros((0, 3, 32, 32), dtype=np.uint8)
                self.val_y = np.zeros((0,), dtype=np.int32)
            else:
                self.train_X_u8 = train_X_u8[:-n_val]
                self.train_y = train_y[:-n_val]
                self.val_X_u8 = train_X_u8[-n_val:]
                self.val_y = train_y[-n_val:]
        else:
            raise RuntimeError('Could not load SVHN dataset')

        self.train_X = ImageArrayUInt8ToFloat32(self.train_X_u8, val_lower,
                                                val_upper)

# Path: batchup/image/utils.py
# class ImageArrayUInt8ToFloat32 (object):
#     """
#     An array wrapper class that converts `uint8` image values in the range
#     0:255 to `float32` with the range being (optionally) provided to the
#     constructor. The default range is 0:1.
# 
#     Note: read-only
#     """
#     def __init__(self, arr_u8, val_lower=0.0, val_upper=1.0):
#         """
#         Constructor
# 
#         Parameters
#         ----------
#         arr_8: array-like object e.g. a NumPy array
#             An object that supports `__len__` and `__getitem__` and
#             optionally has a shape attribute
#         val_lower: float
#             The lower end of the output value range
#         val_upper: float
#             The upper end of the output value range
#         """
#         self.arr_u8 = arr_u8
#         self.offset = np.float32(val_lower)
#         self.scale = np.float32((val_upper - val_lower) / 255.0)
#         self.dtype = np.float32
# 
#     @property
#     def shape(self):
#         """
#         Shape attribute accessor
# 
#         Returns
#         -------
#         The shape of the underlying array if available, otherwise
#         raises `AttributeError`
#         """
#         try:
#             return self.arr_u8.shape
#         except AttributeError:
#             raise AttributeError('Underlying array has no `shape` attribute')
# 
#     def __len__(self):
#         """
#         Length accessor
# 
#         Returns
#         -------
#         The length of the underlying array.
#         """
#         return len(self.arr_u8)
# 
#     def __getitem__(self, *indices):
#         """
#         Item accessor
# 
#         Accesses elements from the underlying array and converts them to
#         `float32` type.
# 
#         Parameters
#         ----------
#         indices
#             Indices into the underlying array
# 
#         Returns
#         -------
#         Items from underlying array converted to `float32`
#         """
#         x = self.arr_u8.__getitem__(*indices)
#         return x.astype(np.float32) * self.scale + self.offset


# Path: batchup/datasets/svhn.py
import os
import numpy as np
import tables
from scipy.io import loadmat
from .. import config
from ..image.utils import ImageArrayUInt8ToFloat32
from . import dataset

def delete_cache():  # pragma: no cover
    dataset.delete_dataset_cache(_H5_SVHN_FILENAME, _H5_EXTRA_FILENAME)


class SVHN (object):
    def __init__(self, n_val=10000, val_lower=0.0, val_upper=1.0):
        h5_path = fetch_svhn_train_test()
        if h5_path is not None:
            f = tables.open_file(h5_path, mode='r')

            train_X_u8 = f.root.svhn.train_X_u8
            train_y = f.root.svhn.train_y
            self.test_X_u8 = f.root.svhn.test_X_u8
            self.test_y = f.root.svhn.test_y

            if n_val == 0 or n_val is None:
                self.train_X_u8 = train_X_u8
                self.train_y = train_y
                self.val_X_u8 = np.zeros((0, 3, 32, 32), dtype=np.uint8)
                self.val_y = np.zeros((0,), dtype=np.int32)
            else:
                self.train_X_u8 = train_X_u8[:-n_val]
                self.train_y = train_y[:-n_val]
                self.val_X_u8 = train_X_u8[-n_val:]
                self.val_y = train_y[-n_val:]
        else:
            raise RuntimeError('Could not load SVHN dataset')

        self.train_X = ImageArrayUInt8ToFloat32(self.train_X_u8, val_lower,
                                                val_upper)

        self.val_X = ImageArrayUInt8ToFloat32(self.val_X_u8, val_lower,