Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/bw_processing/array_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ def create_array(iterable, nrows=None, dtype=np.float32):
array[i, :] = tuple(row)

else:
ncols, data = get_ncols(iterable)
try:
ncols, data = get_ncols(iterable)
except StopIteration:
return np.zeros((0, 0), dtype=dtype)
array = create_chunked_array(data, ncols, dtype)

return array
24 changes: 23 additions & 1 deletion tests/test_array_creation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from bw_processing.array_creation import chunked
import numpy as np

from bw_processing.array_creation import chunked, create_array


def test_chunked():
Expand All @@ -12,3 +14,23 @@ def test_chunked():
for x in next(c):
pass
assert x == 599


def test_create_array_empty_generator_returns_empty_array():
# Empty generator with no nrows previously raised StopIteration (issue #96)
result = create_array(x for x in [])
assert result.shape == (0, 0)
assert result.dtype == np.float32


def test_create_array_empty_generator_respects_dtype():
result = create_array((x for x in []), dtype=np.float64)
assert result.dtype == np.float64


def test_create_array_nonempty_generator():
data = ([float(i), float(i * 2)] for i in range(5))
result = create_array(data)
assert result.shape == (5, 2)
assert np.allclose(result[:, 0], [0, 1, 2, 3, 4])
assert np.allclose(result[:, 1], [0, 2, 4, 6, 8])
Loading