def get_dataset():
    cache_file = 'dataset_cache.pkl.gz')
    if os.path.exists(cache_file):
        with open(cache_file, 'rb') as f:
            dataset = pickle.load(f)
        return dataset['data'], dataset['target']

    raw_dataset = load_raw_images()
    data = load_image_files(raw_dataset.filenames)
    data = np.array(list(data))

    mask_dataset = load_mask_images()
    masks = load_image_files(mask_dataset.filenames)
    target = convert_masks_to_target(masks, negative=True)

    with open(cache_file, 'wb') as f:
        dataset = {'data': data, 'target': target}
        pickle.dump(dataset, f)

    return data, target

myae = ae.AutoEncoder(
            layers=[
                ae.Layer('Tanh', units=128),
                ae.Layer('Sigmoid', units=64),
            ],
            learning_rate=0.002,
            n_iter=10,
        )


for _ in xrange(n_iter):
    print '--------------------------------------------------------'
    p = np.random.randint(0, N, batch_size)
    images = load_image_files(filenames[p], as_grey=True)
    X = np.array([resize(im, (356,534)).reshape(-1) for im in images])
    y = np.zeros((batch_size, n_param), dtype=np.float)

    print 'X:\n{0}\n{1}'.format(X, X.shape)
    print 'y:\n{0}\n{1}'.format(y, y.shape)

    X = X.astype(np.float) / 255.

    print 'X: \n{0}\n{1}'.format(X, X.shape)

    myae.fit(X)

    print 'dir(myae):\n{0}'.format(dir(myae))
    print 'myae.__dict__:\n{0}'.format(myae.__dict__)
    print '--------------------------------------------------------'
import gzip

import numpy as np
from skimage import io
from sklearn.datasets.base import Bunch

from dip.load_data import load_image_files, load_mask_images
from dip.mask import bounding_rect_of_mask


datasets = load_mask_images()

data = []
for f, mask in zip(
        datasets.filenames,
        load_image_files(datasets.filenames),
        ):
    # rect: (min_x, max_x, min_y, max_x)
    rect = bounding_rect_of_mask(mask, negative=True)
    data.append(list(rect))
    print('{0}: {1}'.format(f, rect))

bunch = Bunch(name='mask rects')
bunch.data = np.array(data)
bunch.filenames = datasets.filenames
bunch.target = datasets.target
bunch.target_names = datasets.target_names
bunch.description = 'mask rects: (min_x, min_y, max_x, max_y)'

with gzip.open('rects.pkl.gz', 'wb') as f:
    pickle.dump(bunch, f)