Salome HOME
Improvement of algorithms arguments validation and tests
[modules/adao.git] / src / daComposant / daAlgorithms / ExtendedBlue.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2020 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 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 import logging
24 from daCore import BasicObjects
25 import numpy
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29     def __init__(self):
30         BasicObjects.Algorithm.__init__(self, "EXTENDEDBLUE")
31         self.defineRequiredParameter(
32             name     = "StoreInternalVariables",
33             default  = False,
34             typecast = bool,
35             message  = "Stockage des variables internes ou intermédiaires du calcul",
36             )
37         self.defineRequiredParameter(
38             name     = "StoreSupplementaryCalculations",
39             default  = [],
40             typecast = tuple,
41             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
42             listval  = [
43                 "Analysis",
44                 "APosterioriCorrelations",
45                 "APosterioriCovariance",
46                 "APosterioriStandardDeviations",
47                 "APosterioriVariances",
48                 "BMA",
49                 "CostFunctionJ",
50                 "CostFunctionJAtCurrentOptimum",
51                 "CostFunctionJb",
52                 "CostFunctionJbAtCurrentOptimum",
53                 "CostFunctionJo",
54                 "CostFunctionJoAtCurrentOptimum",
55                 "CurrentOptimum",
56                 "CurrentState",
57                 "Innovation",
58                 "MahalanobisConsistency",
59                 "OMA",
60                 "OMB",
61                 "SigmaBck2",
62                 "SigmaObs2",
63                 "SimulatedObservationAtBackground",
64                 "SimulatedObservationAtCurrentOptimum",
65                 "SimulatedObservationAtCurrentState",
66                 "SimulatedObservationAtOptimum",
67                 "SimulationQuantiles",
68                 ]
69             )
70         self.defineRequiredParameter(
71             name     = "Quantiles",
72             default  = [],
73             typecast = tuple,
74             message  = "Liste des valeurs de quantiles",
75             minval   = 0.,
76             maxval   = 1.,
77             )
78         self.defineRequiredParameter(
79             name     = "SetSeed",
80             typecast = numpy.random.seed,
81             message  = "Graine fixée pour le générateur aléatoire",
82             )
83         self.defineRequiredParameter(
84             name     = "NumberOfSamplesForQuantiles",
85             default  = 100,
86             typecast = int,
87             message  = "Nombre d'échantillons simulés pour le calcul des quantiles",
88             minval   = 1,
89             )
90         self.defineRequiredParameter(
91             name     = "SimulationForQuantiles",
92             default  = "Linear",
93             typecast = str,
94             message  = "Type de simulation pour l'estimation des quantiles",
95             listval  = ["Linear", "NonLinear"]
96             )
97         self.requireInputArguments(
98             mandatory= ("Xb", "Y", "HO", "R", "B"),
99             )
100         self.setAttributes(tags=(
101             "DataAssimilation",
102             "NonLinear",
103             "Filter",
104             ))
105
106     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
107         self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
108         #
109         Hm = HO["Tangent"].asMatrix(Xb)
110         Hm = Hm.reshape(Y.size,Xb.size) # ADAO & check shape
111         Ha = HO["Adjoint"].asMatrix(Xb)
112         Ha = Ha.reshape(Xb.size,Y.size) # ADAO & check shape
113         H  = HO["Direct"].appliedTo
114         #
115         # Utilisation éventuelle d'un vecteur H(Xb) précalculé
116         # ----------------------------------------------------
117         if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
118             HXb = H( Xb, HO["AppliedInX"]["HXb"])
119         else:
120             HXb = H( Xb )
121         HXb = numpy.asmatrix(numpy.ravel( HXb )).T
122         if Y.size != HXb.size:
123             raise ValueError("The size %i of observations Y and %i of observed calculation H(X) are different, they have to be identical."%(Y.size,HXb.size))
124         if max(Y.shape) != max(HXb.shape):
125             raise ValueError("The shapes %s of observations Y and %s of observed calculation H(X) are different, they have to be identical."%(Y.shape,HXb.shape))
126         #
127         # Précalcul des inversions de B et R
128         # ----------------------------------
129         BI = B.getI()
130         RI = R.getI()
131         #
132         # Calcul de l'innovation
133         # ----------------------
134         d  = Y - HXb
135         #
136         # Calcul de la matrice de gain et de l'analyse
137         # --------------------------------------------
138         if Y.size <= Xb.size:
139             _A = R + numpy.dot(Hm, B * Ha)
140             _u = numpy.linalg.solve( _A , d )
141             Xa = Xb + B * Ha * _u
142         else:
143             _A = BI + numpy.dot(Ha, RI * Hm)
144             _u = numpy.linalg.solve( _A , numpy.dot(Ha, RI * d) )
145             Xa = Xb + _u
146         self.StoredVariables["Analysis"].store( Xa.A1 )
147         #
148         # Calcul de la fonction coût
149         # --------------------------
150         if self._parameters["StoreInternalVariables"] or \
151             self._toStore("CostFunctionJ")  or self._toStore("CostFunctionJAtCurrentOptimum") or \
152             self._toStore("CostFunctionJb") or self._toStore("CostFunctionJbAtCurrentOptimum") or \
153             self._toStore("CostFunctionJo") or self._toStore("CostFunctionJoAtCurrentOptimum") or \
154             self._toStore("OMA") or \
155             self._toStore("SigmaObs2") or \
156             self._toStore("MahalanobisConsistency") or \
157             self._toStore("SimulatedObservationAtCurrentOptimum") or \
158             self._toStore("SimulatedObservationAtCurrentState") or \
159             self._toStore("SimulatedObservationAtOptimum") or \
160             self._toStore("SimulationQuantiles"):
161             HXa  = numpy.matrix(numpy.ravel( H( Xa ) )).T
162             oma = Y - HXa
163         if self._parameters["StoreInternalVariables"] or \
164             self._toStore("CostFunctionJ")  or self._toStore("CostFunctionJAtCurrentOptimum") or \
165             self._toStore("CostFunctionJb") or self._toStore("CostFunctionJbAtCurrentOptimum") or \
166             self._toStore("CostFunctionJo") or self._toStore("CostFunctionJoAtCurrentOptimum") or \
167             self._toStore("MahalanobisConsistency"):
168             Jb  = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
169             Jo  = float( 0.5 * oma.T * RI * oma )
170             J   = Jb + Jo
171             self.StoredVariables["CostFunctionJb"].store( Jb )
172             self.StoredVariables["CostFunctionJo"].store( Jo )
173             self.StoredVariables["CostFunctionJ" ].store( J )
174             self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( Jb )
175             self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( Jo )
176             self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( J )
177         #
178         # Calcul de la covariance d'analyse
179         # ---------------------------------
180         if self._toStore("APosterioriCovariance") or \
181             self._toStore("SimulationQuantiles"):
182             if   (Y.size <= Xb.size): K  = B * Ha * (R + numpy.dot(Hm, B * Ha)).I
183             elif (Y.size >  Xb.size): K = (BI + numpy.dot(Ha, RI * Hm)).I * Ha * RI
184             A = B - K * Hm * B
185             if min(A.shape) != max(A.shape):
186                 raise ValueError("The %s a posteriori covariance matrix A is of shape %s, despites it has to be a squared matrix. There is an error in the observation operator, please check it."%(self._name,str(A.shape)))
187             if (numpy.diag(A) < 0).any():
188                 raise ValueError("The %s a posteriori covariance matrix A has at least one negative value on its diagonal. There is an error in the observation operator, please check it."%(self._name,))
189             if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
190                 try:
191                     L = numpy.linalg.cholesky( A )
192                 except:
193                     raise ValueError("The %s a posteriori covariance matrix A is not symmetric positive-definite. Please check your a priori covariances and your observation operator."%(self._name,))
194             self.StoredVariables["APosterioriCovariance"].store( A )
195         #
196         # Calculs et/ou stockages supplémentaires
197         # ---------------------------------------
198         if self._parameters["StoreInternalVariables"] or self._toStore("CurrentState"):
199             self.StoredVariables["CurrentState"].store( numpy.ravel(Xa) )
200         if self._toStore("CurrentOptimum"):
201             self.StoredVariables["CurrentOptimum"].store( numpy.ravel(Xa) )
202         if self._toStore("Innovation"):
203             self.StoredVariables["Innovation"].store( numpy.ravel(d) )
204         if self._toStore("BMA"):
205             self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
206         if self._toStore("OMA"):
207             self.StoredVariables["OMA"].store( numpy.ravel(oma) )
208         if self._toStore("OMB"):
209             self.StoredVariables["OMB"].store( numpy.ravel(d) )
210         if self._toStore("SigmaObs2"):
211             TraceR = R.trace(Y.size)
212             self.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(oma)).T)) ) / TraceR )
213         if self._toStore("SigmaBck2"):
214             self.StoredVariables["SigmaBck2"].store( float( (d.T * Hm * (Xa - Xb))/(Hm * B * Hm.T).trace() ) )
215         if self._toStore("MahalanobisConsistency"):
216             self.StoredVariables["MahalanobisConsistency"].store( float( 2.*J/d.size ) )
217         if self._toStore("SimulationQuantiles"):
218             nech = self._parameters["NumberOfSamplesForQuantiles"]
219             HtM  = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
220             HtM  = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
221             YfQ  = None
222             for i in range(nech):
223                 if self._parameters["SimulationForQuantiles"] == "Linear":
224                     dXr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A) - Xa.A1).T
225                     dYr = numpy.matrix(numpy.ravel( HtM * dXr )).T
226                     Yr = HXa + dYr
227                 elif self._parameters["SimulationForQuantiles"] == "NonLinear":
228                     Xr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A)).T
229                     Yr = numpy.matrix(numpy.ravel( H( Xr ) )).T
230                 if YfQ is None:
231                     YfQ = Yr
232                 else:
233                     YfQ = numpy.hstack((YfQ,Yr))
234             YfQ.sort(axis=-1)
235             YQ = None
236             for quantile in self._parameters["Quantiles"]:
237                 if not (0. <= float(quantile) <= 1.): continue
238                 indice = int(nech * float(quantile) - 1./nech)
239                 if YQ is None: YQ = YfQ[:,indice]
240                 else:          YQ = numpy.hstack((YQ,YfQ[:,indice]))
241             self.StoredVariables["SimulationQuantiles"].store( YQ )
242         if self._toStore("SimulatedObservationAtBackground"):
243             self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
244         if self._toStore("SimulatedObservationAtCurrentState"):
245             self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(HXa) )
246         if self._toStore("SimulatedObservationAtCurrentOptimum"):
247             self.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( numpy.ravel(HXa) )
248         if self._toStore("SimulatedObservationAtOptimum"):
249             self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
250         #
251         self._post_run(HO)
252         return 0
253
254 # ==============================================================================
255 if __name__ == "__main__":
256     print('\n AUTODIAGNOSTIC\n')