From 56279089ec7af92c9149530d52c3b1bd1900660b Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Fri, 14 Dec 2018 17:10:29 +0100 Subject: [PATCH] Simplifying test for variables to store (1) --- .../daAlgorithms/ExtendedKalmanFilter.py | 35 ++++++++++--------- src/daComposant/daAlgorithms/KalmanFilter.py | 30 +++++++++------- src/daComposant/daCore/BasicObjects.py | 12 +++++-- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/daComposant/daAlgorithms/ExtendedKalmanFilter.py b/src/daComposant/daAlgorithms/ExtendedKalmanFilter.py index ae83f92..2a4cb79 100644 --- a/src/daComposant/daAlgorithms/ExtendedKalmanFilter.py +++ b/src/daComposant/daAlgorithms/ExtendedKalmanFilter.py @@ -64,6 +64,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): "CostFunctionJb", "CostFunctionJo", "Innovation", + "PredictedState", ] ) self.defineRequiredParameter( # Pas de type @@ -83,11 +84,6 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # # Opérateurs # ---------- - if B is None: - raise ValueError("Background error covariance matrix has to be properly defined!") - if R is None: - raise ValueError("Observation error covariance matrix has to be properly defined!") - # H = HO["Direct"].appliedControledFormTo # if self._parameters["EstimationOf"] == "State": @@ -107,7 +103,10 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # # Précalcul des inversions de B et R # ---------------------------------- - if self._parameters["StoreInternalVariables"]: + if self._parameters["StoreInternalVariables"] \ + or self._toStore("CostFunctionJ") \ + or self._toStore("CostFunctionJb") \ + or self._toStore("CostFunctionJo"): BI = B.getI() RI = R.getI() # @@ -117,7 +116,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): Pn = B # self.StoredVariables["Analysis"].store( Xn.A1 ) - if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("APosterioriCovariance"): self.StoredVariables["APosterioriCovariance"].store( Pn.asfullmatrix(Xn.size) ) covarianceXa = Pn Xa = Xn @@ -179,16 +178,20 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): Pn = Pn_predicted - Kn * Ht * Pn_predicted # self.StoredVariables["Analysis"].store( Xn.A1 ) - if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("APosterioriCovariance"): self.StoredVariables["APosterioriCovariance"].store( Pn ) - if "Innovation" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("Innovation"): self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) ) - if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]: + if self._parameters["StoreInternalVariables"] \ + or self._toStore("CurrentState"): self.StoredVariables["CurrentState"].store( Xn ) if self._parameters["StoreInternalVariables"] \ - or "CostFunctionJ" in self._parameters["StoreSupplementaryCalculations"] \ - or "CostFunctionJb" in self._parameters["StoreSupplementaryCalculations"] \ - or "CostFunctionJo" in self._parameters["StoreSupplementaryCalculations"]: + or self._toStore("PredictedState"): + self.StoredVariables["PredictedState"].store( Xn_predicted ) + if self._parameters["StoreInternalVariables"] \ + or self._toStore("CostFunctionJ") \ + or self._toStore("CostFunctionJb") \ + or self._toStore("CostFunctionJo"): Jb = 0.5 * (Xn - Xb).T * BI * (Xn - Xb) Jo = 0.5 * d.T * RI * d J = float( Jb ) + float( Jo ) @@ -198,7 +201,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): if J < previousJMinimum: previousJMinimum = J Xa = Xn - if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("APosterioriCovariance"): covarianceXa = Pn else: Xa = Xn @@ -208,10 +211,10 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # ---------------------------------------------------------------- if self._parameters["EstimationOf"] == "Parameters": self.StoredVariables["Analysis"].store( Xa.A1 ) - if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("APosterioriCovariance"): self.StoredVariables["APosterioriCovariance"].store( covarianceXa ) # - if "BMA" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("BMA"): self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) ) # self._post_run(HO) diff --git a/src/daComposant/daAlgorithms/KalmanFilter.py b/src/daComposant/daAlgorithms/KalmanFilter.py index 58bb785..9e6fe0a 100644 --- a/src/daComposant/daAlgorithms/KalmanFilter.py +++ b/src/daComposant/daAlgorithms/KalmanFilter.py @@ -57,6 +57,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): "CostFunctionJb", "CostFunctionJo", "Innovation", + "PredictedState", ] ) self.requireInputArguments( @@ -94,9 +95,9 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # Précalcul des inversions de B et R # ---------------------------------- if self._parameters["StoreInternalVariables"] \ - or "CostFunctionJ" in self._parameters["StoreSupplementaryCalculations"] \ - or "CostFunctionJb" in self._parameters["StoreSupplementaryCalculations"] \ - or "CostFunctionJo" in self._parameters["StoreSupplementaryCalculations"]: + or self._toStore("CostFunctionJ") \ + or self._toStore("CostFunctionJb") \ + or self._toStore("CostFunctionJo"): BI = B.getI() RI = R.getI() # @@ -106,7 +107,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): Pn = B # self.StoredVariables["Analysis"].store( Xn.A1 ) - if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("APosterioriCovariance"): self.StoredVariables["APosterioriCovariance"].store( Pn.asfullmatrix(Xn.size) ) covarianceXa = Pn Xa = Xn @@ -153,17 +154,20 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): Pn = Pn_predicted - Kn * Ht * Pn_predicted # self.StoredVariables["Analysis"].store( Xn.A1 ) - if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("APosterioriCovariance"): self.StoredVariables["APosterioriCovariance"].store( Pn ) - if "Innovation" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("Innovation"): self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) ) if self._parameters["StoreInternalVariables"] \ - or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]: + or self._toStore("CurrentState"): self.StoredVariables["CurrentState"].store( Xn ) if self._parameters["StoreInternalVariables"] \ - or "CostFunctionJ" in self._parameters["StoreSupplementaryCalculations"] \ - or "CostFunctionJb" in self._parameters["StoreSupplementaryCalculations"] \ - or "CostFunctionJo" in self._parameters["StoreSupplementaryCalculations"]: + or self._toStore("PredictedState"): + self.StoredVariables["PredictedState"].store( Xn_predicted ) + if self._parameters["StoreInternalVariables"] \ + or self._toStore("CostFunctionJ") \ + or self._toStore("CostFunctionJb") \ + or self._toStore("CostFunctionJo"): Jb = 0.5 * (Xn - Xb).T * BI * (Xn - Xb) Jo = 0.5 * d.T * RI * d J = float( Jb ) + float( Jo ) @@ -173,7 +177,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): if J < previousJMinimum: previousJMinimum = J Xa = Xn - if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("APosterioriCovariance"): covarianceXa = Pn else: Xa = Xn @@ -183,10 +187,10 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # ---------------------------------------------------------------- if self._parameters["EstimationOf"] == "Parameters": self.StoredVariables["Analysis"].store( Xa.A1 ) - if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("APosterioriCovariance"): self.StoredVariables["APosterioriCovariance"].store( covarianceXa ) # - if "BMA" in self._parameters["StoreSupplementaryCalculations"]: + if self._toStore("BMA"): self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) ) # self._post_run(HO) diff --git a/src/daComposant/daCore/BasicObjects.py b/src/daComposant/daCore/BasicObjects.py index a6ebe42..0ea2aab 100644 --- a/src/daComposant/daCore/BasicObjects.py +++ b/src/daComposant/daCore/BasicObjects.py @@ -508,6 +508,7 @@ class Algorithm(object): self.StoredVariables["GradientOfCostFunctionJb"] = Persistence.OneVector(name = "GradientOfCostFunctionJb") self.StoredVariables["GradientOfCostFunctionJo"] = Persistence.OneVector(name = "GradientOfCostFunctionJo") self.StoredVariables["CurrentState"] = Persistence.OneVector(name = "CurrentState") + self.StoredVariables["PredictedState"] = Persistence.OneVector(name = "PredictedState") self.StoredVariables["Analysis"] = Persistence.OneVector(name = "Analysis") self.StoredVariables["IndexOfOptimum"] = Persistence.OneIndex(name = "IndexOfOptimum") self.StoredVariables["CurrentOptimum"] = Persistence.OneVector(name = "CurrentOptimum") @@ -539,7 +540,7 @@ class Algorithm(object): self.__setParameters(Parameters) # # Corrections et complements - def __test_vvalue( argument, variable, argname): + def __test_vvalue(argument, variable, argname): if argument is None: if variable in self.__required_inputs["RequiredInputValues"]["mandatory"]: raise ValueError("%s %s vector %s has to be properly defined!"%(self._name,argname,variable)) @@ -549,9 +550,11 @@ class Algorithm(object): logging.debug("%s %s vector %s is not set, but is not required."%(self._name,argname,variable)) else: logging.debug("%s %s vector %s is set, and its size is %i."%(self._name,argname,variable,numpy.array(argument).size)) + return 0 __test_vvalue( Xb, "Xb", "Background or initial state" ) __test_vvalue( Y, "Y", "Observation" ) - def __test_cvalue( argument, variable, argname): + # + def __test_cvalue(argument, variable, argname): if argument is None: if variable in self.__required_inputs["RequiredInputValues"]["mandatory"]: raise ValueError("%s %s error covariance matrix %s has to be properly defined!"%(self._name,argname,variable)) @@ -561,6 +564,7 @@ class Algorithm(object): logging.debug("%s %s error covariance matrix %s is not set, but is not required."%(self._name,argname,variable)) else: logging.debug("%s %s error covariance matrix %s is set."%(self._name,argname,variable)) + return 0 __test_cvalue( R, "R", "Observation" ) __test_cvalue( B, "B", "Background" ) __test_cvalue( Q, "Q", "Evolution" ) @@ -607,6 +611,10 @@ class Algorithm(object): logging.debug("%s Terminé", self._name) return 0 + def _toStore(self, key): + "True if in StoreSupplementaryCalculations, else False" + return key in self._parameters["StoreSupplementaryCalculations"] + def get(self, key=None): """ Renvoie l'une des variables stockées identifiée par la clé, ou le -- 2.30.2