decorator.py 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
__all__ = ['buffered', 'compose', 'chain', 'shuffle', 'ComposeNotAligned']
16 17 18

from Queue import Queue
from threading import Thread
19 20
import itertools
import random
21 22


23 24
def shuffle(reader_creator, buf_size):
    """Creates a data reader creator whose data output is suffled.
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    Output from the iterator that created by original reader creator will be
    buffered into shuffle buffer, and then shuffled. The size of shuffle buffer
    is determined by argument buf_size.

    Args:
        reader_creator: the original reader creator whose output will be
            shuffled.
        buf_size: shuffle buffer size.

    Returns:
        the new reader creator whose output is shuffled.
    """

    def create_reader_creator():
        buf = []
        for e in reader_creator():
            buf.append(e)
            if len(buf) >= buf_size:
                random.shuffle(buf)
                for b in buf:
                    yield b
                buf = []

        if len(buf) > 0:
            random.shuffle(buf)
            for b in buf:
                yield b

    return create_reader_creator


def chain(*reader_creators):
    """Creates a data reader creator whose output is the outputs of input data
       reader creators chained together.

    If input reader creators output following data entries:
    [0, 0, 0]
    [1, 1, 1]
    [2, 2, 2]
    The chained reader creator will output:
    [0, 0, 0, 1, 1, 1, 2, 2, 2]

    Args:
        readers_creators: input reader creators

    Returns:
        the new data reader creator.
    """

    def create_reader_creator():
        rs = []
        for r in reader_creators:
            rs.append(r())

        for e in itertools.chain(*rs):
            yield e

    return create_reader_creator


class ComposeNotAligned:
    pass


def compose(*reader_creators, **kwargs):
    """Creates a data reader creator whose output is the combination of input
       readers creators.

    If input reader creators output following data entries:
    (1, 2)    3    (4, 5)
    The composed reader creator will output:
    (1, 2, 3, 4, 5)

    Args:
        *reader_creators: reader creators that will be composed together.
        check_alignment: If True, will check if input reader creators are aligned
            correctly. If False, will not check alignment and trailing outputs
            will be discarded. Defaults to True.

    Returns:
        the new data reader creator.

    Raises:
        ComposeNotAligned: outputs of reader creators are not aligned.
            Will not raise when check_alignment is set to False.
    """
    check_alignment = kwargs.pop('check_alignment', True)

    def make_tuple(x):
        if isinstance(x, tuple):
            return x
        else:
            return (x, )

    def create_reader_creator():
        rs = []
        for r in reader_creators:
            rs.append(r())
        if not check_alignment:
            for outputs in itertools.izip(*rs):
                yield sum(map(make_tuple, outputs), ())
        else:
            for outputs in itertools.izip_longest(*rs):
                for o in outputs:
                    if o is None:
                        # None will be not be present if compose is aligned
                        raise ComposeNotAligned
                yield sum(map(make_tuple, outputs), ())

    return create_reader_creator


def buffered(reader_creator, size):
    """Creates a buffered data reader creator.

    The buffered data reader creator will read and save data entries into a
    buffer. Reading from the buffered data reader creator will proceed as long
    as the buffer is not empty.
144 145
    
    Args:
146
        reader_creator: the data reader creator to read from.
147 148 149
        size: max buffer size.
    
    Returns:
150
        The buffered data reader creator.
151 152 153 154 155 156 157 158 159 160 161 162
    """

    class EndSignal():
        pass

    end = EndSignal()

    def read_worker(r, q):
        for d in r:
            q.put(d)
        q.put(end)

163 164
    def create_reader_creator():
        r = reader_creator()
165 166 167 168 169 170 171 172 173 174 175 176
        q = Queue(maxsize=size)
        t = Thread(
            target=read_worker, args=(
                r,
                q, ))
        t.daemon = True
        t.start()
        e = q.get()
        while e != end:
            yield e
            e = q.get()

177
    return create_reader_creator