From 03d037d0443a65af4ec73bfe5e217fa95af7477c Mon Sep 17 00:00:00 2001 From: Jonathan Helmus Date: Wed, 25 Jul 2012 11:11:25 -0400 Subject: [PATCH] ImageCollection now slices like other iterables --- skimage/io/collection.py | 47 +++++++++++++---------------- skimage/io/tests/test_collection.py | 10 ++++-- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/skimage/io/collection.py b/skimage/io/collection.py index a78dbd14..9908c30d 100644 --- a/skimage/io/collection.py +++ b/skimage/io/collection.py @@ -258,14 +258,15 @@ class ImageCollection(object): Parameters ---------- n : int or slice - Slice selecting images for the new ImageCollection or the image - number to be returned. + The image number to be returned, or a slice selecting the images + and ordering to be returned in a new ImageCollection. Returns ------- - img : ImageCollection or ndarray - Imagecollection of the selected images or an ndarray if a single - image is specified. + img : ndarray or ImageCollection. + The `n`-th image in the collection, or a new ImageCollection with + the selected images. + """ if hasattr(n, '__index__'): n = n.__index__() @@ -283,29 +284,23 @@ class ImageCollection(object): self._cached = n return self.data[idx] - - else: # slice object was provided + else: + # A slice object was provided, so create a new ImageCollection + # object. Any loaded image data in the original ImageCollection + # will be copied by reference to the new object. Image data + # loaded after this creation is not linked. fidx = range(len(self.files))[n] - if len(fidx) == 1: # only one item requested - return self.__getitem__(fidx[0]) - else: - # create a new ImageCollection object, any loaded image data - # in the original ImageCollection will be copied by reference - # to the new object. Image data loaded after this creation - # are not linked. - fidx.sort() - new_ic = copy(self) - new_ic._files = [self.files[i] for i in fidx] - if self.conserve_memory: - if self._cached in fidx: - new_ic._cached = fidx[self._cached] - new_ic.data = np.copy(self.data) - else: - new_ic.data = np.empty(1, dtype=object) + new_ic = copy(self) + new_ic._files = [self.files[i] for i in fidx] + if self.conserve_memory: + if self._cached in fidx: + new_ic._cached = fidx.index(self._cached) + new_ic.data = np.copy(self.data) else: - new_ic.data = self.data[fidx] - - return new_ic + new_ic.data = np.empty(1, dtype=object) + else: + new_ic.data = self.data[fidx] + return new_ic def _check_imgnum(self, n): """Check that the given image number is valid.""" diff --git a/skimage/io/tests/test_collection.py b/skimage/io/tests/test_collection.py index 36e78e13..9db28ff7 100644 --- a/skimage/io/tests/test_collection.py +++ b/skimage/io/tests/test_collection.py @@ -44,10 +44,14 @@ class TestImageCollection(): assert_raises(IndexError, return_img, -num - 1) def test_slicing(self): - assert type(self.collection[:] is ImageCollection) + assert type(self.collection[:]) is ImageCollection assert len(self.collection[:]) == 2 - assert_array_almost_equal(self.collection[0], self.collection[:1]) - assert_array_almost_equal(self.collection[1], self.collection[1:]) + assert len(self.collection[:1]) == 1 + assert len(self.collection[1:]) == 1 + assert_array_almost_equal(self.collection[0], self.collection[:1][0]) + assert_array_almost_equal(self.collection[1], self.collection[1:][0]) + assert_array_almost_equal(self.collection[1], self.collection[::-1][0]) + assert_array_almost_equal(self.collection[0], self.collection[::-1][1]) def test_files_property(self): assert isinstance(self.collection.files, list)