'''Utilities.'''
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import itertools
import operator
import os

import numpy as np
import scipy.io.wavfile as wavfile
import scipy.signal as signal

from chainer.dataset.dataset_mixin import DatasetMixin


def binarize(images, xp=np):
    """
    Stochastically binarize values in [0, 1] by treating them as p-values of
    a Bernoulli distribution.
    """
    return (xp.random.uniform(size=images.shape) < images).astype('i')


def quantisize(images, levels):
    return (np.digitize(images, np.arange(levels) / levels) - 1).astype('i')


def convert_to_rgb(images, xp=np):
    return xp.tile(images, [1, 3, 1, 1])


def sample_from(distribution):
    batch_size, bins = distribution.shape
    return np.array([np.random.choice(bins, p=distr) for distr in distribution])


def extract_labels(data):
    return np.fromiter(map(operator.itemgetter(1), data), dtype='i')


def extract_images(data):
    return np.array(list(map(operator.itemgetter(0), data))).astype('f')


def mulaw(audio, mu=255):
    return np.sign(audio) * np.log1p(mu * np.abs(audio)) / np.log1p(mu)


def wav_to_float(audio, bits=16):
    '''Squash -2 ** 15; 2 ** 15 into [-1, 1] range'''
    return audio / 2 ** (bits-1)


def wav_files_in(dir):
    for path, _, files in os.walk(dir):
        names = [name for name in files if '.wav' in name]
        for name in names:
            yield os.path.join(path, name)


def _preprocess(ifilename, rate, chunk_length):
    baserate, data = wavfile.read(ifilename)
    audio = signal.resample_poly(data, rate, baserate)
    audio = mulaw(wav_to_float(audio))
    while len(audio) >= chunk_length:
        yield audio[:chunk_length]
        audio = audio[chunk_length:]


def nth(iterable, n, default=None):
    "Returns the nth item or a default value (from itertool recipes)"
    return next(itertools.islice(iterable, n, None), default)


class VCTK(DatasetMixin):
    def __init__(self, root_dir, rate, chunk_length):
        self.indices = {}
        self.dir = root_dir
        self.rate = rate
        self.chunk = chunk_length

        self._populate()

    def _populate(self):
        idx = 0
        for wfile in wav_files_in(self.dir):
            for cidx, _ in enumerate(_preprocess(wfile, self.rate, self.chunk)):
                self.indices[idx] = [wfile, cidx]
                idx += 1

    def __len__(self):
        return len(self.indices)

    def get_example(self, i):
        wfile, idx = self.indices[i]
        sample = np.expand_dims(np.expand_dims(
            nth(_preprocess(wfile, self.rate, self.chunk), idx), 0), 0).astype(np.float32)
        return (sample, quantisize(sample, 256), np.array(0))

@@ -1959,16 +1959,19 @@
 fault)%0A%0A
+#%25%25
 %0Aclass V
@@ -2024,84 +2024,74 @@
 _dir
-, rate, chunk_length):%0A        self.indices = %7B%7D%0A        self.dir = root_
+):%0A        self._populate(root_dir)%0A%0A    def _populate(self, 
 dir
+):
 %0A   
@@ -2099,24 +2099,17 @@
     
-self.rate = rate
+data = %5B%5D
 %0A   
@@ -2117,101 +2117,89 @@
     
-self.chunk = chunk_length%0A%0A        self._populate()%0A%0A    def _populate(self):%0A        idx = 0
+files = %5Bos.path.join(dir, name) for name in os.listdir(dir) if 'vctk_' in name%5D%0A
 %0A   
@@ -2211,121 +2211,115 @@
 for 
-wfil
+nam
 e in 
-wav_
 files
-_in(self.dir):%0A            for cidx, _ in enumerate(_preprocess(wfile, self.rate, self.chunk
+:%0A            with open(name, 'rb') as ifile:%0A                fstat = os.fstat(ifile.fileno(
 ))
-:
 %0A   
@@ -2335,154 +2335,172 @@
     
-self.indices%5Bidx%5D = %5Bwfile, cidx%5D%0A                idx += 1%0A%0A    def __len__(self):%0A        return len(self.indices)%0A%0A    def get_example(self, i):
+while ifile.tell() %3C fstat.st_size:%0A                    d = np.load(ifile).astype(np.float32)%0A                    data.append(d)%0A        data = np.concatenate(data)
 %0A   
@@ -2508,223 +2508,271 @@
     
-wfile
+count
 , 
+w
 id
-x = self.indices%5Bi%5D%0A        sample = np.expand_dims(np.expand_dims(%0A            nth(_preprocess(wfile, 
+th = data.shape%0A        self.data = np.reshape(data, %5Bcount, 1, 1, width%5D)%0A        self.labels = quantisize(self.data, 256)%0A%0A    def __len__(self):%0A        return len(
 self.
-rate, self.chunk), idx), 0), 0).astype(np.float32)%0A        return (sample, quantisize(sample, 256)
+data)%0A%0A    def get_example(self, i):%0A        return (self.data%5Bi%5D, self.labels%5Bi%5D
 , np

Reimplement VCTK dataset