Salome HOME
fefe24c8461c31709c20c4068c115e269bdefc16
[modules/adao.git] / src / daComposant / daDiagnostics / ComputeVariance.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2010  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 numpy
29 from daCore import BasicObjects, Persistence
30
31 # ==============================================================================
32 class ElementaryDiagnostic(BasicObjects.Diagnostic,Persistence.OneScalar):
33     def __init__(self, name = "", unit = "", basetype = None, parameters = {}):
34         BasicObjects.Diagnostic.__init__(self, name, parameters)
35         Persistence.OneScalar.__init__( self, name, unit, basetype = float)
36
37     def _formula(self, V):
38         """
39         Calcul de la variance du vecteur en argument. Elle est faite avec une
40         division par la taille du vecteur.
41         """
42         variance = V.var() 
43         #
44         return variance
45
46     def calculate(self, vector = None, step = None):
47         """
48         Teste les arguments, active la formule de calcul et stocke le résultat
49         """
50         if vector is None:
51             raise ValueError("One vector must be given to compute biais")
52         V = numpy.array(vector)
53         if V.size < 1:
54             raise ValueError("The given vector must not be empty")
55         #
56         value = self._formula( V)
57         #
58         self.store( value = value,  step = step )
59
60 #===============================================================================
61 if __name__ == "__main__":
62     print '\n AUTODIAGNOSTIC \n'
63     #
64     D = ElementaryDiagnostic("Ma variance")
65     #
66     # Vecteur de type matrix
67     # ----------------------
68     x = numpy.matrix(([3., 4., 5.]))
69     print " Le vecteur de type 'matrix' choisi est..:", x
70     print " Le moyenne de ce vecteur est............:", x.mean()
71     print " La variance attendue de ce vecteur est..:", x.var()
72     #
73     D.calculate( vector = x)
74     print " La variance obtenue de ce vecteur est...:", D.valueserie(0)
75     print
76     #
77     # Vecteur de type array
78     # ---------------------
79     x = numpy.array(range(11))
80     print " Le vecteur de type 'array' choisi est...:", x
81     print " Le moyenne de ce vecteur est............:", x.mean()
82     print " La variance attendue de ce vecteur est..:", x.var()
83     #
84     D.calculate( vector = x)
85     print " La variance obtenue de ce vecteur est...:", D.valueserie(1)
86     print