# Path: hamaa/datasets/mnist/mnist_decoder.py
# def decode_idx3_ubyte(idx3_ubyte_file, num_data=-1):
# def decode_idx1_ubyte(idx1_ubyte_file, num_data=-1):
# def load_train_images(idx_ubyte_file=train_images_idx3_ubyte_file, num_data=-1):
# def load_train_labels(idx_ubyte_file=train_labels_idx1_ubyte_file, num_data=-1):
# def load_test_images(idx_ubyte_file=test_images_idx3_ubyte_file, num_data=-1):
# def load_test_labels(idx_ubyte_file=test_labels_idx1_ubyte_file, num_data=-1):
# def run():
# 
# Path: hamaa/utils/np_utils.py
# def to_one_hot(y, nb_class):
# def to_categorical(y):
# def split_training_data(data, nb_validation=None, split_ratio=None):
# def padding(images, pad_size):
# def rot180(images):
# def eval_numerical_gradient_array(f, x, df=None, verbose=False, h=1e-4):
# def sum_abs_err(u, v):


import gzip
import os
import urllib
import numpy as np
from PIL import Image
from .mnist import mnist_decoder
from ..utils import np_utils

    # 检查是否缺失解压后的数据文件
    miss_bin_file = False
    for filename in filenames:
        file_path = mnist_bin_dir + filename.split('.')[0]
        if not os.path.exists(file_path):
            miss_bin_file = True
            break

    if not os.path.exists(mnist_bin_dir) or miss_bin_file:
        if not os.path.exists(mnist_bin_dir):
            os.mkdir(mnist_bin_dir)

        # 开始解压
        for filename in filenames:
            print 'unzip ' + filename + ' ...'
            fn = filename.split()
            in_file = gzip.GzipFile(mnist_gz_dir + filename, 'rb')
            out_file = open(mnist_bin_dir + filename.split('.')[0], 'wb')
            out_file.write(in_file.read())
            in_file.close()
            out_file.close()


def load_mnist_data(nb_training, nb_test, preprocess=False, flatten=True, one_hot=True):
    # 自动检查数据，如果数据文件不存在则会先自动下载
    download_mnist_data()
    training_x = mnist_decoder.load_train_images(num_data=nb_training)
    training_y = mnist_decoder.load_train_labels(num_data=nb_training)
    test_x = mnist_decoder.load_test_images(num_data=nb_test)

# Path: hamaa/datasets/mnist/mnist_decoder.py
# def decode_idx3_ubyte(idx3_ubyte_file, num_data=-1):
# def decode_idx1_ubyte(idx1_ubyte_file, num_data=-1):
# def load_train_images(idx_ubyte_file=train_images_idx3_ubyte_file, num_data=-1):
# def load_train_labels(idx_ubyte_file=train_labels_idx1_ubyte_file, num_data=-1):
# def load_test_images(idx_ubyte_file=test_images_idx3_ubyte_file, num_data=-1):
# def load_test_labels(idx_ubyte_file=test_labels_idx1_ubyte_file, num_data=-1):
# def run():
# 
# Path: hamaa/utils/np_utils.py
# def to_one_hot(y, nb_class):
# def to_categorical(y):
# def split_training_data(data, nb_validation=None, split_ratio=None):
# def padding(images, pad_size):
# def rot180(images):
# def eval_numerical_gradient_array(f, x, df=None, verbose=False, h=1e-4):
# def sum_abs_err(u, v):


# Path: hamaa/datasets/datasets.py
import gzip
import os
import urllib
import numpy as np
from PIL import Image
from .mnist import mnist_decoder
from ..utils import np_utils


    # 检查是否缺失解压后的数据文件
    miss_bin_file = False
    for filename in filenames:
        file_path = mnist_bin_dir + filename.split('.')[0]
        if not os.path.exists(file_path):
            miss_bin_file = True
            break

    if not os.path.exists(mnist_bin_dir) or miss_bin_file:
        if not os.path.exists(mnist_bin_dir):
            os.mkdir(mnist_bin_dir)

        # 开始解压
        for filename in filenames:
            print 'unzip ' + filename + ' ...'
            fn = filename.split()
            in_file = gzip.GzipFile(mnist_gz_dir + filename, 'rb')
            out_file = open(mnist_bin_dir + filename.split('.')[0], 'wb')
            out_file.write(in_file.read())
            in_file.close()
            out_file.close()


def load_mnist_data(nb_training, nb_test, preprocess=False, flatten=True, one_hot=True):
    # 自动检查数据，如果数据文件不存在则会先自动下载
    download_mnist_data()
    training_x = mnist_decoder.load_train_images(num_data=nb_training)
    training_y = mnist_decoder.load_train_labels(num_data=nb_training)
    test_x = mnist_decoder.load_test_images(num_data=nb_test)

    test_y = mnist_decoder.load_test_labels(num_data=nb_test)