Source code for samuroi.masks.segment
from .mask import Mask
from ..util.branch import Branch
[docs]class SegmentMask(Mask):
def __init__(self, data, parent):
super(SegmentMask, self).__init__()
self.branch = Branch(data=data)
self.parent = parent
from .polygon import PolygonMask
self.__polygon = PolygonMask(outline=self.outline)
@property
def outline(self):
"""
:return: return the outline of the wrapped :py:class:`samuroi.util.branch.Branch` object.
"""
return self.branch.outline
@property
def data(self):
"""
:return: return the data of the wrapped :py:class:`samuroi.util.branch.Branch` object.
"""
return self.branch.data
def __call__(self, data, mask):
return self.__polygon(data, mask)
[docs] def move(self, offset):
"""Move the segment don't trigger any event since this will be handled by the parent branch object."""
new_x = self.data['x'] + offset[0]
new_y = self.data['y'] + offset[1]
import numpy
dtype = [('x', float), ('y', float), ('z', float), ('radius', float)]
self.branch.data = numpy.rec.fromarrays([new_x, new_y, self.data['z'], self.data['radius']], dtype=dtype)
from .polygon import PolygonMask
self.__polygon = PolygonMask(outline=self.outline)
[docs] def split(self, nsegments=2, length=None, k=1, s=0):
"""Split the segment in n equal parts, and adopt the parent branch accordingly."""
# get the index of the old segment in the parents child list
i = self.parent.segments.index(self)
# split segment and convert new branch objects into segments
subsegments = [SegmentMask(data=s.data, parent=self.parent)
for s in self.branch.split(nsegments=nsegments, length=length, k=k, s=s)]
# insert new items into list at correct position, i.e. replace self
self.parent.segments[i:i + 1] = subsegments
# trigger parents changed signal
self.parent.changed()
[docs] def join(self, next=True):
"""
Join two segments into one. Arguments:
next: True or False, denote whether to join the segment with the preceding or succeeding one.
"""
# the list of children of the parent
children = self.parent.segments
# get the index of the segment in the parents child list
i = children.index(self)
# select the slice of the two segments to join
# this will work event for i = 0,1,len(children)-1 and len(children)
s = slice(i, i + 2) if next else slice(i - 1, i + 1)
# we cant join next/previous, if there is no respective other segment
if len(children[s]) < 2:
return
# get handle on the two children to join
child0, child1 = children[s]
# create joined segment
joined = Branch.append(child0, child1)
# replace the two old ones in list
children[s] = [SegmentMask(data=joined.data, parent=self.parent)]
self.parent.changed()