-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from sagar87/feature/8bit_conversion
Feature/8bit conversion
- Loading branch information
Showing
7 changed files
with
224 additions
and
31 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,50 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
def test_convert_to_8bit(dataset): | ||
# this is already in 8 bit, so should be fine | ||
converted = dataset.pp.convert_to_8bit(key_added="_converted_image") | ||
assert converted["_converted_image"].values.dtype == np.uint8 | ||
|
||
|
||
def test_convert_16_to_8bit(dataset): | ||
img_size = (dataset.sizes["x"], dataset.sizes["y"]) | ||
ds = dataset.pp.add_layer(np.random.randint(0, 2**16, img_size).astype(np.uint16), "_16bit_image") | ||
ds = ds.pp.convert_to_8bit("_16bit_image", key_added="_converted_image") | ||
assert ds["_converted_image"].values.dtype == np.uint8 | ||
|
||
|
||
def test_convert_32_to_8bit(dataset): | ||
img_size = (dataset.sizes["x"], dataset.sizes["y"]) | ||
ds = dataset.pp.add_layer(np.random.randint(0, 2**32, img_size).astype(np.uint32), "_32bit_image") | ||
ds = ds.pp.convert_to_8bit("_32bit_image", key_added="_converted_image") | ||
assert ds["_converted_image"].values.dtype == np.uint8 | ||
|
||
|
||
def test_convert_float_to_8bit(dataset): | ||
img_size = (dataset.sizes["x"], dataset.sizes["y"]) | ||
ds = dataset.pp.add_layer(np.random.rand(*img_size).astype(np.float64), "_float_image") | ||
ds = ds.pp.convert_to_8bit("_float_image", key_added="_converted_image") | ||
assert ds["_converted_image"].values.dtype == np.uint8 | ||
|
||
|
||
def test_convert_float_with_values_larger_than_one(dataset): | ||
img_size = (dataset.sizes["x"], dataset.sizes["y"]) | ||
ds = dataset.pp.add_layer(np.random.rand(*img_size) * 2, "_float_image") | ||
with pytest.raises(ValueError, match="The image is of type float, but the values are not in the range"): | ||
ds.pp.convert_to_8bit("_float_image", key_added="_converted_image") | ||
|
||
|
||
def test_convert_float_with_values_smaller_than_zero(dataset): | ||
img_size = (dataset.sizes["x"], dataset.sizes["y"]) | ||
ds = dataset.pp.add_layer(np.random.rand(*img_size) - 1, "_float_image") | ||
with pytest.raises( | ||
AssertionError, match="The image contains negative values. Please make sure that the image is non-negative." | ||
): | ||
ds.pp.convert_to_8bit("_float_image", key_added="_converted_image") | ||
|
||
|
||
def test_convert_to_8bit_key_does_not_exist(dataset): | ||
with pytest.raises(AssertionError, match="The key non_existing_key does not exist in the object."): | ||
dataset.pp.convert_to_8bit("non_existing_key", key_added="_converted_image") |