Author: tlinnet Date: Fri May 9 18:57:38 2014 New Revision: 23140 URL: http://svn.gna.org/viewcvs/relax?rev=23140&view=rev Log: Added mathematics functions to lib/mathematics.py to calculate percentile. This is because percentile is only available in numpy 1.9. task #7792: (https://gna.org/task/?7792) Make the dx.map write suggest chi surface values. Modified: trunk/lib/mathematics.py Modified: trunk/lib/mathematics.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/mathematics.py?rev=23140&r1=23139&r2=23140&view=diff ============================================================================== --- trunk/lib/mathematics.py (original) +++ trunk/lib/mathematics.py Fri May 9 18:57:38 2014 @@ -23,7 +23,7 @@ """Module for basic mathematical operations.""" # Python module imports. -from math import ceil, log10 +from math import ceil, floor, log10 def order_of_magnitude(value): @@ -56,3 +56,30 @@ # Calculate and return the value. return 10**(order_of_magnitude(value)) + + +def percentile(N, percent, key=lambda x:x): + """ + Find the percentile of a list of values. + + @parameter N: Array of values. + @type N: numpy float array + @parameter percent: Float value from 0.0 to 1.0. + @type percent: float + @parameter key: Optional key function to compute value from each element of N. + @type key: lambda function + + @return: The percentile of the values + """ + + # Sort N. + N.sort() + + k = (len(N)-1) * percent + f = floor(k) + c = ceil(k) + if f == c: + return key(N[int(k)]) + d0 = key(N[int(f)]) * (c-k) + d1 = key(N[int(c)]) * (k-f) + return d0+d1