Mercurial > hg > medcouple
diff pymedcouple @ 23:29a178b23219
Whitespace style fixes
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Sat, 17 Jan 2015 20:04:46 -0500 |
parents | f5b4a2ab6204 |
children | cd3094a08e03 |
line wrap: on
line diff
--- a/pymedcouple +++ b/pymedcouple @@ -5,6 +5,7 @@ from itertools import tee, izip + def partsort(L, n): """This is a partial sort of the weighted list L = [(a,w) for a in A, w in W], so that the element at position n is in the correct spot. @@ -16,9 +17,9 @@ end = len(L) while True: - pivot = random.randint(beg,end-1) + pivot = random.randint(beg, end - 1) pivotval = L[pivot][0] - L[pivot], L[end-1] = L[end-1], L[pivot] + L[pivot], L[end - 1] = L[end - 1], L[pivot] idx = beg for i in xrange(beg, end): @@ -31,22 +32,23 @@ if idx == n: return L[idx] elif idx < n: - beg = idx+1 + beg = idx + 1 else: end = idx + def wmedian(A, W): """This computes the weighted median of array A with corresponding weights W. """ - AW = zip(A,W) + AW = zip(A, W) n = len(AW) wtot = sum(W) beg = 0 - end = n-1 + end = n - 1 while True: mid = (beg + end)//2 @@ -55,7 +57,7 @@ trial = AW[mid][0] wleft = wright = 0 - for (a,w) in AW: + for (a, w) in AW: if a < trial: wleft += w else: @@ -70,6 +72,7 @@ else: return trial + def medcouple_1d(X, eps1 = 2**-52, eps2 = 2**-1022): """Calculates the medcouple robust measure of skewness. @@ -94,7 +97,7 @@ # FIXME: Figure out what to do about NaNs. n = len(X) - n2 = (n-1)//2 + n2 = (n - 1)//2 if n < 3: return 0 @@ -104,7 +107,7 @@ if n % 2 == 1: Zmed = Z[n2] else: - Zmed = (Z[n2] + Z[n2+1])/2 + Zmed = (Z[n2] + Z[n2 + 1])/2 #Check if the median is at the edges up to relative epsilon if abs(Z[0] - Zmed) < eps1*(eps1 + abs(Zmed)): @@ -129,6 +132,7 @@ n_plus = len(Zplus) n_minus = len(Zminus) + def h_kern(i, j): """Kernel function h for the medcouple, closing over the values of Zplus and Zminus just defined above. @@ -143,7 +147,7 @@ if abs(a - b) <= 2*eps2: h = signum(n_plus - 1 - i - j) else: - h = (a+b)/(a-b) + h = (a + b)/(a - b) return h @@ -164,7 +168,7 @@ A = [h_kern(i, (L[i] + R[i])//2) for i in I1] W = [R[i] - L[i] + 1 for i in I2] - Am = wmedian(A,W) + Am = wmedian(A, W) Am_eps = eps1*(eps1 + abs(Am)) @@ -212,7 +216,7 @@ A.sort() A.reverse() - Am = A[medc_idx - Ltot] + Am = A[medc_idx - Ltot] return Am