Skip to content

Commit 4e32b8b

Browse files
committed
handle Roi's properly in cases where channel dimensions are not first
1 parent bc7e71e commit 4e32b8b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

funlib/persistence/arrays/array.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,24 @@ def axis_names(self) -> list[str]:
145145
if uncollapsed
146146
]
147147

148+
@property
149+
def physical_shape(self):
150+
return tuple(
151+
self._source_data.shape[ii]
152+
for ii, (uncollapsed, name) in enumerate(
153+
zip(self.uncollapsed_dims(physical=False), self._metadata.axis_names)
154+
)
155+
if uncollapsed and not name.endswith("^")
156+
)
157+
148158
@property
149159
def roi(self):
150160
"""
151161
Get the Roi associated with this data.
152162
"""
153-
154163
return Roi(
155164
self.offset,
156-
self.voxel_size * Coordinate(self.shape[-self.voxel_size.dims :]),
165+
self.voxel_size * Coordinate(self.physical_shape),
157166
)
158167

159168
@property

tests/test_array.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,16 @@ def test_slicing_channel_dim_last():
283283
(1, 1),
284284
axis_names=["d0", "d1", "c0^"],
285285
)
286+
assert a.roi == Roi((0, 0), (2, 2))
286287

287288
a.lazy_op(np.s_[1, :, 0:3])
289+
assert a.roi == Roi((0,), (2,))
288290
assert a.shape == (2, 3)
289291
assert a.axis_names == ["d1", "c0^"], a.axis_names
290292
assert a.units == [""]
291293

292294
a.lazy_op(np.s_[:, 2])
295+
assert a.roi == Roi((0,), (2,))
293296
assert a.shape == (2,)
294297
assert a.axis_names == ["d1"]
295298
assert a.units == [""]
@@ -307,11 +310,13 @@ def test_slicing_channel_dim_last():
307310
)
308311

309312
a.lazy_op(np.s_[[0, 1], 1, :])
313+
assert a.roi == Roi((0,), (2,))
310314
assert a.shape == (2, 4)
311315
assert a.axis_names == ["d0", "c0^"]
312316
assert a.units == [""]
313317

314318
a.lazy_op(np.s_[1, :])
319+
assert a.roi == Roi(tuple(), tuple())
315320
assert a.shape == (4,)
316321
assert a.axis_names == ["c0^"]
317322
assert a.units == []
@@ -329,6 +334,7 @@ def test_slicing_channel_dim_last():
329334
)
330335

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

347353
a.lazy_op(np.s_[1, :, np.array([True, True, True, False])])
354+
assert a.roi == Roi((0,), (2,))
348355
assert a.shape == (2, 3)
349356
assert a.axis_names == ["d1", "c0^"]
350357
assert a.units == [""]

0 commit comments

Comments
 (0)