<reponame>emadboctorx/stable-baselines<gh_stars>0
import numpy as np


class Dataset(object):
    def __init__(self, data_map, shuffle=True):
        self.data_map = data_map
        self.shuffle = shuffle
        self.n_samples = next(iter(data_map.values())).shape[0]
        self._next_id = 0
        if self.shuffle:
            self.shuffle_dataset()

    def shuffle_dataset(self):
        perm = np.arange(self.n_samples)
        np.random.shuffle(perm)
        for key in self.data_map:
            self.data_map[key] = self.data_map[key][perm]

    def next_batch(self, batch_size):
        if self._next_id >= self.n_samples:
            self._next_id = 0
            if self.shuffle:
                self.shuffle_dataset()
        cur_id = self._next_id
        cur_batch_size = min(batch_size, self.n_samples - self._next_id)
        self._next_id += cur_batch_size
        data_map = dict()
        for key in self.data_map:
            data_map[key] = self.data_map[key][cur_id : cur_id + cur_batch_size]
        return data_map

    def iterate_once(self, batch_size):
        if self.shuffle:
            self.shuffle_dataset()
        while self._next_id <= self.n_samples - batch_size:
            yield self.next_batch(batch_size)
        self._next_id = 0

    def subset(self, num_elements, shuffle=True):
        data_map = dict()
        for key in self.data_map:
            data_map[key] = self.data_map[key][:num_elements]
        return Dataset(data_map, shuffle)


def iterbatches(
    arrays,
    *,
    num_batches=None,
    batch_size=None,
    shuffle=True,
    include_final_partial_batch=True,
):
    assert (num_batches is None) != (
        batch_size is None
    ), 'Provide num_batches or batch_size, but not both'
    arrays = tuple(map(np.asarray, arrays))
    n_samples = arrays[0].shape[0]
    assert all(a.shape[0] == n_samples for a in arrays[1:])
    inds = np.arange(n_samples)
    if shuffle:
        np.random.shuffle(inds)
    sections = (
        np.arange(0, n_samples, batch_size)[1:] if num_batches is None else num_batches
    )
    for batch_inds in np.array_split(inds, sections):
        if include_final_partial_batch or len(batch_inds) == batch_size:
            yield tuple(a[batch_inds] for a in arrays)
