From: Jean-Philippe ARGAUD Date: Sat, 12 Sep 2015 08:47:44 +0000 (+0200) Subject: Minor source corrections for syntax and efficiency X-Git-Tag: V7_7_0rc1~15 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=62675e65bbe725528dcedcce318868ba1bd0dfd6;p=modules%2Fadao.git Minor source corrections for syntax and efficiency --- diff --git a/bin/AdaoCatalogGenerator.py b/bin/AdaoCatalogGenerator.py index 95c2ace..09a11a8 100644 --- a/bin/AdaoCatalogGenerator.py +++ b/bin/AdaoCatalogGenerator.py @@ -244,16 +244,16 @@ def F_ObserverTemplate() : return BLOC(condition = " NodeType == 'Template' ", ValueSeriePrinterSaverAndGnuPlotter = BLOC (condition = " Template == 'ValueSeriePrinterSaverAndGnuPlotter' ", ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "print info, var[:] \\nimport numpy, re\\nv=numpy.array(var[:], ndmin=1)\\nglobal istep\\ntry:\\n istep += 1\\nexcept:\\n istep = 0\\nf='/tmp/value_%s_%05i.txt'%(info,istep)\\nf=re.sub('\s','_',f)\\nprint 'Value saved in \\"%s\\"'%f\\nnumpy.savetxt(f,v)\\nimport Gnuplot\\nglobal ifig,gp\\ntry:\\n ifig += 1\\n gp('set style data lines')\\nexcept:\\n ifig = 0\\n gp = Gnuplot.Gnuplot(persist=1)\\n gp('set style data lines')\\ngp('set title \\"%s (Figure %i)\\"'%(info,ifig))\\ngp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )" ), ), - ValuePrinter = BLOC (condition = " Template == 'ValueMean' ", + ValueMean = BLOC (condition = " Template == 'ValueMean' ", ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy\\nprint info, numpy.nanmean(var[-1])" ), ), - ValuePrinter = BLOC (condition = " Template == 'ValueStandardError' ", + ValueStandardError = BLOC (condition = " Template == 'ValueStandardError' ", ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy\\nprint info, numpy.nanstd(var[-1])" ), ), - ValuePrinter = BLOC (condition = " Template == 'ValueVariance' ", + ValueVariance = BLOC (condition = " Template == 'ValueVariance' ", ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy\\nprint info, numpy.nanvar(var[-1])" ), ), - ValuePrinter = BLOC (condition = " Template == 'ValueRMS' ", + ValueRMS = BLOC (condition = " Template == 'ValueRMS' ", ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy\\nv = numpy.matrix( numpy.ravel( var[-1] ) )\\nprint info, float( numpy.sqrt((1./v.size)*(v*v.T)) )" ), ), ) diff --git a/src/daComposant/daAlgorithms/Blue.py b/src/daComposant/daAlgorithms/Blue.py index 069e937..0ad3195 100644 --- a/src/daComposant/daAlgorithms/Blue.py +++ b/src/daComposant/daAlgorithms/Blue.py @@ -107,21 +107,13 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # Calcul de la matrice de gain et de l'analyse # -------------------------------------------- if Y.size <= Xb.size: - if Y.size > 100: # len(R) - _A = R + Hm * B * Ha - _u = numpy.linalg.solve( _A , d ) - Xa = Xb + B * Ha * _u - else: - K = B * Ha * (R + Hm * B * Ha).I - Xa = Xb + K*d + _A = R + Hm * B * Ha + _u = numpy.linalg.solve( _A , d ) + Xa = Xb + B * Ha * _u else: - if Y.size > 100: # len(R) - _A = BI + Ha * RI * Hm - _u = numpy.linalg.solve( _A , Ha * RI * d ) - Xa = Xb + _u - else: - K = (BI + Ha * RI * Hm).I * Ha * RI - Xa = Xb + K*d + _A = BI + Ha * RI * Hm + _u = numpy.linalg.solve( _A , Ha * RI * d ) + Xa = Xb + _u self.StoredVariables["Analysis"].store( Xa.A1 ) # # Calcul de la fonction coût @@ -150,9 +142,8 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # --------------------------------- if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"] or \ "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]: - if (Y.size <= Xb.size) and (Y.size > 100): K = B * Ha * (R + Hm * B * Ha).I - elif (Y.size > Xb.size) and (Y.size > 100): K = (BI + Ha * RI * Hm).I * Ha * RI - else: pass # K deja calcule + if (Y.size <= Xb.size): K = B * Ha * (R + Hm * B * Ha).I + elif (Y.size > Xb.size): K = (BI + Ha * RI * Hm).I * Ha * RI A = B - K * Hm * B if min(A.shape) != max(A.shape): 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))) diff --git a/src/daComposant/daAlgorithms/ExtendedBlue.py b/src/daComposant/daAlgorithms/ExtendedBlue.py index c36ded9..441c600 100644 --- a/src/daComposant/daAlgorithms/ExtendedBlue.py +++ b/src/daComposant/daAlgorithms/ExtendedBlue.py @@ -108,21 +108,13 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # Calcul de la matrice de gain et de l'analyse # -------------------------------------------- if Y.size <= Xb.size: - if Y.size > 100: # len(R) - _A = R + Hm * B * Ha - _u = numpy.linalg.solve( _A , d ) - Xa = Xb + B * Ha * _u - else: - K = B * Ha * (R + Hm * B * Ha).I - Xa = Xb + K*d + _A = R + Hm * B * Ha + _u = numpy.linalg.solve( _A , d ) + Xa = Xb + B * Ha * _u else: - if Y.size > 100: # len(R) - _A = BI + Ha * RI * Hm - _u = numpy.linalg.solve( _A , Ha * RI * d ) - Xa = Xb + _u - else: - K = (BI + Ha * RI * Hm).I * Ha * RI - Xa = Xb + K*d + _A = BI + Ha * RI * Hm + _u = numpy.linalg.solve( _A , Ha * RI * d ) + Xa = Xb + _u self.StoredVariables["Analysis"].store( Xa.A1 ) # # Calcul de la fonction coût @@ -151,9 +143,8 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # --------------------------------- if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"] or \ "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]: - if (Y.size <= Xb.size) and (Y.size > 100): K = B * Ha * (R + Hm * B * Ha).I - elif (Y.size > Xb.size) and (Y.size > 100): K = (BI + Ha * RI * Hm).I * Ha * RI - else: pass # K deja calcule + if (Y.size <= Xb.size): K = B * Ha * (R + Hm * B * Ha).I + elif (Y.size > Xb.size): K = (BI + Ha * RI * Hm).I * Ha * RI A = B - K * Hm * B if min(A.shape) != max(A.shape): 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))) diff --git a/src/daComposant/daAlgorithms/SamplingTest.py b/src/daComposant/daAlgorithms/SamplingTest.py index 41d0bfa..b4cbeb7 100644 --- a/src/daComposant/daAlgorithms/SamplingTest.py +++ b/src/daComposant/daAlgorithms/SamplingTest.py @@ -105,7 +105,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): if len(dim) != 3: raise ValueError("For dimension %i, the variable definition \"%s\" is incorrect, it should be [min,max,step]."%(i,dim)) else: - coordinatesList.append(numpy.linspace(dim[0],dim[1],1+int(float(dim[1])-float(dim[0])/float(dim[2])))) + coordinatesList.append(numpy.linspace(dim[0],dim[1],1+int((float(dim[1])-float(dim[0]))/float(dim[2])))) sampleList = itertools.product(*coordinatesList) elif len(self._parameters["SampleAsIndependantRandomVariables"]) > 0: coordinatesList = [] diff --git a/src/daComposant/daCore/AssimilationStudy.py b/src/daComposant/daCore/AssimilationStudy.py index fa942df..3e26d02 100644 --- a/src/daComposant/daCore/AssimilationStudy.py +++ b/src/daComposant/daCore/AssimilationStudy.py @@ -273,16 +273,16 @@ class AssimilationStudy: } """ if (type(asFunction) is type({})) and \ - asFunction.has_key("useApproximatedDerivatives") and bool(asFunction["useApproximatedDerivatives"]) and \ - asFunction.has_key("Direct") and (asFunction["Direct"] is not None): - if not asFunction.has_key("withCenteredDF"): asFunction["withCenteredDF"] = False - if not asFunction.has_key("withIncrement"): asFunction["withIncrement"] = 0.01 - if not asFunction.has_key("withdX"): asFunction["withdX"] = None - if not asFunction.has_key("withAvoidingRedundancy"): asFunction["withAvoidingRedundancy"] = True - if not asFunction.has_key("withToleranceInRedundancy"): asFunction["withToleranceInRedundancy"] = 1.e-18 - if not asFunction.has_key("withLenghtOfRedundancy"): asFunction["withLenghtOfRedundancy"] = -1 - if not asFunction.has_key("withmpEnabled"): asFunction["withmpEnabled"] = False - if not asFunction.has_key("withmpWorkers"): asFunction["withmpWorkers"] = None + ("useApproximatedDerivatives" in asFunction) and bool(asFunction["useApproximatedDerivatives"]) and \ + ("Direct" in asFunction) and (asFunction["Direct"] is not None): + if "withCenteredDF" not in asFunction: asFunction["withCenteredDF"] = False + if "withIncrement" not in asFunction: asFunction["withIncrement"] = 0.01 + if "withdX" not in asFunction: asFunction["withdX"] = None + if "withAvoidingRedundancy" not in asFunction: asFunction["withAvoidingRedundancy"] = True + if "withToleranceInRedundancy" not in asFunction: asFunction["withToleranceInRedundancy"] = 1.e-18 + if "withLenghtOfRedundancy" not in asFunction: asFunction["withLenghtOfRedundancy"] = -1 + if "withmpEnabled" not in asFunction: asFunction["withmpEnabled"] = False + if "withmpWorkers" not in asFunction: asFunction["withmpWorkers"] = None from daNumerics.ApproximatedDerivatives import FDApproximation FDA = FDApproximation( Function = asFunction["Direct"], @@ -299,12 +299,12 @@ class AssimilationStudy: self.__HO["Tangent"] = Operator( fromMethod = FDA.TangentOperator, avoidingRedundancy = avoidRC ) self.__HO["Adjoint"] = Operator( fromMethod = FDA.AdjointOperator, avoidingRedundancy = avoidRC ) elif (type(asFunction) is type({})) and \ - asFunction.has_key("Tangent") and asFunction.has_key("Adjoint") and \ + ("Tangent" in asFunction) and ("Adjoint" in asFunction) and \ (asFunction["Tangent"] is not None) and (asFunction["Adjoint"] is not None): - if not asFunction.has_key("Direct") or (asFunction["Direct"] is None): + if ("Direct" not in asFunction) or (asFunction["Direct"] is None): self.__HO["Direct"] = Operator( fromMethod = asFunction["Tangent"], avoidingRedundancy = avoidRC ) else: - self.__HO["Direct"] = Operator( fromMethod = asFunction["Direct"], avoidingRedundancy = avoidRC ) + self.__HO["Direct"] = Operator( fromMethod = asFunction["Direct"], avoidingRedundancy = avoidRC ) self.__HO["Tangent"] = Operator( fromMethod = asFunction["Tangent"], avoidingRedundancy = avoidRC ) self.__HO["Adjoint"] = Operator( fromMethod = asFunction["Adjoint"], avoidingRedundancy = avoidRC ) elif asMatrix is not None: @@ -377,16 +377,16 @@ class AssimilationStudy: } """ if (type(asFunction) is type({})) and \ - asFunction.has_key("useApproximatedDerivatives") and bool(asFunction["useApproximatedDerivatives"]) and \ - asFunction.has_key("Direct") and (asFunction["Direct"] is not None): - if not asFunction.has_key("withCenteredDF"): asFunction["withCenteredDF"] = False - if not asFunction.has_key("withIncrement"): asFunction["withIncrement"] = 0.01 - if not asFunction.has_key("withdX"): asFunction["withdX"] = None - if not asFunction.has_key("withAvoidingRedundancy"): asFunction["withAvoidingRedundancy"] = True - if not asFunction.has_key("withToleranceInRedundancy"): asFunction["withToleranceInRedundancy"] = 1.e-18 - if not asFunction.has_key("withLenghtOfRedundancy"): asFunction["withLenghtOfRedundancy"] = -1 - if not asFunction.has_key("withmpEnabled"): asFunction["withmpEnabled"] = False - if not asFunction.has_key("withmpWorkers"): asFunction["withmpWorkers"] = None + ("useApproximatedDerivatives" in asFunction) and bool(asFunction["useApproximatedDerivatives"]) and \ + ("Direct" in asFunction) and (asFunction["Direct"] is not None): + if "withCenteredDF" not in asFunction: asFunction["withCenteredDF"] = False + if "withIncrement" not in asFunction: asFunction["withIncrement"] = 0.01 + if "withdX" not in asFunction: asFunction["withdX"] = None + if "withAvoidingRedundancy" not in asFunction: asFunction["withAvoidingRedundancy"] = True + if "withToleranceInRedundancy" not in asFunction: asFunction["withToleranceInRedundancy"] = 1.e-18 + if "withLenghtOfRedundancy" not in asFunction: asFunction["withLenghtOfRedundancy"] = -1 + if "withmpEnabled" not in asFunction: asFunction["withmpEnabled"] = False + if "withmpWorkers" not in asFunction: asFunction["withmpWorkers"] = None from daNumerics.ApproximatedDerivatives import FDApproximation FDA = FDApproximation( Function = asFunction["Direct"], @@ -399,13 +399,13 @@ class AssimilationStudy: mpEnabled = asFunction["withmpEnabled"], mpWorkers = asFunction["withmpWorkers"], ) - self.__EM["Direct"] = Operator( fromMethod = FDA.DirectOperator, avoidingRedundancy = avoidRC ) + self.__EM["Direct"] = Operator( fromMethod = FDA.DirectOperator, avoidingRedundancy = avoidRC ) self.__EM["Tangent"] = Operator( fromMethod = FDA.TangentOperator, avoidingRedundancy = avoidRC ) self.__EM["Adjoint"] = Operator( fromMethod = FDA.AdjointOperator, avoidingRedundancy = avoidRC ) elif (type(asFunction) is type({})) and \ - asFunction.has_key("Tangent") and asFunction.has_key("Adjoint") and \ + ("Tangent" in asFunction) and ("Adjoint" in asFunction) and \ (asFunction["Tangent"] is not None) and (asFunction["Adjoint"] is not None): - if not asFunction.has_key("Direct") or (asFunction["Direct"] is None): + if ("Direct" not in asFunction) or (asFunction["Direct"] is None): self.__EM["Direct"] = Operator( fromMethod = asFunction["Tangent"], avoidingRedundancy = avoidRC ) else: self.__EM["Direct"] = Operator( fromMethod = asFunction["Direct"], avoidingRedundancy = avoidRC ) @@ -503,16 +503,16 @@ class AssimilationStudy: } """ if (type(asFunction) is type({})) and \ - asFunction.has_key("useApproximatedDerivatives") and bool(asFunction["useApproximatedDerivatives"]) and \ - asFunction.has_key("Direct") and (asFunction["Direct"] is not None): - if not asFunction.has_key("withCenteredDF"): asFunction["withCenteredDF"] = False - if not asFunction.has_key("withIncrement"): asFunction["withIncrement"] = 0.01 - if not asFunction.has_key("withdX"): asFunction["withdX"] = None - if not asFunction.has_key("withAvoidingRedundancy"): asFunction["withAvoidingRedundancy"] = True - if not asFunction.has_key("withToleranceInRedundancy"): asFunction["withToleranceInRedundancy"] = 1.e-18 - if not asFunction.has_key("withLenghtOfRedundancy"): asFunction["withLenghtOfRedundancy"] = -1 - if not asFunction.has_key("withmpEnabled"): asFunction["withmpEnabled"] = False - if not asFunction.has_key("withmpWorkers"): asFunction["withmpWorkers"] = None + ("useApproximatedDerivatives" in asFunction) and bool(asFunction["useApproximatedDerivatives"]) and \ + ("Direct" in asFunction) and (asFunction["Direct"] is not None): + if "withCenteredDF" not in asFunction: asFunction["withCenteredDF"] = False + if "withIncrement" not in asFunction: asFunction["withIncrement"] = 0.01 + if "withdX" not in asFunction: asFunction["withdX"] = None + if "withAvoidingRedundancy" not in asFunction: asFunction["withAvoidingRedundancy"] = True + if "withToleranceInRedundancy" not in asFunction: asFunction["withToleranceInRedundancy"] = 1.e-18 + if "withLenghtOfRedundancy" not in asFunction: asFunction["withLenghtOfRedundancy"] = -1 + if "withmpEnabled" not in asFunction: asFunction["withmpEnabled"] = False + if "withmpWorkers" not in asFunction: asFunction["withmpWorkers"] = None from daNumerics.ApproximatedDerivatives import FDApproximation FDA = FDApproximation( Function = asFunction["Direct"], @@ -525,16 +525,16 @@ class AssimilationStudy: mpEnabled = asFunction["withmpEnabled"], mpWorkers = asFunction["withmpWorkers"], ) - self.__CM["Direct"] = Operator( fromMethod = FDA.DirectOperator, avoidingRedundancy = avoidRC ) + self.__CM["Direct"] = Operator( fromMethod = FDA.DirectOperator, avoidingRedundancy = avoidRC ) self.__CM["Tangent"] = Operator( fromMethod = FDA.TangentOperator, avoidingRedundancy = avoidRC ) self.__CM["Adjoint"] = Operator( fromMethod = FDA.AdjointOperator, avoidingRedundancy = avoidRC ) elif (type(asFunction) is type({})) and \ - asFunction.has_key("Tangent") and asFunction.has_key("Adjoint") and \ + ("Tangent" in asFunction) and ("Adjoint" in asFunction) and \ (asFunction["Tangent"] is not None) and (asFunction["Adjoint"] is not None): - if not asFunction.has_key("Direct") or (asFunction["Direct"] is None): + if ("Direct" not in asFunction) or (asFunction["Direct"] is None): self.__CM["Direct"] = Operator( fromMethod = asFunction["Tangent"], avoidingRedundancy = avoidRC ) else: - self.__CM["Direct"] = Operator( fromMethod = asFunction["Direct"], avoidingRedundancy = avoidRC ) + self.__CM["Direct"] = Operator( fromMethod = asFunction["Direct"], avoidingRedundancy = avoidRC ) self.__CM["Tangent"] = Operator( fromMethod = asFunction["Tangent"], avoidingRedundancy = avoidRC ) self.__CM["Adjoint"] = Operator( fromMethod = asFunction["Adjoint"], avoidingRedundancy = avoidRC ) elif asMatrix is not None: @@ -687,9 +687,9 @@ class AssimilationStudy: # # Instancie un objet du type élémentaire du fichier # ------------------------------------------------- - if self.__StoredInputs.has_key(name): + if name in self.__StoredInputs: raise ValueError("A default input with the same name \"%s\" already exists."%str(name)) - elif self.__StoredDiagnostics.has_key(name): + elif name in self.__StoredDiagnostics: raise ValueError("A diagnostic with the same name \"%s\" already exists."%str(name)) else: self.__StoredDiagnostics[name] = self.__diagnosticFile.ElementaryDiagnostic( @@ -809,8 +809,8 @@ class AssimilationStudy: if self.__CM is not None and len(self.__CM) > 0 and not(type(self.__CM) is type({})) and not( __CM_shape[1] == max(__U_shape) ): raise ValueError("Shape characteristic of control model (CM) \"%s\" and control (U) \"%s\" are incompatible."%(__CM_shape,__U_shape)) # - if self.__StoredInputs.has_key("AlgorithmParameters") \ - and self.__StoredInputs["AlgorithmParameters"].has_key("Bounds") \ + if ("AlgorithmParameters" in self.__StoredInputs) \ + and ("Bounds" in self.__StoredInputs["AlgorithmParameters"]) \ and (type(self.__StoredInputs["AlgorithmParameters"]["Bounds"]) is type([]) or type(self.__StoredInputs["AlgorithmParameters"]["Bounds"]) is type(())) \ and (len(self.__StoredInputs["AlgorithmParameters"]["Bounds"]) != max(__Xb_shape)): raise ValueError("The number \"%s\" of bound pairs for the state (X) components is different of the size \"%s\" of the state itself." \ @@ -854,11 +854,11 @@ class AssimilationStudy: stockée qui est renvoyée, et le diagnostic est inatteignable. """ if key is not None: - if self.__algorithm.has_key(key): + if key in self.__algorithm: return self.__algorithm.get( key ) - elif self.__StoredInputs.has_key(key): + elif key in self.__StoredInputs: return self.__StoredInputs[key] - elif self.__StoredDiagnostics.has_key(key): + elif key in self.__StoredDiagnostics: return self.__StoredDiagnostics[key] else: raise ValueError("The requested key \"%s\" does not exists as an input, a diagnostic or a stored variable."%key) @@ -996,7 +996,7 @@ class AssimilationStudy: # Association interne de l'observer à la variable # ----------------------------------------------- for n in VariableNames: - if not self.__algorithm.has_key( n ): + if n not in self.__algorithm: raise ValueError("An observer requires to be set on a variable named %s which does not exist."%n) else: self.__algorithm.StoredVariables[ n ].setDataObserver( @@ -1028,7 +1028,7 @@ class AssimilationStudy: # Association interne de l'observer à la variable # ----------------------------------------------- for n in VariableNames: - if not self.__algorithm.has_key( n ): + if n not in self.__algorithm: raise ValueError("An observer requires to be removed on a variable named %s which does not exist."%n) else: self.__algorithm.StoredVariables[ n ].removeDataObserver( diff --git a/src/daComposant/daCore/BasicObjects.py b/src/daComposant/daCore/BasicObjects.py index eda0a11..69aace2 100644 --- a/src/daComposant/daCore/BasicObjects.py +++ b/src/daComposant/daCore/BasicObjects.py @@ -763,6 +763,122 @@ class Covariance: def __len__(self): return self.shape[0] +# ============================================================================== +def CostFunction3D( + _x, + _Hm = None, # Pour simuler Hm(x) : HO["Direct"].appliedTo + _HmX = None, # Simulation déjà faite de Hm(x) + _arg = None, # Arguments supplementaires pour Hm, sous la forme d'un tuple + _BI = None, + _RI = None, + _Xb = None, + _Y = None, + _SIV = False, # A résorber pour la 8.0 + _SSC = [], # self._parameters["StoreSupplementaryCalculations"] + _nPS = 0, # nbPreviousSteps + _QM = "DA", # QualityMeasure + _SSV = {}, # Entrée et/ou sortie : self.StoredVariables + _fRt = False, # Restitue ou pas la sortie étendue + _sSc = True, # Stocke ou pas les SSC + ): + """ + Fonction-coût générale utile pour les algorithmes statiques/3D : 3DVAR, BLUE + et dérivés, Kalman et dérivés, LeastSquares, SamplingTest, PSO, SA, Tabu, + DFO, QuantileRegression + """ + if not _sSc: + _SIV = False + _SSC = {} + else: + for k in ["CostFunctionJ", + "CostFunctionJb", + "CostFunctionJo", + "CurrentOptimum", + "CurrentState", + "IndexOfOptimum", + "SimulatedObservationAtCurrentOptimum", + "SimulatedObservationAtCurrentState", + ]: + if k not in _SSV: + _SSV[k] = [] + if hasattr(_SSV[k],"store"): + _SSV[k].append = _SSV[k].store # Pour utiliser "append" au lieu de "store" + # + _X = numpy.asmatrix(numpy.ravel( _x )).T + if _SIV or "CurrentState" in _SSC or "CurrentOptimum" in _SSC: + _SSV["CurrentState"].append( _X ) + # + if _HmX is not None: + _HX = _HmX + else: + if _Hm is None: + raise ValueError("%s Operator has to be defined."%(self.__name,)) + if _arg is None: + _HX = _Hm( _X ) + else: + _HX = _Hm( _X, *_arg ) + _HX = numpy.asmatrix(numpy.ravel( _HX )).T + # + if "SimulatedObservationAtCurrentState" in _SSC or \ + "SimulatedObservationAtCurrentOptimum" in _SSC: + _SSV["SimulatedObservationAtCurrentState"].append( _HX ) + # + if numpy.any(numpy.isnan(_HX)): + Jb, Jo, J = numpy.nan, numpy.nan, numpy.nan + else: + _Y = numpy.asmatrix(numpy.ravel( _Y )).T + if _QM in ["AugmentedWeightedLeastSquares", "AWLS", "AugmentedPonderatedLeastSquares", "APLS", "DA"]: + if _BI is None or _RI is None: + raise ValueError("Background and Observation error covariance matrix has to be properly defined!") + _Xb = numpy.asmatrix(numpy.ravel( _Xb )).T + Jb = 0.5 * (_X - _Xb).T * _BI * (_X - _Xb) + Jo = 0.5 * (_Y - _HX).T * _RI * (_Y - _HX) + elif _QM in ["WeightedLeastSquares", "WLS", "PonderatedLeastSquares", "PLS"]: + if _RI is None: + raise ValueError("Observation error covariance matrix has to be properly defined!") + Jb = 0. + Jo = 0.5 * (_Y - _HX).T * _RI * (_Y - _HX) + elif _QM in ["LeastSquares", "LS", "L2"]: + Jb = 0. + Jo = 0.5 * (_Y - _HX).T * (_Y - _HX) + elif _QM in ["AbsoluteValue", "L1"]: + Jb = 0. + Jo = numpy.sum( numpy.abs(_Y - _HX) ) + elif _QM in ["MaximumError", "ME"]: + Jb = 0. + Jo = numpy.max( numpy.abs(_Y - _HX) ) + elif _QM in ["QR", "Null"]: + Jb = 0. + Jo = 0. + else: + raise ValueError("Unknown asked quality measure!") + # + J = float( Jb ) + float( Jo ) + # + if _sSc: + _SSV["CostFunctionJb"].append( Jb ) + _SSV["CostFunctionJo"].append( Jo ) + _SSV["CostFunctionJ" ].append( J ) + # + if "IndexOfOptimum" in _SSC or \ + "CurrentOptimum" in _SSC or \ + "SimulatedObservationAtCurrentOptimum" in _SSC: + IndexMin = numpy.argmin( _SSV["CostFunctionJ"][_nPS:] ) + _nPS + if "IndexOfOptimum" in _SSC: + _SSV["IndexOfOptimum"].append( IndexMin ) + if "CurrentOptimum" in _SSC: + _SSV["CurrentOptimum"].append( _SSV["CurrentState"][IndexMin] ) + if "SimulatedObservationAtCurrentOptimum" in _SSC: + _SSV["SimulatedObservationAtCurrentOptimum"].append( _SSV["SimulatedObservationAtCurrentState"][IndexMin] ) + # + if _fRt: + return _SSV + else: + if _QM in ["QR"]: # Pour le QuantileRegression + return _HX + else: + return J + # ============================================================================== if __name__ == "__main__": print '\n AUTODIAGNOSTIC \n' diff --git a/src/daSalome/daYacsSchemaCreator/methods.py b/src/daSalome/daYacsSchemaCreator/methods.py index 8b662a1..614d9c9 100644 --- a/src/daSalome/daYacsSchemaCreator/methods.py +++ b/src/daSalome/daYacsSchemaCreator/methods.py @@ -19,7 +19,7 @@ # # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -# Author: André Ribes, andre.ribes@edf.fr, EDF R&D +# Author: Andre Ribes, andre.ribes@edf.fr, EDF R&D import sys import traceback @@ -31,6 +31,20 @@ import os from daYacsSchemaCreator.infos_daComposant import * +def _Internal_Add_dir_script_ports(node, sf, ed, br, t_type): + # On conserve le pointeur de "node" et "typ" + __scriptfile = str( sf ) + __exist_dir = bool( ed ) + __base_dir = str( br ) + __full_name = os.path.join(__base_dir, os.path.basename(__scriptfile)) + if os.path.exists(__full_name): + node.getInputPort("script").edInitPy(__full_name) + else: + node.getInputPort("script").edInitPy(__scriptfile) + if __exist_dir: + node.edAddInputPort("studydir", t_type) + node.getInputPort("studydir").edInitPy(__base_dir) + def create_yacs_proc(study_config): logging.debug("[create_yacs_proc]") @@ -56,11 +70,12 @@ def create_yacs_proc(study_config): t_bool = proc.getTypeCode("bool") t_param_input = proc.getTypeCode("SALOME_TYPES/ParametricInput") t_param_output = proc.getTypeCode("SALOME_TYPES/ParametricOutput") - repertory = False - base_repertory = "" if "Repertory" in study_config.keys(): base_repertory = study_config["Repertory"] - repertory = True + repertory = True + else: + base_repertory = "" + repertory = False # Create ADAO case bloc ADAO_Case = runtime.createBloc("ADAO_Case_Bloc") @@ -121,14 +136,7 @@ def create_yacs_proc(study_config): init_config = study_config["UserDataInit"] factory_init_node = catalogAd.getNodeFromNodeMap("UserDataInitFromScript") init_node = factory_init_node.cloneNode("UserDataInit") - if repertory and not os.path.exists(init_config["Data"]): - init_node.getInputPort("script").edInitPy(os.path.join(base_repertory, os.path.basename(init_config["Data"]))) - elif repertory and os.path.exists(init_config["Data"]): - init_node.getInputPort("script").edInitPy(init_config["Data"]) - init_node.edAddInputPort("studydir", t_string) - init_node.getInputPort("studydir").edInitPy(base_repertory) - else: - init_node.getInputPort("script").edInitPy(init_config["Data"]) + _Internal_Add_dir_script_ports( init_node, init_config["Data"], repertory, base_repertory, t_string) init_node_script = init_node.getScript() init_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n" init_node_script += "init_data = user_script_module.init_data\n" @@ -137,8 +145,12 @@ def create_yacs_proc(study_config): # Step 1: get input data from user configuration - for key in study_config.keys(): - if key in AssimData: + st_keys = study_config.keys() + st_keys.sort() + for key in st_keys: + ad_keys = AssimData + ad_keys.sort() + if key in ad_keys: data_config = study_config[key] key_type = key + "Type" @@ -148,14 +160,7 @@ def create_yacs_proc(study_config): # Create node factory_back_node = catalogAd.getNodeFromNodeMap("CreateDictFromScript") back_node = factory_back_node.cloneNode("Get" + key) - if repertory and not os.path.exists(data_config["Data"]): - back_node.getInputPort("script").edInitPy(os.path.join(base_repertory, os.path.basename(data_config["Data"]))) - elif repertory and os.path.exists(data_config["Data"]): - back_node.getInputPort("script").edInitPy(data_config["Data"]) - back_node.edAddInputPort("studydir", t_string) - back_node.getInputPort("studydir").edInitPy(base_repertory) - else: - back_node.getInputPort("script").edInitPy(data_config["Data"]) + _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string) back_node.edAddOutputPort(key, t_pyobj) ADAO_Case.edAddChild(back_node) # Set content of the node @@ -220,14 +225,7 @@ def create_yacs_proc(study_config): # Create node factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorFromScript") back_node = factory_back_node.cloneNode("Get" + key) - if repertory and not os.path.exists(data_config["Data"]): - back_node.getInputPort("script").edInitPy(os.path.join(base_repertory, os.path.basename(data_config["Data"]))) - elif repertory and os.path.exists(data_config["Data"]): - back_node.getInputPort("script").edInitPy(data_config["Data"]) - back_node.edAddInputPort("studydir", t_string) - back_node.getInputPort("studydir").edInitPy(base_repertory) - else: - back_node.getInputPort("script").edInitPy(data_config["Data"]) + _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string) back_node.edAddOutputPort(key, t_pyobj) ADAO_Case.edAddChild(back_node) # Set content of the node @@ -276,14 +274,7 @@ def create_yacs_proc(study_config): # Create node factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorSerieFromScript") back_node = factory_back_node.cloneNode("Get" + key) - if repertory and not os.path.exists(data_config["Data"]): - back_node.getInputPort("script").edInitPy(os.path.join(base_repertory, os.path.basename(data_config["Data"]))) - elif repertory and os.path.exists(data_config["Data"]): - back_node.getInputPort("script").edInitPy(data_config["Data"]) - back_node.edAddInputPort("studydir", t_string) - back_node.getInputPort("studydir").edInitPy(base_repertory) - else: - back_node.getInputPort("script").edInitPy(data_config["Data"]) + _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string) back_node.edAddOutputPort(key, t_pyobj) ADAO_Case.edAddChild(back_node) # Set content of the node @@ -332,14 +323,7 @@ def create_yacs_proc(study_config): # Create node factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpy%sFromScript"%(data_config["Type"],)) back_node = factory_back_node.cloneNode("Get" + key) - if repertory and not os.path.exists(data_config["Data"]): - back_node.getInputPort("script").edInitPy(os.path.join(base_repertory, os.path.basename(data_config["Data"]))) - elif repertory and os.path.exists(data_config["Data"]): - back_node.getInputPort("script").edInitPy(data_config["Data"]) - back_node.edAddInputPort("studydir", t_string) - back_node.getInputPort("studydir").edInitPy(base_repertory) - else: - back_node.getInputPort("script").edInitPy(data_config["Data"]) + _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string) back_node.edAddOutputPort(key, t_pyobj) ADAO_Case.edAddChild(back_node) # Set content of the node @@ -366,16 +350,14 @@ def create_yacs_proc(study_config): for FunctionName in TheData["Function"]: port_name = key + FunctionName CAS_node.edAddInputPort(port_name, t_string) - if repertory and not os.path.exists(TheData["Script"][FunctionName]): + if os.path.exists(os.path.join(base_repertory, os.path.basename(TheData["Script"][FunctionName]))): CAS_node.getInputPort(port_name).edInitPy(os.path.join(base_repertory, os.path.basename(TheData["Script"][FunctionName]))) - elif repertory and os.path.exists(TheData["Script"][FunctionName]): - CAS_node.getInputPort(port_name).edInitPy(TheData["Script"][FunctionName]) - try: - CAS_node.edAddInputPort("studydir", t_string) - CAS_node.getInputPort("studydir").edInitPy(base_repertory) - except: pass else: CAS_node.getInputPort(port_name).edInitPy(TheData["Script"][FunctionName]) + try: + CAS_node.edAddInputPort("studydir", t_string) + CAS_node.getInputPort("studydir").edInitPy(base_repertory) + except: pass # Step 3: create compute bloc compute_bloc = runtime.createBloc("compute_bloc") @@ -402,7 +384,7 @@ def create_yacs_proc(study_config): break # We create a new pyscript node opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO") - if repertory and not os.path.exists(script_filename): + if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))): script_filename = os.path.join(base_repertory, os.path.basename(script_filename)) try: script_str= open(script_filename, 'r') @@ -430,7 +412,7 @@ def create_yacs_proc(study_config): # We create a new pyscript node opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO") - if repertory and not os.path.exists(script_filename): + if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))): script_filename = os.path.join(base_repertory, os.path.basename(script_filename)) try: script_str= open(script_filename, 'r') @@ -510,7 +492,7 @@ def create_yacs_proc(study_config): # We create a new pyscript node opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO") - if repertory and not os.path.exists(script_filename): + if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))): script_filename = os.path.join(base_repertory, os.path.basename(script_filename)) try: script_str= open(script_filename, 'r') @@ -599,7 +581,7 @@ def create_yacs_proc(study_config): break # We create a new pyscript node opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM") - if repertory and not os.path.exists(script_filename): + if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))): script_filename = os.path.join(base_repertory, os.path.basename(script_filename)) try: script_str= open(script_filename, 'r') @@ -626,7 +608,7 @@ def create_yacs_proc(study_config): break # We create a new pyscript node opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM") - if repertory and not os.path.exists(script_filename): + if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))): script_filename = os.path.join(base_repertory, os.path.basename(script_filename)) try: script_str= open(script_filename, 'r') @@ -708,7 +690,7 @@ def create_yacs_proc(study_config): break # We create a new pyscript node opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM") - if repertory and not os.path.exists(script_filename): + if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))): script_filename = os.path.join(base_repertory, os.path.basename(script_filename)) try: script_str= open(script_filename, 'r') @@ -842,14 +824,7 @@ def create_yacs_proc(study_config): else: factory_observation_node = catalogAd.getNodeFromNodeMap("ObservationNodeFile") observation_node = factory_observation_node.cloneNode("Observation") - if repertory and not os.path.exists(observer_cfg["Script"]): - observation_node.getInputPort("script").edInitPy(os.path.join(base_repertory, os.path.basename(observer_cfg["Script"]))) - elif repertory and os.path.exists(observer_cfg["Script"]): - observation_node.getInputPort("script").edInitPy(observer_cfg["Script"]) - observation_node.edAddInputPort("studydir", t_string) - observation_node.getInputPort("studydir").edInitPy(base_repertory) - else: - observation_node.getInputPort("script").edInitPy(observer_cfg["Script"]) + _Internal_Add_dir_script_ports( observation_node, observer_cfg["Script"], repertory, base_repertory, t_string) observer_bloc.edAddChild(observation_node) ADAO_Case.edAddDFLink(extract_data_node.getOutputPort("var"), observation_node.getInputPort("var")) ADAO_Case.edAddDFLink(extract_data_node.getOutputPort("info"), observation_node.getInputPort("info")) @@ -940,7 +915,7 @@ def create_yacs_proc(study_config): analysis_node = factory_analysis_node.cloneNode("UserPostAnalysis") default_script = analysis_node.getScript() analysis_file_name = analysis_config["Data"] - if repertory and not os.path.exists(analysis_file_name): + if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(analysis_file_name))): analysis_file_name = os.path.join(base_repertory, os.path.basename(analysis_file_name)) try: analysis_file = open(analysis_file_name, 'r')