Skip to content

Instantly share code, notes, and snippets.

@dwf
Created September 28, 2016 17:04
Show Gist options
  • Save dwf/e3a811a9463c802c5b5e4248806de1d1 to your computer and use it in GitHub Desktop.
Save dwf/e3a811a9463c802c5b5e4248806de1d1 to your computer and use it in GitHub Desktop.
Fuel DataStream that segments the epochs of another DataStream.
"""Fuel DataStream that segments the epochs of another DataStream."""
__license__ = """
Copyright (c) 2016 Universite de Montreal
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from functools import partial
from itertools import chain, islice
from operator import itemgetter, methodcaller
from fuel.streams import AbstractDataStream
from toolz import compose
class SegmentedEpochDataStream(AbstractDataStream):
"""Segment the epochs from one stream into many.
Parameters
----------
stream : AbstractDataStream
The stream to get "super" epoch iterators from.
epoch_length : int
Maximum number of iterations of the "super" epoch that
constitute one epoch of this stream. Last one may be
shorter.
Notes
-----
Not super well tested, may be corner cases.
This uses Python 3 super() syntax. Also, if you ever plan on serializing
this, it various iterator objects used in this class are picklable on
Python 3 but not before. If you need legacy Python support, see
https://github.com/mila-udem/picklable-itertools
"""
def __init__(self, stream, epoch_length):
self.stream = stream
self.epoch_length = epoch_length
self.base_iter = iter([])
super().__init__()
def get_epoch_iterator(self, as_dict=False):
try:
next_batch = next(self.base_iter)
self.base_iter = chain([next_batch], self.base_iter)
except StopIteration:
self.base_iter = self.stream.get_epoch_iterator(as_dict=True)
this_epoch = islice(self.base_iter, 0, self.epoch_length)
# Support non-dict epoch iterators by constructing a tuple from the
# dict,
if not as_dict:
make_tuple_batch = compose(tuple,
itemgetter(1),
partial(sorted, key=itemgetter(0)),
methodcaller('items'))
return map(make_tuple_batch, this_epoch)
else:
return
# Mandatory but at least somewhat useful methods.
def close(self):
self.stream.close()
def reset(self):
self.stream.reset()
self.base_iter = iter([])
# These are broadly useful properties to have access to.
@property
def produces_examples(self):
return self.stream.produces_examples
@property
def sources(self):
return self.stream.sources
# We stub these out because AbstractDataStream wants them implemented,
# but they aren't really useful to us.
def get_data(self):
raise NotImplementedError
def next_epoch(self):
raise NotImplementedError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment