|
2 | 2 |
|
3 | 3 | """Tests for `rawtools` package.""" |
4 | 4 |
|
| 5 | +import numpy as np |
5 | 6 | import pytest |
| 7 | +from numpy import uint8, uint16 |
| 8 | +from rawtools import rawtools |
6 | 9 |
|
| 10 | +DIMS = (4, 5) |
7 | 11 |
|
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)) |
9 | 23 |
|
10 | 24 |
|
11 | 25 | @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) |
14 | 53 |
|
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') |
19 | 54 |
|
| 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)) |
20 | 66 |
|
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