diff --git a/mcutils.py b/mcutils.py index 7972ca9..cf8198d 100644 --- a/mcutils.py +++ b/mcutils.py @@ -14,8 +14,11 @@ import collections import os import pylab as plt from itertools import chain + sqrt2=np.sqrt(2) +_svd_ret = collections.namedtuple("svd_return",["basis","s",\ + "population","data"]) ### COLORS, ETC ### colors = None @@ -136,6 +139,27 @@ def poly_approximant(x,y,order=10,allowExtrapolation=False,fill_value=0): return f +def svd(data,ncomp=None,truncate=True): + """ do SVD based cleaning + + Parameters + ---------- + ncomp : int + number of components to keep + truncate : bool + if True, returns only basis and populations of up to ncomp + """ + # first index is time/T/P + u,s,v = np.linalg.svd(data,full_matrices=False) + if ncomp is not None: + s[ncomp:]=0 + data = np.dot(u,np.dot(np.diag(s),v)) + if truncate: + v = v[:ncomp] + u = u[:,:ncomp] + return _svd_ret(basis=v,s=s,population=u.T,data=data) + + def smoothing(x,y,err=None,k=5,s=None,newx=None,derivative_order=0): idx = np.isnan(x)|np.isnan(y) idx = ~ idx