Reduced cyclomatic complexity in mctp2tc

master
Per A Brodtkorb 8 years ago
parent afc11e9852
commit 59df510f0a

@ -1332,43 +1332,23 @@ def mctp2tc(f_Mm, utc, param, f_mM=None):
f_tc = the matrix with frequences of upcrossing troughs and crests,
"""
# raise NotImplementedError('')
if f_mM is None:
f_mM = np.copy(f_Mm)
u = np.linspace(*param)
udisc = np.fliplr(u)
ntc = np.sum(udisc >= utc)
n = len(f_Mm)
def _check_ntc(ntc, n):
if ntc > n - 1:
raise IndexError('index for mean-level out of range, stop')
def _check_discretization(param, ntc):
if param[2] - 1 < ntc or ntc < 2:
raise ValueError('the reference level out of range, stop')
# normalization of frequency matrices
def _normalize_rows(arr):
n = len(arr)
for i in range(n):
rowsum = np.sum(f_Mm[i])
rowsum = np.sum(arr[i])
if rowsum != 0:
f_Mm[i] = f_Mm[i] / rowsum
arr[i] = arr[i] / rowsum
return arr
P = np.fliplr(f_Mm)
Ph = np.rot90(np.fliplr(f_mM), -1)
for i in range(n):
rowsum = np.sum(Ph[i])
if rowsum != 0:
Ph[i] = Ph[i] / rowsum
Ph = np.fliplr(Ph)
F = np.zeros((n, n))
F[:ntc - 1, :(n - ntc)] = f_mM[:ntc - 1, :(n - ntc)]
F = cmat2nt(F)
for i in range(1, ntc):
for j in range(ntc, n - 1):
if i < ntc:
def _make_tempp(P, Ph, i, ntc):
Ap = P[i:ntc - 1, i + 1:ntc]
Bp = Ph[i + 1:ntc, i:ntc - 1]
dim_p = ntc - i
@ -1388,8 +1368,9 @@ def mctp2tc(f_Mm, utc, param, f_mM=None):
# end
# end
# end
elif j > ntc:
return tempp
def _make_tempm(P, Ph, j, ntc):
Am = P[ntc:j - 1, ntc + 1:j]
Bm = Ph[ntc + 1:j, ntc:j - 1]
dim_m = j - ntc
@ -1408,34 +1389,59 @@ def mctp2tc(f_Mm, utc, param, f_mM=None):
tempm = np.dot(Bm, linalg.lstsq(rh, em)[0])
# end
# end
return tempm
if f_mM is None:
f_mM = np.copy(f_Mm)
u = np.linspace(*param)
udisc = np.fliplr(u)
ntc = np.sum(udisc >= utc)
n = len(f_Mm)
_check_ntc(ntc, n)
_check_discretization(param, ntc)
# normalization of frequency matrices
f_Mm = _normalize_rows(f_Mm)
P = np.fliplr(f_Mm)
Ph = np.rot90(np.fliplr(f_mM), -1)
Ph = _normalize_rows(Ph)
Ph = np.fliplr(Ph)
F = np.zeros((n, n))
F[:ntc - 1, :(n - ntc)] = f_mM[:ntc - 1, :(n - ntc)]
F = cmat2nt(F)
for i in range(1, ntc):
for j in range(ntc-1, n - 1):
if i < ntc-1:
tempp = _make_tempp(P, Ph, i, ntc)
b = np.dot(np.dot(tempp.T, f_mM[i:ntc - 1, n - j:-1:-1]),
ones((n - j, 1)))
# end
if j > ntc-1:
tempm = _make_tempm(P,Ph, j, ntc)
c = np.dot(np.dot(ones((1, i - 1)),
f_mM[:i - 1, n - ntc:n - j + 1:-1]),
tempm)
# end
if (j > ntc) and (i < ntc):
if (j > ntc-1) and (i < ntc-1):
a = np.dot(np.dot(tempp.T,
f_mM[i:ntc - 1, n - ntc:-1:n - j + 1]),
tempm)
b = np.dot(np.dot(tempp.T, f_mM[i:ntc - 1, n - j:-1:1]),
ones((n - j, 1)))
c = np.dot(np.dot(ones((1, i - 1)),
f_mM[1:i - 1, n - ntc:-1:n - j + 1]),
tempm)
F[i, n - j + 1] = F[i, n - j + 1] + a + b + c
# end
if (j == ntc) and (i < ntc):
b = np.dot(np.dot(tempp.T, f_mM[i:ntc - 1, n - j:-1:1]),
ones((n - j, 1)))
if (j == ntc-1) and (i < ntc-1):
F[i, n - j + 1] = F[i, n - j + 1] + b
for k in range(ntc):
F[i, n - k + 1] = F[i, n - ntc + 1]
# end
# end
if (j > ntc) and (i == ntc):
c = np.dot(np.dot(ones((1, i - 1)),
f_mM[1:i - 1, n - ntc:-1:n - j + 1]),
tempm)
if (j > ntc-1) and (i == ntc-1):
F[i, n - j + 1] = F[i, n - j + 1] + c
for k in range(ntc, n):
F[k, n - j + 1] = F[ntc, n - j + 1]
for k in range(ntc-1, n):
F[k, n - j + 1] = F[ntc-1, n - j + 1]
# end
# end
# end

Loading…
Cancel
Save