Skip to content

Commit

Permalink
Fix some potential memory leaks in the array configs
Browse files Browse the repository at this point in the history
  • Loading branch information
pattonw committed Dec 5, 2024
1 parent 34bdc25 commit fd5e117
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ def array(self, mode="r") -> Array:
assert num_channels is None, "Input labels cannot have a channel dimension"

def group_array(data):
out = da.zeros((len(self.groupings), *array.physical_shape), dtype=np.uint8)
for i, (_, group_ids) in enumerate(self.groupings):
if len(group_ids) == 0:
out[i] = data != self.background
else:
out[i] = da.isin(data, group_ids)
groups = [
da.isin(data, group_ids)
if len(group_ids) > 0
else data != self.background
for _, group_ids in self.groupings
]
out = da.stack(groups, axis=0)
return out

data = group_array(array.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from typing import List, Dict, Optional
from funlib.persistence import Array
import numpy as np
import dask.array as da


Expand Down Expand Up @@ -45,18 +44,15 @@ class ConcatArrayConfig(ArrayConfig):
def array(self, mode: str = "r") -> Array:
arrays = [config.array(mode) for _, config in self.source_array_configs.items()]

out_data = da.stack([array.data for array in arrays], axis=0)
out_array = Array(
da.zeros(len(arrays), *arrays[0].physical_shape, dtype=arrays[0].dtype),
out_data,
offset=arrays[0].offset,
voxel_size=arrays[0].voxel_size,
axis_names=["c^"] + arrays[0].axis_names,
units=arrays[0].units,
)

def set_channels(data):
for i, array in enumerate(arrays):
data[i] = array.data[:]
return data

out_array.lazy_op(set_channels)
# callable lazy op so funlib.persistence doesn't try to recoginize this data as writable
out_array.lazy_op(lambda data: data)
return out_array
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def array(self, mode: str = "r") -> Array:
assert num_channels_from_array(array) is not None

out_array = Array(
da.zeros(*array.physical_shape, dtype=array.dtype),
da.zeros(array.physical_shape, dtype=array.dtype),
offset=array.offset,
voxel_size=array.voxel_size,
axis_names=array.axis_names[1:],
units=array.units,
)

out_array.data = da.maximum(array.data, axis=0)
out_array.data = da.max(array.data, axis=0)

# mark data as non-writable
out_array.lazy_op(lambda data: data)
Expand Down

0 comments on commit fd5e117

Please sign in to comment.