SPPAS 4.22

https://sppas.org/

Module sppas.src.anndata

Class sppasFuzzyPoint

Description

Data structure to represent a point (x,y) with a radius (r).

Mainly used to represent a point in an image with a vagueness around the midpoint. The fuzzy point is then representing a rectangle area. The radius is half of the vagueness.

  • - - - - - - - - + | | | (x,y) | | r . | |<------> | | |
  • - - - - - - - - +

Two fuzzy points are equals if their area overlaps.

Constructor

Create a sppasFuzzyPoint instance.

The given coordinates of the midpoint can be a tuple of int values or a string representing it.

Parameters
  • coord: (int,int) x,y coords of the midpoint value.
  • radius: (int) the radius around the midpoint.
View Source
def __init__(self, coord, radius=None):
    """Create a sppasFuzzyPoint instance.

    The given coordinates of the midpoint can be a tuple of int values
    or a string representing it.

    :param coord: (int,int) x,y coords of the midpoint value.
    :param radius: (int) the radius around the midpoint.

    """
    super(sppasFuzzyPoint, self).__init__()
    self.__x = 0
    self.__y = 0
    self.__radius = None
    self.set_midpoint(coord)
    self.set_radius(radius)

Public functions

set

Set self members from another sppasFuzzyPoint instance.

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

        :param other: (sppasFuzzyPoint)

        """
    if isinstance(other, sppasFuzzyPoint) is False:
        raise AnnDataTypeError(other, 'sppasFuzzyPoint')
    self.__radius = None
    self.set_midpoint(other.get_midpoint())
    self.set_radius(other.get_radius())

copy

Return a deep copy of self.

View Source
def copy(self):
    """Return a deep copy of self."""
    return sppasFuzzyPoint((self.__x, self.__y), self.__radius)

get_midpoint

Return the midpoint coords (x,y).

View Source
def get_midpoint(self):
    """Return the midpoint coords (x,y)."""
    return (self.__x, self.__y)

parse

Return a tuple (x,y) or (x,y,r).

Parameters
  • str_point: (str) A string representing a fuzzy point.
View Source
@staticmethod
def parse(str_point):
    """Return a tuple (x,y) or (x,y,r).

        :param str_point: (str) A string representing a fuzzy point.

        """
    tc = str_point.lower()[1:-1]
    tab = tc.split(',')
    x = int(tab[0])
    y = int(tab[1])
    if len(tab) == 3:
        return (x, y, int(tab[2]))
    return (x, y, None)

set_midpoint

Set the midpoint value.

Parameters
  • midpoint: (tuple(int,int), str) the new midpoint coords.
Raises

AnnDataTypeError

View Source
def set_midpoint(self, midpoint):
    """Set the midpoint value.

        :param midpoint: (tuple(int,int), str) the new midpoint coords.
        :raise: AnnDataTypeError

        """
    if isinstance(midpoint, str) is True:
        x, y, r = sppasFuzzyPoint.parse(midpoint)
        midpoint = (x, y)
    self.__check_coords(midpoint)
    self.__x = int(midpoint[0])
    self.__y = int(midpoint[1])

get_radius

Return the radius value (float or None).

View Source
def get_radius(self):
    """Return the radius value (float or None)."""
    return self.__radius

set_radius

Fix the radius value, ie. the vagueness of the point.

The midpoint value must be set first.

Parameters
  • radius: (int, str, None) the radius value
Raises

AnnDataTypeError, AnnDataNegValueError

View Source
def set_radius(self, radius=None):
    """Fix the radius value, ie. the vagueness of the point.

        The midpoint value must be set first.

        :param radius: (int, str, None) the radius value
        :raise: AnnDataTypeError, AnnDataNegValueError

        """
    if radius is not None:
        if isinstance(radius, str) is True:
            try:
                radius = int(radius)
            except ValueError:
                raise AnnDataTypeError(radius, 'int')
        if isinstance(radius, (int, float)) is False:
            raise AnnDataTypeError(radius, 'int')
    if radius is None:
        self.__radius = None
    else:
        self.__radius = int(radius)

contains

Check if the given midpoint is inside the vagueness of self.

Parameters
  • coord: (tuple) An (x, y) coordinates
View Source
def contains(self, coord):
    """Check if the given midpoint is inside the vagueness of self.

        :param coord: (tuple) An (x, y) coordinates

        """
    self.__check_coords(coord)
    rs = self.__radius
    if self.__radius is None:
        rs = 0
    if coord[0] < self.__x - rs:
        return False
    if coord[0] > self.__x + rs:
        return False
    if coord[1] < self.__y - rs:
        return False
    if coord[1] > self.__y + rs:
        return False
    return True

Protected functions

__check_coords

Check if given midpoint is a valid coord.

Parameters
  • midpoint
View Source
def __check_coords(self, midpoint):
    """Check if given midpoint is a valid coord.

        """
    if isinstance(midpoint, tuple) is False:
        raise AnnDataTypeError(midpoint, 'tuple')
    if len(midpoint) != 2:
        raise AnnDataTypeError(midpoint, 'tuple(int, int)')
    if isinstance(midpoint[0], (int, float)) is False:
        raise AnnDataTypeError(midpoint, 'tuple(int, int)')
    if isinstance(midpoint[1], (int, float)) is False:
        raise AnnDataTypeError(midpoint, 'tuple(int, int)')

Overloads

__format__

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

__repr__

View Source
def __repr__(self):
    if self.__radius is None:
        return 'sppasFuzzyPoint: ({:d},{:d})'.format(self.__x, self.__y)
    return 'sppasFuzzyPoint: ({:d},{:d},{:d})) '.format(self.__x, self.__y, self.__radius)

__str__

View Source
def __str__(self):
    if self.__radius is None:
        return '({:d},{:d})'.format(self.__x, self.__y)
    return '({:d},{:d},{:d})'.format(self.__x, self.__y, self.__radius)

__eq__

Test equality of self with other.

Two fuzzy points are equals if the midpoint of one of them is in the area of the other.

Parameters
  • other
View Source
def __eq__(self, other):
    """Test equality of self with other.

        Two fuzzy points are equals if the midpoint of one of them is in the
        area of the other.

        """
    point = None
    if isinstance(other, str) is True:
        point = sppasFuzzyPoint.parse(other)
    elif isinstance(other, tuple) is True:
        if len(other) in (2, 3):
            point = sppasFuzzyPoint((other[0], other[1]))
            if len(other) == 3:
                point.set_radius(other[2])
    elif isinstance(other, sppasFuzzyPoint) is True:
        point = other
    if point is None:
        raise AnnDataTypeError('sppasFuzzyPoint', 'tuple(int,int)')
    other_x, other_y = point.get_midpoint()
    other_r = point.get_radius()
    if other_r is None:
        return self.contains((other_x, other_y))
    if self.contains((other_x - other_r, other_y - other_r)) is True:
        return True
    if self.contains((other_x - other_r, other_y + other_r)) is True:
        return True
    if self.contains((other_x + other_r, other_y - other_r)) is True:
        return True
    if self.contains((other_x + other_r, other_y + other_r)) is True:
        return True
    return False

__hash__

View Source
def __hash__(self):
    return hash((self.__x, self.__y, self.__radius))