Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed May 1, 2024
1 parent 1089192 commit abf7a20
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
3 changes: 1 addition & 2 deletions src/mo_pack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#
# This file is part of mo_pack and is released under the BSD license.
# See LICENSE in the root of the repository for full licensing details.
from ._packing import (compress_rle, compress_wgdos,
decompress_rle, decompress_wgdos)
from ._packing import compress_rle, compress_wgdos, decompress_rle, decompress_wgdos

try:
from ._version import version as __version__
Expand Down
7 changes: 3 additions & 4 deletions src/mo_pack/tests/test_compress_rle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ class Test(unittest.TestCase):
def test_no_mdi(self):
data = np.arange(42, dtype=np.float32).reshape(7, 6)
compressed_data = compress_rle(data)
expected = np.arange(42, dtype='f4')
expected = np.arange(42, dtype="f4")
expected.byteswap(True)
self.assertEqual(compressed_data, expected.data)

def test_mdi(self):
data = np.arange(12, dtype=np.float32).reshape(3, 4) + 5
data[1, 1:] = 999
compressed_data = compress_rle(data, missing_data_indicator=999)
expected = np.array([5, 6, 7, 8, 9, 999, 3, 13, 14, 15, 16],
dtype='f4')
expected = np.array([5, 6, 7, 8, 9, 999, 3, 13, 14, 15, 16], dtype="f4")
expected.byteswap(True)
self.assertEqual(compressed_data, expected.data)

Expand All @@ -37,5 +36,5 @@ def test_mdi_larger(self):
compress_rle(data, missing_data_indicator=666)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
16 changes: 7 additions & 9 deletions src/mo_pack/tests/test_decompress_rle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,30 @@
class Test(unittest.TestCase):
def test_no_mdi(self):
# The input to decompress_rle must be big-endian 32-bit floats.
src_buffer = np.arange(12, dtype='>f4').data
src_buffer = np.arange(12, dtype=">f4").data
result = decompress_rle(src_buffer, 3, 4)
assert_array_equal(result, np.arange(12).reshape(3, 4))
self.assertEqual(result.dtype, '=f4')
self.assertEqual(result.dtype, "=f4")

def test_mdi(self):
# The input to decompress_rle must be big-endian 32-bit floats.
src_floats = np.arange(11, dtype='>f4')
src_floats = np.arange(11, dtype=">f4")
src_floats[5] = 666
src_floats[6] = 3
src_buffer = src_floats.data
result = decompress_rle(src_buffer, 3, 4, missing_data_indicator=666)
assert_array_equal(result, [[0, 1, 2, 3],
[4, 666, 666, 666],
[7, 8, 9, 10]])
assert_array_equal(result, [[0, 1, 2, 3], [4, 666, 666, 666], [7, 8, 9, 10]])

def test_not_enough_source_data(self):
src_buffer = np.arange(4, dtype='>f4').data
src_buffer = np.arange(4, dtype=">f4").data
with self.assertRaises(ValueError):
decompress_rle(src_buffer, 5, 6)

def test_too_much_source_data(self):
src_buffer = np.arange(10, dtype='>f4').data
src_buffer = np.arange(10, dtype=">f4").data
with self.assertRaises(ValueError):
decompress_rle(src_buffer, 2, 3)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
5 changes: 2 additions & 3 deletions src/mo_pack/tests/test_rle.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
class Test(unittest.TestCase):
def _test(self, original, rows, cols):
compressed_data = compress_rle(original, missing_data_indicator=MDI)
result = decompress_rle(compressed_data, rows, cols,
missing_data_indicator=MDI)
result = decompress_rle(compressed_data, rows, cols, missing_data_indicator=MDI)
assert_array_equal(result, original)

def test_no_mdi(self):
Expand All @@ -36,5 +35,5 @@ def test_mdi(self):
self._test(data, 3, 4)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
41 changes: 22 additions & 19 deletions src/mo_pack/tests/test_wgdos.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@


class TestPackWGDOS(unittest.TestCase):
def assert_equal_when_decompressed(self, compressed_data, expected_array,
mdi=0):
def assert_equal_when_decompressed(self, compressed_data, expected_array, mdi=0):
x, y = expected_array.shape
decompressed_data = mo_pack.decompress_wgdos(
compressed_data, x, y, mdi)
decompressed_data = mo_pack.decompress_wgdos(compressed_data, x, y, mdi)
np.testing.assert_array_equal(decompressed_data, expected_array)

def test_pack_wgdos(self):
Expand All @@ -32,20 +30,24 @@ def test_pack_wgdos(self):

def test_mdi(self):
data = np.arange(12, dtype=np.float32).reshape(3, 4)
compressed_data = mo_pack.compress_wgdos(data,
missing_data_indicator=4.0)
compressed_data = mo_pack.compress_wgdos(data, missing_data_indicator=4.0)
expected_data = data
data[1, 0] = 4.0
self.assert_equal_when_decompressed(compressed_data, data, mdi=4.0)

def test_accuracy(self):
data = np.array([[0.1234, 0.2345, 0.3456], [0.4567, 0.5678, 0.6789]],
dtype=np.float32)
data = np.array(
[[0.1234, 0.2345, 0.3456], [0.4567, 0.5678, 0.6789]], dtype=np.float32
)
compressed = mo_pack.compress_wgdos(data, accuracy=-4)
decompressed_data = mo_pack.decompress_wgdos(compressed, 2, 3)
expected = np.array([[0.12340003, 0.18590003, 0.34560001],
[0.40810001, 0.56779999, 0.63029999]],
dtype=np.float32)
expected = np.array(
[
[0.12340003, 0.18590003, 0.34560001],
[0.40810001, 0.56779999, 0.63029999],
],
dtype=np.float32,
)
np.testing.assert_array_equal(decompressed_data, expected)


Expand All @@ -64,18 +66,19 @@ def test_different_shape(self):

def test_real_data(self):
test_dir = os.path.dirname(os.path.abspath(__file__))
fname = os.path.join(test_dir, 'test_data',
'nae.20100104-06_0001_0001.pp')
with open(fname, 'rb') as fh:
fname = os.path.join(test_dir, "test_data", "nae.20100104-06_0001_0001.pp")
with open(fname, "rb") as fh:
fh.seek(268)
data = mo_pack.decompress_wgdos(fh.read(339464), 360, 600)
assert_almost_equal(data.mean(), 130.84694, decimal=1)
expected = [[388.78125, 389.46875, 384.0625, 388.46875],
[388.09375, 381.375, 374.28125, 374.875],
[382.34375, 373.671875, 371.171875, 368.25],
[385.265625, 373.921875, 368.5, 365.3125]]
expected = [
[388.78125, 389.46875, 384.0625, 388.46875],
[388.09375, 381.375, 374.28125, 374.875],
[382.34375, 373.671875, 371.171875, 368.25],
[385.265625, 373.921875, 368.5, 365.3125],
]
assert_array_equal(data[:4, :4], expected)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

0 comments on commit abf7a20

Please sign in to comment.