Commit 13bda83d authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Added function numeric.array.get_array_tilebounds + tests.

parent 29c088b7
Pipeline #1454 failed with stages
in 1 minute and 4 seconds
......@@ -2,6 +2,7 @@
__author__ = "Daniel Scheffler"
import numpy as np
from typing import List # noqa F401 # flake8 issue
def get_outFillZeroSaturated(dtype):
......@@ -17,3 +18,31 @@ def get_outFillZeroSaturated(dtype):
dict_outZero = {'int8': 0, 'uint8': 1, 'int16': 0, 'uint16': 1, 'float32': 0.}
dict_outSaturated = {'int8': 127, 'uint8': 256, 'int16': 32767, 'uint16': 65535, 'float32': 65535.}
return dict_outFill[dtype], dict_outZero[dtype], dict_outSaturated[dtype]
def get_array_tilebounds(array_shape, tile_shape):
# type: (tuple, tuple) -> List[List[tuple]]
"""Calculate row/col bounds for image tiles according to the given parameters.
:param array_shape: dimensions of array to be tiled: (rows, columns, bands) or (rows, columns)
:param tile_shape: dimensions of target tile: (rows, columns, bands) or (rows, columns)
"""
rows, cols = array_shape[:2]
tgt_rows, tgt_cols = tile_shape[:2]
row_bounds = [0]
while row_bounds[-1] + tgt_rows < rows:
row_bounds.append(row_bounds[-1] + tgt_rows - 1)
row_bounds.append(row_bounds[-2] + tgt_rows)
else:
row_bounds.append(rows - 1)
col_bounds = [0]
while col_bounds[-1] + tgt_cols < cols:
col_bounds.append(col_bounds[-1] + tgt_cols - 1)
col_bounds.append(col_bounds[-2] + tgt_cols)
else:
col_bounds.append(cols - 1)
return [[tuple([row_bounds[r], row_bounds[r + 1]]), tuple([col_bounds[c], col_bounds[c + 1]])]
for r in range(0, len(row_bounds), 2) for c in range(0, len(col_bounds), 2)]
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_array
----------
Tests for `py_tools_ds.numeric.array` module.
"""
import unittest
import numpy as np
from py_tools_ds.numeric.array import get_array_tilebounds
class Test_get_array_tilebounds(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Testjob Landsat-8
cls.arr = np.random.randint(1, 10, (100, 100, 5))
def test_block_tile(self):
tile_shape = (10, 10)
bounds = get_array_tilebounds(self.arr.shape, tile_shape)
(rS, rE), (cS, cE) = bounds[0]
self.assertEqual(self.arr[rS: rE + 1, cS: cE + 1].shape[:2], tile_shape)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment