• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

/home/mark/model/software/ScrumPy/ScrumPy/Structural/Decompose.py

00001 import sys
00002 
00003 import ScrumPy
00004 from ScrumPy.Util import Sci
00005 from ScrumPy.Data import DataSets
00006 
00007 
00008 def Consistify(E, Obs):
00009     "delete and reorder columns in Modes and Obs so as to leave them consistent"
00010 
00011     for c in E.cnames[:]:
00012         if  not c  in Obs.cnames:
00013             E.DelCol(c)
00014 
00015     for c in Obs.cnames[:]:
00016         if not c in E.cnames:
00017             Obs.DelCol(c)
00018 
00019     Obs.ColReorder(E.cnames)  # column ordering of E and Obs now identical
00020 
00021 
00022 
00023 def DecomposeVec(E, v,Irrevs):
00024 
00025     sciv = Sci.vector(v)
00026     E = E.Copy(float)
00027     sciE = Sci.matrix(E.rows)
00028     g = Sci.pinv(sciE)
00029 
00030     e = v*g # <<<------  the decomposition : e = v.E#
00031     evals = e[0].tolist()[0]
00032 
00033     Again = False
00034     Erownames = E.rnames[:]
00035     for i in range(len(evals)):
00036         if evals[i] <0 and Erownames[i] in Irrevs:
00037             Again = True
00038             E.DelRow(Erownames[i])
00039     if Again:
00040         print "Again"
00041         return  DecomposeVec(E,v, Irrevs)
00042     else:
00043         vrec = e*sciE
00044         RelErr = Sci.Mod(vrec - v)/Sci.Mod(v)
00045         return E.rnames, evals, RelErr, vrec
00046 
00047 
00048 
00049 def DecomposeSet(Modes, Obs):
00050 
00051     E = Modes.mo.Copy(float)
00052     Obs = Obs.Copy()
00053     
00054 
00055     Consistify(E,Obs)
00056 
00057     rv = []
00058     Irrevs = Modes.Irrevs()
00059 
00060     for v in Obs:
00061         rv.append(DecomposeVec(E,v,Irrevs))
00062         print ".",
00063 
00064     return rv
00065 
00066 
00067 class Decompose(DataSets.DataSet):
00068 
00069     def __init__(self,Modes, Obs):
00070 
00071         
00072         self.Recs = []                                # a list of recovered fluxes
00073         self.Modes = Modes
00074         ItemNames=["RelErr"] + Modes.mo.rnames
00075         ScrumPy.Data.DataSets.DataSet.__init__(self, ItemNames=ItemNames)  # create self as  an empty data set for the assignments
00076         
00077         Raw_e =  DecomposeSet(Modes,Obs)            # get a list of result tuples
00078 
00079         for e in Raw_e:
00080             names,vals,relerr = e[0],e[1],e[2]
00081             self.NewRow()
00082             self[-1,0] = relerr
00083             for n in range(len(names)):
00084                 self[-1,names[n]] = vals[n]
00085       
00086         #self.Recs.append(e[3])                     # hang on to the recovered fluxes - ? Why we need ??
00087             
00088         self.rnames = Obs.rnames[:]
00089         for c in Obs.cnames:                           # populate self with unused obs (e.g. time)
00090             if c not in Modes.mo.cnames:               # not an observation used in the calculation
00091                 self.NewCol(Obs.GetCol(c),c)           # so copy it in
00092 
00093 
00094 
00095 
00096     def FromDS(self,ds,Copy=1):
00097 
00098         if Copy:
00099             ds = ds.Copy()
00100         self.rows = ds.rows
00101         self.rnames = ds.rnames
00102         self.cnames = ds.cnames
00103         self.Conv = ds.Conv
00104 
00105 
00106     def Filter(self, *args, **kwargs):
00107 
00108         rv = Decompose(self.Modes)
00109         rv.FromDS(
00110             ScrumPy.Data.DataSets.DataSet.Filter(self, *args, **kwargs), Copy=0
00111         )
00112         return rv
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 

Generated on Tue Sep 4 2012 15:38:02 for ScrumPy by  doxygen 1.7.1