# Path: rlvision/grid.py
# class GridDataSampler(object):
#     """A grid data sampler from the raw data."""
# 
#     def __init__(self, grid_data, value_data, im_size, states_xy,
#                  label_data):
#         """Init grid data sampler.
#         Pararmeters
#         -----------
#         im_data : numpy.ndarray
#             The image data
#         value_data : numpy.ndarray
#             the value data
#         im_size : tuple
#             the size of the map
#         states_xy : numpy.ndarry
#             the one that encode the paths (n_states, 2)
#         """
#         self.grid_data = grid_data
#         self.value_data = value_data
# 
#         if im_size[0]*im_size[1] == grid_data.shape[1]:
#             self.im_size = im_size
# 
#         self.states_xy = states_xy
#         self.label_data = label_data
#         self.curr_idx = 0
#         self.grid_available = True
# 
#     def compare_pos(self, pos1, pos2):
#         """Compare position."""
#         if pos1[0] == pos2[0] and pos1[1] == pos2[1]:
#             return True
#         return False
# 
#     def get_next_state(self, pos, action):
#         """Get next state according to action."""
#         new_pos = [0, 0]
#         if action in [5, 0, 4]:
#             new_pos[0] = pos[0]-1
#         elif action in [7, 1, 6]:
#             new_pos[0] = pos[0]+1
#         else:
#             new_pos[0] = pos[0]
# 
#         if action in [5, 3, 7]:
#             new_pos[1] = pos[1]-1
#         elif action in [4, 2, 6]:
#             new_pos[1] = pos[1]+1
#         else:
#             new_pos[1] = pos[1]
#         return tuple(new_pos)
# 
#     def get_goal_pos(self, value_grid):
#         """Get goal position."""
#         value_map = np.reshape(value_grid.copy(), self.im_size)
#         goal_list = np.where(value_map == value_map.max())
#         # assume the first one
#         return (goal_list[0][0], goal_list[1][0])
# 
#     def next(self):
#         """Sample next sample."""
#         if self.grid_available:
#             grid = self.grid_data[self.curr_idx]
#             value = self.value_data[self.curr_idx]
#             goal_pos = self.get_goal_pos(value)
# 
#             # find end block idx
#             curr_idx = self.curr_idx
#             while curr_idx != self.grid_data.shape[0]:
#                 if np.array_equal(grid, self.grid_data[curr_idx]):
#                     curr_idx += 1
#                 else:
#                     break
# 
#             # parse grid traj
#             start_pos_list = []
#             pos_traj = []
#             flag = True
#             for idx in xrange(self.curr_idx, curr_idx):
#                 curr_pos = (self.states_xy[idx][0], self.states_xy[idx][1])
#                 next_pos = self.get_next_state(curr_pos, self.label_data[idx])
# 
#                 if flag is True:
#                     # when find a new start
#                     # append the new start to the list
#                     start_pos_list.append(curr_pos)
#                     # construct a new pos traj
#                     temp_pos_traj = []
#                     # start searching
#                     flag = False
#                 # evaluate if the next state is the goal position
#                 if self.compare_pos(goal_pos, next_pos):
#                     # if yes, for next state, a new start begin
#                     flag = True
#                     # append the temp pos traj to collector
#                     pos_traj.append(temp_pos_traj)
# 
#                 temp_pos_traj.append(curr_pos)
# 
#             # update current idx
#             self.curr_idx = curr_idx
#             if self.curr_idx == self.grid_data.shape[0]:
#                 self.grid_available = False
# 
#             return grid, value, start_pos_list, pos_traj, goal_pos
#         else:
#             print ("[MESSAGE] No grid available""")


import os
import numpy as np
import rlvision
import rlvision.utils as utils
from rlvision.grid import GridDataSampler
"""Restructure data.

As the original data has shuffled the data randomly,
therefore it's difficult to reconstruct the exact path.
Here we manually select the necessary data from raw data.

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

from __future__ import print_function



# import data

db, imsize = utils.load_grid40(split=3)

# let's have a look at data

im_data = db['im_data']
value_data = db['value_data']
states = db['state_xy_data']
label_data = db['label_data']

print ("[MESSAGE] DATA LOADED")


# Path: rlvision/grid.py
# class GridDataSampler(object):
#     """A grid data sampler from the raw data."""
# 
#     def __init__(self, grid_data, value_data, im_size, states_xy,
#                  label_data):
#         """Init grid data sampler.
#         Pararmeters
#         -----------
#         im_data : numpy.ndarray
#             The image data
#         value_data : numpy.ndarray
#             the value data
#         im_size : tuple
#             the size of the map
#         states_xy : numpy.ndarry
#             the one that encode the paths (n_states, 2)
#         """
#         self.grid_data = grid_data
#         self.value_data = value_data
# 
#         if im_size[0]*im_size[1] == grid_data.shape[1]:
#             self.im_size = im_size
# 
#         self.states_xy = states_xy
#         self.label_data = label_data
#         self.curr_idx = 0
#         self.grid_available = True
# 
#     def compare_pos(self, pos1, pos2):
#         """Compare position."""
#         if pos1[0] == pos2[0] and pos1[1] == pos2[1]:
#             return True
#         return False
# 
#     def get_next_state(self, pos, action):
#         """Get next state according to action."""
#         new_pos = [0, 0]
#         if action in [5, 0, 4]:
#             new_pos[0] = pos[0]-1
#         elif action in [7, 1, 6]:
#             new_pos[0] = pos[0]+1
#         else:
#             new_pos[0] = pos[0]
# 
#         if action in [5, 3, 7]:
#             new_pos[1] = pos[1]-1
#         elif action in [4, 2, 6]:
#             new_pos[1] = pos[1]+1
#         else:
#             new_pos[1] = pos[1]
#         return tuple(new_pos)
# 
#     def get_goal_pos(self, value_grid):
#         """Get goal position."""
#         value_map = np.reshape(value_grid.copy(), self.im_size)
#         goal_list = np.where(value_map == value_map.max())
#         # assume the first one
#         return (goal_list[0][0], goal_list[1][0])
# 
#     def next(self):
#         """Sample next sample."""
#         if self.grid_available:
#             grid = self.grid_data[self.curr_idx]
#             value = self.value_data[self.curr_idx]
#             goal_pos = self.get_goal_pos(value)
# 
#             # find end block idx
#             curr_idx = self.curr_idx
#             while curr_idx != self.grid_data.shape[0]:
#                 if np.array_equal(grid, self.grid_data[curr_idx]):
#                     curr_idx += 1
#                 else:
#                     break
# 
#             # parse grid traj
#             start_pos_list = []
#             pos_traj = []
#             flag = True
#             for idx in xrange(self.curr_idx, curr_idx):
#                 curr_pos = (self.states_xy[idx][0], self.states_xy[idx][1])
#                 next_pos = self.get_next_state(curr_pos, self.label_data[idx])
# 
#                 if flag is True:
#                     # when find a new start
#                     # append the new start to the list
#                     start_pos_list.append(curr_pos)
#                     # construct a new pos traj
#                     temp_pos_traj = []
#                     # start searching
#                     flag = False
#                 # evaluate if the next state is the goal position
#                 if self.compare_pos(goal_pos, next_pos):
#                     # if yes, for next state, a new start begin
#                     flag = True
#                     # append the temp pos traj to collector
#                     pos_traj.append(temp_pos_traj)
# 
#                 temp_pos_traj.append(curr_pos)
# 
#             # update current idx
#             self.curr_idx = curr_idx
#             if self.curr_idx == self.grid_data.shape[0]:
#                 self.grid_available = False
# 
#             return grid, value, start_pos_list, pos_traj, goal_pos
#         else:
#             print ("[MESSAGE] No grid available""")


# Path: rlvision/tests/restructure_dataset.py
import os
import numpy as np
import rlvision
import rlvision.utils as utils
from rlvision.grid import GridDataSampler

"""Restructure data.

As the original data has shuffled the data randomly,
therefore it's difficult to reconstruct the exact path.
Here we manually select the necessary data from raw data.

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

from __future__ import print_function



# import data

db, imsize = utils.load_grid40(split=3)

# let's have a look at data

im_data = db['im_data']
value_data = db['value_data']
states = db['state_xy_data']
label_data = db['label_data']

print ("[MESSAGE] DATA LOADED")


grid_sampler = GridDataSampler(im_data, value_data, imsize, states,