Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tiff files when using images.fromtif default 'tif extension #357

Merged
merged 6 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion test/test_images_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,19 @@ def test_to_tif_roundtrip_multipage(tmpdir, eng):
assert allclose(data.toarray(), loaded.toarray())


def test_to_tiff_roundtrip_multipage(tmpdir, eng):
a = [arange(24, dtype='int16').reshape((2, 3, 4)), arange(24, dtype='int16').reshape((2, 3, 4))]
data = fromlist(a, engine=eng)
data.totif(os.path.join(str(tmpdir), 'images'), prefix='image')
# rename to tiff
for filename in glob.iglob(os.path.join(str(tmpdir), 'images', '*.tif')):
os.rename(filename, filename[:-4] + '.tiff')
files = [os.path.basename(f) for f in glob.glob(str(tmpdir) + '/images/image*')]
assert sorted(files) == ['image-00000.tiff', 'image-00001.tiff']
loaded = fromtif(os.path.join(str(tmpdir), 'images'))
assert allclose(data.toarray(), loaded.toarray())


def test_to_tif_roundtrip_8bit(tmpdir, eng):
a = [arange(8, dtype='uint8').reshape((4, 2))]
data = fromlist(a, engine=eng)
Expand All @@ -303,6 +316,6 @@ def test_to_tif_roundtrip_16bit(tmpdir, eng):
def test_from_example(eng):
return
data = fromexample('fish', engine=eng)
assert allclose(data.shape, (20, 76, 87, 2))
assert allclose(data.shape, (20, 2, 76, 87))
data = fromexample('mouse', engine=eng)
assert allclose(data.shape, (20, 64, 64))
21 changes: 21 additions & 0 deletions test/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,24 @@ def test_local_recursive_nested(tmpdir):
make(tmpdir, filenames)
actual = LocalFileReader().list(str(tmpdir), recursive=True)
assert parse(actual) == expected

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should probably be in test_images_io.py, the test_readers.py are for testing generic io functionality relevant to all data types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried moving them but I would need to either import the make and parse functions or copy paste them into test_images_io.py. I think they belong in the test_readers.py that is the main function that I changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, that's fine then.


def test_tif_tiff_flat(tmpdir):
filenames = ['b.tif', 'a.tif', 'c.tiff']
expected = ['a.tif', 'b.tif', 'c.tiff']
make(tmpdir, filenames)
actual = LocalParallelReader().list(str(tmpdir), ext='tif', recursive=False)
assert parse(actual) == expected
actual = LocalParallelReader().list(str(tmpdir), ext='tif', recursive=True)
assert parse(actual) == expected


def test_tif_tiff_recursive(tmpdir):
filenames = ['foo/b.tif', 'foo/bar/q.tiff', 'bar/a', 'c.tif', 'd.tiff']
expected = ['c.tif', 'd.tiff']
make(tmpdir, filenames)
actual = LocalParallelReader().list(str(tmpdir), ext='tif', recursive=False)
assert parse(actual) == expected
expected = ['c.tif', 'd.tiff', 'b.tif', 'q.tiff']
actual = LocalParallelReader().list(str(tmpdir), ext='tif', recursive=True)
assert parse(actual) == expected
21 changes: 17 additions & 4 deletions thunder/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def listrecursive(path, ext=None):
filenames = set()
for root, dirs, files in os.walk(path):
if ext:
files = fnmatch.filter(files, '*.' + ext)
if ext == 'tif' or ext == 'tiff':
tmp = fnmatch.filter(files, '*.' + 'tiff')
files = tmp + fnmatch.filter(files, '*.' + 'tif')
else:
files = fnmatch.filter(files, '*.' + ext)
for filename in files:
filenames.add(os.path.join(root, filename))
filenames = list(filenames)
Expand All @@ -89,13 +93,17 @@ def listflat(path, ext=None):
"""
if os.path.isdir(path):
if ext:
files = glob.glob(os.path.join(path, '*.' + ext))
if ext == 'tif' or ext == 'tiff':
files = glob.glob(os.path.join(path, '*.tif'))
files = files + glob.glob(os.path.join(path, '*.tiff'))
else:
files = glob.glob(os.path.join(path, '*.' + ext))
else:
files = [os.path.join(path, fname) for fname in os.listdir(path)]
else:
files = glob.glob(path)
# filter out directories
files = [fpath for fpath in files if not os.path.isdir(fpath)]
files = [fpath for fpath in files if not isinstance(fpath, list) and not os.path.isdir(fpath)]
return sorted(files)

def uri_to_path(uri):
Expand Down Expand Up @@ -341,7 +349,12 @@ def getfiles(self, path, ext=None, start=None, stop=None, recursive=False):
bucket, parse[2], prefix=parse[3], postfix=parse[4], recursive=recursive)
keylist = [key.name for key in keys]
if ext:
keylist = [keyname for keyname in keylist if keyname.endswith(ext)]
if ext == 'tif' or ext == 'tiff':
keylist = [keyname for keyname in keylist if keyname.endswith('tif')]
keylist.append([keyname for keyname in keylist if keyname.endswith('tiff')])
else:
keylist = [keyname for keyname in keylist if keyname.endswith(ext)]

keylist.sort()
keylist = select(keylist, start, stop)

Expand Down