# Path: rlvision/utils.py
# def load_grid_selection(path):
# def process_map_data(path, return_full=False):
# def mask_grid(pos, grid, radius, one_is_free=True):
# def accumulate_map(source_grid, new_grid, one_is_free=True):
# def plot_grid(data, imsize, start=None, pos=None, goal=None, title=None):
# def process_gridworld_data(data_in, imsize):
# def load_mat_data(file_path):
# def find_layout(data, verbose=0):
# def init_h5_db(db_name, save_dir):
# def add_h5_group(group_name, db, db_path=None):
# def add_h5_ds(data, ds_name, db, group_name=None, data_type=np.uint8,
#               renew=False):
# def create_grid_8_dataset(mat_file_path, db_name, save_dir):
# def create_ds_from_splits(mat_file_prefix, db, splits):
# def create_grid_16_dataset(mat_file_prefix, db_name, save_dir, splits=5):
# def create_grid_28_dataset(mat_file_prefix, db_name, save_dir, splits=20):
# def create_grid_40_dataset(mat_file_prefix, db_name, save_dir, splits=100):
# def load_grid8(return_imsize=True):
# def load_grid16(split=None, return_imsize=True):
# def load_grid28(split=None, return_imsize=True):
# def load_grid40(split=None, return_imsize=True):
# 
# Path: rlvision/utils.py
# def process_map_data(path, return_full=False):
#     data = joblib.load(path)
# 
#     im_data = data['im']
#     value_data = data['value']
#     state_data = data['state']
# 
#     if return_full:
#         im_full = np.concatenate((np.expand_dims(im_data, 1),
#                                   np.expand_dims(value_data, 1)),
#                                  axis=1).astype(dtype=np.uint8)
#         return im_full, state_data, data['label'], data['sample_idx']
# 
#     label_data = np.array([np.eye(1, 8, l)[0] for l in data['label']])
#     num = im_data.shape[0]
#     num_train = num - num / 5
# 
#     im_train = np.concatenate((np.expand_dims(im_data[:num_train], 1),
#                                np.expand_dims(value_data[:num_train], 1)),
#                               axis=1).astype(dtype=np.float32)
#     state_train = state_data[:num_train]
#     label_train = label_data[:num_train]
# 
#     im_test = np.concatenate((np.expand_dims(im_data[num_train:], 1),
#                               np.expand_dims(value_data[num_train:], 1)),
#                              axis=1).astype(dtype=np.float32)
#     state_test = state_data[num_train:]
#     label_test = label_data[num_train:]
# 
#     return (im_train, state_train, label_train), \
#            (im_test, state_test, label_test), data['sample_idx']
# 
# Path: rlvision/grid.py
# class GridSampler(object):
#     """Sample grid from data."""
#     def __init__(self, im_data, state_data, label_data, sample_idx, im_size):
#         """Init data sampler."""
#         self.im_data = im_data
#         self.state_data = state_data
#         self.label_data = label_data
#         self.sample_idx = sample_idx
#         self.im_size = im_size
#         self.grid_idx = 0
# 
#     def find_goal(self, value_grid):
#         """find goal position."""
#         goal = np.argwhere(value_grid.max() == value_grid)[0][::-1]
#         return (goal[0], goal[1])
# 
#     def get_grid(self, grid_idx):
#         """get a grid according to grid idx."""
#         end_idx = self.sample_idx[grid_idx]
#         start_idx = self.sample_idx[grid_idx-1] if grid_idx != 0 else 0
#         grid = self.im_data[start_idx]
#         label = self.label_data[start_idx:end_idx]
#         state = self.state_data[start_idx:end_idx]
#         goal = self.find_goal(grid[1])
#         return grid, state, label, goal
# 
#     def compare_grid(self, grid_1, grid_2):
#         """compare grid."""
#         return np.array_equal(grid_1[0], grid_2[0])
# 
#     def next(self):
#         """Find next grid."""
#         if self.grid_idx < len(self.sample_idx):
#             grid_data = self.get_grid(self.grid_idx)
#             self.grid_idx += 1
# 
#         return grid_data


import os
import cPickle as pickle
import numpy as np
import rlvision
from rlvision import utils
from rlvision.utils import process_map_data
from rlvision.grid import GridSampler
"""Vis vin data.

Author: Yuhuang Hu
Email : duguyue100@gmail.com
"""



file_name = os.path.join(rlvision.RLVISION_DATA,
                         "chain_data", "grid8_po.pkl")

im_data, state_data, label_data, sample_idx = process_map_data(
    file_name, return_full=True)
sampler = GridSampler(im_data, state_data, label_data, sample_idx, (28, 28))

for grid_id in xrange(len(sample_idx)):
    grid, state, label = sampler.get_grid(grid_id)
    start_idx = sample_idx[grid_id-1] if grid_id != 0 else 0

    for state_idx in xrange(state.shape[0]):

# Path: rlvision/utils.py
# def load_grid_selection(path):
# def process_map_data(path, return_full=False):
# def mask_grid(pos, grid, radius, one_is_free=True):
# def accumulate_map(source_grid, new_grid, one_is_free=True):
# def plot_grid(data, imsize, start=None, pos=None, goal=None, title=None):
# def process_gridworld_data(data_in, imsize):
# def load_mat_data(file_path):
# def find_layout(data, verbose=0):
# def init_h5_db(db_name, save_dir):
# def add_h5_group(group_name, db, db_path=None):
# def add_h5_ds(data, ds_name, db, group_name=None, data_type=np.uint8,
#               renew=False):
# def create_grid_8_dataset(mat_file_path, db_name, save_dir):
# def create_ds_from_splits(mat_file_prefix, db, splits):
# def create_grid_16_dataset(mat_file_prefix, db_name, save_dir, splits=5):
# def create_grid_28_dataset(mat_file_prefix, db_name, save_dir, splits=20):
# def create_grid_40_dataset(mat_file_prefix, db_name, save_dir, splits=100):
# def load_grid8(return_imsize=True):
# def load_grid16(split=None, return_imsize=True):
# def load_grid28(split=None, return_imsize=True):
# def load_grid40(split=None, return_imsize=True):
# 
# Path: rlvision/utils.py
# def process_map_data(path, return_full=False):
#     data = joblib.load(path)
# 
#     im_data = data['im']
#     value_data = data['value']
#     state_data = data['state']
# 
#     if return_full:
#         im_full = np.concatenate((np.expand_dims(im_data, 1),
#                                   np.expand_dims(value_data, 1)),
#                                  axis=1).astype(dtype=np.uint8)
#         return im_full, state_data, data['label'], data['sample_idx']
# 
#     label_data = np.array([np.eye(1, 8, l)[0] for l in data['label']])
#     num = im_data.shape[0]
#     num_train = num - num / 5
# 
#     im_train = np.concatenate((np.expand_dims(im_data[:num_train], 1),
#                                np.expand_dims(value_data[:num_train], 1)),
#                               axis=1).astype(dtype=np.float32)
#     state_train = state_data[:num_train]
#     label_train = label_data[:num_train]
# 
#     im_test = np.concatenate((np.expand_dims(im_data[num_train:], 1),
#                               np.expand_dims(value_data[num_train:], 1)),
#                              axis=1).astype(dtype=np.float32)
#     state_test = state_data[num_train:]
#     label_test = label_data[num_train:]
# 
#     return (im_train, state_train, label_train), \
#            (im_test, state_test, label_test), data['sample_idx']
# 
# Path: rlvision/grid.py
# class GridSampler(object):
#     """Sample grid from data."""
#     def __init__(self, im_data, state_data, label_data, sample_idx, im_size):
#         """Init data sampler."""
#         self.im_data = im_data
#         self.state_data = state_data
#         self.label_data = label_data
#         self.sample_idx = sample_idx
#         self.im_size = im_size
#         self.grid_idx = 0
# 
#     def find_goal(self, value_grid):
#         """find goal position."""
#         goal = np.argwhere(value_grid.max() == value_grid)[0][::-1]
#         return (goal[0], goal[1])
# 
#     def get_grid(self, grid_idx):
#         """get a grid according to grid idx."""
#         end_idx = self.sample_idx[grid_idx]
#         start_idx = self.sample_idx[grid_idx-1] if grid_idx != 0 else 0
#         grid = self.im_data[start_idx]
#         label = self.label_data[start_idx:end_idx]
#         state = self.state_data[start_idx:end_idx]
#         goal = self.find_goal(grid[1])
#         return grid, state, label, goal
# 
#     def compare_grid(self, grid_1, grid_2):
#         """compare grid."""
#         return np.array_equal(grid_1[0], grid_2[0])
# 
#     def next(self):
#         """Find next grid."""
#         if self.grid_idx < len(self.sample_idx):
#             grid_data = self.get_grid(self.grid_idx)
#             self.grid_idx += 1
# 
#         return grid_data


# Path: rlvision/tests/vis_vin_data.py
import os
import cPickle as pickle
import numpy as np
import rlvision
from rlvision import utils
from rlvision.utils import process_map_data
from rlvision.grid import GridSampler

"""Vis vin data.

Author: Yuhuang Hu
Email : duguyue100@gmail.com
"""



file_name = os.path.join(rlvision.RLVISION_DATA,
                         "chain_data", "grid8_po.pkl")

im_data, state_data, label_data, sample_idx = process_map_data(
    file_name, return_full=True)
sampler = GridSampler(im_data, state_data, label_data, sample_idx, (28, 28))

for grid_id in xrange(len(sample_idx)):
    grid, state, label = sampler.get_grid(grid_id)
    start_idx = sample_idx[grid_id-1] if grid_id != 0 else 0

    for state_idx in xrange(state.shape[0]):

        utils.plot_grid(1-im_data[start_idx+state_idx][0], (8, 8),