Skip to content

Commit 78fe55d

Browse files
committed
Mock input arrays to increase speed and avoid pipeline OOM
1 parent f63188d commit 78fe55d

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

tests/unit/test_live_mask_chunksize.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
from mdio.core import Grid
1010

1111

12+
class MockArray:
13+
"""Mock array class that mimics numpy array attributes without allocating memory."""
14+
15+
def __init__(self, shape, dtype):
16+
self.shape = shape
17+
self.dtype = np.dtype(dtype)
18+
self.itemsize = self.dtype.itemsize
19+
20+
1221
def test_small_grid_no_chunking():
1322
"""Test that small grids return -1 (no chunking needed)."""
1423
# Create a small grid that fits within INT32_MAX
@@ -18,7 +27,7 @@ def test_small_grid_no_chunking():
1827
Dimension(coords=range(0, 100, 1), name="sample"),
1928
]
2029
grid = Grid(dims=dims)
21-
grid.live_mask = np.ones((100, 100), dtype=bool)
30+
grid.live_mask = MockArray((100, 100), bool)
2231

2332
result = _calculate_live_mask_chunksize(grid)
2433
assert result == (100, 100)
@@ -34,7 +43,7 @@ def test_large_2d_grid_chunking():
3443
Dimension(coords=range(0, 100, 1), name="sample"),
3544
]
3645
grid = Grid(dims=dims)
37-
grid.live_mask = np.ones((50000, 50000), dtype=bool)
46+
grid.live_mask = MockArray((50000, 50000), bool)
3847

3948
result = _calculate_live_mask_chunksize(grid)
4049

@@ -53,7 +62,7 @@ def test_large_3d_grid_chunking():
5362
Dimension(coords=range(0, 100, 1), name="sample"),
5463
]
5564
grid = Grid(dims=dims)
56-
grid.live_mask = np.ones((1500, 1500, 1500), dtype=bool)
65+
grid.live_mask = MockArray((1500, 1500, 1500), bool)
5766

5867
result = _calculate_live_mask_chunksize(grid)
5968

@@ -78,7 +87,7 @@ def test_uneven_dimensions_chunking():
7887
Dimension(coords=range(0, 100, 1), name="sample"),
7988
]
8089
grid = Grid(dims=dims)
81-
grid.live_mask = np.ones((50000, 50000), dtype=bool)
90+
grid.live_mask = MockArray((50000, 50000), bool)
8291

8392
result = _calculate_live_mask_chunksize(grid)
8493
assert result == (25000, 25000)
@@ -101,15 +110,15 @@ def test_prestack_land_survey_chunking():
101110
Dimension(coords=range(0, 1000, 1), name="sample"),
102111
]
103112
grid = Grid(dims=dims)
104-
grid.live_mask = np.ones((1000, 1000, 100, 36), dtype=bool)
113+
grid.live_mask = MockArray((1000, 1000, 100, 36), bool)
105114

106115
result = _calculate_live_mask_chunksize(grid)
107116
assert result == (334, 334, 100, 36)
108117

109118

110119
def test_one_dim_full_chunk():
111120
"""Test one-dimensional volume where the whole dimension can be used as chunk."""
112-
arr = np.zeros((100,), dtype=np.int8)
121+
arr = MockArray((100,), np.int8)
113122
# With n_bytes = 100, max_elements_allowed = 100, thus optimal chunk should be (100,)
114123
result = _calculate_optimal_chunksize(arr, 100)
115124
assert result == (100,)
@@ -120,7 +129,7 @@ def test_two_dim_optimal():
120129
121130
For a shape of (8,6) with n_bytes=20, the optimal chunk is expected to be (8,2).
122131
"""
123-
arr = np.zeros((8, 6), dtype=np.int8)
132+
arr = MockArray((8, 6), np.int8)
124133
result = _calculate_optimal_chunksize(arr, 20)
125134
assert result == (4, 4)
126135

@@ -130,7 +139,7 @@ def test_three_dim_optimal():
130139
131140
For a shape of (9,6,4) with n_bytes=100, the expected chunk is (9,2,4).
132141
"""
133-
arr = np.zeros((9, 6, 4), dtype=np.int8)
142+
arr = MockArray((9, 6, 4), np.int8)
134143
result = _calculate_optimal_chunksize(arr, 100)
135144
assert result == (5, 5, 4)
136145

@@ -140,21 +149,21 @@ def test_minimal_chunk_for_large_dtype():
140149
141150
Using int32 (itemsize=4) with shape (4,5) and n_bytes=4 yields (1,1).
142151
"""
143-
arr = np.zeros((4, 5), dtype=np.int32)
152+
arr = MockArray((4, 5), np.int32)
144153
result = _calculate_optimal_chunksize(arr, 4)
145154
assert result == (1, 1)
146155

147156

148157
def test_large_nbytes():
149158
"""Test that a very large n_bytes returns the full volume shape as the optimal chunk."""
150-
arr = np.zeros((10, 10), dtype=np.int8)
159+
arr = MockArray((10, 10), np.int8)
151160
result = _calculate_optimal_chunksize(arr, 1000)
152161
assert result == (10, 10)
153162

154163

155164
def test_two_dim_non_int8():
156165
"""Test with a non-int8 dtype where n_bytes exactly covers the full volume in bytes."""
157-
arr = np.zeros((6, 8), dtype=np.int16) # int16 has itemsize 2
166+
arr = MockArray((6, 8), np.int16) # int16 has itemsize 2
158167
# Total bytes of full volume = 6*8*2 = 96, so optimal chunk should be (6,8)
159168
result = _calculate_optimal_chunksize(arr, 96)
160169
assert result == (6, 8)
@@ -165,14 +174,14 @@ def test_irregular_dimensions():
165174
166175
For shape (7,5) with n_bytes=35, optimal chunk should be (7,5) since 7*5 = 35.
167176
"""
168-
arr = np.zeros((7, 5), dtype=np.int8)
177+
arr = MockArray((7, 5), np.int8)
169178
result = _calculate_optimal_chunksize(arr, 35)
170179
assert result == (7, 5)
171180

172181

173182
def test_primes():
174183
"""Test volume with prime dimensions where divisors are limited."""
175-
arr = np.zeros((7, 5), dtype=np.int8)
184+
arr = MockArray((7, 5), np.int8)
176185
result = _calculate_optimal_chunksize(arr, 23)
177186
assert result == (4, 4)
178187

@@ -211,13 +220,11 @@ def test_altay():
211220

212221
# Create grid and set live mask
213222
grid = Grid(dims=dims)
214-
grid.live_mask = np.ones(shape, dtype=bool)
223+
grid.live_mask = MockArray(shape, bool)
215224

216225
# Calculate chunk size using the live mask function
217226
result = _calculate_live_mask_chunksize(grid)
218227
print(f"{kind}: {result}")
219-
220-
221228

222229
# Verify that the chunk size is valid
223230
assert all(chunk > 0 for chunk in result), f"Invalid chunk size for {kind}"

0 commit comments

Comments
 (0)