Skip to content

Commit

Permalink
fix setter failing in github actions
Browse files Browse the repository at this point in the history
dask arrays are not supposed to cache results from reads, so if you change the data that the dask array wraps you should get the updated data on the next read.
This does not seem to be happening for dask arrays wrapping in-memory numpy arrays on some hardware. It is consistent behavior in the github actions but I cannot recreate it locally.
  • Loading branch information
pattonw committed Dec 30, 2024
1 parent 158ecbc commit 0b67093
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
7 changes: 7 additions & 0 deletions funlib/persistence/arrays/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ def __setitem__(self, key, value: np.ndarray):

self._source_data[region_slices] = value

# If the source data is an in-memory numpy array, writing to the numpy
# array does not always result in the dask array reading the new data.
# It seems to be a caching issue. To work around this, we create a new
# dask array from the source data.
if isinstance(self._source_data, np.ndarray):
self.data = da.from_array(self._source_data)

else:
raise RuntimeError(
"This array is not writeable since you have applied a custom callable "
Expand Down
17 changes: 9 additions & 8 deletions tests/test_array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dask.array as da
import numpy as np
import pytest
import dask.array as da

from funlib.geometry import Coordinate, Roi
from funlib.persistence.arrays import Array
Expand Down Expand Up @@ -73,13 +73,14 @@ def test_setitem():

a = Array(np.zeros((2, 5)), (0, 0), (1, 1))

a[Roi((0, 0), (2, 5))] = np.arange(0, 10).reshape(2, 5)
assert a[Coordinate((0, 0))] == 0
assert a[Coordinate((0, 1))] == 1
assert a[Coordinate((0, 2))] == 2
assert a[Coordinate((1, 0))] == 5
assert a[Coordinate((1, 1))] == 6
assert a[Coordinate((1, 4))] == 9
data = np.arange(0, 10).reshape(2, 5)
a[Roi((0, 0), (2, 5))] = data
assert a[Coordinate((0, 0))] == a._source_data[0, 0] == 0
assert a[Coordinate((0, 1))] == a._source_data[0, 1] == 1
assert a[Coordinate((0, 2))] == a._source_data[0, 2] == 2
assert a[Coordinate((1, 0))] == a._source_data[1, 0] == 5
assert a[Coordinate((1, 1))] == a._source_data[1, 1] == 6
assert a[Coordinate((1, 4))] == a._source_data[1, 4] == 9

# set entirely with numpy array and channels

Expand Down

0 comments on commit 0b67093

Please sign in to comment.