Example #1
0
    def contiguous(self, edge, abs_tol=1E-6, allow_swap=True):
        """
        check if edge can be appended to the chain
        :param edge: :class:`Entity` to append
        :param tol: float tolerance on contiguity
        :param allow_swap: if True (default), tries to swap edge or self to find contiguity
        :return: int,bool index where to append in chain, swap of edge required
        """

        if len(self) == 0:  # init the chain with edge
            return -1, False

        if isclose(self.end.distance(edge.start), 0, abs_tol=abs_tol):
            return -1, False  # append

        if isclose(self.start.distance(edge.end), 0, abs_tol=abs_tol):
            return 0, False  # prepend

        if not allow_swap:
            return None, False

        if isclose(self.end.distance(edge.end), 0, abs_tol=abs_tol):
            return -1, True  # append swapped
        if isclose(self.start.distance(edge.start), 0, abs_tol=abs_tol):
            return 0, True  # prepend swapped

        return None, False
Example #2
0
def chains(group, tol=1E-6, mergeable=None):
    """build chains from all possible segments in group
    :param mergeable: function(e1,e2) returning True if entities e1,e2 can be merged
    """

    res = Group()
    changed = False
    # step 1 : add all entities in group to chains in res
    for e in group:
        if e is None or isclose(e.length, 0, tol):
            continue  # will not be present in res
        for c in res:
            # if c.isclosed(): continue #reopen closed chains might be good
            if c.append(e, tol=tol, mergeable=mergeable):
                changed = True
                break
        else:
            if isinstance(e, Chain):
                res.append(e)
            else:
                res.append(Chain([e]))
    # step 2 : try to merge chains
    if changed:
        res = chains(res, tol, mergeable)
    return res
Example #3
0
def normal_pdf(x, mu, sigma):
    """Return the probability density function at x"""
    try:
        return 1. / (math.sqrt(2 * math.pi) * sigma) * math.exp(-0.5 *
                                                                (1. / sigma *
                                                                 (x - mu))**2)
    except ZeroDivisionError:
        return 1 if math2.isclose(x, mean) else 0
Example #4
0
 def _switch_points(self, xmin, xmax):
     prevy = None
     firstpoint, lastpoint = False, False
     for x, y in self:
         y = y(x)
         if x < xmin:
             if firstpoint:
                 continue
             firstpoint = True
             x = xmin
         if x > xmax:
             if lastpoint:
                 break
             lastpoint = True
             x = xmax
         if prevy is not None and not math2.isclose(y, prevy):  # step
             yield x, prevy
         yield x, y
         prevy = y
Example #5
0
 def ishorizontal(self, tol=0.01):
     return self.isline() and isclose(self.start.y, self.end.y, tol)
Example #6
0
 def isvertical(self, tol=0.01):
     return self.isline() and isclose(self.start.x, self.end.x, tol)