Skip to content

Commit

Permalink
Replace scikit-image with tifffile (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Tung authored Aug 26, 2019
1 parent ec0c58c commit 4b6cf0b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ packaging
pathlib;python_version<"3.0"
numpy
requests
scikit-image
tifffile
19 changes: 13 additions & 6 deletions slicedimage/_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ def to_file_obj_or_str(obj):


def tiff_reader():
# lazy load skimage
import skimage.io
# lazy load tifffile
import tifffile

return lambda f: skimage.io.imread(to_file_obj_or_str(f))
def reader(f):
with tifffile.TiffFile(to_file_obj_or_str(f)) as tiff:
return tiff.asarray(maxworkers=1)

return reader

def numpy_reader():
# lazy load numpy
Expand All @@ -30,10 +33,14 @@ def tiff_writer():
Return a method that accepts (file, array) and saves it to the file. File may be a file-like
object, str, or pathlib.Path.
"""
# lazy load skimage
import skimage.io
# lazy load tifffile
import tifffile

return lambda f, arr: skimage.io.imsave(to_file_obj_or_str(f), arr, plugin="tifffile")
def writer(f, arr):
with tifffile.TiffWriter(to_file_obj_or_str(f)) as tiff:
return tiff.save(arr, datetime=None)

return writer


def numpy_writer():
Expand Down
5 changes: 3 additions & 2 deletions tests/io_/v0_0_0/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import tempfile
import unittest

import tifffile
import numpy as np
import skimage.io

import slicedimage
from slicedimage._dimensions import DimensionNames
Expand Down Expand Up @@ -62,7 +62,8 @@ def test_tiff(self):
with tempfile.TemporaryDirectory() as tempdir:
# write the tiff file
data = np.random.randint(0, 65535, size=(120, 80), dtype=np.uint16)
skimage.io.imsave(os.path.join(tempdir, "tile.tiff"), data, plugin="tifffile")
with tifffile.TiffWriter(os.path.join(tempdir, "tile.tiff")) as tiff:
tiff.save(data)

# TODO: (ttung) We should really be producing a tileset programmatically and writing it
# disk. However, our current write path only produces numpy output files.
Expand Down
5 changes: 3 additions & 2 deletions tests/io_/v0_0_0/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tempfile
import unittest

import tifffile
import numpy as np
import skimage.io

import slicedimage
from slicedimage import ImageFormat
Expand Down Expand Up @@ -133,7 +133,8 @@ def test_checksum_on_write(self):
with tempfile.TemporaryDirectory() as tempdir:
data = np.random.randint(0, 65535, size=(120, 80), dtype=np.uint16)
file_path = os.path.join(tempdir, "tile.tiff")
skimage.io.imsave(file_path, data, plugin="tifffile")
with tifffile.TiffWriter(file_path) as tiff:
tiff.save(data)
with open(file_path, "rb") as fh:
checksum = hashlib.sha256(fh.read()).hexdigest()

Expand Down
5 changes: 3 additions & 2 deletions tests/io_/v0_1_0/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import tempfile
import unittest

import tifffile
import numpy as np
import skimage.io

import slicedimage
from slicedimage._dimensions import DimensionNames
Expand Down Expand Up @@ -62,7 +62,8 @@ def test_tiff(self):
with tempfile.TemporaryDirectory() as tempdir:
# write the tiff file
data = np.random.randint(0, 65535, size=(120, 80), dtype=np.uint16)
skimage.io.imsave(os.path.join(tempdir, "tile.tiff"), data, plugin="tifffile")
with tifffile.TiffWriter(os.path.join(tempdir, "tile.tiff")) as tiff:
tiff.save(data)

# TODO: (ttung) We should really be producing a tileset programmatically and writing it
# disk. However, our current write path only produces numpy output files.
Expand Down
5 changes: 3 additions & 2 deletions tests/io_/v0_1_0/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tempfile
import unittest

import tifffile
import numpy as np
import skimage.io

import slicedimage
from slicedimage import ImageFormat
Expand Down Expand Up @@ -133,7 +133,8 @@ def test_checksum_on_write(self):
with tempfile.TemporaryDirectory() as tempdir:
data = np.random.randint(0, 65535, size=(120, 80), dtype=np.uint16)
file_path = os.path.join(tempdir, "tile.tiff")
skimage.io.imsave(file_path, data, plugin="tifffile")
with tifffile.TiffWriter(file_path) as tiff:
tiff.save(data)
with open(file_path, "rb") as fh:
checksum = hashlib.sha256(fh.read()).hexdigest()

Expand Down

0 comments on commit 4b6cf0b

Please sign in to comment.