Math & Stats Utilities

This class defines math and stats utility methods. Defined in src/utils/EllipseUtils.h, src/imgproc/Graph.h, src/imgproc/ZernikeMoments.h, src/utils/MathUtils.h and src/utils/StatsUtils.h

class EllipseUtils : public TObject

Public Functions

EllipseUtils()

Class constructor: initialize structures.

virtual ~EllipseUtils()

Class destructor: free allocated memory.

Public Static Functions

static double ellipse_ellipse_overlap(double PHI_1, double A1, double B1, double H1, double K1, double PHI_2, double A2, double B2, double H2, double K2, double X[4], double Y[4], int *NROOTS, int *rtnCode, int choice)

Compute ellipse overlap.

Compute overlap area polygon

class MathUtils : public TObject

Public Functions

MathUtils()

Class constructor: initialize structures.

virtual ~MathUtils()

Class destructor: free allocated memory.

Public Static Functions

static int Compute2DGrid(std::vector<long int> &ix_min, std::vector<long int> &ix_max, std::vector<long int> &iy_min, std::vector<long int> &iy_max, long int Nx, long int Ny, long int boxSizeX, long int boxSizeY, float gridStepSizeX, float gridStepSizeY)

Return 2D grid partition given Nx x Ny pixels and box sizes.

static int Compute2DFloatGrid(std::vector<float> &ix_min, std::vector<float> &ix_max, std::vector<float> &iy_min, std::vector<float> &iy_max, float xmin, float xmax, float xstep, float ymin, float ymax, float ystep)

Return 2D grid partition given x & y range and step sizes.

static int FindGrid2DAxisBin(float x, long int nx, float xmin, float xmax, float xstep)

Return 2D grid axis bin given grid parameters.

static long int FindGrid2DBin(float x, float y, long int nx, float xmin, float xmax, float xstep, long int ny, float ymin, float ymax, float ystep)

Return 2D grid global bin given grid parameters.

static int BiLinearInterpolation(std::vector<double> const &sampled_gridX, std::vector<double> const &sampled_gridY, std::vector<double> const &sampledZ, std::vector<double> const &interp_gridX, std::vector<double> const &interp_gridY, std::vector<double> &interpZ)

Perform bilinear interpolation on regular grid.

static cv::Mat GetConvolution(cv::Mat I, cv::Mat kernel)

Get opencv mat convolutions.

static cv::Mat GetConvolution2(cv::Mat I, cv::Mat kernel)

Get convolution (2nd version)

static cv::Mat GetATrousConvolution(cv::Mat I, cv::Mat kernel, int scale)

Get atrous convolution.

static int GetMirrorIndex(int index, int N)

Get mirror index.

static inline double GetMatrixTrace(TMatrixD *T)

Compute matrix trace.

static std::vector<std::complex<double>> DFTShifted(std::vector<std::complex<double>> data, int n)

Compute DFT shifted.

static std::vector<std::complex<double>> DFT(std::vector<std::complex<double>> data, int n)

Compute DFT.

static std::vector<std::complex<double>> IDFT(std::vector<std::complex<double>> data, int n)

Compute IDFT.

static inline int EtaAuxiliaryFcn(int s, int N)

Eta function definition.

static std::vector<double> GetContourCurvature(std::vector<std::complex<double>> data, double sigma)

Compute contout curvature.

static inline long int factorial(int n)

Compute factorial.

static inline double Compute2DGausIntegral(double A, double sigmaX, double sigmaY)

Compute 2d gaussian integral.

static inline double Compute2DGausEllipseIntegral(double A, double Bmaj, double Bmin)

Compute 2d gaussian ellipse integral.

static inline double ComputeEllipseArea(TEllipse *ellipse)

Compute ellipse area from ROOT TEllipse.

static inline double ComputeEllipseArea(double bmaj, double bmin)

Compute ellipse area.

static int ComputeEllipseOverlapArea(double &overlapArea, double &err, int &rtn, TEllipse *ellipse1, TEllipse *ellipse2, int method = 1, Contour *overlapContour = 0)

Compute overlap area between two ellipses.

static double ComputePolygonArea(polygon_2d &poly)

Compute polygon area.

static int ComputeContourArea(double &area, Contour *contour)

Compute contour area.

static int ComputeContourOverlapArea(double &overlapArea, int &overlapFlag, Contour *contour, Contour *contour2, Contour *overlapContour = 0)

Compute contour overlap area.

static int ComputePolygonOverlapArea(double &overlapArea, polygon_2d &overlap_poly, polygon_2d &poly, polygon_2d &poly2)

Compute overlap area between two polygons (in BOOST format)

static int Contour2Polygon(polygon_2d &poly, Contour *contour)

Convert contour to polygon (in BOOST format)

static int Ellipse2Polygon(polygon_2d &poly, double Cx, double Cy, double R1, double R2, double theta, int n = 20)

Convert an ellipse to polygon (in BOOST format)

static int Ellipse2Polygon(polygon_2d &poly, TEllipse *ellipse, int n)

Convert a TEllipse to polygon (in BOOST format)

static double ComputeEllipseEccentricity(TEllipse *ellipse)

Compute ellipse eccentricity from ROOT TEllipse.

static double ComputeEllipseEccentricity(double bmaj, double bmin)

Compute ellipse eccentricity from (bmaj, bmin)

template<typename T>
static inline T Mod(T x, T y)

Modulus operator.

static inline double GetAngleInRange(double theta, double theta_limit)

Limit angle in range [-theta_limit,theta_limit].

static inline double EvalGaus2D(double X, double Y, double A, double X0, double Y0, double sigmaX, double sigmaY, double theta)

Evaluate 2d elliptical gaussian at given (x,y) (NB: theta in radians)

static bool IsPointInsidePolygon(double x, double y, const std::vector<TVector2> &polygon)

Check if 2D point with coordinates (x,y) is inside a polygon.

static inline bool IsPointInsidePolygon(const TVector2 &point, const std::vector<TVector2> &polygon)

Check if 2D point with coordinates (x,y) is inside a polygon.

static bool IsPointInsideSegment(TVector2 point, TVector2 a, TVector2 b, double epsilon = 1.e-12)

Check if 2D point is lying on segment a-b including vertex.

static bool IsPointOnPolygonBoundary(const TVector2 &point, const std::vector<TVector2> &polygon)

Check if 2D point is lying on polygon boundary, including vertices.

static inline bool IsPointOnPolygonBoundary(double x, double y, const std::vector<TVector2> &polygon)

Check if 2D point is lying on polygon boundary, including vertices.

static void ComputeRotatedCoords(double &xrot, double &yrot, double x, double y, double cx, double cy, double theta)

Check if 2D point with coordinates (x,y) is inside a polygon (version 2)

Check if 2D point with coordinates (x,y) is inside a polygon (version 2) Check if a point lies inside, on or outside any polygon.

Winding number algorithm can be used to check if any point lies inside a polygon. A more detailed explanation can be found in the blog post. The link is attached at the top of the file.

Parameters
  • query_point – Point to check.

  • vertices – Vertices making up the polygon in anticlockwise direction.

  • pt1 – First point to form equation of line.

  • pt2 – Second point to form equation of line.

  • query_point – Query point

Returns

= 1: query_point lies inside the polygon. = 0: query_point lies on the polygon. =-1: query_point lies outside the polygon. The result can be used to test if the query point lies on the left or right side of the line formed by pt1 and pt2 when viewed in anticlockwise direction.

Returns

: > 0: Query point lies on left of the line. = 0: Query point lies on the line. < 0: Query point lies on right of the line. Compute rotated coordinates (x,y)

static inline double GetEuclideanDist(double x1, double y1, double x2, double y2)

Compute Euclidean distance between two points.

static double SynchrotronSelfAbsSED(double *x, double *pars)

SynchrotronSelfAbsSED model.

static double SynchrotronExtFreeFreeAbsSED(double *x, double *pars)

SynchrotronExtFreeFreeAbsSED model.

static double SynchrotronIntFreeFreeAbsSED(double *x, double *pars)

SynchrotronIntFreeFreeAbsSED model.

static double FreeFreeSED(double *x, double *pars)

FreeFreeSED model.

class StatsUtils : public TObject

Public Functions

StatsUtils()

Class constructor: initialize structures.

virtual ~StatsUtils()

Class destructor: free allocated memory.

Public Static Functions

template<typename T>
static inline BoxStats<T> ComputeBoxStats(std::vector<T> &vec, bool sorted = false)

Compute box plot stats (min/max, median, Q1, Q3)

template<typename T>
static inline T GetMedianFast(std::vector<T> &vec, bool useParallelVersion = false, bool alreadySorted = false)

Compute median & 1st & 3rd quantiles.

Compute median using nth_element (should run in O(n)) Compute median using nth_element (should run in O(n))

template<typename T>
static inline T GetMADFast(std::vector<T> const &vec, T median, bool useParallelVersion = false)

Compute MAD using nth_element (should run in O(n))

template<typename T>
static inline T GetMedian(std::vector<T> &vec, bool isSorted = false)

Compute median using vector sorting (should run in O(nlog n))

template<typename T>
static inline T GetRangeMedian(std::vector<T> &vec, int start, int end, bool isSorted = false)

Compute median using vector sorting (should run in O(nlog n))

template<typename T>
static inline T GetMAD(std::vector<T> const &vec, T median)

Compute MAD using vector sorting (should run in O(nlog n))

template<typename T>
static inline void ComputeMeanAndRMS(T &mean, T &stddev, std::vector<T> const &vec)

Compute mean & std dev.

template<typename T>
static inline int UpdateClippedStats(ClippedStats<T> &stats_clipped, std::vector<T> &vec_clipped, std::vector<T> const &vec, ClippedStats<T> &stats, double clipsig = 3, bool useParallelVersion = false)

Compute clipped estimators.

template<typename T>
static inline int GetClippedEstimators(ClippedStats<T> &stats, std::vector<T> const &vec, T median, T mean, T stddev, double clipsig = 3, int maxiter = 5, double tol = 0.1, bool useParallelVersion = false)

Compute clipped stat estimators.

static double GetMahalanobisDistance(TMatrixD x, TMatrixD mean, TMatrixD Sigma, bool isInverted = false)

Compute Mahalanobis distance of input matrix.

static int ComputePageRank(std::vector<double> &ranks, TMatrixD &M, double d = 0.85, double tol = 1.e-4)

Compute page rank of input matrix.

template<typename T>
static inline T ComputeNormMean(T mean, T DataMin, T DataMax, T NormMin, T NormMax)

Compute norm mean after linear transformation from [DataMin,DataMax] to [NormMin,NormMax].

template<typename T>
static inline T ComputeNormDiff(T diff, T DataMin, T DataMax, T NormMin, T NormMax)

Compute norm difference between two values after linear transformation from [DataMin,DataMax] to [NormMin,NormMax].

template<typename T>
static inline void ComputeNormMeanAndRMS(T &mean_transf, T &rms_transf, T mean, T rms, T DataMin, T DataMax, T NormMin, T NormMax)

Get mean/rms after linear transformation from [DataMin,DataMax] to [NormMin,NormMax].

template<typename T>
static inline T GetNormValue(T x, T xmin, T xmax, T NormMin, T NormMax)

Get value normalized in a range.

template<typename T>
static inline void NormalizeVector(std::vector<T> &data, T NormMin = 0, T NormMax = 1)

Normalize a vector in a range.

static inline double GetEllipseAxisSigmaContourScaleFactor(double nsigma)

Multiply gaussian 2D ellipse axis by this factor to get the nsigma ellipse contour.

static inline double GetEllipseAxisCLContourScaleFactor(double CL)

Multiply gaussian 2D ellipse axis by this factor to get the CL ellipse contour.

static inline int GetEllipseParsFromCovMatrix(double &sigmaX_rot, double &sigmaY_rot, double &theta, double sigmaX, double sigmaY, double covXY)

Get 2D gaussian confidence ellipse.

static inline TEllipse *GetFitCLEllipse(double x0, double y0, double sigmaX, double sigmaY, double covXY, double CL)

Get 2D gaussian confidence ellipse.

static inline double GetGaus2DThetaPar(double sigmaX, double sigmaY, double covXY)

Get elliptical 2D gaussian.

static inline TEllipse *GetFitEllipse(double x0, double y0, double sigmaX, double sigmaY, double covXY, bool useFWHM = false)

Get ellipse corresponding to 2D gaussian.

template<typename T>
static inline int RemoveOutliers(std::vector<T> &data, double iqr_factor = 1.5, bool sorted = false)

Remove outliers from vector by iteratively removing data points outside from 1.5*IQR.

template<typename T>
static inline int ComputeStatsBootstrapError(std::map<std::string, T> &statsErr, const std::vector<T> &data, int nSamples = 100)

Extract a number of random samples from data vector with given sample size, with/without repetitions and uniform weights.

static int GenerateFitParsAroundCovMatrix(std::vector<std::vector<double>> &fitPars_rand, const std::vector<double> &fitPars, TMatrixD &fitCovMatrix, int nsamples = 1)

Generate fit parameters randomized around fit covariance matrix.

static int GenerateFitParsAroundCovMatrix(std::vector<std::vector<double>> &fitPars_rand, const std::vector<double> &fitPars, const std::vector<std::vector<double>> &fitCovMatrix, int nsamples = 1)

Generate fit parameters randomized around fit covariance matrix.

class Graph : public TObject

Public Functions

Graph()

Standard constructor.

Graph(size_t nvertex)

Constructor with nvertex initializer.

Graph(TMatrixD const &A)

Construct from adjacency matrix.

virtual ~Graph()

Standard destructor.

inline int GetNVertexes()

Get number of vertex.

inline void AddVertex()

Add vertex.

int AddEdge(int v, int w)

Add edge.

TMatrixD *GetAdjacencyMatrix()

Get adjacency matrix.

int GetConnectedComponents(std::vector<std::vector<int>> &connected_items)

Get connected components in the aundirected graph.

class ZernikeMoments

Public Functions

ZernikeMoments()

Class constructor.

~ZernikeMoments()

Class destructor.

Public Static Functions

static std::vector<double> GetZernike2D_Direct(Image *img, double order, double radius)

Compute zernike 2D moments with direct method.

static std::vector<double> GetZernike2D(Image *img, double order, double rad)

Compute zernike 2D moments.

static std::vector<double> GetZernike2DOld(Image *img, double D, double R)

Compute zernike 2D moments (TO BE DEPRECATED)

static std::vector<double> mb_Znl(double *X, double *Y, double *P, int size, double D, double m10_m00, double m01_m00, double R, double psum)

Define Zernike moment generating function.