Beispiel #1
0
def test_extensions():
    d = Interval(1, 3)
    assert d.get_lte().equals(Interval.lte(3))
    assert d.get_gte().equals(Interval.gte(1))
    assert d.get_lt().equals(Interval.lt(1))
    assert d.get_gt().equals(Interval.gt(3))

    d = Interval.open(1, 3)
    assert d.get_lte().equals(Interval.lt(3))
    assert d.get_gte().equals(Interval.gt(1))
    assert d.get_lt().equals(Interval.lte(1))
    assert d.get_gt().equals(Interval.gte(3))

    d = Interval.empty()
    assert d.get_lte().is_empty
    assert d.get_gte().is_empty
    assert d.get_lt().is_empty
    assert d.get_gt().is_empty
Beispiel #2
0
    def update_points_of_interest(self, underlying_points):
        if not self.is_ready:
            return False

        i_end = len(underlying_points)
        if i_end == 0:
            return False

        def process_points_on_trend(points_on_trend, insert_index):
            if len(points_on_trend) == 0:
                return
            closest_point = None
            closest_dist = 0
            for p in points_on_trend:
                dist = abs(self.y(p[0]) - p[1])
                if closest_point is None or dist < closest_dist:
                    closest_dist = dist
                    closest_point = p
            self.tangent_points.insert(insert_index, closest_point)

        # find intersections, tangent points and extremas
        phase = 0
        phase_point = None
        previous_phase = phase
        intersection_found = False
        intersection_insert_index = len(self.intersections)
        tangent_insert_index = len(self.tangent_points)
        points_on_trend = []
        new_extremas = []
        extrema = None
        extrema_dist = 0

        # search up to the last intersection or tangent point
        update_interval = self.search_interval
        if len(self.intersections) != 0:
            update_interval = Interval.intersection(
                [update_interval,
                 Interval.gte(self.intersections[-1][0])])
        if len(self.tangent_points) != 0:
            update_interval = Interval.intersection(
                [update_interval,
                 Interval.gt(self.tangent_points[-1][0])])

        for i in reversed(range(len(underlying_points))):
            p = underlying_points[i]
            if not update_interval.contains(p[0]):
                break

            if self.is_point_on_trend(p):
                points_on_trend.insert(0, p)
                if extrema is not None:
                    new_extremas.insert(0, extrema)
                    extrema = None
                continue
            elif len(points_on_trend) != 0:
                process_points_on_trend(points_on_trend, tangent_insert_index)
                points_on_trend = []

            phase = p[1] - self.y(p[0])
            at_intersection = previous_phase != 0 and phase * previous_phase < 0

            if at_intersection:
                # found intersection
                intersection = _line_intersection(self._line, (phase_point, p))
                if intersection is None or intersection[0] < self._line[0][0]:
                    intersection = _average_point_of_2(phase_point, p)
                self.intersections.insert(intersection_insert_index,
                                          intersection)
                intersection_found = True

                if extrema is not None:
                    new_extremas.insert(0, extrema)
                    extrema = None

            if phase > 0:
                dist = p[1]
            else:
                dist = -p[1]
            if extrema is None or dist > extrema_dist:
                extrema = p
                extrema_dist = dist

            previous_phase = phase
            phase_point = p

        if len(points_on_trend) != 0:
            process_points_on_trend(points_on_trend, tangent_insert_index)

        if extrema is not None:
            new_extremas.insert(0, extrema)
        if len(new_extremas) != 0:
            # merge extremas with existing ones
            if len(self.extremas) == 0:
                self.extremas += new_extremas
            else:
                if self.extremas[-1][0] >= update_interval.start:
                    # old extrema may be outdated
                    del self.extremas[-1]
                self.extremas += new_extremas

        if intersection_found:
            self.update_interval()

        return intersection_found