Skip to content

Commit

Permalink
fix KeyError: "None of [Index(['0_x', '1_x', '0_y', '1_y'], dtype='ob…
Browse files Browse the repository at this point in the history
…ject')] are in the [columns]"

Added test
  • Loading branch information
joshua-gould committed Jul 18, 2024
1 parent 77a29ba commit 81bf7c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
15 changes: 12 additions & 3 deletions dask_image/ndmeasure/_utils/_find_objects.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dask.dataframe as dd
import numpy as np
import pandas as pd
from dask.delayed import Delayed
import dask.dataframe as dd


def _array_chunk_location(block_id, chunks):
Expand All @@ -28,7 +28,8 @@ def _find_bounding_boxes(x, array_location):
result = {}
for val in unique_vals:
positions = np.where(x == val)
slices = tuple(slice(np.min(pos) + array_location[i], np.max(pos) + 1 + array_location[i]) for i, pos in enumerate(positions))
slices = tuple(slice(np.min(pos) + array_location[i], np.max(pos) + 1 + array_location[i]) for i, pos in
enumerate(positions))
result[val] = slices
column_names = [i for i in range(x.ndim)] # column names are: 0, 1, ... nD
return pd.DataFrame.from_dict(result, orient='index', columns=column_names)
Expand Down Expand Up @@ -70,6 +71,14 @@ def _find_objects(ndim, df1, df2):
df1 = dd.from_delayed(df1, meta=meta)
if isinstance(df2, Delayed):
df2 = dd.from_delayed(df2, meta=meta)
ddf = dd.merge(df1, df2, how="outer", left_index=True, right_index=True)

if len(df1) > 0 and len(df2) > 0:
ddf = dd.merge(df1, df2, how="outer", left_index=True, right_index=True)
elif len(df1) > 0:
ddf = df1
elif len(df2) > 0:
ddf = df2
else:
ddf = pd.DataFrame()
result = ddf.apply(_merge_bounding_boxes, ndim=ndim, axis=1, meta=meta)
return result
28 changes: 18 additions & 10 deletions tests/test_dask_image/test_ndmeasure/test_find_objects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from dask_image.ndmeasure._utils import _labeled_comprehension_delayed
import dask.array as da
import dask.dataframe as dd
import numpy as np
Expand All @@ -22,9 +21,9 @@ def label_image():
"""
label_image = np.zeros((5, 10)).astype(int)
label_image[1:3,0:2] = 111
label_image[3,3:-2] = 222
label_image[0:2,-3:] = 333
label_image[1:3, 0:2] = 111
label_image[3, 3:-2] = 222
label_image[0:2, -3:] = 333
label_image = da.from_array(label_image, chunks=(5, 5))
return label_image

Expand All @@ -43,8 +42,8 @@ def label_image_with_empty_chunk():
[ 0, 0, 0, 0, 0, 0]])
"""
label_image = np.zeros((6, 6)).astype(int)
label_image[1:3,0:2] = 111
label_image[4,3:] = 222
label_image[1:3, 0:2] = 111
label_image[4, 3:] = 222
label_image = da.from_array(label_image, chunks=(3, 3))
return label_image

Expand All @@ -55,14 +54,23 @@ def test_find_objects_err(label_image):
dask_image.ndmeasure.find_objects(label_image)


def test_empty_chunk():
test_labels = da.zeros((10, 10), dtype='int', chunks=(3, 3))
test_labels[0, 0] = 1
computed_result = dask_image.ndmeasure.find_objects(test_labels).compute()
expected = pd.DataFrame.from_dict({0: {1: slice(0, 1)},
1: {1: slice(0, 1)}, })
assert computed_result.equals(expected)


def test_find_objects(label_image):
result = dask_image.ndmeasure.find_objects(label_image)
assert isinstance(result, dd.DataFrame)
computed_result = result.compute()
assert isinstance(computed_result, pd.DataFrame)
expected = pd.DataFrame.from_dict(
{0: {111: slice(1, 3), 222: slice(3, 4), 333: slice(0, 2)},
1: {111: slice(0, 2), 222: slice(3, 8), 333: slice(7, 10)}}
1: {111: slice(0, 2), 222: slice(3, 8), 333: slice(7, 10)}}
)
assert computed_result.equals(expected)

Expand All @@ -75,8 +83,8 @@ def test_3d_find_objects(label_image):
assert isinstance(computed_result, pd.DataFrame)
expected = pd.DataFrame.from_dict(
{0: {111: slice(1, 3), 222: slice(3, 4), 333: slice(0, 2)},
1: {111: slice(0, 2), 222: slice(3, 8), 333: slice(7, 10)},
2: {111: slice(0, 2), 222: slice(0, 2), 333: slice(0, 2)}}
1: {111: slice(0, 2), 222: slice(3, 8), 333: slice(7, 10)},
2: {111: slice(0, 2), 222: slice(0, 2), 333: slice(0, 2)}}
)
assert computed_result.equals(expected)

Expand All @@ -88,6 +96,6 @@ def test_find_objects_with_empty_chunks(label_image_with_empty_chunk):
assert isinstance(computed_result, pd.DataFrame)
expected = pd.DataFrame.from_dict(
{0: {111: slice(1, 3, None), 222: slice(4, 5, None)},
1: {111: slice(0, 2, None), 222: slice(3, 6, None)}}
1: {111: slice(0, 2, None), 222: slice(3, 6, None)}}
)
assert computed_result.equals(expected)

0 comments on commit 81bf7c6

Please sign in to comment.