commit b1da6fb4e644dcdec6d90c3f0ae018001a98a4b6
parent 0be3ebaa19f2cf8a630565434e785e5c24929a14
Author: Étienne Simon <esimon@esimon.eu>
Date: Fri, 24 Apr 2015 16:38:12 -0400
Add basic transformers
Diffstat:
1 file changed, 25 insertions(+), 0 deletions(-)
diff --git a/transformers.py b/transformers.py
@@ -0,0 +1,25 @@
+from fuel.transformers import Transformer, Filter, Mapping
+
+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_extremities(stream, k):
+ id_polyline=stream.sources.index('polyline')
+ def extremities(x):
+ return (x[id_polyline][:k], x[id_polyline][-k:])
+ stream = Filter(stream, lambda x: len(x[id_polyline])>=k)
+ stream = Mapping(stream, extremeties, ('first_k', 'last_k'))
+ return stream
+
+def add_destination(stream):
+ id_polyline=stream.sources.index('polyline')
+ return Mapping(stream, lambda x: x[id_polyline][-1], ('destination',))