SPPAS 4.22

https://sppas.org/

Module sppas.src.anndata

Class sppasInterval

Description

Localization of an interval between two sppasPoint instances.

An interval is identified by two sppasPoint objects:

  • one is representing the beginning of the interval;
  • the other is representing the end of the interval.

Constructor

Create a new sppasInterval instance.

Parameters
  • begin: (sppasPoint)
  • end: (sppasPoint)

Degenerated interval is forbidden, i.e. begin > end.

View Source
def __init__(self, begin, end):
    """Create a new sppasInterval instance.

    :param begin: (sppasPoint)
    :param end: (sppasPoint)

    Degenerated interval is forbidden, i.e. begin > end.

    """
    super(sppasInterval, self).__init__()
    if isinstance(begin, sppasPoint) is False:
        AnnDataTypeError(begin, 'sppasPoint')
    if isinstance(end, sppasPoint) is False:
        AnnDataTypeError(end, 'sppasPoint')
    if sppasInterval.check_types(begin, end) is False:
        raise AnnDataEqTypeError(begin, end)
    if sppasInterval.check_interval_bounds(begin, end) is False:
        raise IntervalBoundsError(begin, end)
    if begin >= end:
        logging.warning('begin ({!s:s} >= end {!s:s})'.format(begin, end))
    self.__begin = begin
    self.__end = end

Public functions

set

Set self members from another sppasInterval instance.

Parameters
  • other: (sppasInterval)
View Source
def set(self, other):
    """Set self members from another sppasInterval instance.

        :param other: (sppasInterval)

        """
    if isinstance(other, sppasInterval) is False:
        raise AnnDataTypeError(other, 'sppasInterval')
    self.__begin = other.get_begin()
    self.__end = other.get_end()

is_interval

Overrides. Return True, because self represents an interval.

View Source
def is_interval(self):
    """Overrides. Return True, because self represents an interval."""
    return True

copy

Return a deep copy of self.

View Source
def copy(self):
    """Return a deep copy of self."""
    return sppasInterval(self.__begin.copy(), self.__end.copy())

get_begin

Return the begin sppasPoint instance.

View Source
def get_begin(self):
    """Return the begin sppasPoint instance."""
    return self.__begin

set_begin

Set the begin of the interval to a new sppasPoint.

Attention: it is a reference assignment.

Parameters
  • tp: (sppasPoint)
View Source
def set_begin(self, tp):
    """Set the begin of the interval to a new sppasPoint.

        Attention: it is a reference assignment.

        :param tp: (sppasPoint)

        """
    if isinstance(tp, sppasPoint) is False:
        raise AnnDataTypeError(tp, 'sppasPoint')
    if sppasInterval.check_types(tp, self.__end) is False:
        raise AnnDataEqTypeError(tp, self.__end)
    if sppasInterval.check_interval_bounds(tp, self.__end) is False:
        raise IntervalBoundsError(tp, self.__end)
    self.__begin = tp

get_end

Return the end sppasPoint instance.

View Source
def get_end(self):
    """Return the end sppasPoint instance."""
    return self.__end

set_end

Set the end of the interval to a new sppasPoint.

Attention: it is a reference assignment.

Parameters
  • tp: (sppasPoint)
View Source
def set_end(self, tp):
    """Set the end of the interval to a new sppasPoint.

        Attention: it is a reference assignment.

        :param tp: (sppasPoint)

        """
    if isinstance(tp, sppasPoint) is False:
        raise AnnDataTypeError(tp, 'sppasPoint')
    if sppasInterval.check_types(self.__begin, tp) is False:
        raise AnnDataEqTypeError(self.__begin, tp)
    if sppasInterval.check_interval_bounds(self.__begin, tp) is False:
        raise IntervalBoundsError(self.__begin, tp)
    self.__end = tp

is_bound

Return True if point is the begin or the end of the interval.

Parameters
  • point
View Source
def is_bound(self, point):
    """Return True if point is the begin or the end of the interval."""
    return self.__begin == point or self.__end == point

combine

Return a sppasInterval, the combination of two intervals.

Parameters
  • other: (sppasInterval) the other interval to combine with.
View Source
def combine(self, other):
    """Return a sppasInterval, the combination of two intervals.

        :param other: (sppasInterval) the other interval to combine with.

        """
    if isinstance(other, sppasInterval) is False:
        AnnDataTypeError(other, 'sppasInterval')
    if self > other:
        other, self = (self, other)
    if self.__end <= other.get_begin():
        return sppasInterval(self.__begin, other.get_end())
    return sppasInterval(other.get_begin(), self.__end)

union

Return a sppasInterval representing the union of two intervals.

Parameters
  • other: (sppasInterval) the other interval to merge with.
View Source
def union(self, other):
    """Return a sppasInterval representing the union of two intervals.

        :param other: (sppasInterval) the other interval to merge with.

        """
    if isinstance(other, sppasInterval) is False:
        AnnDataTypeError(other, 'sppasInterval')
    if self > other:
        other, self = (self, other)
    return sppasInterval(self.__begin, other.get_end())

duration

Overridden. Return the duration of the time interval.

Returns
  • (sppasDuration) Duration and its vagueness.
View Source
def duration(self):
    """Overridden. Return the duration of the time interval.

        :returns: (sppasDuration) Duration and its vagueness.

        """
    value = self.__end.get_midpoint() - self.__begin.get_midpoint()
    vagueness = 0
    if self.__begin.get_radius() is not None:
        vagueness += self.__begin.get_radius()
    if self.__end.get_radius() is not None:
        vagueness += self.__end.get_radius()
    return sppasDuration(value, vagueness)

middle

Return a sppasPoint() at the middle of the time interval.

To be tested.

Returns
  • (sppasPoint)
View Source
def middle(self):
    """Return a sppasPoint() at the middle of the time interval.

        To be tested.

        :returns: (sppasPoint)

        """
    duration = self.duration()
    m = float(self.__begin.get_midpoint()) + float(duration.get_value()) / 2.0
    r = float(duration.get_margin()) / 2.0
    if self.is_float():
        return sppasPoint(m, r)
    return sppasPoint(int(m), int(r))

middle_value

Return the middle value of the time interval.

Return a float value even if points are integers.

Returns
  • (float) value.
View Source
def middle_value(self):
    """Return the middle value of the time interval.

        Return a float value even if points are integers.

        :returns: (float) value.

        """
    duration = self.__end.get_midpoint() - self.__begin.get_midpoint()
    return float(self.__begin.get_midpoint()) + float(duration) / 2.0

set_radius

Set a radius value to begin and end points.

Parameters
  • radius: (int or float)
Raises

ValueError

View Source
def set_radius(self, radius):
    """Set a radius value to begin and end points.

        :param radius: (int or float)
        :raise: ValueError

        """
    duration = self.__end.get_midpoint() - self.__begin.get_midpoint()
    if float(radius) * 2.0 > float(duration):
        raise ValueError("The radius value {r} can't be more than half-duration of the interval [{b};{e}].".format(r=radius, b=self.__begin.get_midpoint(), e=self.__end.get_midpoint()))
    self.__begin.set_radius(radius)
    self.__end.set_radius(radius)

shift

Shift the interval to a given delay.

Parameters
  • delay: (int, float) delay to shift bounds
Raises

AnnDataTypeError

View Source
def shift(self, delay):
    """Shift the interval to a given delay.

        :param delay: (int, float) delay to shift bounds
        :raise: AnnDataTypeError

        """
    self.__begin.shift(delay)
    self.__end.shift(delay)

is_int

View Source
def is_int(self):
    return self.__begin.is_int()

is_float

View Source
def is_float(self):
    return self.__begin.is_float()

check_interval_bounds

Check bounds of a virtual interval.

Parameters
  • begin: (sppasPoint)
  • end: (sppasPoint)
View Source
@staticmethod
def check_interval_bounds(begin, end):
    """Check bounds of a virtual interval.

        :param begin: (sppasPoint)
        :param end: (sppasPoint)

        """
    if begin.get_midpoint() >= end.get_midpoint():
        return False
    if begin.get_radius() is not None and end.get_radius() is not None:
        if begin.get_midpoint() - begin.get_radius() > end.get_midpoint() - end.get_radius():
            return False
    return True

check_types

True only if begin and end are both the same types of sppasPoint.

Parameters
  • begin: any kind of data
  • end: any kind of data
Returns
  • Boolean
View Source
@staticmethod
def check_types(begin, end):
    """True only if begin and end are both the same types of sppasPoint.

        :param begin: any kind of data
        :param end: any kind of data
        :returns: Boolean

        """
    try:
        begin.get_midpoint()
        end.get_midpoint()
    except AttributeError:
        return False
    return isinstance(begin.get_midpoint(), type(end.get_midpoint()))

Overloads

__format__

View Source
def __format__(self, fmt):
    return str(self).__format__(fmt)

__repr__

View Source
def __repr__(self):
    return 'sppasInterval: [{!s:s},{!s:s}]'.format(self.get_begin(), self.get_end())

__str__

View Source
def __str__(self):
    return '[{!s:s},{!s:s}]'.format(self.get_begin(), self.get_end())

__contains__

Return True if the given data is contained in the interval.

Parameters
  • other: (sppasInterval, sppasPoint, int, float)
View Source
def __contains__(self, other):
    """Return True if the given data is contained in the interval.

        :param other: (sppasInterval, sppasPoint, int, float)

        """
    if isinstance(other, (sppasInterval, sppasPoint, float, int)) is False:
        raise AnnDataTypeError(other, 'sppasInterval, sppasPoint, float, int')
    if isinstance(other, sppasInterval):
        return self.__begin <= other.get_begin() and other.get_end() <= self.__end
    return self.__begin <= other <= self.__end

__eq__

Equal.

Parameters
  • other: (sppasInterval) the other interval to compare with.
View Source
def __eq__(self, other):
    """Equal.

        :param other: (sppasInterval) the other interval to compare with.

        """
    if isinstance(other, sppasInterval) is False:
        return False
    return self.__begin == other.get_begin() and self.__end == other.get_end()

__lt__

LowerThan.

Parameters
  • other: (sppasInterval, sppasPoint, float, int)
View Source
def __lt__(self, other):
    """LowerThan.

        :param other: (sppasInterval, sppasPoint, float, int)

        """
    if isinstance(other, (sppasPoint, float, int)):
        return self.__end < other
    if isinstance(other, sppasInterval) is False:
        return False
    return self.__begin < other.get_begin()

__gt__

GreaterThan.

Parameters
  • other: (sppasInterval, sppasPoint, float, int)
View Source
def __gt__(self, other):
    """GreaterThan.

        :param other: (sppasInterval, sppasPoint, float, int)

        """
    if isinstance(other, (int, float, sppasPoint)):
        return self.__begin > other
    if isinstance(other, sppasInterval) is False:
        return False
    return self.__begin > other.get_begin()