Skip to content

Commit

Permalink
handle Roi's properly in cases where channel dimensions are not first
Browse files Browse the repository at this point in the history
  • Loading branch information
pattonw committed Sep 12, 2024
1 parent bc7e71e commit 4e32b8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions funlib/persistence/arrays/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,24 @@ def axis_names(self) -> list[str]:
if uncollapsed
]

@property
def physical_shape(self):
return tuple(
self._source_data.shape[ii]
for ii, (uncollapsed, name) in enumerate(
zip(self.uncollapsed_dims(physical=False), self._metadata.axis_names)
)
if uncollapsed and not name.endswith("^")
)

@property
def roi(self):
"""
Get the Roi associated with this data.
"""

return Roi(
self.offset,
self.voxel_size * Coordinate(self.shape[-self.voxel_size.dims :]),
self.voxel_size * Coordinate(self.physical_shape),
)

@property
Expand Down
7 changes: 7 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,16 @@ def test_slicing_channel_dim_last():
(1, 1),
axis_names=["d0", "d1", "c0^"],
)
assert a.roi == Roi((0, 0), (2, 2))

a.lazy_op(np.s_[1, :, 0:3])
assert a.roi == Roi((0,), (2,))
assert a.shape == (2, 3)
assert a.axis_names == ["d1", "c0^"], a.axis_names
assert a.units == [""]

a.lazy_op(np.s_[:, 2])
assert a.roi == Roi((0,), (2,))
assert a.shape == (2,)
assert a.axis_names == ["d1"]
assert a.units == [""]
Expand All @@ -307,11 +310,13 @@ def test_slicing_channel_dim_last():
)

a.lazy_op(np.s_[[0, 1], 1, :])
assert a.roi == Roi((0,), (2,))
assert a.shape == (2, 4)
assert a.axis_names == ["d0", "c0^"]
assert a.units == [""]

a.lazy_op(np.s_[1, :])
assert a.roi == Roi(tuple(), tuple())
assert a.shape == (4,)
assert a.axis_names == ["c0^"]
assert a.units == []
Expand All @@ -329,6 +334,7 @@ def test_slicing_channel_dim_last():
)

a.lazy_op(np.s_[[2, 2, 2], 1, :])
# assert a.roi == None # TODO: This doesn't make sense???
assert a.shape == (3, 2)
assert a.axis_names == ["d0", "c0^"]
assert a.units == [""]
Expand All @@ -345,6 +351,7 @@ def test_slicing_channel_dim_last():
)

a.lazy_op(np.s_[1, :, np.array([True, True, True, False])])
assert a.roi == Roi((0,), (2,))
assert a.shape == (2, 3)
assert a.axis_names == ["d1", "c0^"]
assert a.units == [""]
Expand Down

0 comments on commit 4e32b8b

Please sign in to comment.