Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions package/PartSegImage/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ def read_image(
instance.set_default_spacing(default_spacing)
return instance.read(image_path, mask_path)

@staticmethod
def _reduce_obsolete_dummy_axes(array, axes) -> typing.Tuple[np.ndarray, str]:
"""
If there are duplicates in axes string then remove dimensions of size one

:return: reduced array and axes
"""
if len(axes) == len(set(axes)):
return array, axes

ax_li = []
shape_li = []
for dim, ax in zip(array.shape, axes):
if dim != 1:
ax_li.append(ax)
shape_li.append(dim)
axes = "".join(ax_li)
array = np.reshape(array, shape_li)
return array, axes

@classmethod
def update_array_shape(cls, array: np.ndarray, axes: str):
"""
Expand All @@ -112,6 +132,8 @@ def update_array_shape(cls, array: np.ndarray, axes: str):
:param array: array to reorder
:param axes: current order of array axes as string like "TZYXC"
"""
array, axes = cls._reduce_obsolete_dummy_axes(array, axes)

try:
final_mapping_dict = {letter: i for i, letter in enumerate(cls.return_order())}
for let1, let2 in [("Z", "I"), ("Z", "Q"), ("C", "S")]:
Expand Down
7 changes: 7 additions & 0 deletions package/tests/test_PartSegImage/test_image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def test_obsep_deconv_read(self, data_test_dir, tmp_path):
image = GenericImageReader.read_image(tmp_path / "test.obsep")
assert image.channels == 3

def test_double_axes_in_dim_read(self, data_test_dir):
image = GenericImageReader.read_image(os.path.join(data_test_dir, "double_q_in_axes.tif"))
assert image.layers == 360
assert image.channels == 1
assert image.stack_pos == 1
assert image.plane_shape == (360, 32)


class CustomImage(Image):
axis_order = "TCXYZ"
Expand Down