Salome HOME
Updating copyright date information (1)
[modules/adao.git] / src / daComposant / daAlgorithms / Blue.py
index a382264c4cc6b9a17e28a677e15c6d97828140ae..ab5040c96b8baee1c4d6f212d1ff2c264cc1c722 100644 (file)
@@ -1,6 +1,6 @@
-#-*-coding:iso-8859-1-*-
+# -*- coding: utf-8 -*-
 #
-# Copyright (C) 2008-2017 EDF R&D
+# Copyright (C) 2008-2018 EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -32,14 +32,34 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             name     = "StoreInternalVariables",
             default  = False,
             typecast = bool,
-            message  = "Stockage des variables internes ou intermédiaires du calcul",
+            message  = "Stockage des variables internes ou intermédiaires du calcul",
             )
         self.defineRequiredParameter(
             name     = "StoreSupplementaryCalculations",
             default  = [],
             typecast = tuple,
-            message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
-            listval  = ["APosterioriCorrelations", "APosterioriCovariance", "APosterioriStandardDeviations", "APosterioriVariances", "BMA", "OMA", "OMB", "CurrentState", "CostFunctionJ", "CostFunctionJb", "CostFunctionJo", "Innovation", "SigmaBck2", "SigmaObs2", "MahalanobisConsistency", "SimulationQuantiles", "SimulatedObservationAtBackground", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
+            message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
+            listval  = [
+                "APosterioriCorrelations",
+                "APosterioriCovariance",
+                "APosterioriStandardDeviations",
+                "APosterioriVariances",
+                "BMA",
+                "OMA",
+                "OMB",
+                "CurrentState",
+                "CostFunctionJ",
+                "CostFunctionJb",
+                "CostFunctionJo",
+                "Innovation",
+                "SigmaBck2",
+                "SigmaObs2",
+                "MahalanobisConsistency",
+                "SimulationQuantiles",
+                "SimulatedObservationAtBackground",
+                "SimulatedObservationAtCurrentState",
+                "SimulatedObservationAtOptimum",
+                ]
             )
         self.defineRequiredParameter(
             name     = "Quantiles",
@@ -52,13 +72,13 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         self.defineRequiredParameter(
             name     = "SetSeed",
             typecast = numpy.random.seed,
-            message  = "Graine fixée pour le générateur aléatoire",
+            message  = "Graine fixée pour le générateur aléatoire",
             )
         self.defineRequiredParameter(
             name     = "NumberOfSamplesForQuantiles",
             default  = 100,
             typecast = int,
-            message  = "Nombre d'échantillons simulés pour le calcul des quantiles",
+            message  = "Nombre d'échantillons simulés pour le calcul des quantiles",
             minval   = 1,
             )
         self.defineRequiredParameter(
@@ -68,19 +88,22 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             message  = "Type de simulation pour l'estimation des quantiles",
             listval  = ["Linear", "NonLinear"]
             )
+        self.requireInputArguments(
+            mandatory= ("Xb", "Y", "HO", "R", "B"),
+            )
 
     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
-        self._pre_run(Parameters)
+        self._pre_run(Parameters, Xb, Y, R, B, Q)
         #
         Hm = HO["Tangent"].asMatrix(Xb)
         Hm = Hm.reshape(Y.size,Xb.size) # ADAO & check shape
         Ha = HO["Adjoint"].asMatrix(Xb)
         Ha = Ha.reshape(Xb.size,Y.size) # ADAO & check shape
         #
-        # Utilisation éventuelle d'un vecteur H(Xb) précalculé (sans cout)
-        # ----------------------------------------------------------------
-        if HO["AppliedToX"] is not None and "HXb" in HO["AppliedToX"]:
-            HXb = HO["AppliedToX"]["HXb"]
+        # Utilisation éventuelle d'un vecteur H(Xb) précalculé
+        # ----------------------------------------------------
+        if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
+            HXb = HO["AppliedInX"]["HXb"]
         else:
             HXb = Hm * Xb
         HXb = numpy.asmatrix(numpy.ravel( HXb )).T
@@ -89,7 +112,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         if max(Y.shape) != max(HXb.shape):
             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))
         #
-        # Précalcul des inversions de B et R
+        # Précalcul des inversions de B et R
         # ----------------------------------
         BI = B.getI()
         RI = R.getI()
@@ -110,7 +133,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             Xa = Xb + _u
         self.StoredVariables["Analysis"].store( Xa.A1 )
         #
-        # Calcul de la fonction coût
+        # Calcul de la fonction coût
         # --------------------------
         if self._parameters["StoreInternalVariables"] or \
            "CostFunctionJ"                      in self._parameters["StoreSupplementaryCalculations"] or \
@@ -125,11 +148,9 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         if self._parameters["StoreInternalVariables"] or \
            "CostFunctionJ"                 in self._parameters["StoreSupplementaryCalculations"] or \
            "MahalanobisConsistency"        in self._parameters["StoreSupplementaryCalculations"]:
-            #
             Jb  = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
             Jo  = float( 0.5 * oma.T * RI * oma )
             J   = Jb + Jo
-            #
             self.StoredVariables["CostFunctionJb"].store( Jb )
             self.StoredVariables["CostFunctionJo"].store( Jo )
             self.StoredVariables["CostFunctionJ" ].store( J )
@@ -152,7 +173,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                     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,))
             self.StoredVariables["APosterioriCovariance"].store( A )
         #
-        # Calculs et/ou stockages supplémentaires
+        # Calculs et/ou stockages supplémentaires
         # ---------------------------------------
         if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
             self.StoredVariables["CurrentState"].store( numpy.ravel(Xa) )
@@ -172,7 +193,6 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         if "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]:
             self.StoredVariables["MahalanobisConsistency"].store( float( 2.*J/d.size ) )
         if "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]:
-            Qtls = map(float, self._parameters["Quantiles"])
             nech = self._parameters["NumberOfSamplesForQuantiles"]
             YfQ  = None
             for i in range(nech):
@@ -189,9 +209,9 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                     YfQ = numpy.hstack((YfQ,Yr))
             YfQ.sort(axis=-1)
             YQ = None
-            for quantile in Qtls:
-                if not (0. <= quantile <= 1.): continue
-                indice = int(nech * quantile - 1./nech)
+            for quantile in self._parameters["Quantiles"]:
+                if not (0. <= float(quantile) <= 1.): continue
+                indice = int(nech * float(quantile) - 1./nech)
                 if YQ is None: YQ = YfQ[:,indice]
                 else:          YQ = numpy.hstack((YQ,YfQ[:,indice]))
             self.StoredVariables["SimulationQuantiles"].store( YQ )