This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
laydi/fluents/lib/cx_utils.py

116 lines
2.7 KiB
Python
Raw Normal View History

2006-12-18 12:59:12 +01:00
from scipy import apply_along_axis,newaxis,zeros,\
median,round_,nonzero,dot,argmax,any,sqrt,ndarray,\
2007-01-25 12:58:10 +01:00
trace,zeros_like,sign,sort,real,argsort,rand,array,\
2007-07-23 19:33:21 +02:00
matrix,nan
2006-12-18 12:59:12 +01:00
from scipy.linalg import norm,svd,inv,eig
from scipy.stats import median,mean
2007-07-23 19:33:21 +02:00
def normalise(a, axis=0, return_scales=False):
s = apply_along_axis(norm, axis, a)
if axis==0:
s = s[newaxis]
else:
s = s[:,newaxis]
a_s = a/s
if return_scales:
return a_s, s
return a_s
2006-12-18 12:59:12 +01:00
2007-07-23 19:33:21 +02:00
def sub2ind(shape, i, j):
2006-12-18 12:59:12 +01:00
"""Indices from subscripts. Only support for 2d"""
row,col = shape
ind = []
for k in xrange(len(i)):
for m in xrange(len(j)):
ind.append(i[k]*col + j[m])
return ind
def sorted_eig(a, b=None,sort_by='sm'):
"""
Just eig with real part of output sorted:
This is for convenience only, not general!
sort_by='sm': return the eigenvectors by eigenvalues
of smallest magnitude first. (default)
'lm': returns largest eigenvalues first
output: just as eig with 2 outputs
-- s,v (eigvals,eigenvectors)
(This is reversed output compared to matlab)
"""
2007-07-23 19:33:21 +02:00
s,v = eig(a, b)
2006-12-18 12:59:12 +01:00
s = real(s) # dont expect any imaginary part
v = real(v)
ind = argsort(s)
if sort_by=='lm':
ind = ind[::-1]
2007-07-23 19:33:21 +02:00
v = v.take(ind, 1)
2006-12-18 12:59:12 +01:00
s = s.take(ind)
return s,v
def str2num(string_number):
"""Convert input (string number) into number, if float(string_number) fails, a nan is inserted.
"""
missings = ['','nan','NaN','NA']
try:
num = float(string_number)
except:
if string_number in missings:
num = nan
else:
print "Found strange entry: %s" %string_number
raise
return num
def randperm(n):
2007-07-23 19:33:21 +02:00
r = rand(n)
2006-12-18 12:59:12 +01:00
dict={}
for i in range(n):
2007-07-23 19:33:21 +02:00
dict[r[i]] = i
r = sort(r)
out = zeros(n)
2006-12-18 12:59:12 +01:00
for i in range(n):
2007-07-23 19:33:21 +02:00
out[i] = dict[r[i]]
return array(out).astype('i')
2006-12-18 12:59:12 +01:00
def mat_center(X,axis=0,ret_mn=False):
"""Mean center matrix along axis.
X -- matrix, data
axis -- dim,
ret_mn -- bool, return mean
output:
Xc, [mnX]
NB: axis = 1 is column-centering, axis=0=row-centering
default is row centering (axis=0)
"""
try:
rows,cols = X.shape
except ValueError:
print "The X data needs to be two-dimensional"
if axis==0:
mnX = mean(X,axis)[newaxis]
Xs = X - mnX
elif axis==1:
mnX = mean(X,axis)[newaxis]
Xs = (X.T - mnX).T
if ret_mn:
return Xs,mnX
else:
return Xs
2007-01-25 12:58:10 +01:00
def m_shape(array):
"""Returns the array shape on the form of a numpy.matrix."""
return matrix(array).shape