Skip to content

Commit

Permalink
Handle empty arrays in gridpp::window
Browse files Browse the repository at this point in the history
  • Loading branch information
tnipen committed Jan 10, 2024
1 parent 421142e commit d8cfa5e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/api/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@ vec2 gridpp::window(const vec2& array,
int length, gridpp::Statistic statistic, bool before,
bool keep_missing, bool missing_edges) {

vec2 output = gridpp::init_vec2(array.size(), array[0].size(), gridpp::MV);
if(length <= 0) {
throw std::invalid_argument("Length variable must be > 0");
}

int nY = array.size();
if(nY == 0) {
return gridpp::init_vec2(0, 0);
}

int nX = array[0].size();
if(nX == 0) {
return gridpp::init_vec2(nY, 0);
}

vec2 output = gridpp::init_vec2(nY, nX, gridpp::MV);

if(length % 2 == 0 && !before) {
throw std::invalid_argument("Length variable must be an odd number");
Expand Down
34 changes: 31 additions & 3 deletions tests/window_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def test_sum_keep_missing(self):
np.testing.assert_array_equal(output, [[0,1,3,6,9],[1,3,6,9,12],[2,5,9,12,15],[3,7,12,np.nan,np.nan],[4,9,15,18,21]])

def test_edge_case(self):
# Check when window is beigger than array
output = gridpp.window(self.small_inputs, 5, gridpp.Sum, False, False, False)
np.testing.assert_array_equal(output, [[2,2],[2,2]])

Expand Down Expand Up @@ -103,8 +104,35 @@ def test_centered(self):
output = gridpp.window(input, 3, gridpp.Sum, False, True, True)
np.testing.assert_array_equal(output, [[np.nan, 3, np.nan, np.nan, np.nan, 12, np.nan]])

if __name__ == '__main__':
unittest.main()
def test_no_times(self):
input = np.zeros([10, 0])
output = gridpp.window(input, 3, gridpp.Sum)
np.testing.assert_array_equal(output, input)

def test_no_cases(self):
input = np.zeros([0, 10], np.float32)
output = gridpp.window(input, 3, gridpp.Sum)
np.testing.assert_array_equal(output, np.zeros([0,0]))

def test_no_anything(self):
input = np.zeros([0, 0], np.float32)
output = gridpp.window(input, 3, gridpp.Sum)
np.testing.assert_array_equal(output, np.zeros([0,0]))

def test_invalid_length(self):
input = np.zeros([10, 3], np.float32)
for length in [0, -1]:
with self.assertRaises(ValueError) as e:
output = gridpp.window(input, length, gridpp.Sum)

#if window is beigger than array () # Edge cases
def test_long_length(self):
# Check when window is beigger than array
output = gridpp.window([[0, 1, 2, 3]], 1001, gridpp.Sum, False, False, False)
np.testing.assert_array_equal(output, [[6, 6, 6, 6]])

def test_time_length_1(self):
output = gridpp.window([[1], [2]], 1001, gridpp.Sum, False, False, False)
np.testing.assert_array_equal(output, [[1], [2]])

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

0 comments on commit d8cfa5e

Please sign in to comment.