-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* compressed tiff files * removed files created by tests? * variable incorrectly named * missed one
- Loading branch information
Showing
9 changed files
with
112 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import skimage.io as io | ||
import pathlib | ||
import numpy as np | ||
from typing import Union | ||
|
||
|
||
def save_image(fname: Union[str, pathlib.Path], data: np.ndarray, | ||
compression_level: int = 6) -> None: | ||
""" | ||
A thin wrapper around `skimage.io.imsave()`. | ||
Args: | ||
fname (str): The location to save the tiff file. | ||
data (np.ndarray): The Numpy array to save. | ||
compression_level (int, optional): The compression level for skimage.io.imsave. Increasing | ||
`compress` increases memory consumption, decreases compression speed and moderately | ||
increases compression ratio. The range of compress is `[1,9]`. Defaults to 6. | ||
""" | ||
# Compression Config: | ||
plugin_args: dict[str, any] = { | ||
'compress': compression_level, | ||
} | ||
if isinstance(fname, pathlib.Path): | ||
fname: str = fname.as_posix() | ||
|
||
io.imsave(fname=fname, arr=data, plugin="tifffile", check_contrast=False, **plugin_args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import skimage.io as io | ||
import numpy as np | ||
from toffy import image_utils | ||
import os | ||
import pytest | ||
import pathlib | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def create_img_data() -> np.ndarray: | ||
""" | ||
A Fixture which creates a numpy array for tiff file compression testing. | ||
Returns: | ||
np.ndarray: Returns a randomly generated (1000 x 1000) numpy array. | ||
""" | ||
|
||
# Initialize a new generator - set seed for reproducibility | ||
rng = np.random.default_rng(12345) | ||
|
||
# Create testing data array | ||
data: np.ndarray = rng.integers(low=0, high=256, size=(1000, 1000), dtype=np.int16) | ||
|
||
yield data | ||
|
||
|
||
class TestSaveImage(): | ||
@pytest.fixture(autouse=True) | ||
def _setup(self, tmp_path, create_img_data): | ||
self.uncompressed_fname: pathlib.Path = (tmp_path / "test_img.tiff") | ||
self.compressed_fname: pathlib.Path = (tmp_path / "test_img_compressed.tiff") | ||
self.data: np.ndarray = create_img_data | ||
|
||
# save uncompressed image | ||
io.imsave(self.uncompressed_fname.as_posix(), arr=self.data, plugin="tifffile", | ||
check_contrast=False) | ||
|
||
@pytest.mark.parametrize("compress_level", [1, 6, 9, | ||
pytest.param(10, marks=pytest.mark.xfail)]) | ||
def test_save_compressed_img(self, compress_level): | ||
|
||
# Fails when compression_level > 9 | ||
image_utils.save_image(fname=self.compressed_fname, data=self.data, | ||
compression_level=compress_level) | ||
|
||
# Assert that the compressed tiff file is smaller than the uncompressed tiff file | ||
uncompressed_tiff_file_size: int = os.path.getsize(self.uncompressed_fname) | ||
compressed_tiff_file_size: int = os.path.getsize(self.compressed_fname) | ||
|
||
assert compressed_tiff_file_size < uncompressed_tiff_file_size | ||
|
||
# Assert that the values in the compressed tiff file and the uncompressed | ||
# tiff file are equal. | ||
|
||
uncompressed_data: np.ndarary = io.imread(self.uncompressed_fname) | ||
compressed_data: np.ndarray = io.imread(self.compressed_fname) | ||
|
||
np.testing.assert_array_equal(compressed_data, uncompressed_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters