From bf8d666f061786b6f4c4645a4c34b195ebdaf02f Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Mon, 1 Feb 2021 21:36:31 +0100 Subject: [PATCH] Minor improvements for internal variables --- src/daComposant/daCore/BasicObjects.py | 5 +++- src/daComposant/daCore/NumericObjects.py | 35 ++++++++++++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/daComposant/daCore/BasicObjects.py b/src/daComposant/daCore/BasicObjects.py index 76fd292..0cc2cb0 100644 --- a/src/daComposant/daCore/BasicObjects.py +++ b/src/daComposant/daCore/BasicObjects.py @@ -224,7 +224,8 @@ class Operator(object): else: if self.__Matrix is not None: self.__addOneMatrixCall() - _hv = self.__Matrix * xv + _xv = numpy.matrix(numpy.ravel(xv)).T + _hv = self.__Matrix * _xv else: self.__addOneMethodCall() _xserie.append( xv ) @@ -272,6 +273,7 @@ class Operator(object): _HxValue = [] for paire in _xuValue: _xValue, _uValue = paire + _xValue = numpy.matrix(numpy.ravel(_xValue)).T self.__addOneMatrixCall() _HxValue.append( self.__Matrix * _xValue ) else: @@ -318,6 +320,7 @@ class Operator(object): _HxValue = [] for paire in _nxValue: _xNominal, _xValue = paire + _xValue = numpy.matrix(numpy.ravel(_xValue)).T self.__addOneMatrixCall() _HxValue.append( self.__Matrix * _xValue ) else: diff --git a/src/daComposant/daCore/NumericObjects.py b/src/daComposant/daCore/NumericObjects.py index 1e97bf6..6ae0705 100644 --- a/src/daComposant/daCore/NumericObjects.py +++ b/src/daComposant/daCore/NumericObjects.py @@ -508,8 +508,24 @@ def mmqr( return variables, Ecart, [n,p,iteration,increment,0] # ============================================================================== -def BackgroundEnsembleGeneration( _bgcenter, _bgcovariance, _nbmembers, _withSVD = True): - "Génération d'un ensemble d'ébauches aléatoires de taille _nbmembers-1" +def EnsembleOfCenteredPerturbations( _bgcenter, _bgcovariance, _nbmembers ): + "Génération d'un ensemble de taille _nbmembers-1 d'états aléatoires centrés" + # + _bgcenter = numpy.ravel(_bgcenter)[:,None] + if _nbmembers < 1: + raise ValueError("Number of members has to be strictly more than 1 (given number: %s)."%(str(_nbmembers),)) + # + if _bgcovariance is None: + BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers) + else: + _Z = numpy.random.multivariate_normal(numpy.zeros(_bgcenter.size), _bgcovariance, size=_nbmembers).T + BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers) + _Z + # + return BackgroundEnsemble + +# ============================================================================== +def EnsembleOfBackgroundPerturbations( _bgcenter, _bgcovariance, _nbmembers, _withSVD = True): + "Génération d'un ensemble de taille _nbmembers-1 d'états aléatoires centrés" def __CenteredRandomAnomalies(Zr, N): """ Génère une matrice de N anomalies aléatoires centrées sur Zr selon les @@ -523,14 +539,15 @@ def BackgroundEnsembleGeneration( _bgcenter, _bgcovariance, _nbmembers, _withSVD Zr = numpy.dot(Q,Zr) return Zr.T # + _bgcenter = numpy.ravel(_bgcenter)[:,None] if _nbmembers < 1: raise ValueError("Number of members has to be strictly more than 1 (given number: %s)."%(str(_nbmembers),)) if _bgcovariance is None: - BackgroundEnsemble = numpy.tile( numpy.ravel(_bgcenter)[:,None], _nbmembers) + BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers) else: if _withSVD: U, s, V = numpy.linalg.svd(_bgcovariance, full_matrices=False) - _nbctl = numpy.ravel(_bgcenter).size + _nbctl = _bgcenter.size if _nbmembers > _nbctl: _Z = numpy.concatenate((numpy.dot( numpy.diag(numpy.sqrt(s[:_nbctl])), V[:_nbctl]), @@ -538,20 +555,20 @@ def BackgroundEnsembleGeneration( _bgcenter, _bgcovariance, _nbmembers, _withSVD else: _Z = numpy.dot(numpy.diag(numpy.sqrt(s[:_nbmembers-1])), V[:_nbmembers-1]) _Zca = __CenteredRandomAnomalies(_Z, _nbmembers) - BackgroundEnsemble = numpy.ravel(_bgcenter)[:,None] + _Zca + BackgroundEnsemble = _bgcenter + _Zca else: if max(abs(_bgcovariance.flatten())) > 0: - _nbctl = numpy.ravel(_bgcenter).size + _nbctl = _bgcenter.size _Z = numpy.random.multivariate_normal(numpy.zeros(_nbctl),_bgcovariance,_nbmembers-1) _Zca = __CenteredRandomAnomalies(_Z, _nbmembers) - BackgroundEnsemble = numpy.ravel(_bgcenter)[:,None] + _Zca + BackgroundEnsemble = _bgcenter + _Zca else: - BackgroundEnsemble = numpy.tile( numpy.ravel(_bgcenter)[:,None], _nbmembers) + BackgroundEnsemble = numpy.tile( _bgcenter, _nbmembers) # return BackgroundEnsemble # ============================================================================== -def EnsembleCenteredAnomalies( _ensemble, _optmean = None): +def EnsembleOfAnomalies( _ensemble, _optmean = None): "Renvoie les anomalies centrées à partir d'un ensemble TailleEtat*NbMembres" if _optmean is None: Em = numpy.asarray(_ensemble).mean(axis=1, dtype=mfp).astype('float')[:,numpy.newaxis] -- 2.39.2