Salome HOME
Initial Commit
[modules/adao.git] / src / daComposant / daDiagnostics / VarianceOrder.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     Diagnostic sur les variances dans B et R par rapport à l'ébauche Xb et aux
23     observations Y. On teste si on a les conditions :
24         1%*xb < sigma_b < 10%*xb
25             et
26         1%*yo < sigma_o < 10%*yo
27     Le diagnostic renvoie True si les deux conditions sont simultanément
28     vérifiées, False dans les autres cas.
29 """
30 __author__ = "Sophie RICCI, Jean-Philippe ARGAUD - Septembre 2008"
31
32 import sys ; sys.path.insert(0, "../daCore")
33
34 import numpy
35 import Persistence
36 from BasicObjects import Diagnostic
37 from scipy.linalg import eig
38 import logging
39
40 # ==============================================================================
41 class ElementaryDiagnostic(Diagnostic,Persistence.OneScalar):
42     def __init__(self, name = "", unit = "", basetype = None, parameters = {}):
43         Diagnostic.__init__(self, name, parameters)
44         Persistence.OneScalar.__init__( self, name, unit, basetype = bool )
45
46     def _formula(self, xb, B, yo, R):
47         """
48         Comparaison des variables et de leur variance relative
49         """
50         valpB = eig(B, left = False, right = False)
51         valpR = eig(R, left = False, right = False)
52         logging.info(" Si l on souhaite 1%s*xb < sigma_b < 10%s*xb, les valeurs propres de B doivent etre comprises dans l intervalle [%.3e,%.3e]"%("%","%",1.e-4*xb.mean()*xb.mean(),1.e-2*xb.mean()*xb.mean()))
53         logging.info(" Si l on souhaite 1%s*yo < sigma_o < 10%s*yo, les valeurs propres de R doivent etre comprises dans l intervalle [%.3e,%.3e]"%("%","%",1.e-4*yo.mean()*yo.mean(),1.e-2*yo.mean()*yo.mean()))
54         #
55         limite_inf_valp = 1.e-4*xb.mean()*xb.mean()
56         limite_sup_valp = 1.e-2*xb.mean()*xb.mean()
57         variancexb = (valpB >= limite_inf_valp).all() and (valpB <= limite_sup_valp).all()
58         logging.info(" La condition empirique sur la variance de Xb est....: %s"%variancexb)
59         #
60         limite_inf_valp = 1.e-4*yo.mean()*yo.mean()
61         limite_sup_valp = 1.e-2*yo.mean()*yo.mean()
62         varianceyo = (valpR >= limite_inf_valp).all() and (valpR <= limite_sup_valp).all()
63         logging.info(" La condition empirique sur la variance de Y est.....: %s",varianceyo)
64         #
65         variance = variancexb and varianceyo
66         logging.info(" La condition empirique sur la variance globale est..: %s"%variance)
67         #
68         return variance
69
70     def calculate(self, Xb = None, B = None, Y = None, R = None,  step = None):
71         """
72         Teste les arguments, active la formule de calcul et stocke le résultat
73         Arguments :
74             - Xb : valeur d'ébauche du paramêtre
75             - B  : matrice de covariances d'erreur d'ébauche
76             - yo : vecteur d'observation
77             - R  : matrice de covariances d'erreur d'observation 
78         """
79         if (Xb is None) or (B is None) or (Y is None) or (R is None):
80             raise ValueError("You must specify Xb, B, Y, R")
81         yo = numpy.array(Y)
82         BB = numpy.matrix(B)
83         xb = numpy.array(Xb)
84         RR = numpy.matrix(R)
85         if (RR.size < 1 ) or (BB.size < 1) :
86             raise ValueError("The background and the observation covariance matrices must not be empty")
87         if ( yo.size < 1 ) or ( xb.size < 1 ):
88             raise ValueError("The Xb background and the Y observation vectors must not be empty")
89         if xb.size*xb.size != BB.size:
90             raise ValueError("Xb background vector and B covariance matrix sizes are not consistent")
91         if yo.size*yo.size != RR.size:
92             raise ValueError("Y observation vector and R covariance matrix sizes are not consistent")
93         if yo.all() == 0. or xb.all() == 0. :
94             raise ValueError("The diagnostic can not be applied to zero vectors")
95         #
96         value = self._formula( xb, BB, yo, RR)
97         #
98         self.store( value = value,  step = step )
99
100 #===============================================================================
101 if __name__ == "__main__":
102     print '\n AUTODIAGNOSTIC \n'
103     #
104     # Instanciation de l'objet diagnostic
105     # -----------------------------------
106     D = ElementaryDiagnostic("Mon OrdreVariance")
107     #
108     # Vecteur de type matrix
109     # ----------------------
110     xb = numpy.array([11000.])
111     yo = numpy.array([1.e12 , 2.e12, 3.e12 ])
112     B = 1.e06 * numpy.matrix(numpy.identity(1))
113     R = 1.e22 * numpy.matrix(numpy.identity(3))
114     #
115     D.calculate( Xb = xb, B = B, Y = yo, R = R)
116     print " L'ébauche est.......................................:",xb
117     print " Les observations sont...............................:",yo
118     print " La valeur moyenne des observations est..............: %.2e"%yo.mean()
119     print " La valeur moyenne de l'ebauche est..................: %.2e"%xb.mean()
120     print " La variance d'ébauche specifiée est.................: %.2e"%1.e6
121     print " La variance d'observation spécifiée est.............: %.2e"%1.e22
122     #
123     if D.valueserie(0) :
124             print " Les variances specifiées sont de l'ordre de 1% a 10% de l'ébauche et des observations"
125     else :
126             print " Les variances specifiées ne sont pas de l'ordre de 1% a 10% de l'ébauche et des observations"
127     print
128
129