]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daDiagnostics/ComputeVariance.py
Salome HOME
22ae8e604ffb23c88955d0e2abdcf7e05433b03d
[modules/adao.git] / src / daComposant / daDiagnostics / ComputeVariance.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2009  EDF R&D
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 __doc__ = """
22     Calcul de la variance d'un vecteur à chaque pas. Ce diagnostic très simple
23     est présent pour rappeller à l'utilisateur de l'assimilation qu'il faut
24     qu'il vérifie les variances de ses écarts en particulier.
25 """
26 __author__ = "Jean-Philippe ARGAUD - Septembre 2008"
27
28 import sys ; sys.path.insert(0, "../daCore") 
29
30 import numpy
31 import Persistence
32 from BasicObjects import Diagnostic
33 from AssimilationStudy import AssimilationStudy
34
35 # ==============================================================================
36 class ElementaryDiagnostic(Diagnostic,Persistence.OneScalar):
37     def __init__(self, name = "", unit = "", basetype = None, parameters = {}):
38         Diagnostic.__init__(self, name, parameters)
39         Persistence.OneScalar.__init__( self, name, unit, basetype = float)
40
41     def _formula(self, V):
42         """
43         Calcul de la variance du vecteur en argument. Elle est faite avec une
44         division par la taille du vecteur.
45         """
46         variance = V.var() 
47         #
48         return variance
49
50     def calculate(self, vector = None, step = None):
51         """
52         Teste les arguments, active la formule de calcul et stocke le résultat
53         """
54         if vector is None:
55             raise ValueError("One vector must be given to compute biais")
56         V = numpy.array(vector)
57         if V.size < 1:
58             raise ValueError("The given vector must not be empty")
59         #
60         value = self._formula( V)
61         #
62         self.store( value = value,  step = step )
63
64 #===============================================================================
65 if __name__ == "__main__":
66     print '\n AUTODIAGNOSTIC \n'
67     #
68     D = ElementaryDiagnostic("Ma variance")
69     #
70     # Vecteur de type matrix
71     # ----------------------
72     x = numpy.matrix(([3., 4., 5.]))
73     print " Le vecteur de type 'matrix' choisi est..:", x
74     print " Le moyenne de ce vecteur est............:", x.mean()
75     print " La variance attendue de ce vecteur est..:", x.var()
76     #
77     D.calculate( vector = x)
78     print " La variance obtenue de ce vecteur est...:", D.valueserie(0)
79     print
80     #
81     # Vecteur de type array
82     # ---------------------
83     x = numpy.array(range(11))
84     print " Le vecteur de type 'array' choisi est...:", x
85     print " Le moyenne de ce vecteur est............:", x.mean()
86     print " La variance attendue de ce vecteur est..:", x.var()
87     #
88     D.calculate( vector = x)
89     print " La variance obtenue de ce vecteur est...:", D.valueserie(1)
90     print