Package sc.fiji.snt.analysis.sholl.math
Class LinearProfileStats
java.lang.Object
org.scijava.AbstractContextual
org.scijava.command.ContextCommand
sc.fiji.snt.analysis.sholl.math.LinearProfileStats
- All Implemented Interfaces:
Runnable,org.scijava.Cancelable,org.scijava.command.Command,org.scijava.Contextual,org.scijava.plugin.SciJavaPlugin,ShollStats
Retrieves descriptive statistics and calculates Sholl Metrics from sampled
Sholl profiles, including those relying on polynomial fitting. Fitting to
polynomials of arbitrary degree is supported. Relies heavily on the
org.apache.commons.math3 package.- Author:
- Tiago Ferreira
-
Nested Class Summary
Nested classes/interfaces inherited from interface sc.fiji.snt.analysis.sholl.math.ShollStats
ShollStats.DataMode -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected double[]protected final double[]protected final double[]protected Loggerprotected intprotected ShollPlotprotected final Profileprotected static final doubleFields inherited from interface sc.fiji.snt.analysis.sholl.math.ShollStats
ANNULUS, AREA, GUESS_SLOG, LOG_LOG, PERIMETER, S_SHELL, SEMI_LOG, SURFACE, VOLUME -
Constructor Summary
ConstructorsConstructorDescriptionLinearProfileStats(Profile profile) Instantiates the Linear Profile Statistics.LinearProfileStats(Profile profile, ShollStats.DataMode dataMode) Instantiates the Linear Profile Statistics. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidintfindBestFit(int fromDegree, int toDegree, double minRSquared, double pvalue) Computes the 'best fit' polynomial between a specified range of degrees for this profile and keeps the fit in memory.intfindBestFit(int fromDegree, int toDegree, org.scijava.prefs.PrefService prefService) RunsfindBestFit(int, int, double, double)using the preferences specified by the user using theShollAnalysisPrefsCmdcommand.voidfitPolynomial(int degree) Fits sampled data to a polynomial function and keeps the fit in memory.protected doublegetAdjustedRSquaredOfFit(int p) doublegetBranchingIndex(boolean fittedData) Calculates the branching index (BI) as defined in PMID 24503022(doi: 10.1016/j.jneumeth.2014.01.016).getCenteredMaximum(boolean fittedData) Returns the average coordinates of all maxima.getCentroid(boolean fittedData) Retrieves the centroid from all pairs of data (radius, inters. counts).doublegetEnclosingRadius(boolean fittedData, double cutoff) Returns the largest radius associated with at least the number of specified counts.doublegetEnclosingRadius(double cutoff) double[]Returns the ordinates of the sampled linear plot of fitted data.double[]getFitYValues(boolean asCumulativeFrequencies) protected intgetIndex(double[] array, double value) intgetIndexOfInters(boolean fittedData, double inters) Returns the closest index of the intersections data associated with the specified valueintgetIndexOfRadius(double radius) Returns the closest index of sampled distances associated with the specified valueintintgetIntersectingRadii(boolean fittedData) Returns the number of intersecting radii.doubleReturns the two-sample Kolmogorov-Smirnov (K-S) test between the polynomial fit and sampled intersections as a measurement of goodness of fit.doubledoublegetKurtosis(boolean fittedData) Calculates the kurtosis.doublegetMax()doublegetMax(boolean fittedData) Returns the largest value of intersection count.intGets the function evaluation limit for solversgetMaxima(boolean fittedData) Returns a list of all the points in the linear Sholl profile associated with the highest intersections countdoublegetMean()doublegetMean(boolean fittedData) Calculates the arithmetic mean.doublegetMeanValueOfPolynomialFit(double lowerBound, double upperBound) Calculates the mean value of polynomial fit using the default integration method (Simpson's).doublegetMeanValueOfPolynomialFit(String integrator, double lowerBound, double upperBound) Gets the mean value of polynomial fit.doubledoublegetMedian(boolean fittedData) Calculates the median.doublegetMin()doublegetMin(boolean fittedData) Returns the lowest value of intersection count.intgetN()getPlot(boolean cumulativeFrequencies) getPolygonCentroid(boolean fittedData) Calculates the centroid from all (radius, inters. counts) pairs assuming such points define a non-self-intersecting closed polygon.org.apache.commons.math3.analysis.polynomials.PolynomialFunctionGets the polynomial function.getPolynomialAsString(boolean detailed) Returns a string describing the polynomial fitintReturns the degree of the polynomial.getPolynomialMaxima(double lowerBound, double upperBound, double initialGuess) Calculates local maxima (critical points at which the derivative of the polynomial is zero) within the specified interval.doubledoublegetPrimaryBranches(boolean fittedData) Returns intersection counts at the smallest radius (inferred no. of primary branches), or the number of the actual number of primary branches specified bysetPrimaryBranches(int).protected ProfiledoubledoublegetRamificationIndex(boolean fittedData) Calculates the ramification index (the highest intersections count divided by the n. of primary branches).doubledoublegetRSquaredOfFit(boolean adjusted) Returns RSquared of the polynomial fit.doubledoublegetSkewness(boolean fittedData) Calculates the skewness.doublegetSum()doublegetSum(boolean fittedData) Calculates the sum.doublegetSumSq()doublegetSumSq(boolean fittedData) Calculates the sum of the squared values.doubledoublegetVariance(boolean fittedData) Calculates the variance.double[]Returns the abscissae of the sampled linear plot for sampled data.double[]Returns the ordinates of the sampled linear plot of sampled data.double[]getYValues(boolean asCumulativeFrequencies) booleanChecks whether the number of primary branches is being inferred from the number of intersections at starting radius or if it has been set explicitly.plot()voidrun()voidsetContext(org.scijava.Context context) voidvoidsetDebug(boolean debug) voidvoidvoidsetMaxEvaluations(int maxEval) Sets the function evaluation limit for solvers.voidsetPrimaryBranches(int nBranches) Sets the number of primary branches associated with this profile, used to calculate ramification indices.protected voidbooleanvalidFit()Checks if valid fitted data exists.Methods inherited from class org.scijava.command.ContextCommand
cancel, getCancelReason, isCanceledMethods inherited from class org.scijava.AbstractContextual
context, getContextMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface sc.fiji.snt.analysis.sholl.math.ShollStats
getDataMode, getFitYValues, getN, getProfile, getYValues, setDataMode
-
Field Details
-
UNASSIGNED_VALUE
protected static final double UNASSIGNED_VALUE- See Also:
-
inputRadii
protected final double[] inputRadii -
inputCounts
protected final double[] inputCounts -
profile
-
nPoints
protected int nPoints -
fCounts
protected double[] fCounts -
plot
-
logger
-
-
Constructor Details
-
LinearProfileStats
Instantiates the Linear Profile Statistics.- Parameters:
profile- the profile to be analyzed
-
LinearProfileStats
Instantiates the Linear Profile Statistics.- Parameters:
profile- the profile to be analyzed
-
-
Method Details
-
getCentroid
Retrieves the centroid from all pairs of data (radius, inters. counts).- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the centroid {x,y} coordinates
-
getCentroid
- Returns:
getCentroid(false)
-
getPolygonCentroid
Calculates the centroid from all (radius, inters. counts) pairs assuming such points define a non-self-intersecting closed polygon. Implementation from wikipedia.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the point by the centroid {x,y} coordinates
-
getPolygonCentroid
- Returns:
getPolygonCentroid(false)
-
getEnclosingRadius
public double getEnclosingRadius(boolean fittedData, double cutoff) Returns the largest radius associated with at least the number of specified counts.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled datacutoff- the cutoff for intersection counts- Returns:
- the largest radius associated with the same or more cutoff counts
-
getEnclosingRadius
public double getEnclosingRadius(double cutoff) - Parameters:
cutoff- the cutoff for intersection counts- Returns:
getEnclosingRadius(false, cutoff)
-
getIntersectingRadii
public int getIntersectingRadii(boolean fittedData) Returns the number of intersecting radii.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the count of all radii associated with at least one intersection
-
getIntersectingRadii
public int getIntersectingRadii()- Returns:
getIntersectingRadii(false)
-
getKurtosis
public double getKurtosis(boolean fittedData) Calculates the kurtosis.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- kurtosis of intersection counts
-
getKurtosis
public double getKurtosis()- Returns:
getKurtosis(false)
-
getMaxima
Returns a list of all the points in the linear Sholl profile associated with the highest intersections count- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the list of points of all maxima
-
getMaxima
- Returns:
getMaxima(false)
-
getCenteredMaximum
Returns the average coordinates of all maxima.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the averaged x,y coordinates of maxima
-
getCenteredMaximum
- Returns:
getCenteredMaximum(false)
-
getMean
public double getMean(boolean fittedData) Calculates the arithmetic mean.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the mean of intersection counts
-
getMean
public double getMean()- Returns:
getMean(false)
-
getIndexOfRadius
public int getIndexOfRadius(double radius) Returns the closest index of sampled distances associated with the specified value- Parameters:
radius- the query value- Returns:
- the position index (zero-based) or -1 if no index could be calculated
-
getIndexOfInters
public int getIndexOfInters(boolean fittedData, double inters) Returns the closest index of the intersections data associated with the specified value- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled datainters- the query value- Returns:
- the position index (zero-based) or -1 if no index could be calculated
-
fitPolynomial
public void fitPolynomial(int degree) Fits sampled data to a polynomial function and keeps the fit in memory.- Parameters:
degree- Degree of the polynomial to be fitted- Throws:
org.apache.commons.math3.exception.NullArgumentException- if the computed polynomial coefficients were nullorg.apache.commons.math3.exception.NoDataException- if the computed polynomial coefficients were emptyorg.apache.commons.math3.exception.ConvergenceException- if optimization failed
-
findBestFit
public int findBestFit(int fromDegree, int toDegree, double minRSquared, double pvalue) Computes the 'best fit' polynomial between a specified range of degrees for this profile and keeps the fit in memory.Note that in some edge cases specified constrains may be bypassed: E.g., If the profile is constant, a constant function is fitted. If the profile contains only two data points, a linear function is fitted
- Parameters:
fromDegree- the lowest degree to be considered. Will be set to (ShollStats.getN()-1) if higher than (ShollStats.getN()-1).toDegree- the highest degree to be considered. Will be set to (ShollStats.getN()-1) if higher than (ShollStats.getN()-1)minRSquared- the lowest value for adjusted RSquared. Only fits associated with an equal or higher value will be consideredpvalue- the two-sample Kolmogorov-Smirnov (K-S) test statistic (p-value) used to discard 'unsuitable fits'. It is used to evaluate the null hypothesis that profiled data and polynomial fit represent samples drawn from the same probability distribution. Set it to -1 to skip K-S testing.- Returns:
- the degree of the 'best fit' polynomial or -1 if no suitable fit could be performed.
-
findBestFit
public int findBestFit(int fromDegree, int toDegree, org.scijava.prefs.PrefService prefService) RunsfindBestFit(int, int, double, double)using the preferences specified by the user using theShollAnalysisPrefsCmdcommand.- Parameters:
fromDegree- the lowest degree to be considered. SeefindBestFit(int, int, double, double)toDegree- the highest degree to be considered. SeefindBestFit(int, int, double, double)prefService- thePrefServiceused to read preferences- Returns:
- the degree of the 'best fit' polynomial. See
findBestFit(int, int, double, double)
-
getXValues
public double[] getXValues()Returns the abscissae of the sampled linear plot for sampled data.- Specified by:
getXValuesin interfaceShollStats- Returns:
- the sampled distances.
-
getYValues
public double[] getYValues()Returns the ordinates of the sampled linear plot of sampled data.- Specified by:
getYValuesin interfaceShollStats- Returns:
- the sampled intersection counts.
-
getFitYValues
public double[] getFitYValues()Returns the ordinates of the sampled linear plot of fitted data.- Specified by:
getFitYValuesin interfaceShollStats- Returns:
- the y-values of the polynomial fit retrieved at sampling distances
- Throws:
NullPointerException- iffitPolynomial(int)has not been called
-
getPolynomial
public org.apache.commons.math3.analysis.polynomials.PolynomialFunction getPolynomial()Gets the polynomial function.- Returns:
- the polynomial
- Throws:
NullPointerException- iffitPolynomial(int)has not been called
-
getPolynomialDegree
public int getPolynomialDegree()Returns the degree of the polynomial.- Returns:
- the polynomial degree
- Throws:
NullPointerException- iffitPolynomial(int)has not been called
-
getPolynomialAsString
Returns a string describing the polynomial fit- Parameters:
detailed- if true description includes extended details on the fit- Returns:
- the description, e.g., "8th degree", or an empty string if no valid fit exists
-
getPolynomialMaxima
public Set<ShollPoint> getPolynomialMaxima(double lowerBound, double upperBound, double initialGuess) Calculates local maxima (critical points at which the derivative of the polynomial is zero) within the specified interval.- Parameters:
lowerBound- the lower bound of the intervalupperBound- the upper bound of the intervalinitialGuess- initial guess for a solution (solver's starting point)- Returns:
- the set of Points defined by the {x,y} coordinates of maxima (sorted by descendant order)
- Throws:
org.apache.commons.math3.exception.TooManyEvaluationsException- if the maximum number of evaluations is exceeded when solving for one of the rootsNullPointerException- iffitPolynomial(int)has not been called
-
getMaxEvaluations
public int getMaxEvaluations()Gets the function evaluation limit for solvers- Returns:
- the set maximum of evaluations (1000 by default)
-
setMaxEvaluations
public void setMaxEvaluations(int maxEval) Sets the function evaluation limit for solvers.- Parameters:
maxEval- the new maximum of evaluations
-
getRSquaredOfFit
public double getRSquaredOfFit(boolean adjusted) Returns RSquared of the polynomial fit. Implementation from wikipedia. \( R^2 = 1 - (SSres/SStot) \) with \( SSres = SUM(i) (yi - fi)^2 \) \( SStot = SUM(i) (yi - yavg)^2 \)- Parameters:
adjusted- iftruereturns adjusted RSquared, i.e., adjusted for the number of terms of the polynomial model- Returns:
- RSquared, a measure for the goodness of fit
- Throws:
NullPointerException- iffitPolynomial(int)has not been called
-
getMeanValueOfPolynomialFit
Gets the mean value of polynomial fit.- Parameters:
integrator- the integration method to retrieve the integral of the polynomial fit. Either "Simpson" (the default), or "Romberg" (case-insensitive)lowerBound- the lower bound (smallest radius) for the intervalupperBound- the upper bound (largest radius) for the interval- Returns:
- the mean value of polynomial fit
- Throws:
org.apache.commons.math3.exception.MathIllegalArgumentException- if bounds do not satisfy the integrator requirementsorg.apache.commons.math3.exception.TooManyEvaluationsException- if the maximum number of function evaluations is exceeded by the integratororg.apache.commons.math3.exception.MaxCountExceededException- if the maximum iteration count is exceeded by the integratorNullPointerException- iffitPolynomial(int)has not been called
-
getMeanValueOfPolynomialFit
public double getMeanValueOfPolynomialFit(double lowerBound, double upperBound) Calculates the mean value of polynomial fit using the default integration method (Simpson's).- Parameters:
lowerBound- the lower bound (smallest radius) for the intervalupperBound- the upper bound (largest radius) for the interval- Returns:
- the mean value of polynomial fit, or
Double.NaNif calculation failed. - Throws:
NullPointerException- iffitPolynomial(int)has not been called
-
getMedian
public double getMedian(boolean fittedData) Calculates the median.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the median of intersection counts
-
getMedian
public double getMedian()- Returns:
getMedian(false)
-
setPrimaryBranches
public void setPrimaryBranches(int nBranches) Sets the number of primary branches associated with this profile, used to calculate ramification indices. When the number is set to -1 (the default), the number of primary branches is inferred from the number of intersections at starting radius.- Parameters:
nBranches- the new primary branches- See Also:
-
isPrimaryBranchesInferred
public boolean isPrimaryBranchesInferred()Checks whether the number of primary branches is being inferred from the number of intersections at starting radius or if it has been set explicitly.- Returns:
- true, if number of primary branches is being inferred
- See Also:
-
getPrimaryBranches
public double getPrimaryBranches(boolean fittedData) Returns intersection counts at the smallest radius (inferred no. of primary branches), or the number of the actual number of primary branches specified bysetPrimaryBranches(int).- Parameters:
fittedData- Iftrue, andsetPrimaryBranches(int)has not been called, calculation is performed on polynomial fitted values, otherwise from sampled data. It is ignored if the number of primary branches has been successfully specified by callingsetPrimaryBranches(int)- Returns:
- the number of primary branches
- See Also:
-
getPrimaryBranches
public double getPrimaryBranches()- Returns:
getPrimaryBranches(false)- See Also:
-
getRamificationIndex
public double getRamificationIndex(boolean fittedData) Calculates the ramification index (the highest intersections count divided by the n. of primary branches). If the number of primary branches has not been specified, it is assumed to be the n. intersections at starting radius.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the ramification index
- See Also:
-
getBranchingIndex
public double getBranchingIndex(boolean fittedData) Calculates the branching index (BI) as defined in PMID 24503022(doi: 10.1016/j.jneumeth.2014.01.016). Note that this index is very sensitive to radius step size.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the branching index
- See Also:
-
getRamificationIndex
public double getRamificationIndex()- Returns:
getRamificationIndex(false)
-
getMax
public double getMax(boolean fittedData) Returns the largest value of intersection count.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the largest value of intersection counts
-
getMax
public double getMax()- Returns:
getMax(false)
-
getMin
public double getMin(boolean fittedData) Returns the lowest value of intersection count.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the lowest value of intersection counts
-
getMin
public double getMin()- Returns:
getMin(false)
-
getSkewness
public double getSkewness(boolean fittedData) Calculates the skewness.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the skewness of intersection counts
-
getSkewness
public double getSkewness()- Returns:
getSkewness(false)
-
getSum
public double getSum(boolean fittedData) Calculates the sum.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the sum of intersection counts
-
getSum
public double getSum()- Returns:
getSum(false)
-
getSumSq
public double getSumSq(boolean fittedData) Calculates the sum of the squared values.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the sum of the squared values of intersection counts
-
getSumSq
public double getSumSq()- Returns:
getSumSq(false)
-
getVariance
public double getVariance(boolean fittedData) Calculates the variance.- Parameters:
fittedData- Iftrue, calculation is performed on polynomial fitted values, otherwise from sampled data- Returns:
- the variance of intersection counts
-
plot
-
getProfileCopy
-
getVariance
public double getVariance()- Returns:
getVariance(false)
-
validFit
public boolean validFit()Checks if valid fitted data exists.- Specified by:
validFitin interfaceShollStats- Returns:
trueif polynomial fitted data exists
-
getKStestOfFit
public double getKStestOfFit()Returns the two-sample Kolmogorov-Smirnov (K-S) test between the polynomial fit and sampled intersections as a measurement of goodness of fit.- Returns:
- the test statistic (p-value) used to evaluate the null hypothesis that sampled data and polynomial fit represent samples drawn from the same probability distribution
- Throws:
NullPointerException- if curve fitting has not been performedorg.apache.commons.math3.exception.InsufficientDataException- if sampled data contains fewer than two data points
-
getRSquaredOfFit
public double getRSquaredOfFit() -
getPlot
-
getAdjustedRSquaredOfFit
protected double getAdjustedRSquaredOfFit(int p) -
getIndex
protected int getIndex(double[] array, double value) -
validateFit
protected void validateFit() -
debug
-
setLogger
-
setLogger
-
setDebug
public void setDebug(boolean debug) -
setContext
public void setContext(org.scijava.Context context) - Specified by:
setContextin interfaceorg.scijava.Contextual
-
getYValues
public double[] getYValues(boolean asCumulativeFrequencies) - Specified by:
getYValuesin interfaceShollStats
-
getN
public int getN()- Specified by:
getNin interfaceShollStats
-
getFitYValues
public double[] getFitYValues(boolean asCumulativeFrequencies) - Specified by:
getFitYValuesin interfaceShollStats
-
getProfile
- Specified by:
getProfilein interfaceShollStats
-
getDataMode
- Specified by:
getDataModein interfaceShollStats
-
setDataMode
- Specified by:
setDataModein interfaceShollStats
-
run
public void run()
-