]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daDiagnostics/ReduceVariance.py
Salome HOME
Python 3 compatibility improvement (UTF-8) and data interface changes
[modules/adao.git] / src / daComposant / daDiagnostics / ReduceVariance.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2017 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
22 import numpy
23 from daCore import BasicObjects, Persistence
24
25 # ==============================================================================
26 class ElementaryDiagnostic(BasicObjects.Diagnostic,Persistence.OneScalar):
27     """
28     Diagnostic sur la reduction de la variance lors de l'analyse
29     """
30     def __init__(self, name = "", unit = "", basetype = None, parameters = {}):
31         BasicObjects.Diagnostic.__init__(self, name, parameters)
32         Persistence.OneScalar.__init__( self, name, unit, basetype = bool )
33
34     def _formula(self, V1, V2):
35         """
36         Vérification de la reduction de variance sur les écarts entre OMB et OMA
37         lors de l'analyse
38         """
39         varianceOMB = V1.var()
40         varianceOMA = V2.var()
41         #
42         if varianceOMA > varianceOMB:
43             reducevariance = False
44         else :
45             reducevariance = True
46         #
47         return reducevariance
48
49     def calculate(self, vectorOMB = None, vectorOMA = None, step = None):
50         """
51         Teste les arguments, active la formule de calcul et stocke le résultat
52         Arguments :
53             - vectorOMB : vecteur d'écart entre les observations et l'ébauche
54             - vectorOMA : vecteur d'écart entre les observations et l'analyse
55         """
56         if ( (vectorOMB is None) or (vectorOMA is None) ):
57             raise ValueError("Two vectors must be given to test the reduction of the variance after analysis")
58         V1 = numpy.array(vectorOMB)
59         V2 = numpy.array(vectorOMA)
60         if V1.size < 1 or V2.size < 1:
61             raise ValueError("The given vectors must not be empty")
62         if V1.size != V2.size:
63             raise ValueError("The two given vectors must have the same size")
64         #
65         value = self._formula( V1, V2 )
66         #
67         self.store( value = value,  step = step )
68
69 #===============================================================================
70 if __name__ == "__main__":
71     print('\n AUTODIAGNOSTIC \n')
72     #
73     # Instanciation de l'objet diagnostic
74     # -----------------------------------
75     D = ElementaryDiagnostic("Mon ReduceVariance")
76     #
77     # Vecteur de type matrix
78     # ----------------------
79     x1 = numpy.matrix(([3. , 4., 5. ]))
80     x2 = numpy.matrix(([1.5, 2., 2.5]))
81     print(" L'écart entre les observations et l'ébauche est OMB : %s"%(x1,))
82     print(" La moyenne de OMB (i.e. le biais) est de............: %s"%(x1.mean(),))
83     print(" La variance de OMB est de...........................: %s"%(x1.var(),))
84     print(" L'écart entre les observations et l'analyse est OMA : %s"%(x2,))
85     print(" La moyenne de OMA (i.e. le biais) est de............: %s"%(x2.mean(),))
86     print(" La variance de OMA est de...........................: %s"%(x2.var(),))
87     #
88     D.calculate( vectorOMB = x1,  vectorOMA = x2)
89     if not D[0] :
90             print(" Résultat : l'analyse NE RÉDUIT PAS la variance")
91     else :
92             print(" Résultat : l'analyse RÉDUIT la variance")
93     print("")
94     #
95     # Vecteur de type array
96     # ---------------------
97     x1 = numpy.array(range(11))
98     x2 = numpy.matrix(range(-10,12,2))
99     print(" L'écart entre les observations et l'ébauche est OMB : %s"%(x1,))
100     print(" La moyenne de OMB (i.e. le biais) est de............: %s"%(x1.mean(),))
101     print(" La variance de OMB est de...........................: %s"%(x1.var(),))
102     print(" L'écart entre les observations et l'analyse est OMA : %s"%(x2,))
103     print(" La moyenne de OMA (i.e. le biais) est de............: %s"%(x2.mean(),))
104     print(" La variance de OMA est de...........................: %s"%(x2.var(),))
105     #
106     D.calculate( vectorOMB = x1,  vectorOMA = x2)
107     if not D[1] :
108             print(" Résultat : l'analyse NE RÉDUIT PAS la variance")
109     else :
110             print(" Résultat : l'analyse RÉDUIT la variance")
111     print("")