Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[easy] Modularize segmentation mask #1578

Merged
merged 1 commit into from
Sep 27, 2019
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
1 change: 1 addition & 0 deletions starfish/core/segmentation_mask/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .segmentation_mask import SegmentationMaskCollection
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,10 @@
from skimage.measure import regionprops

from starfish.core.types import Axes, Coordinates
from .util import _get_axes_names


AXES = [a.value for a in Axes if a not in (Axes.ROUND, Axes.CH)]
COORDS = [c.value for c in Coordinates]


def _get_axes_names(ndim: int) -> Tuple[List[str], List[str]]:
"""Get needed axes names given the number of dimensions.

Parameters
----------
ndim : int
Number of dimensions.

Returns
-------
axes : List[str]
Axes names.
coords : List[str]
Coordinates names.
"""
if ndim == 2:
axes = [axis for axis in AXES if axis != Axes.ZPLANE.value]
coords = [coord for coord in COORDS if coord != Coordinates.Z.value]
elif ndim == 3:
axes = AXES
coords = COORDS
else:
raise TypeError('expected 2- or 3-D image')

return axes, coords


def validate_segmentation_mask(arr: xr.DataArray):
def _validate_segmentation_mask(arr: xr.DataArray):
"""Validate if the given array is a segmentation mask.

Parameters
Expand Down Expand Up @@ -108,7 +78,7 @@ def append(self, mask: xr.DataArray):
arr : xr.DataArray
Segmentation mask.
"""
validate_segmentation_mask(mask)
_validate_segmentation_mask(mask)
self._masks.append(mask)

for axis in Axes:
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import pytest
import xarray as xr

from starfish.core.segmentation_mask import (
SegmentationMaskCollection,
validate_segmentation_mask,
)
from starfish.core.types import Axes, Coordinates
from ..segmentation_mask import _validate_segmentation_mask, SegmentationMaskCollection


def test_validate_segmentation_mask():
Expand All @@ -19,7 +16,7 @@ def test_validate_segmentation_mask():
y=[0, 1],
xc=('x', [0.5, 1.5, 2.5]),
yc=('y', [0.5, 1.5])))
validate_segmentation_mask(good)
_validate_segmentation_mask(good)

good = xr.DataArray([[[True], [False], [False]],
[[False], [True], [True]]],
Expand All @@ -30,7 +27,7 @@ def test_validate_segmentation_mask():
zc=('z', [0.5, 1.5]),
yc=('y', [1.5, 2.5, 3.5]),
xc=('x', [42.5])))
validate_segmentation_mask(good)
_validate_segmentation_mask(good)

bad = xr.DataArray([[1, 2, 3],
[4, 5, 6]],
Expand All @@ -40,14 +37,14 @@ def test_validate_segmentation_mask():
xc=('x', [0.5, 1.5, 2.5]),
yc=('y', [0.5, 1.5])))
with pytest.raises(TypeError):
validate_segmentation_mask(bad)
_validate_segmentation_mask(bad)

bad = xr.DataArray([True],
dims=('x'),
coords=dict(x=[0],
xc=('x', [0.5])))
with pytest.raises(TypeError):
validate_segmentation_mask(bad)
_validate_segmentation_mask(bad)

bad = xr.DataArray([[True]],
dims=('z', 'y'),
Expand All @@ -56,12 +53,12 @@ def test_validate_segmentation_mask():
zc=('z', [0.5]),
yc=('y', [0.5])))
with pytest.raises(TypeError):
validate_segmentation_mask(bad)
_validate_segmentation_mask(bad)

bad = xr.DataArray([[True]],
dims=('x', 'y'))
with pytest.raises(TypeError):
validate_segmentation_mask(bad)
_validate_segmentation_mask(bad)


def test_from_label_image():
Expand Down
34 changes: 34 additions & 0 deletions starfish/core/segmentation_mask/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import List, Tuple

from starfish.core.types import Axes, Coordinates


AXES = [a.value for a in Axes if a not in (Axes.ROUND, Axes.CH)]
COORDS = [c.value for c in Coordinates]


def _get_axes_names(ndim: int) -> Tuple[List[str], List[str]]:
"""Get needed axes names given the number of dimensions.

Parameters
----------
ndim : int
Number of dimensions.

Returns
-------
axes : List[str]
Axes names.
coords : List[str]
Coordinates names.
"""
if ndim == 2:
axes = [axis for axis in AXES if axis != Axes.ZPLANE.value]
coords = [coord for coord in COORDS if coord != Coordinates.Z.value]
elif ndim == 3:
axes = AXES
coords = COORDS
else:
raise TypeError('expected 2- or 3-D image')

return axes, coords