changeset 313:009ce84e9f52

behaviour is now the same as a list in pylearn, so if len(ds) = 10, ds[10] raise an IndexError, same thing for ds[[1,10]], and ds[0:14:1] returns 10 elements
author Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
date Wed, 11 Jun 2008 13:53:39 -0400
parents 96cca78de3ed
children 105b54ac8260
files dataset.py
diffstat 1 files changed, 5 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dataset.py	Wed Jun 11 13:22:00 2008 -0400
+++ b/dataset.py	Wed Jun 11 13:53:39 2008 -0400
@@ -455,6 +455,8 @@
 
         if type(i) is int:
             assert i >= 0 # TBM: see if someone complains and want negative i
+            if i >= len(self) :
+                raise IndexError
             i_batch = self.minibatches_nowrap(self.fieldNames(),
                     minibatch_size=1, n_batches=1, offset=i)
             return DataSet.MinibatchToSingleExampleIterator(i_batch).next()
@@ -463,6 +465,7 @@
         if type(i) is slice and (i.step in (None, 1)):
             offset = 0 if i.start is None else i.start
             upper_bound = len(self) if i.stop is None else i.stop
+            upper_bound = min(len(self) , upper_bound)
             #return MinibatchDataSet(self.minibatches_nowrap(self.fieldNames(),
             #        minibatch_size=upper_bound - offset,
             #        n_batches=1,
@@ -486,6 +489,8 @@
                 #dis-allow nested slices
                 if not isinstance(idx, int):
                     raise TypeError(idx)
+                if idx >= len(self) :
+                    raise IndexError
             # call back into self.__getitem__
             examples = [self.minibatches_nowrap(self.fieldNames(),
                     minibatch_size=1, n_batches=1, offset=ii).next()