Exemplo n.º 1
0
Arquivo: range.py Projeto: lepy/sdata
    def open(lower, upper):
        """ Creates a range excluding the endpoints (i.e. (lower, upper))

        Parameters
        ----------
        lower : comparable, of same type as or subclass of upper type
            The lower bound
        upper : comparable, of same type as or subclass of lower type
            The upper bound

        Raises
        ------
        ValueError
            If type(s) are not comparable or compatible or if constructing
            a range of type (v,v), which is invalid
        
        Returns
        -------
        A Range object (lower, upper)
        """
        # Ensure cutpoints are of compatible, appropriate types
        Range._validate_cutpoints(lower, upper)
        the_type = Range._get_type(lower, upper)
        if lower == upper:
            raise TypeError("Range of type (v,v) is not valid")
        return Range(Cut.above_value(lower, the_type=the_type),
                     Cut.below_value(upper, the_type=the_type))
Exemplo n.º 2
0
Arquivo: range.py Projeto: lepy/sdata
    def at_least(val):
        """ Makes range including all values greater than or equal to
        some value (i.e. [val, inf))

        Parameters
        ----------
        val : comparable
            The lower bound

        Raises
        ------
        ValueError
            If type not comparable

        Returns
        -------
        A Range object [val, inf)
        """
        Range._validate_cutpoints(val)
        the_type = Range._get_type(val)
        return Range(Cut.below_value(val, the_type=the_type),
                     Cut.above_all(the_type=the_type))
Exemplo n.º 3
0
Arquivo: range.py Projeto: lepy/sdata
    def at_most(val):
        """ Makes range including all values less than or equal to
        some value (i.e. (-inf, val])

        Parameters
        ----------
        val : comparable
            The upper bound

        Raises
        ------
        ValueError
            If type not comparable

        Returns
        -------
        A Range object (-inf, val]
        """
        Range._validate_cutpoints(val)
        the_type = Range._get_type(val)
        return Range(Cut.below_all(the_type=the_type),
                     Cut.above_value(val, the_type=the_type))
Exemplo n.º 4
0
Arquivo: range.py Projeto: lepy/sdata
    def open_closed(lower, upper):
        """ Creates a range including the upper (i.e. (lower, upper])

        Parameters
        ----------
        lower : comparable, of same type as or subclass of upper type
            The lower bound
        upper : comparable, of same type as or subclass of lower type
            The upper bound

        Raises
        ------
        ValueError
            If type(s) are not comparable or compatible
        
        Returns
        -------
        A Range object (lower, upper]
        """
        # Ensure cutpoints are of compatible, appropriate types
        Range._validate_cutpoints(lower, upper)
        the_type = Range._get_type(lower, upper)
        return Range(Cut.above_value(lower, the_type=the_type),
                     Cut.above_value(upper, the_type=the_type))
Exemplo n.º 5
0
    def iteritems(self, start=None, end=None):
        """ Iterates over pairs of (Range, value)

        Parameters
        ----------
        start : comparable, optional
            The starting point for iterating, inclusive
        end : comparable, optional
            The ending point for iterating, inclusive

        Returns
        -------
        Generator of (Range intersecting [start,end], value), ordered by start point
        """
        if start is None:
            start = self.lower_cuts[0]
        else:
            start = Cut.below_value(start)
        if end is None:
            end = self.upper_cuts[-1]
        else:
            end = Cut.above_value(end)
        bounding_range = Range(start, end)
        # Get the bounding indices
        ovlapLowerInd = max(bisect_left(self.lower_cuts, start) - 1, 0)
        ovlapUpperInd = bisect_left(self.lower_cuts, end)
        # Create queue of values that need to be generated
        yield_vals = deque()
        # Create dictionary of values to be generated -> indices containing them
        vals_inds_dict = {}
        for i in range(ovlapLowerInd, ovlapUpperInd):
            # Check if anything can be released from the queue
            while len(yield_vals) > 0:
                if vals_inds_dict[yield_vals[0]][-1] < i - 1:
                    # Yield the full range, value. Remove value from queue
                    val = yield_vals.popleft()
                    yield Range(
                        max(self.lower_cuts[vals_inds_dict[val][0]], start),
                        min(self.upper_cuts[vals_inds_dict[val][-1]],
                            end)), val
                    # Remove value from dict
                    del vals_inds_dict[val]
                else:
                    break
            try:
                # Get intersection of the ranges
                intersect = bounding_range.intersection(self.ranges[i])
                if not intersect.is_empty():
                    # If overlapping with this range, put into queue
                    for val in self.items[i]:
                        if val not in vals_inds_dict:
                            yield_vals.append(val)
                            vals_inds_dict[val] = deque()
                        vals_inds_dict[val].append(i)
            except ValueError:
                # Continue if no overlap with this range
                continue
        ## Yield remaining values
        while len(yield_vals) > 0:
            # Yield the full range, value. Remove value from queue
            val = yield_vals.popleft()
            yield Range(max(self.lower_cuts[vals_inds_dict[val][0]], start),
                        min(self.upper_cuts[vals_inds_dict[val][-1]],
                            end)), val
            # Remove value from dict
            del vals_inds_dict[val]