Public functions
get_x
Return the list of x values.
Returns
- (list) The list that contains all x values
View Source
def get_x(self) -> list:
"""Return the list of x values.
:return: (list) The list that contains all x values
"""
return self.__x.copy()
get_y
Return the list of y values.
Returns
- (list) The list that contains all y values
View Source
def get_y(self) -> list:
""" Return the list of y values.
:return: (list) The list that contains all y values
"""
return self.__y.copy()
get_z
Return the list of z values or None.
Returns
- (list or None) The list that contains all z values, or None if 3D axis is not set.
View Source
def get_z(self) -> list:
"""Return the list of z values or None.
:return: (list or None) The list that contains all z values,
or None if 3D axis is not set.
"""
if self.__z is None:
return None
return self.__z.copy()
get_score
Return the list of confidence score values or None.
Returns
- (list or None) The list that contains all score values, or None if scores are not set.
View Source
def get_score(self) -> list:
"""Return the list of confidence score values or None.
:return: (list or None) The list that contains all score values,
or None if scores are not set.
"""
if self.__confidence is None:
return None
return self.__confidence.copy()
x
Return the x-axe value of the sight at the given index.
Parameters
- index: (int) Index of the sight
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
Returns
View Source
def x(self, index: int) -> int:
"""Return the x-axe value of the sight at the given index.
:param index: (int) Index of the sight
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
:return: (int) The x value
"""
checked_index = self.check_index(index)
return self.__x[checked_index]
y
Return the y-axe value of the sight at the given index.
Parameters
- index: (int) Index of the sight
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
Returns
View Source
def y(self, index: int) -> int:
"""Return the y-axe value of the sight at the given index.
:param index: (int) Index of the sight
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
:return: (int) The y value
"""
checked_index = self.check_index(index)
return self.__y[checked_index]
z
Return the z-axe value of the sight at the given index or None.
Parameters
- index: (int) Index of the sight
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
Returns
- (int or None) The z value or None if 3D axis is not set
View Source
def z(self, index: int) -> int:
"""Return the z-axe value of the sight at the given index or None.
:param index: (int) Index of the sight
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
:return: (int or None) The z value or None if 3D axis is not set
"""
if self.__z is None:
return None
checked_index = self.check_index(index)
return self.__z[checked_index]
score
Return the score of the sight at the given index or None.
Parameters
- index: (int) Index of the sight
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
Returns
- (float or None) The score value or None if scores are not set
View Source
def score(self, index: int) -> float:
"""Return the score of the sight at the given index or None.
:param index: (int) Index of the sight
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
:return: (float or None) The score value or None if scores are not set
"""
if self.__confidence is None:
return None
checked_index = self.check_index(index)
return self.__confidence[checked_index]
get_sight
Return the (x, y, z, s) of the given sight.
Parameters
- index: (int) Index of the sight
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
Returns
- (tuple[int, int, int, float]) The data of the sight associated with the given index
View Source
def get_sight(self, index: int) -> tuple:
"""Return the (x, y, z, s) of the given sight.
:param index: (int) Index of the sight
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
:return: (tuple[int, int, int, float]) The data of the sight associated with the given index
"""
checked_index = self.check_index(index)
return (self.__x[checked_index], self.__y[checked_index], self.z(checked_index), self.score(checked_index))
set_sight
Set the sight at the given index.
Parameters
- index: (int) Index of the sight
- x: (int) pixel position on the x-axis (width)
- y: (int) pixel position on the y-axis (height)
- z: (int or None) pixel position on the z axis
- score: (float or None) An optional confidence score
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
View Source
def set_sight(self, index: int, x: int, y: int, z=None, score=None):
"""Set the sight at the given index.
:param index: (int) Index of the sight
:param x: (int) pixel position on the x-axis (width)
:param y: (int) pixel position on the y-axis (height)
:param z: (int or None) pixel position on the z axis
:param score: (float or None) An optional confidence score
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
"""
checked_index = self.check_index(index)
checked_x = sppasCoords.to_dtype(x, int, unsigned=True)
checked_y = sppasCoords.to_dtype(y, int, unsigned=True)
self.__x[checked_index] = checked_x
self.__y[checked_index] = checked_y
self.set_sight_z(checked_index, z)
self.set_sight_score(checked_index, score)
set_sight_z
Set a z value to the sight at the given index.
Parameters
- index: (int) Index of the sight
- z: (int or None) An optional z value
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
View Source
def set_sight_z(self, index: int, z: int):
"""Set a z value to the sight at the given index.
:param index: (int) Index of the sight
:param z: (int or None) An optional z value
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
"""
checked_index = self.check_index(index)
if z is None:
if self.__z is not None:
self.__z[checked_index] = None
else:
checked_z = sppasCoords.to_dtype(z, int, unsigned=False)
if self.__z is None:
self.__z = [None] * self.__nb
self.__z[checked_index] = checked_z
set_sight_score
Set a score to the sight at the given index.
Parameters
- index: (int) Index of the sight
- score: (float or None) An optional confidence score
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
View Source
def set_sight_score(self, index: int, score: float):
"""Set a score to the sight at the given index.
:param index: (int) Index of the sight
:param score: (float or None) An optional confidence score
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
"""
checked_index = self.check_index(index)
if score is None:
if self.__confidence is not None:
self.__confidence[checked_index] = None
else:
checked_sight = sppasCoords.to_dtype(score, float, unsigned=False)
if self.__confidence is None:
self.__confidence = [None] * self.__nb
self.__confidence[checked_index] = checked_sight
reset
Reset all values to their default.
View Source
def reset(self):
"""Reset all values to their default."""
self.__x = [0] * self.__nb
self.__y = [0] * self.__nb
self.__z = None
self.__confidence = None
copy
Return a deep copy of the current sppasSights instance.
Returns
- (sppasSights) The copy of the instance
View Source
def copy(self):
"""Return a deep copy of the current sppasSights instance.
:return: (sppasSights) The copy of the instance
"""
copied = sppasSights(nb=self.__nb)
for i in range(self.__nb):
x, y, z, s = self.get_sight(i)
copied.set_sight(i, x, y, z, s)
return copied
check_index
Raise an exception if the given index is not valid.
Parameters
- value: (int) The index to check
Raises
- sppasTypeError: If the index is not an integer
- NegativeValueError: If the index is negative (Impossible for an index !)
- IndexRangeException: If the index is superior of the number of sights
Returns
View Source
def check_index(self, value: int):
"""Raise an exception if the given index is not valid.
:param value: (int) The index to check
:raises: sppasTypeError: If the index is not an integer
:raises: NegativeValueError: If the index is negative (Impossible for an index !)
:raises: IndexRangeException: If the index is superior of the number of sights
:return: (int) The value given
"""
if not isinstance(value, int):
raise sppasTypeError(value, 'int')
if value < 0:
raise NegativeValueError(value)
if self.__nb < value:
raise IndexRangeException(value, 0, self.__nb)
return value
get_mean_score
Return the mean score or None.
Returns
- (float or None) The mean score or None if no score is set
View Source
def get_mean_score(self):
"""Return the mean score or None.
:return: (float or None) The mean score or None if no score is set
"""
if self.__confidence is None:
return None
values = [v for v in self.__confidence if v is not None]
if len(values) == 0:
return None
return sum(values) / len(values)
intermediate
Return the sights with the intermediate positions.
Parameters
- other: (sppasSights) The other instance of sights
Returns
- (sppasSights) The computed intermediate sights
View Source
def intermediate(self, other):
"""Return the sights with the intermediate positions.
:param other: (sppasSights) The other instance of sights
:return: (sppasSights) The computed intermediate sights
"""
if isinstance(other, sppasSights) is False:
raise sppasTypeError(other, 'sppasSights')
if len(other) != self.__nb:
raise sppasValueError(self.__nb, len(other))
intermediate_sights = sppasSights(self.__nb)
i = 0
for s1, s2 in zip(self, other):
intermediate_x = s1[0] + (s2[0] - s1[0]) // 2
intermediate_y = s1[1] + (s2[1] - s1[1]) // 2
intermediate_z = None
if s1[2] is not None and s2[2] is not None:
intermediate_z = s1[2] + (s2[2] - s1[2]) // 2
intermediate_score = None
if s1[3] is not None and s2[3] is not None:
intermediate_score = (s1[3] + s2[3]) / 2.0
intermediate_sights.set_sight(i, intermediate_x, intermediate_y, intermediate_z, intermediate_score)
i += 1
return intermediate_sights
center
Return the center of the sights.
View Source
def center(self) -> tuple:
"""Return the center of the sights."""
x_min = min(self.__x)
x_max = max(self.__x)
y_min = min(self.__y)
y_max = max(self.__y)
x_center = x_min + (x_max - x_min) // 2
y_center = y_min + (y_max - y_min) // 2
if self.__z is not None:
z_min = min(self.__z)
z_max = max(self.__z)
z_center = z_min + (z_max - z_min) // 2
return (x_center, y_center, z_center)
else:
return (x_center, y_center)
scale
Apply a scaling transformation with respect to a given center.
The scaling operation enlarges or shrinks the distance between the
point and the center by a given factor, without affecting the center
itself. The transformation is applied using the following formula:
x' = xc + factor * (x - xc)
y' = yc + factor * (y - yc)
z' = zc + factor * (z - zc)
where:
- (x, y, z) are the original coordinates of the point,
- (xc, yc, z_c) are the coordinates of the center,
- factor is the scaling factor.
This ensures that:
- If the point is to the left of the center (x < xc), it moves further left when factor > 1.
- If the point is to the right of the center (x > xc), it moves further right when factor > 1.
- The same logic applies to y and z coordinates, ensuring consistent scaling in all directions.
Parameters
- factor: (float) Scaling factor
- center: (int) Index of the center point
Raises
- sppasTypeError: If factor or center is not a valid number
- IndexRangeException: If center index is out of range
View Source
def scale(self, center: int, factor: float=2.0):
"""Apply a scaling transformation with respect to a given center.
The scaling operation enlarges or shrinks the distance between the
point and the center by a given factor, without affecting the center
itself. The transformation is applied using the following formula:
x' = x_c + factor * (x - x_c)
y' = y_c + factor * (y - y_c)
z' = z_c + factor * (z - z_c)
where:
- (x, y, z) are the original coordinates of the point,
- (x_c, y_c, z_c) are the coordinates of the center,
- factor is the scaling factor.
This ensures that:
- If the point is to the left of the center (x < x_c), it moves further left when factor > 1.
- If the point is to the right of the center (x > x_c), it moves further right when factor > 1.
- The same logic applies to y and z coordinates, ensuring consistent scaling in all directions.
:param factor: (float) Scaling factor
:param center: (int) Index of the center point
:raises: sppasTypeError: If factor or center is not a valid number
:raises: IndexRangeException: If center index is out of range
"""
center_x = self.x(center)
center_y = self.y(center)
center_z = self.z(center) if self.__z is not None else None
for i in range(self.__nb):
if self.__x[i] < center_x:
self.__x[i] = center_x - int(factor * (center_x - self.__x[i]))
elif self.__x[i] > center_x:
self.__x[i] = center_x + int(factor * (self.__x[i] - center_x))
if self.__y[i] < center_y:
self.__y[i] = center_y - int(factor * (center_y - self.__y[i]))
elif self.__y[i] > center_y:
self.__y[i] = center_y + int(factor * (self.__y[i] - center_y))
if self.__z is not None and center_z is not None:
if self.__z[i] < center_z:
self.__z[i] = center_z - int(factor * (center_z - self.__z[i]))
elif self.__z[i] > center_z:
self.__z[i] = center_z + int(factor * (self.__z[i] - center_z))