diff options
author | Alex Auvolat <alex.auvolat@ens.fr> | 2015-04-29 18:23:45 -0400 |
---|---|---|
committer | Alex Auvolat <alex.auvolat@ens.fr> | 2015-04-29 18:23:45 -0400 |
commit | fbcaf8643a68ef824c72b003c85c4a96e5362bb6 (patch) | |
tree | 263abef6ed7fae7ef3f9d552dc1719265ed3bda2 /transformers.py | |
parent | b07bf7612b07a08bd1298b87347889a71d390012 (diff) | |
download | taxi-fbcaf8643a68ef824c72b003c85c4a96e5362bb6.tar.gz taxi-fbcaf8643a68ef824c72b003c85c4a96e5362bb6.zip |
Fix dumping and loading
Diffstat (limited to 'transformers.py')
-rw-r--r-- | transformers.py | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/transformers.py b/transformers.py index c5b8d87..5ad9a87 100644 --- a/transformers.py +++ b/transformers.py @@ -26,41 +26,64 @@ class Select(Transformer): raise ValueError data=next(self.child_epoch_iterator) return [data[id] for id in self.ids] + +class first_k(object): + def __init__(self, k, id_latitude, id_longitude): + self.k = k + self.id_latitude = id_latitude + self.id_longitude = id_longitude + def __call__(self, data): + return (numpy.array(at_least_k(self.k, data[self.id_latitude], False, False)[:self.k], + dtype=theano.config.floatX), + numpy.array(at_least_k(self.k, data[self.id_longitude], False, True)[:self.k], + dtype=theano.config.floatX)) def add_first_k(k, stream): id_latitude = stream.sources.index('latitude') id_longitude = stream.sources.index('longitude') - return Mapping(stream, - lambda data: - (numpy.array(at_least_k(k, data[id_latitude], False, False)[:k], dtype=theano.config.floatX), - numpy.array(at_least_k(k, data[id_longitude], False, True)[:k], dtype=theano.config.floatX)), - ('first_k_latitude', 'first_k_longitude')) + return Mapping(stream, first_k(k, id_latitude, id_longitude), ('first_k_latitude', 'first_k_longitude')) +class random_k(object): + def __init__(self, k, id_latitude, id_longitude): + self.k = k + self.id_latitude = id_latitude + self.id_longitude = id_longitude + def __call__(self, x): + lat = at_least_k(self.k, x[self.id_latitude], True, False) + lon = at_least_k(self.k, x[self.id_longitude], True, True) + loc = random.randrange(len(lat)-self.k+1) + return (numpy.array(lat[loc:loc+self.k], dtype=theano.config.floatX), + numpy.array(lon[loc:loc+self.k], dtype=theano.config.floatX)) def add_random_k(k, stream): id_latitude = stream.sources.index('latitude') id_longitude = stream.sources.index('longitude') - def random_k(x): - lat = at_least_k(k, x[id_latitude], True, False) - lon = at_least_k(k, x[id_longitude], True, True) - loc = random.randrange(len(lat)-k+1) - return (numpy.array(lat[loc:loc+k], dtype=theano.config.floatX), - numpy.array(lon[loc:loc+k], dtype=theano.config.floatX)) - return Mapping(stream, random_k, ('last_k_latitude', 'last_k_longitude')) + return Mapping(stream, random_k(k, id_latitude, id_longitude), ('last_k_latitude', 'last_k_longitude')) +class last_k(object): + def __init__(self, k, id_latitude, id_longitude): + self.k = k + self.id_latitude = id_latitude + self.id_longitude = id_longitude + def __call__(self, data): + return (numpy.array(at_least_k(self.k, data[self.id_latitude], True, False)[-self.k:], + dtype=theano.config.floatX), + numpy.array(at_least_k(self.k, data[self.id_longitude], True, True)[-self.k:], + dtype=theano.config.floatX)) def add_last_k(k, stream): id_latitude = stream.sources.index('latitude') id_longitude = stream.sources.index('longitude') - return Mapping(stream, - lambda data: - (numpy.array(at_least_k(k, data[id_latitude], True, False)[-k:], dtype=theano.config.floatX), - numpy.array(at_least_k(k, data[id_longitude], True, True)[-k:], dtype=theano.config.floatX)), - ('last_k_latitude', 'last_k_longitude')) + return Mapping(stream, last_k(k, id_latitude, id_longitude), ('last_k_latitude', 'last_k_longitude')) +class destination(object): + def __init__(self, id_latitude, id_longitude): + self.id_latitude = id_latitude + self.id_longitude = id_longitude + def __call__(self, data): + return (numpy.array(at_least_k(1, data[self.id_latitude], True, False)[-1], + dtype=theano.config.floatX), + numpy.array(at_least_k(1, data[self.id_longitude], True, True)[-1], + dtype=theano.config.floatX)) def add_destination(stream): id_latitude = stream.sources.index('latitude') id_longitude = stream.sources.index('longitude') - return Mapping(stream, - lambda data: - (numpy.array(at_least_k(1, data[id_latitude], True, False)[-1], dtype=theano.config.floatX), - numpy.array(at_least_k(1, data[id_longitude], True, True)[-1], dtype=theano.config.floatX)), - ('destination_latitude', 'destination_longitude')) + return Mapping(stream, destination(id_latitude, id_longitude), ('destination_latitude', 'destination_longitude')) |