Salome HOME
8b677eec6df3edc1c07ee93dbecea0de59a679b0
[modules/adao.git] / src / daComposant / daCore / NumericObjects.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 __doc__ = """
24     Définit les versions approximées des opérateurs tangents et adjoints.
25 """
26 __author__ = "Jean-Philippe ARGAUD"
27
28 import os, time, copy, types, sys, logging
29 import math, numpy, scipy
30 from daCore.BasicObjects import Operator
31 from daCore.PlatformInfo import PlatformInfo
32 mpr = PlatformInfo().MachinePrecision()
33 mfp = PlatformInfo().MaximumPrecision()
34 # logging.getLogger().setLevel(logging.DEBUG)
35
36 # ==============================================================================
37 def ExecuteFunction( paire ):
38     assert len(paire) == 2, "Incorrect number of arguments"
39     X, funcrepr = paire
40     __X = numpy.asmatrix(numpy.ravel( X )).T
41     __sys_path_tmp = sys.path ; sys.path.insert(0,funcrepr["__userFunction__path"])
42     __module = __import__(funcrepr["__userFunction__modl"], globals(), locals(), [])
43     __fonction = getattr(__module,funcrepr["__userFunction__name"])
44     sys.path = __sys_path_tmp ; del __sys_path_tmp
45     __HX  = __fonction( __X )
46     return numpy.ravel( __HX )
47
48 # ==============================================================================
49 class FDApproximation(object):
50     """
51     Cette classe sert d'interface pour définir les opérateurs approximés. A la
52     création d'un objet, en fournissant une fonction "Function", on obtient un
53     objet qui dispose de 3 méthodes "DirectOperator", "TangentOperator" et
54     "AdjointOperator". On contrôle l'approximation DF avec l'incrément
55     multiplicatif "increment" valant par défaut 1%, ou avec l'incrément fixe
56     "dX" qui sera multiplié par "increment" (donc en %), et on effectue de DF
57     centrées si le booléen "centeredDF" est vrai.
58     """
59     def __init__(self,
60             Function              = None,
61             centeredDF            = False,
62             increment             = 0.01,
63             dX                    = None,
64             avoidingRedundancy    = True,
65             toleranceInRedundancy = 1.e-18,
66             lenghtOfRedundancy    = -1,
67             mpEnabled             = False,
68             mpWorkers             = None,
69             mfEnabled             = False,
70             ):
71         if mpEnabled:
72             try:
73                 import multiprocessing
74                 self.__mpEnabled = True
75             except ImportError:
76                 self.__mpEnabled = False
77         else:
78             self.__mpEnabled = False
79         self.__mpWorkers = mpWorkers
80         if self.__mpWorkers is not None and self.__mpWorkers < 1:
81             self.__mpWorkers = None
82         logging.debug("FDA Calculs en multiprocessing : %s (nombre de processus : %s)"%(self.__mpEnabled,self.__mpWorkers))
83         #
84         if mfEnabled:
85             self.__mfEnabled = True
86         else:
87             self.__mfEnabled = False
88         logging.debug("FDA Calculs en multifonctions : %s"%(self.__mfEnabled,))
89         #
90         if avoidingRedundancy:
91             self.__avoidRC = True
92             self.__tolerBP = float(toleranceInRedundancy)
93             self.__lenghtRJ = int(lenghtOfRedundancy)
94             self.__listJPCP = [] # Jacobian Previous Calculated Points
95             self.__listJPCI = [] # Jacobian Previous Calculated Increment
96             self.__listJPCR = [] # Jacobian Previous Calculated Results
97             self.__listJPPN = [] # Jacobian Previous Calculated Point Norms
98             self.__listJPIN = [] # Jacobian Previous Calculated Increment Norms
99         else:
100             self.__avoidRC = False
101         #
102         if self.__mpEnabled:
103             if isinstance(Function,types.FunctionType):
104                 logging.debug("FDA Calculs en multiprocessing : FunctionType")
105                 self.__userFunction__name = Function.__name__
106                 try:
107                     mod = os.path.join(Function.__globals__['filepath'],Function.__globals__['filename'])
108                 except:
109                     mod = os.path.abspath(Function.__globals__['__file__'])
110                 if not os.path.isfile(mod):
111                     raise ImportError("No user defined function or method found with the name %s"%(mod,))
112                 self.__userFunction__modl = os.path.basename(mod).replace('.pyc','').replace('.pyo','').replace('.py','')
113                 self.__userFunction__path = os.path.dirname(mod)
114                 del mod
115                 self.__userOperator = Operator( fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled )
116                 self.__userFunction = self.__userOperator.appliedTo # Pour le calcul Direct
117             elif isinstance(Function,types.MethodType):
118                 logging.debug("FDA Calculs en multiprocessing : MethodType")
119                 self.__userFunction__name = Function.__name__
120                 try:
121                     mod = os.path.join(Function.__globals__['filepath'],Function.__globals__['filename'])
122                 except:
123                     mod = os.path.abspath(Function.__func__.__globals__['__file__'])
124                 if not os.path.isfile(mod):
125                     raise ImportError("No user defined function or method found with the name %s"%(mod,))
126                 self.__userFunction__modl = os.path.basename(mod).replace('.pyc','').replace('.pyo','').replace('.py','')
127                 self.__userFunction__path = os.path.dirname(mod)
128                 del mod
129                 self.__userOperator = Operator( fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled )
130                 self.__userFunction = self.__userOperator.appliedTo # Pour le calcul Direct
131             else:
132                 raise TypeError("User defined function or method has to be provided for finite differences approximation.")
133         else:
134             self.__userOperator = Operator( fromMethod = Function, avoidingRedundancy = self.__avoidRC, inputAsMultiFunction = self.__mfEnabled )
135             self.__userFunction = self.__userOperator.appliedTo
136         #
137         self.__centeredDF = bool(centeredDF)
138         if abs(float(increment)) > 1.e-15:
139             self.__increment  = float(increment)
140         else:
141             self.__increment  = 0.01
142         if dX is None:
143             self.__dX     = None
144         else:
145             self.__dX     = numpy.asmatrix(numpy.ravel( dX )).T
146         logging.debug("FDA Reduction des doublons de calcul : %s"%self.__avoidRC)
147         if self.__avoidRC:
148             logging.debug("FDA Tolerance de determination des doublons : %.2e"%self.__tolerBP)
149
150     # ---------------------------------------------------------
151     def __doublon__(self, e, l, n, v=None):
152         __ac, __iac = False, -1
153         for i in range(len(l)-1,-1,-1):
154             if numpy.linalg.norm(e - l[i]) < self.__tolerBP * n[i]:
155                 __ac, __iac = True, i
156                 if v is not None: logging.debug("FDA Cas%s déja calculé, récupération du doublon %i"%(v,__iac))
157                 break
158         return __ac, __iac
159
160     # ---------------------------------------------------------
161     def DirectOperator(self, X ):
162         """
163         Calcul du direct à l'aide de la fonction fournie.
164         """
165         logging.debug("FDA Calcul DirectOperator (explicite)")
166         if self.__mfEnabled:
167             _HX = self.__userFunction( X, argsAsSerie = True )
168         else:
169             _X = numpy.asmatrix(numpy.ravel( X )).T
170             _HX = numpy.ravel(self.__userFunction( _X ))
171         #
172         return _HX
173
174     # ---------------------------------------------------------
175     def TangentMatrix(self, X ):
176         """
177         Calcul de l'opérateur tangent comme la Jacobienne par différences finies,
178         c'est-à-dire le gradient de H en X. On utilise des différences finies
179         directionnelles autour du point X. X est un numpy.matrix.
180
181         Différences finies centrées (approximation d'ordre 2):
182         1/ Pour chaque composante i de X, on ajoute et on enlève la perturbation
183            dX[i] à la  composante X[i], pour composer X_plus_dXi et X_moins_dXi, et
184            on calcule les réponses HX_plus_dXi = H( X_plus_dXi ) et HX_moins_dXi =
185            H( X_moins_dXi )
186         2/ On effectue les différences (HX_plus_dXi-HX_moins_dXi) et on divise par
187            le pas 2*dXi
188         3/ Chaque résultat, par composante, devient une colonne de la Jacobienne
189
190         Différences finies non centrées (approximation d'ordre 1):
191         1/ Pour chaque composante i de X, on ajoute la perturbation dX[i] à la
192            composante X[i] pour composer X_plus_dXi, et on calcule la réponse
193            HX_plus_dXi = H( X_plus_dXi )
194         2/ On calcule la valeur centrale HX = H(X)
195         3/ On effectue les différences (HX_plus_dXi-HX) et on divise par
196            le pas dXi
197         4/ Chaque résultat, par composante, devient une colonne de la Jacobienne
198
199         """
200         logging.debug("FDA Début du calcul de la Jacobienne")
201         logging.debug("FDA   Incrément de............: %s*X"%float(self.__increment))
202         logging.debug("FDA   Approximation centrée...: %s"%(self.__centeredDF))
203         #
204         if X is None or len(X)==0:
205             raise ValueError("Nominal point X for approximate derivatives can not be None or void (given X: %s)."%(str(X),))
206         #
207         _X = numpy.asmatrix(numpy.ravel( X )).T
208         #
209         if self.__dX is None:
210             _dX  = self.__increment * _X
211         else:
212             _dX = numpy.asmatrix(numpy.ravel( self.__dX )).T
213         #
214         if (_dX == 0.).any():
215             moyenne = _dX.mean()
216             if moyenne == 0.:
217                 _dX = numpy.where( _dX == 0., float(self.__increment), _dX )
218             else:
219                 _dX = numpy.where( _dX == 0., moyenne, _dX )
220         #
221         __alreadyCalculated  = False
222         if self.__avoidRC:
223             __bidon, __alreadyCalculatedP = self.__doublon__(_X,  self.__listJPCP, self.__listJPPN, None)
224             __bidon, __alreadyCalculatedI = self.__doublon__(_dX, self.__listJPCI, self.__listJPIN, None)
225             if __alreadyCalculatedP == __alreadyCalculatedI > -1:
226                 __alreadyCalculated, __i = True, __alreadyCalculatedP
227                 logging.debug("FDA Cas J déja calculé, récupération du doublon %i"%__i)
228         #
229         if __alreadyCalculated:
230             logging.debug("FDA   Calcul Jacobienne (par récupération du doublon %i)"%__i)
231             _Jacobienne = self.__listJPCR[__i]
232         else:
233             logging.debug("FDA   Calcul Jacobienne (explicite)")
234             if self.__centeredDF:
235                 #
236                 if self.__mpEnabled and not self.__mfEnabled:
237                     funcrepr = {
238                         "__userFunction__path" : self.__userFunction__path,
239                         "__userFunction__modl" : self.__userFunction__modl,
240                         "__userFunction__name" : self.__userFunction__name,
241                     }
242                     _jobs = []
243                     for i in range( len(_dX) ):
244                         _dXi            = _dX[i]
245                         _X_plus_dXi     = numpy.array( _X.A1, dtype=float )
246                         _X_plus_dXi[i]  = _X[i] + _dXi
247                         _X_moins_dXi    = numpy.array( _X.A1, dtype=float )
248                         _X_moins_dXi[i] = _X[i] - _dXi
249                         #
250                         _jobs.append( (_X_plus_dXi,  funcrepr) )
251                         _jobs.append( (_X_moins_dXi, funcrepr) )
252                     #
253                     import multiprocessing
254                     self.__pool = multiprocessing.Pool(self.__mpWorkers)
255                     _HX_plusmoins_dX = self.__pool.map( ExecuteFunction, _jobs )
256                     self.__pool.close()
257                     self.__pool.join()
258                     #
259                     _Jacobienne  = []
260                     for i in range( len(_dX) ):
261                         _Jacobienne.append( numpy.ravel( _HX_plusmoins_dX[2*i] - _HX_plusmoins_dX[2*i+1] ) / (2.*_dX[i]) )
262                     #
263                 elif self.__mfEnabled:
264                     _xserie = []
265                     for i in range( len(_dX) ):
266                         _dXi            = _dX[i]
267                         _X_plus_dXi     = numpy.array( _X.A1, dtype=float )
268                         _X_plus_dXi[i]  = _X[i] + _dXi
269                         _X_moins_dXi    = numpy.array( _X.A1, dtype=float )
270                         _X_moins_dXi[i] = _X[i] - _dXi
271                         #
272                         _xserie.append( _X_plus_dXi )
273                         _xserie.append( _X_moins_dXi )
274                     #
275                     _HX_plusmoins_dX = self.DirectOperator( _xserie )
276                      #
277                     _Jacobienne  = []
278                     for i in range( len(_dX) ):
279                         _Jacobienne.append( numpy.ravel( _HX_plusmoins_dX[2*i] - _HX_plusmoins_dX[2*i+1] ) / (2.*_dX[i]) )
280                     #
281                 else:
282                     _Jacobienne  = []
283                     for i in range( _dX.size ):
284                         _dXi            = _dX[i]
285                         _X_plus_dXi     = numpy.array( _X.A1, dtype=float )
286                         _X_plus_dXi[i]  = _X[i] + _dXi
287                         _X_moins_dXi    = numpy.array( _X.A1, dtype=float )
288                         _X_moins_dXi[i] = _X[i] - _dXi
289                         #
290                         _HX_plus_dXi    = self.DirectOperator( _X_plus_dXi )
291                         _HX_moins_dXi   = self.DirectOperator( _X_moins_dXi )
292                         #
293                         _Jacobienne.append( numpy.ravel( _HX_plus_dXi - _HX_moins_dXi ) / (2.*_dXi) )
294                 #
295             else:
296                 #
297                 if self.__mpEnabled and not self.__mfEnabled:
298                     funcrepr = {
299                         "__userFunction__path" : self.__userFunction__path,
300                         "__userFunction__modl" : self.__userFunction__modl,
301                         "__userFunction__name" : self.__userFunction__name,
302                     }
303                     _jobs = []
304                     _jobs.append( (_X.A1, funcrepr) )
305                     for i in range( len(_dX) ):
306                         _X_plus_dXi    = numpy.array( _X.A1, dtype=float )
307                         _X_plus_dXi[i] = _X[i] + _dX[i]
308                         #
309                         _jobs.append( (_X_plus_dXi, funcrepr) )
310                     #
311                     import multiprocessing
312                     self.__pool = multiprocessing.Pool(self.__mpWorkers)
313                     _HX_plus_dX = self.__pool.map( ExecuteFunction, _jobs )
314                     self.__pool.close()
315                     self.__pool.join()
316                     #
317                     _HX = _HX_plus_dX.pop(0)
318                     #
319                     _Jacobienne = []
320                     for i in range( len(_dX) ):
321                         _Jacobienne.append( numpy.ravel(( _HX_plus_dX[i] - _HX ) / _dX[i]) )
322                     #
323                 elif self.__mfEnabled:
324                     _xserie = []
325                     _xserie.append( _X.A1 )
326                     for i in range( len(_dX) ):
327                         _X_plus_dXi    = numpy.array( _X.A1, dtype=float )
328                         _X_plus_dXi[i] = _X[i] + _dX[i]
329                         #
330                         _xserie.append( _X_plus_dXi )
331                     #
332                     _HX_plus_dX = self.DirectOperator( _xserie )
333                     #
334                     _HX = _HX_plus_dX.pop(0)
335                     #
336                     _Jacobienne = []
337                     for i in range( len(_dX) ):
338                         _Jacobienne.append( numpy.ravel(( _HX_plus_dX[i] - _HX ) / _dX[i]) )
339                    #
340                 else:
341                     _Jacobienne  = []
342                     _HX = self.DirectOperator( _X )
343                     for i in range( _dX.size ):
344                         _dXi            = _dX[i]
345                         _X_plus_dXi     = numpy.array( _X.A1, dtype=float )
346                         _X_plus_dXi[i]  = _X[i] + _dXi
347                         #
348                         _HX_plus_dXi = self.DirectOperator( _X_plus_dXi )
349                         #
350                         _Jacobienne.append( numpy.ravel(( _HX_plus_dXi - _HX ) / _dXi) )
351                 #
352             #
353             _Jacobienne = numpy.asmatrix( numpy.vstack( _Jacobienne ) ).T
354             if self.__avoidRC:
355                 if self.__lenghtRJ < 0: self.__lenghtRJ = 2 * _X.size
356                 while len(self.__listJPCP) > self.__lenghtRJ:
357                     self.__listJPCP.pop(0)
358                     self.__listJPCI.pop(0)
359                     self.__listJPCR.pop(0)
360                     self.__listJPPN.pop(0)
361                     self.__listJPIN.pop(0)
362                 self.__listJPCP.append( copy.copy(_X) )
363                 self.__listJPCI.append( copy.copy(_dX) )
364                 self.__listJPCR.append( copy.copy(_Jacobienne) )
365                 self.__listJPPN.append( numpy.linalg.norm(_X) )
366                 self.__listJPIN.append( numpy.linalg.norm(_Jacobienne) )
367         #
368         logging.debug("FDA Fin du calcul de la Jacobienne")
369         #
370         return _Jacobienne
371
372     # ---------------------------------------------------------
373     def TangentOperator(self, paire ):
374         """
375         Calcul du tangent à l'aide de la Jacobienne.
376         """
377         if self.__mfEnabled:
378             assert len(paire) == 1, "Incorrect lenght of arguments"
379             _paire = paire[0]
380             assert len(_paire) == 2, "Incorrect number of arguments"
381         else:
382             assert len(paire) == 2, "Incorrect number of arguments"
383             _paire = paire
384         X, dX = _paire
385         _Jacobienne = self.TangentMatrix( X )
386         if dX is None or len(dX) == 0:
387             #
388             # Calcul de la forme matricielle si le second argument est None
389             # -------------------------------------------------------------
390             if self.__mfEnabled: return [_Jacobienne,]
391             else:                return _Jacobienne
392         else:
393             #
394             # Calcul de la valeur linéarisée de H en X appliqué à dX
395             # ------------------------------------------------------
396             _dX = numpy.asmatrix(numpy.ravel( dX )).T
397             _HtX = numpy.dot(_Jacobienne, _dX)
398             if self.__mfEnabled: return [_HtX.A1,]
399             else:                return _HtX.A1
400
401     # ---------------------------------------------------------
402     def AdjointOperator(self, paire ):
403         """
404         Calcul de l'adjoint à l'aide de la Jacobienne.
405         """
406         if self.__mfEnabled:
407             assert len(paire) == 1, "Incorrect lenght of arguments"
408             _paire = paire[0]
409             assert len(_paire) == 2, "Incorrect number of arguments"
410         else:
411             assert len(paire) == 2, "Incorrect number of arguments"
412             _paire = paire
413         X, Y = _paire
414         _JacobienneT = self.TangentMatrix( X ).T
415         if Y is None or len(Y) == 0:
416             #
417             # Calcul de la forme matricielle si le second argument est None
418             # -------------------------------------------------------------
419             if self.__mfEnabled: return [_JacobienneT,]
420             else:                return _JacobienneT
421         else:
422             #
423             # Calcul de la valeur de l'adjoint en X appliqué à Y
424             # --------------------------------------------------
425             _Y = numpy.asmatrix(numpy.ravel( Y )).T
426             _HaY = numpy.dot(_JacobienneT, _Y)
427             if self.__mfEnabled: return [_HaY.A1,]
428             else:                return _HaY.A1
429
430 # ==============================================================================
431 def mmqr(
432         func     = None,
433         x0       = None,
434         fprime   = None,
435         bounds   = None,
436         quantile = 0.5,
437         maxfun   = 15000,
438         toler    = 1.e-06,
439         y        = None,
440         ):
441     """
442     Implémentation informatique de l'algorithme MMQR, basée sur la publication :
443     David R. Hunter, Kenneth Lange, "Quantile Regression via an MM Algorithm",
444     Journal of Computational and Graphical Statistics, 9, 1, pp.60-77, 2000.
445     """
446     #
447     # Recuperation des donnees et informations initiales
448     # --------------------------------------------------
449     variables = numpy.ravel( x0 )
450     mesures   = numpy.ravel( y )
451     increment = sys.float_info[0]
452     p         = variables.size
453     n         = mesures.size
454     quantile  = float(quantile)
455     #
456     # Calcul des parametres du MM
457     # ---------------------------
458     tn      = float(toler) / n
459     e0      = -tn / math.log(tn)
460     epsilon = (e0-tn)/(1+math.log(e0))
461     #
462     # Calculs d'initialisation
463     # ------------------------
464     residus  = mesures - numpy.ravel( func( variables ) )
465     poids    = 1./(epsilon+numpy.abs(residus))
466     veps     = 1. - 2. * quantile - residus * poids
467     lastsurrogate = -numpy.sum(residus*veps) - (1.-2.*quantile)*numpy.sum(residus)
468     iteration = 0
469     #
470     # Recherche iterative
471     # -------------------
472     while (increment > toler) and (iteration < maxfun) :
473         iteration += 1
474         #
475         Derivees  = numpy.array(fprime(variables))
476         Derivees  = Derivees.reshape(n,p) # Necessaire pour remettre en place la matrice si elle passe par des tuyaux YACS
477         DeriveesT = Derivees.transpose()
478         M         =   numpy.dot( DeriveesT , (numpy.array(numpy.matrix(p*[poids,]).T)*Derivees) )
479         SM        =   numpy.transpose(numpy.dot( DeriveesT , veps ))
480         step      = - numpy.linalg.lstsq( M, SM, rcond=-1 )[0]
481         #
482         variables = variables + step
483         if bounds is not None:
484             while( (variables < numpy.ravel(numpy.asmatrix(bounds)[:,0])).any() or (variables > numpy.ravel(numpy.asmatrix(bounds)[:,1])).any() ):
485                 step      = step/2.
486                 variables = variables - step
487         residus   = mesures - numpy.ravel( func(variables) )
488         surrogate = numpy.sum(residus**2 * poids) + (4.*quantile-2.) * numpy.sum(residus)
489         #
490         while ( (surrogate > lastsurrogate) and ( max(list(numpy.abs(step))) > 1.e-16 ) ) :
491             step      = step/2.
492             variables = variables - step
493             residus   = mesures - numpy.ravel( func(variables) )
494             surrogate = numpy.sum(residus**2 * poids) + (4.*quantile-2.) * numpy.sum(residus)
495         #
496         increment     = lastsurrogate-surrogate
497         poids         = 1./(epsilon+numpy.abs(residus))
498         veps          = 1. - 2. * quantile - residus * poids
499         lastsurrogate = -numpy.sum(residus * veps) - (1.-2.*quantile)*numpy.sum(residus)
500     #
501     # Mesure d'écart
502     # --------------
503     Ecart = quantile * numpy.sum(residus) - numpy.sum( residus[residus<0] )
504     #
505     return variables, Ecart, [n,p,iteration,increment,0]
506
507 # ==============================================================================
508
509 def _BackgroundEnsembleGeneration( _bgcenter, _bgcovariance, _nbmembers, _withSVD = True):
510     "Génération d'un ensemble d'ébauche de taille _nbmembers-1"
511     # ~ numpy.random.seed(1234567)
512     if _nbmembers < 1:
513         raise ValueError("Number of members has to be strictly more than 1 (given number: %s)."%(str(_nbmembers),))
514     if _withSVD:
515         U, s, V = numpy.linalg.svd(_bgcovariance, full_matrices=False)
516         _nbctl = len(_bgcenter)
517         if _nbmembers > _nbctl:
518             _Z = numpy.concatenate((numpy.dot(
519                 numpy.diag(numpy.sqrt(s[:_nbctl])), V[:_nbctl]),
520                 numpy.random.multivariate_normal(numpy.zeros(_nbctl),_bgcovariance,_nbmembers-1-_nbctl)), axis = 0)
521         else:
522             _Z = numpy.dot(numpy.diag(numpy.sqrt(s[:_nbmembers-1])), V[:_nbmembers-1])
523         _Zca = _CenteredAnomalies(_Z, _nbmembers)
524         BackgroundEnsemble = (_bgcenter + _Zca.T).T
525     else:
526         if max(abs(_bgcovariance.flatten())) > 0:
527             _nbctl = len(_bgcenter)
528             _Z = numpy.random.multivariate_normal(numpy.zeros(_nbctl),_bgcovariance,_nbmembers-1)
529             _Zca = _CenteredAnomalies(_Z, _nbmembers)
530             BackgroundEnsemble = (_bgcenter + _Zca.T).T
531         else:
532             BackgroundEnsemble = numpy.tile([_bgcenter],(_nbmembers,1)).T
533     return BackgroundEnsemble
534
535 def _CenteredAnomalies(Zr, N):
536     """
537     Génère une matrice d'anomalies centrées selon les notes manuscrites de MB
538     et conforme au code de PS avec eps = -1
539     """
540     eps = -1
541     Q = numpy.eye(N-1)-numpy.ones((N-1,N-1))/numpy.sqrt(N)/(numpy.sqrt(N)-eps)
542     Q = numpy.concatenate((Q, [eps*numpy.ones(N-1)/numpy.sqrt(N)]), axis=0)
543     R, _ = numpy.linalg.qr(numpy.random.normal(size = (N-1,N-1)))
544     Q = numpy.dot(Q,R)
545     Zr = numpy.dot(Q,Zr)
546     return Zr.T
547
548 def _IEnKF_cycle_Lag_1_SDA_GN(
549         E0         = None,
550         yObs       = None,
551         RIdemi     = None,
552         Mnnpu      = None,
553         Hn         = None,
554         variant    = "IEnKF", # IEnKF or IEKF
555         iMaximum   = 15000,
556         sTolerance = mfp,
557         jTolerance = mfp,
558         epsilonE   = 1e-5,
559         nbPS       = 0,  # nbPreviousSteps
560         ):
561     # 201206
562     if logging.getLogger().level < logging.WARNING:
563         assert len(E0.shape) == 2, "Ensemble E0 is not well formed: not of shape 2!"
564         assert len(RIdemi.shape) == 2, "R^{-1/2} is not well formed: not of shape 2!"
565         assert variant in ("IEnKF", "IEKF"), "Variant has to be IEnKF or IEKF"
566     #
567     nbCtl, nbMbr = E0.shape
568     nbObs = yObs.size
569     #
570     if logging.getLogger().level < logging.WARNING:
571         assert RIdemi.shape[0] == RIdemi.shape[1] == nbObs, "R^{-1} not of good size: not of size nbObs!"
572     #
573     yo  = yObs.reshape((nbObs,1))
574     IN  = numpy.identity(nbMbr)
575     if variant == "IEnKF":
576         T    = numpy.identity(nbMbr)
577         Tinv = numpy.identity(nbMbr)
578     x00 = numpy.mean(E0, axis = 1)
579     Ah0 = E0 - x00
580     Ap0 = numpy.linalg.pinv( Ah0.T.dot(Ah0) )
581     if logging.getLogger().level < logging.WARNING:
582         assert len(Ah0.shape) == 2, "Ensemble A0 is not well formed, of shape 2!"
583         assert Ah0.shape[0] == nbCtl and Ah0.shape[1] == nbMbr, "Ensemble A0 is not well shaped!"
584         assert abs(max(numpy.mean(Ah0, axis = 1))) < nbMbr*mpr, "Ensemble A0 seems not to be centered!"
585     #
586     def _convergence_condition(j, dx, JCurr, JPrev):
587         if j > iMaximum:
588             logging.debug("Convergence on maximum number of iterations per cycle, that reach the limit of %i."%iMaximum)
589             return True
590         #---------
591         if j == 1:
592             _deltaOnJ = 1.
593         else:
594             _deltaOnJ = abs(JCurr - JPrev) / JPrev
595         if _deltaOnJ <= jTolerance:
596             logging.debug("Convergence on cost decrement tolerance, that is below the threshold of %.1e."%jTolerance)
597             return True
598         #---------
599         _deltaOnX = numpy.linalg.norm(dx)
600         if _deltaOnX <= sTolerance:
601             logging.debug("Convergence on norm of state correction, that is below the threshold of %.1e."%sTolerance)
602             return True # En correction de l'état
603         #---------
604         return False
605     #
606     St = dict([(k,[]) for k in [
607         "CurrentState", "CurrentEnsemble",
608         "CostFunctionJb", "CostFunctionJo", "CostFunctionJ",
609         ]])
610     #
611     j, convergence, JPrev = 1, False, numpy.nan
612     x1 = x00
613     while not convergence:
614         logging.debug("Internal IEnKS step number %i"%j)
615         St["CurrentState"].append( x1.squeeze() )
616         if variant == "IEnKF": # Transform
617             E1 = x1 + Ah0.dot(T)
618         else: # IEKF
619             E1 = x1 + epsilonE * Ah0
620         St["CurrentEnsemble"].append( E1 )
621         E2  = numpy.array([Mnnpu(_x) for _x in E1.T]).reshape((nbCtl, nbMbr)) # Evolution 1->2
622         HEL = numpy.array([Hn(_x) for _x in E2.T]).T     # Observation à 2
623         yLm = numpy.mean( HEL, axis = 1).reshape((nbObs,1))
624         HA2 = HEL - yLm
625         if variant == "IEnKF":
626             HA2 = HA2.dot(Tinv)
627         else:
628             HA2 = HA2 / epsilonE
629         RIdemidy = RIdemi.dot(yo - yLm)
630         xs = RIdemidy / math.sqrt(nbMbr-1)
631         ES = RIdemi.dot(HA2) / math.sqrt(nbMbr-1)
632         G  = numpy.linalg.inv(IN + ES.T.dot(ES))
633         xb = G.dot(ES.T.dot(xs))
634         dx = Ah0.dot(xb) + Ah0.dot(G.dot(Ap0.dot(Ah0.T.dot(x00 - x1))))
635         #
636         Jb = float(dx.T.dot(dx))
637         Jo = float(RIdemidy.T.dot(RIdemidy))
638         J  = Jo + Jb
639         logging.debug("Values for cost functions are: J = %.5e  Jo = %.5e  Jb = %.5e"%(J,Jo,Jb))
640         St["CostFunctionJb"].append( Jb )
641         St["CostFunctionJo"].append( Jo )
642         St["CostFunctionJ"].append( J )
643         #
644         x1 = x1 + dx
645         j = j + 1
646         convergence = _convergence_condition(j, dx, J, JPrev)
647         JPrev = J
648         #
649         if variant == "IEnKF":
650             T    = numpy.real_if_close(scipy.linalg.sqrtm(G))
651             Tinv = numpy.linalg.inv(T)
652     #
653     # Stocke le dernier pas
654     x2 = numpy.mean( E2, axis = 1)
655     if variant == "IEKF":
656         A2 = E2 - x2
657         A2 = A2.dot(numpy.linalg.cholesky(G)) / epsilonE
658         E2 = x2 + A2
659     St["CurrentState"].append( x2.squeeze() )
660     St["CurrentEnsemble"].append( E2 )
661     #
662     IndexMin = numpy.argmin( St["CostFunctionJ"][nbPS:] ) + nbPS
663     xa = St["CurrentState"][IndexMin]
664     Ea = St["CurrentEnsemble"][IndexMin]
665     #
666     return (xa, Ea, St)
667
668 def ienkf(
669         xb         = None,          # Background (None si E0)
670         E0         = None,          # Background ensemble (None si xb)
671         yObs       = None,          # Observation (série)
672         B          = None,          # B
673         RIdemi     = None,          # R^(-1/2)
674         Mnnpu      = None,          # Evolution operator
675         Hn         = None,          # Observation operator
676         variant    = "IEnKF",       # IEnKF or IEKF
677         nMembers   = 5,             # Number of members
678         sMaximum   = 0,             # Number of spinup steps
679         cMaximum   = 15000,         # Number of steps or cycles
680         iMaximum   = 15000,         # Number of iterations per cycle
681         sTolerance = mfp,           # State correction tolerance
682         jTolerance = mfp,           # Cost decrement tolerance
683         epsilon    = 1e-5,
684         inflation  = 1.,
685         nbPS       = 0,             # Number of previous steps
686         setSeed    = None,
687         ):
688     #
689     # Initial
690     if setSeed is not None: numpy.random.seed(setSeed)
691     if E0 is None: E0 = _BackgroundEnsembleGeneration( xb, B, nMembers)
692     #
693     # Spinup
694     # ------
695     #
696     # Cycles
697     # ------
698     xa, Ea, Sa = [xb,], [E0,], [{}]
699     for step in range(cMaximum):
700         if hasattr(yObs,"store"):         Ynpu = numpy.ravel( yObs[step+1] )
701         elif type(yObs) in [list, tuple]: Ynpu = numpy.ravel( yObs[step+1] )
702         else:                             Ynpu = numpy.ravel( yObs )
703         #
704         (xa_c, Ea_c, Sa_c) = _IEnKF_cycle_Lag_1_SDA_GN(
705             E0,
706             Ynpu,
707             RIdemi,
708             Mnnpu,
709             Hn,
710             variant,
711             iMaximum,
712             sTolerance,
713             jTolerance,
714             epsilon,
715             nbPS,
716             )
717         xa.append( xa_c )
718         Ea.append( Ea_c )
719         Sa.append( Sa_c )
720         #
721         # Inflation for next cycle
722         E0 = xa_c + inflation * (Ea_c - xa_c)
723     #
724     return (xa, Ea, Sa)
725
726 def _IEnKS_cycle_Lag_L_SDA_GN(
727         E0         = None,
728         yObs       = None,
729         RIdemi     = None,
730         Mnnpu      = None,
731         Hn         = None,
732         method     = "Transform",
733         iMaximum   = 15000,
734         sTolerance = mfp,
735         jTolerance = mfp,
736         Lag        = 1,
737         epsilon    = -1.,
738         nbPS       = 0,
739         ):
740     # 201407 & 201905
741     if logging.getLogger().level < logging.WARNING:
742         assert len(E0.shape) == 2, "Ensemble E0 is not well formed: not of shape 2!"
743         assert len(RIdemi.shape) == 2, "R^{-1/2} is not well formed: not of shape 2!"
744         assert method in ("Transform", "Bundle"), "Method has to be Transform or Bundle"
745     #
746     nbCtl, nbMbr = E0.shape
747     nbObs = yObs.size
748     #
749     if logging.getLogger().level < logging.WARNING:
750         assert RIdemi.shape[0] == RIdemi.shape[1] == nbObs, "R^{-1} not of good size: not of size nbObs!"
751     #
752     yo  = yObs.reshape((nbObs,1))
753     IN  = numpy.identity(nbMbr)
754     if method == "Transform":
755         T    = numpy.identity(nbMbr)
756         Tinv = numpy.identity(nbMbr)
757     x00 = numpy.mean(E0, axis = 1)
758     Ah0 = E0 - x00
759     Am0  = (1/math.sqrt(nbMbr - 1)) * Ah0
760     w   = numpy.zeros((nbMbr,1))
761     if logging.getLogger().level < logging.WARNING:
762         assert len(Ah0.shape) == 2, "Ensemble A0 is not well formed, of shape 2!"
763         assert Ah0.shape[0] == nbCtl and Ah0.shape[1] == nbMbr, "Ensemble A0 is not well shaped!"
764         assert abs(max(numpy.mean(Ah0, axis = 1))) < nbMbr*mpr, "Ensemble A0 seems not to be centered!"
765     #
766     def _convergence_condition(j, dw, JCurr, JPrev):
767         if j > iMaximum:
768             logging.debug("Convergence on maximum number of iterations per cycle, that reach the limit of %i."%iMaximum)
769             return True
770         #---------
771         if j == 1:
772             _deltaOnJ = 1.
773         else:
774             _deltaOnJ = abs(JCurr - JPrev) / JPrev
775         if _deltaOnJ <= jTolerance:
776             logging.debug("Convergence on cost decrement tolerance, that is below the threshold of %.1e."%jTolerance)
777             return True
778         #---------
779         _deltaOnW = numpy.sqrt(numpy.mean(dw.squeeze()**2))
780         if _deltaOnW <= sTolerance:
781             logging.debug("Convergence on norm of weights correction, that is below the threshold of %.1e."%sTolerance)
782             return True # En correction des poids
783         #---------
784         return False
785     #
786     St = dict([(k,[]) for k in [
787         "CurrentState", "CurrentEnsemble", "CurrentWeights",
788         "CostFunctionJb", "CostFunctionJo", "CostFunctionJ",
789         ]])
790     #
791     j, convergence, JPrev = 1, False, numpy.nan
792     while not convergence:
793         logging.debug("Internal IEnKS step number %i"%j)
794         x0 = x00 + Am0.dot( w )
795         St["CurrentState"].append( x0.squeeze() )
796         if method == "Transform":
797             E0 = x0 + Ah0.dot(T)
798         else:
799             E0 = x0 + epsilon * Am0
800         St["CurrentEnsemble"].append( E0 )
801         Ek = E0
802         yHmean = numpy.mean(E0, axis = 1)
803         for k in range(1, Lag+1):
804             Ek  = numpy.array([Mnnpu(_x) for _x in Ek.T]).reshape((nbCtl, nbMbr)) # Evolution 0->L
805             if method == "Transform":
806                 yHmean = Mnnpu(yHmean)
807         HEL = numpy.array([Hn(_x) for _x in Ek.T]).T     # Observation à L
808         #
809         if method == "Transform":
810             yLm = Hn( yHmean ).reshape((nbObs,1))
811             YL = RIdemi.dot( (HEL - numpy.mean( HEL, axis = 1).reshape((nbObs,1))).dot(Tinv) ) / math.sqrt(nbMbr-1)
812         else:
813             yLm = numpy.mean( HEL, axis = 1).reshape((nbObs,1))
814             YL = RIdemi.dot(HEL - yLm) / epsilon
815         dy = RIdemi.dot(yo - yLm)
816         #
817         Jb = float(w.T.dot(w))
818         Jo = float(dy.T.dot(dy))
819         J  = Jo + Jb
820         logging.debug("Values for cost functions are: J = %.5e  Jo = %.5e  Jb = %.5e"%(J,Jo,Jb))
821         St["CurrentWeights"].append( w.squeeze() )
822         St["CostFunctionJb"].append( Jb )
823         St["CostFunctionJo"].append( Jo )
824         St["CostFunctionJ"].append( J )
825         if method == "Transform":
826             GradJ = w - YL.T.dot(dy)
827             HTild = IN + YL.T.dot(YL)
828         else:
829             GradJ = (nbMbr - 1)*w - YL.T.dot(RIdemi.dot(dy))
830             HTild = (nbMbr - 1)*IN + YL.T.dot(RIdemi.dot(YL))
831         HTild = numpy.array(HTild, dtype=float)
832         dw = numpy.linalg.solve( HTild, numpy.array(GradJ, dtype=float) )
833         w = w - dw
834         j = j + 1
835         convergence = _convergence_condition(j, dw, J, JPrev)
836         JPrev = J
837         #
838         if method == "Transform":
839             (U, s, _) = numpy.linalg.svd(HTild, full_matrices=False) # Hess = U s V
840             T    = U.dot(numpy.diag(numpy.sqrt(1./s)).dot(U.T))   # T = Hess^(-1/2)
841             Tinv = U.dot(numpy.diag(numpy.sqrt(s)).dot(U.T))      # Tinv = T^(-1)
842     #
843     # Stocke le dernier pas
844     St["CurrentState"].append( numpy.mean( Ek, axis = 1).squeeze() )
845     St["CurrentEnsemble"].append( Ek )
846     #
847     IndexMin = numpy.argmin( St["CostFunctionJ"][nbPS:] ) + nbPS
848     xa = St["CurrentState"][IndexMin]
849     Ea = St["CurrentEnsemble"][IndexMin]
850     #
851     return (xa, Ea, St)
852
853 def ienks(
854         xb         = None,          # Background
855         yObs       = None,          # Observation (série)
856         E0         = None,          # Background ensemble
857         B          = None,          # B
858         RIdemi     = None,          # R^(-1/2)
859         Mnnpu      = None,          # Evolution operator
860         Hn         = None,          # Observation operator
861         method     = "Transform",   # Bundle ou Transform
862         nMembers   = 5,             # Number of members
863         cMaximum   = 15000,         # Number of steps or cycles
864         iMaximum   = 15000,         # Number of iterations per cycle
865         sTolerance = mfp,           # Weights correction tolerance
866         jTolerance = mfp,           # Cost decrement tolerance
867         Lag        = 1,             # Lenght of smoothing window
868         epsilon    = -1.,
869         inflation  = 1.,
870         nbPS       = 0,             # Number of previous steps
871         setSeed    = None,
872         ):
873     #
874     # Initial
875     if setSeed is not None: numpy.random.seed(setSeed)
876     if E0 is None: E0 = _BackgroundEnsembleGeneration( xb, B, nMembers)
877     #
878     # Spinup
879     # ------
880     #
881     # Cycles
882     # ------
883     xa, Ea, Sa = [], [], []
884     for i in range(Lag): # Lag void results
885         xa.append([])
886         Ea.append([])
887         Sa.append([])
888     for i in range(Lag,cMaximum):
889         (xa_c, Ea_c, Sa_c) = _IEnKS_cycle_Lag_L_SDA_GN(
890             E0,
891             yObs[i-Lag:i],
892             RIdemi,
893             Mnnpu,
894             Hn,
895             method,
896             iMaximum,
897             sTolerance,
898             jTolerance,
899             Lag,
900             epsilon,
901             nbPS,
902             )
903         xa.append( xa_c )
904         Ea.append( Ea_c )
905         Sa.append( Sa_c )
906         #
907         # Inflation for next cycle
908         E0 = xa_c + inflation * (Ea_c - xa_c)
909     #
910     return (xa, Ea, Sa)
911
912 # ==============================================================================
913 if __name__ == "__main__":
914     print('\n AUTODIAGNOSTIC\n')