aboutsummaryrefslogtreecommitdiff
path: root/transformers.py
blob: c60d36238084b1cbb46c32ed49d5802de4139872 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
from fuel.transformers import Transformer, Filter, Mapping
import numpy
import theano
import random

def at_least_k(k, pl, pad_at_begin):
    if len(pl) == 0:
        pl = [[ -8.61612, 41.1573]]
    if len(pl) < k:
        if pad_at_begin:
            pl = [pl[0]] * (k - len(pl)) + pl
        else:
            pl = pl + [pl[-1]] * (k - len(pl))
    return pl


class Select(Transformer):
    def __init__(self, data_stream, sources):
        super(Select, self).__init__(data_stream)
        self.ids = [data_stream.sources.index(source) for source in sources]
        self.sources=sources

    def get_data(self, request=None):
        if request is not None:
            raise ValueError
        data=next(self.child_epoch_iterator)
        return [data[id] for id in self.ids]

def add_first_k(k, stream):
    id_polyline=stream.sources.index('polyline')
    def first_k(x):
        pl = at_least_k(k, x[id_polyline], False)
        return (numpy.array(pl[:k], dtype=theano.config.floatX).flatten(),)
    stream = Mapping(stream, first_k, ('first_k',))
    return stream

def add_random_k(k, stream):
    id_polyline=stream.sources.index('polyline')
    def random_k(x):
        pl = at_least_k(k, x[id_polyline], True)
        loc = random.randrange(len(pl)-k+1)
        return (numpy.array(pl[loc:loc+k], dtype=theano.config.floatX).flatten(),)
    stream = Mapping(stream, random_k, ('last_k',))
    return stream

def add_last_k(k, stream):
    id_polyline=stream.sources.index('polyline')
    def last_k(x):
        pl = at_least_k(k, x[id_polyline], True)
        return (numpy.array(pl[-k:], dtype=theano.config.floatX).flatten(),)
    stream = Mapping(stream, last_k, ('last_k',))
    return stream

def add_destination(stream):
    id_polyline=stream.sources.index('polyline')
    return Mapping(stream,
        lambda x:
            (numpy.array(at_least_k(1, x[id_polyline], True)[-1], dtype=theano.config.floatX),),
        ('destination',))

def concat_destination_xy(stream):
    id_dx=stream.sources.index('destination_x')
    id_dy=stream.sources.index('destination_y')
    return Mapping(stream, lambda x: (numpy.array([x[id_dx], x[id_dy]], dtype=theano.config.floatX),), ('destination',))