AnalyseMorphologique/utils/math/utils.py

95 lines
2.9 KiB
Python

"""
This module contains some utility functions for math operations.
"""
import numpy as np
def get_mean(values:list):
"""
Get the mean of the values.
"""
return np.mean(values)
def get_standard_deviation(values:list):
"""
Get the standard deviation of the values.
:param values: values
:return: standard deviation of the values
"""
return np.std(values)
def get_x_y_z_mean(discrete_values:list):
"""
Get the mean of the x and y coordinates in the discrete range.
:param x: x coordinates
:param y: y coordinates
:return: mean of x and y coordinates in the discrete range
"""
x = [vertex[0] for vertex in discrete_values]
y = [vertex[1] for vertex in discrete_values]
z = [vertex[2] for vertex in discrete_values]
return get_mean(x), get_mean(y), get_mean(z)
def get_radius_from_x_y(xi:float, yi:float, x_mean:float, y_mean:float):
"""
Get the radius from the x and y coordinates.
:param xi: x coordinate
:param yi: y coordinate
:param x_mean: mean of x coordinates in the discrete range
:param y_mean: mean of y coordinates in the discrete range
:return: radius for this point
"""
return np.sqrt((xi - x_mean) ** 2 + (yi - y_mean) ** 2)
def get_mean_radius(discrete_values:list):
"""
Get the mean of the radius in the discrete range.
:param discrete_values: discrete values
:return: mean of the radius in the discrete range
"""
x_mean, y_mean, z_mean = get_x_y_z_mean(discrete_values)
radius = []
for x,y,z in discrete_values:
radius.append(get_radius_from_x_y(x,y,x_mean,y_mean))
return get_mean(radius)
def get_radius_std(discrete_values:list):
"""
Get the standard deviation of the radius in the discrete range.
:param discrete_values: discrete values
:return: standard deviation of the radius in the discrete range
"""
x_mean, y_mean, z_mean = get_x_y_z_mean(discrete_values)
radius = []
for x,y,z in discrete_values:
radius.append(get_radius_from_x_y(x,y,x_mean,y_mean))
return get_standard_deviation(radius)
def get_mean_teta(discrete_values:list):
"""
Get the mean of the teta in the discrete range.
:param discrete_values: discrete values
:return: mean of the teta in the discrete range
"""
x_mean, y_mean, z_mean = get_x_y_z_mean(discrete_values)
teta = []
for x,y,z in discrete_values:
teta.append(get_teta_from_x_y(x,y,x_mean,y_mean))
return get_mean(teta)
def get_teta_from_x_y(xi:float, yi:float, x_mean:float, y_mean:float):
"""
Get the teta from the x and y coordinates.
:param xi: x coordinate
:param yi: y coordinate
:param x_mean: mean of x coordinates in the discrete range
:param y_mean: mean of y coordinates in the discrete range
:return: teta for this point
"""
return np.arctan2((xi - x_mean),(yi - y_mean))