Skip to content

Commit 4f4fba8

Browse files
committed
Add basic tests for scaling
1 parent 56e6999 commit 4f4fba8

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

setup.cfg

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,3 @@ exclude = docs
1919

2020
[aliases]
2121
test = pytest
22-
23-
[tool:pytest]
24-
collect_ignore = ['setup.py']

tests/test_rawtools.py

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,66 @@
22

33
"""Tests for `rawtools` package."""
44

5+
import numpy as np
56
import pytest
7+
from numpy import uint8, uint16
8+
from rawtools import rawtools
69

10+
DIMS = (4, 5)
711

8-
from rawtools import rawtools
12+
13+
@pytest.fixture
14+
def slice_uint8():
15+
"""Sample uint8 slice"""
16+
return np.rint(np.arange(0, 20, dtype=uint8).reshape(DIMS))
17+
18+
19+
@pytest.fixture
20+
def slice_uint16():
21+
"""Sample uint16 slice"""
22+
return np.rint(np.arange(0, 20, dtype=uint16).reshape(DIMS))
923

1024

1125
@pytest.fixture
12-
def response():
13-
"""Sample pytest fixture.
26+
def slice_uint16_high_variance():
27+
"""Sample uint16 slice with variable values"""
28+
return np.array([-1, 0, 100, 1000, 5000, 14830, 50321, 65535, 65536], dtype=uint16)
29+
30+
31+
def test_scale_uint8(slice_uint8):
32+
"""Test scaling a unsigned 8-bit integer array to own bounds."""
33+
from rawtools.convert import scale
34+
xs = np.arange(0, 20, dtype=uint8).reshape(DIMS)
35+
lbound = np.iinfo(uint8).min
36+
ubound = np.iinfo(uint8).max
37+
scaled_slice = scale(xs, lbound, ubound, lbound, ubound)
38+
np.testing.assert_array_equal(scaled_slice, slice_uint8)
39+
40+
41+
def test_scale_uint16_to_uint8(slice_uint16):
42+
"""Test scaling an unsigned 16-bit integer array to an unsigned 8-bit array's bounds."""
43+
from rawtools.convert import scale
44+
lbound = np.iinfo(uint16).min
45+
ubound = np.iinfo(uint16).max
46+
new_lbound = np.iinfo(uint8).min
47+
new_ubound = np.iinfo(uint8).max
48+
slice_uint8 = np.zeros(DIMS, dtype=uint8)
49+
scaled_slice = np.rint(
50+
scale(slice_uint16, lbound, ubound, new_lbound, new_ubound))
51+
52+
np.testing.assert_array_equal(scaled_slice, slice_uint8)
1453

15-
See more at: http://doc.pytest.org/en/latest/fixture.html
16-
"""
17-
# import requests
18-
# return requests.get('https://github.com/audreyr/cookiecutter-pypackage')
1954

55+
def test_scale_uint16_to_uint8_large_variance(slice_uint16_high_variance):
56+
"""Test scaling an unsigned 16-bit integer array with high variance to an unsigned 8-bit array's bounds."""
57+
from rawtools.convert import scale
58+
lbound = np.iinfo(uint16).min
59+
ubound = np.iinfo(uint16).max
60+
new_lbound = np.iinfo(uint8).min
61+
new_ubound = np.iinfo(uint8).max
62+
# Mapped values should wrap as they exceed target bit depth
63+
slice_uint8 = np.array([255, 0, 0, 4, 19, 58, 196, 255, 0], dtype=uint8)
64+
scaled_slice = np.rint(
65+
scale(slice_uint16_high_variance, lbound, ubound, new_lbound, new_ubound))
2066

21-
def test_content(response):
22-
"""Sample pytest test function with the pytest fixture as an argument."""
23-
# from bs4 import BeautifulSoup
24-
# assert 'GitHub' in BeautifulSoup(response.content).title.string
67+
np.testing.assert_array_equal(scaled_slice, slice_uint8)

0 commit comments

Comments
 (0)