def test_rolling_window_exception_too_many_dims(array_size_5): """Test an exception is raised if shape has too many dimensions.""" msg = ("Number of dimensions of the input array must be greater than or " "equal to the length of the neighbourhood shape used for " "constructing rolling window neighbourhoods.") with pytest.raises(ValueError) as exc_info: rolling_window(array_size_5, (2, 2, 2)) assert msg in str(exc_info.value)
def test_rolling_window_writable(array_size_5): """Test that result is writable if and only if `writable` is True.""" windows = rolling_window(array_size_5, (2, 2)) msg = "assignment destination is read-only" with pytest.raises(ValueError) as exc_info: windows[0, 0, 0, 0] = -1 assert msg in str(exc_info.value) windows = rolling_window(array_size_5, (2, 2), writeable=True) windows[0, 0, 0, 0] = -1 assert windows[0, 0, 0, 0] == -1
def test_rolling_window_exception_dims_too_large(array_size_5): """Test an exception is raised if dimensions of shape are larger than corresponding dimensions of input array.""" msg = ("The calculated shape of the output array view contains a " "dimension that is negative or zero. Each dimension of the " "neighbourhood shape must be less than or equal to the " "corresponding dimension of the input array.") with pytest.raises(RuntimeError) as exc_info: rolling_window(array_size_5, (2, 6)) assert msg in str(exc_info.value)
def test_rolling_window_neighbourhood_size_2(array_size_5): """Test producing a 2 * 2 neighbourhood.""" windows = rolling_window(array_size_5, (2, 2)) expected = np.zeros((4, 4, 2, 2), dtype=np.int32) for i in range(4): for j in range(4): expected[i, j] = array_size_5[i:i + 2, j:j + 2] np.testing.assert_array_equal(windows, expected)
def test_padding_neighbourhood_size_2(array_size_5): """Test that result is same as result of rolling_window with a border of zeros.""" padded = pad_and_roll(array_size_5, (2, 2), mode="constant") window = rolling_window(array_size_5, (2, 2)) inner_part = padded[1:-1, 1:-1, ::] np.testing.assert_array_equal(inner_part, window) border_index = ([[0, i, 0, j] for i in range(5) for j in [0, 1]] + [[5, i, 1, j] for i in range(5) for j in [0, 1]] + [[i, 0, j, 0] for i in range(5) for j in [0, 1]] + [[i, 5, j, 1] for i in range(5) for j in [0, 1]]) outer_part = padded[list(zip(*border_index))] np.testing.assert_array_equal(outer_part, np.zeros(40, dtype=np.int32))