1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2024 EDF R&D
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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
23 import numpy, copy, logging
24 from daCore import BasicObjects
25 from daCore.PlatformInfo import PlatformInfo, vfloat
26 mpr = PlatformInfo().MachinePrecision()
27 mfp = PlatformInfo().MaximumPrecision()
29 # ==============================================================================
30 class ElementaryAlgorithm(BasicObjects.Algorithm):
32 BasicObjects.Algorithm.__init__(self, "OBSERVATIONSIMULATIONCOMPARISONTEST")
33 self.defineRequiredParameter(
34 name = "ShowElementarySummary",
37 message = "Calcule et affiche un résumé à chaque évaluation élémentaire",
39 self.defineRequiredParameter(
40 name = "NumberOfPrintedDigits",
43 message = "Nombre de chiffres affichés pour les impressions de réels",
46 self.defineRequiredParameter(
47 name = "NumberOfRepetition",
50 message = "Nombre de fois où l'exécution de la fonction est répétée",
53 self.defineRequiredParameter(
57 message = "Titre du tableau et de la figure",
59 self.defineRequiredParameter(
63 message = "Activation du mode debug lors de l'exécution",
65 self.defineRequiredParameter(
66 name = "StoreSupplementaryCalculations",
69 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
76 "InnovationAtCurrentState",
78 "SimulatedObservationAtCurrentState",
81 self.requireInputArguments(
82 mandatory= ("Xb", "Y", "HO", "R", "B"),
93 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
94 self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
96 Hm = HO["Direct"].appliedTo
102 if len(self._parameters["StoreSupplementaryCalculations"]) > 0:
106 def CostFunction(x, HmX):
107 _X = numpy.ravel( x )
108 _HX = numpy.ravel( HmX )
109 _X0 = numpy.ravel( X0 )
110 _Y0 = numpy.ravel( Y0 )
111 Jb = vfloat( 0.5 * (_X - _X0).T * (BI * (_X - _X0)) ) # noqa: E222
112 Jo = vfloat( 0.5 * (_Y0 - _HX).T * (RI * (_Y0 - _HX)) )
114 self.StoredVariables["CostFunctionJb"].store( Jb )
115 self.StoredVariables["CostFunctionJo"].store( Jo )
116 self.StoredVariables["CostFunctionJ" ].store( J )
119 __s = self._parameters["ShowElementarySummary"]
120 __p = self._parameters["NumberOfPrintedDigits"]
121 __r = self._parameters["NumberOfRepetition"]
124 __flech = 3 * "=" + "> "
126 if len(self._parameters["ResultTitle"]) > 0:
127 __rt = str(self._parameters["ResultTitle"])
128 msgs += (__marge + "====" + "=" * len(__rt) + "====\n")
129 msgs += (__marge + " " + __rt + "\n")
130 msgs += (__marge + "====" + "=" * len(__rt) + "====\n")
132 msgs += (__marge + "%s\n"%self._name)
133 msgs += (__marge + "%s\n"%("=" * len(self._name),))
136 msgs += (__marge + "This test allows to analyze the (repetition of the) launch of some\n")
137 msgs += (__marge + "given simulation operator F, applied to one single vector argument x,\n")
138 msgs += (__marge + "and its comparison to observations or measures y through the innovation\n")
139 msgs += (__marge + "difference OMB = y - F(x) (Observation minus evaluation at Background)\n")
140 msgs += (__marge + "and (if required) the data assimilation standard cost function J.\n")
141 msgs += (__marge + "The output shows simple statistics related to its successful execution,\n")
142 msgs += (__marge + "or related to the similarities of repetition of its execution.\n")
144 msgs += (__flech + "Information before launching:\n")
145 msgs += (__marge + "-----------------------------\n")
147 msgs += (__marge + "Characteristics of input vector X, internally converted:\n")
148 msgs += (__marge + " Type...............: %s\n")%type( X0 )
149 msgs += (__marge + " Length of vector...: %i\n")%max(numpy.ravel( X0 ).shape)
150 msgs += (__marge + " Minimum value......: %." + str(__p) + "e\n")%numpy.min( X0 )
151 msgs += (__marge + " Maximum value......: %." + str(__p) + "e\n")%numpy.max( X0 )
152 msgs += (__marge + " Mean of vector.....: %." + str(__p) + "e\n")%numpy.mean( X0, dtype=mfp )
153 msgs += (__marge + " Standard error.....: %." + str(__p) + "e\n")%numpy.std( X0, dtype=mfp )
154 msgs += (__marge + " L2 norm of vector..: %." + str(__p) + "e\n")%numpy.linalg.norm( X0 )
156 msgs += (__marge + "Characteristics of input vector of observations Yobs, internally converted:\n")
157 msgs += (__marge + " Type...............: %s\n")%type( Y0 )
158 msgs += (__marge + " Length of vector...: %i\n")%max(numpy.ravel( Y0 ).shape)
159 msgs += (__marge + " Minimum value......: %." + str(__p) + "e\n")%numpy.min( Y0 )
160 msgs += (__marge + " Maximum value......: %." + str(__p) + "e\n")%numpy.max( Y0 )
161 msgs += (__marge + " Mean of vector.....: %." + str(__p) + "e\n")%numpy.mean( Y0, dtype=mfp )
162 msgs += (__marge + " Standard error.....: %." + str(__p) + "e\n")%numpy.std( Y0, dtype=mfp )
163 msgs += (__marge + " L2 norm of vector..: %." + str(__p) + "e\n")%numpy.linalg.norm( Y0 )
165 msgs += (__marge + "%s\n\n"%("-" * 75,))
167 if self._parameters["SetDebug"]:
168 CUR_LEVEL = logging.getLogger().getEffectiveLevel()
169 logging.getLogger().setLevel(logging.DEBUG)
171 msgs += (__flech + "Beginning of repeated evaluation, activating debug\n")
173 msgs += (__flech + "Beginning of evaluation, activating debug\n")
176 msgs += (__flech + "Beginning of repeated evaluation, without activating debug\n")
178 msgs += (__flech + "Beginning of evaluation, without activating debug\n")
182 HO["Direct"].disableAvoidingRedundancy()
187 _Y0 = numpy.ravel( Y0 )
189 if self._toStore("CurrentState"):
190 self.StoredVariables["CurrentState"].store( X0 )
192 msgs = (__marge + "%s\n"%("-" * 75,)) # 2-1
195 msgs += (__flech + "Repetition step number %i on a total of %i\n"%(i + 1, __r))
197 msgs += (__flech + "Launching operator sequential evaluation\n")
202 if _Y0.size != Yn.size:
203 raise ValueError("The size %i of observations Y and %i of observed calculation F(X) are different, they have to be identical."%(Y0.size, Yn.size)) # noqa: E501
205 Dn = _Y0 - numpy.ravel( Yn )
207 if len(self._parameters["StoreSupplementaryCalculations"]) > 0:
208 J, Jb, Jo = CostFunction( X0, Yn )
209 if self._toStore("CostFunctionJ"):
213 msgs += (__flech + "End of operator sequential evaluation\n")
215 msgs += (__flech + "Information after evaluation:\n")
217 msgs += (__marge + "Characteristics of simulated output vector Y=F(X), to compare to others:\n")
218 msgs += (__marge + " Type...............: %s\n")%type( Yn )
219 msgs += (__marge + " Length of vector...: %i\n")%max(numpy.ravel( Yn ).shape)
220 msgs += (__marge + " Minimum value......: %." + str(__p) + "e\n")%numpy.min( Yn )
221 msgs += (__marge + " Maximum value......: %." + str(__p) + "e\n")%numpy.max( Yn )
222 msgs += (__marge + " Mean of vector.....: %." + str(__p) + "e\n")%numpy.mean( Yn, dtype=mfp )
223 msgs += (__marge + " Standard error.....: %." + str(__p) + "e\n")%numpy.std( Yn, dtype=mfp )
224 msgs += (__marge + " L2 norm of vector..: %." + str(__p) + "e\n")%numpy.linalg.norm( Yn )
226 msgs += (__marge + "Characteristics of OMB differences between observations Yobs and simulated output vector Y=F(X):\n") # noqa: E501
227 msgs += (__marge + " Type...............: %s\n")%type( Dn )
228 msgs += (__marge + " Length of vector...: %i\n")%max(numpy.ravel( Dn ).shape)
229 msgs += (__marge + " Minimum value......: %." + str(__p) + "e\n")%numpy.min( Dn )
230 msgs += (__marge + " Maximum value......: %." + str(__p) + "e\n")%numpy.max( Dn )
231 msgs += (__marge + " Mean of vector.....: %." + str(__p) + "e\n")%numpy.mean( Dn, dtype=mfp )
232 msgs += (__marge + " Standard error.....: %." + str(__p) + "e\n")%numpy.std( Dn, dtype=mfp )
233 msgs += (__marge + " L2 norm of vector..: %." + str(__p) + "e\n")%numpy.linalg.norm( Dn )
234 if len(self._parameters["StoreSupplementaryCalculations"]) > 0:
235 if self._toStore("CostFunctionJ"):
237 msgs += (__marge + " Cost function J....: %." + str(__p) + "e\n")%J
238 msgs += (__marge + " Cost function Jb...: %." + str(__p) + "e\n")%Jb
239 msgs += (__marge + " Cost function Jo...: %." + str(__p) + "e\n")%Jo
240 msgs += (__marge + " (Remark: the Jb background part of the cost function J is zero by hypothesis)\n") # noqa: E501
242 if self._toStore("SimulatedObservationAtCurrentState"):
243 self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(Yn) )
244 if self._toStore("Innovation"):
245 self.StoredVariables["Innovation"].store( Dn )
246 if self._toStore("OMB"):
247 self.StoredVariables["OMB"].store( Dn )
248 if self._toStore("InnovationAtCurrentState"):
249 self.StoredVariables["InnovationAtCurrentState"].store( Dn )
251 Ys.append( copy.copy( numpy.ravel(
254 Ds.append( copy.copy( numpy.ravel(
258 HO["Direct"].enableAvoidingRedundancy()
261 msgs = (__marge + "%s\n\n"%("-" * 75,)) # 3
262 if self._parameters["SetDebug"]:
264 msgs += (__flech + "End of repeated evaluation, deactivating debug if necessary\n")
266 msgs += (__flech + "End of evaluation, deactivating debug if necessary\n")
267 logging.getLogger().setLevel(CUR_LEVEL)
270 msgs += (__flech + "End of repeated evaluation, without deactivating debug\n")
272 msgs += (__flech + "End of evaluation, without deactivating debug\n")
274 msgs += (__marge + "%s\n"%("-" * 75,))
278 msgs += (__flech + "Launching statistical summary calculation for %i states\n"%__r)
280 msgs += (__marge + "%s\n"%("-" * 75,))
282 msgs += (__flech + "Statistical analysis of the outputs obtained through sequential repeated evaluations\n") # noqa: E501
284 msgs += (__marge + "(Remark: numbers that are (about) under %.0e represent 0 to machine precision)\n"%mpr) # noqa: E501
286 Yy = numpy.array( Ys )
287 msgs += (__marge + "Number of evaluations...........................: %i\n")%len( Ys )
289 msgs += (__marge + "Characteristics of the whole set of outputs Y:\n")
290 msgs += (__marge + " Size of each of the outputs...................: %i\n")%Ys[0].size
291 msgs += (__marge + " Minimum value of the whole set of outputs.....: %." + str(__p) + "e\n")%numpy.min( Yy ) # noqa: E501
292 msgs += (__marge + " Maximum value of the whole set of outputs.....: %." + str(__p) + "e\n")%numpy.max( Yy ) # noqa: E501
293 msgs += (__marge + " Mean of vector of the whole set of outputs....: %." + str(__p) + "e\n")%numpy.mean( Yy, dtype=mfp ) # noqa: E501
294 msgs += (__marge + " Standard error of the whole set of outputs....: %." + str(__p) + "e\n")%numpy.std( Yy, dtype=mfp ) # noqa: E501
296 Ym = numpy.mean( numpy.array( Ys ), axis=0, dtype=mfp )
297 msgs += (__marge + "Characteristics of the vector Ym, mean of the outputs Y:\n")
298 msgs += (__marge + " Size of the mean of the outputs...............: %i\n")%Ym.size
299 msgs += (__marge + " Minimum value of the mean of the outputs......: %." + str(__p) + "e\n")%numpy.min( Ym ) # noqa: E501
300 msgs += (__marge + " Maximum value of the mean of the outputs......: %." + str(__p) + "e\n")%numpy.max( Ym ) # noqa: E501
301 msgs += (__marge + " Mean of the mean of the outputs...............: %." + str(__p) + "e\n")%numpy.mean( Ym, dtype=mfp ) # noqa: E501
302 msgs += (__marge + " Standard error of the mean of the outputs.....: %." + str(__p) + "e\n")%numpy.std( Ym, dtype=mfp ) # noqa: E501
304 Ye = numpy.mean( numpy.array( Ys ) - Ym, axis=0, dtype=mfp )
305 msgs += (__marge + "Characteristics of the mean of the differences between the outputs Y and their mean Ym:\n") # noqa: E501
306 msgs += (__marge + " Size of the mean of the differences...........: %i\n")%Ye.size
307 msgs += (__marge + " Minimum value of the mean of the differences..: %." + str(__p) + "e\n")%numpy.min( Ye ) # noqa: E501
308 msgs += (__marge + " Maximum value of the mean of the differences..: %." + str(__p) + "e\n")%numpy.max( Ye ) # noqa: E501
309 msgs += (__marge + " Mean of the mean of the differences...........: %." + str(__p) + "e\n")%numpy.mean( Ye, dtype=mfp ) # noqa: E501
310 msgs += (__marge + " Standard error of the mean of the differences.: %." + str(__p) + "e\n")%numpy.std( Ye, dtype=mfp ) # noqa: E501
312 msgs += (__marge + "%s\n"%("-" * 75,))
314 msgs += (__flech + "Statistical analysis of the OMB differences obtained through sequential repeated evaluations\n") # noqa: E501
316 msgs += (__marge + "(Remark: numbers that are (about) under %.0e represent 0 to machine precision)\n"%mpr) # noqa: E501
318 Dy = numpy.array( Ds )
319 msgs += (__marge + "Number of evaluations...........................: %i\n")%len( Ds )
321 msgs += (__marge + "Characteristics of the whole set of OMB differences:\n")
322 msgs += (__marge + " Size of each of the outputs...................: %i\n")%Ds[0].size
323 msgs += (__marge + " Minimum value of the whole set of differences.: %." + str(__p) + "e\n")%numpy.min( Dy ) # noqa: E501
324 msgs += (__marge + " Maximum value of the whole set of differences.: %." + str(__p) + "e\n")%numpy.max( Dy ) # noqa: E501
325 msgs += (__marge + " Mean of vector of the whole set of differences: %." + str(__p) + "e\n")%numpy.mean( Dy, dtype=mfp ) # noqa: E501
326 msgs += (__marge + " Standard error of the whole set of differences: %." + str(__p) + "e\n")%numpy.std( Dy, dtype=mfp ) # noqa: E501
328 Dm = numpy.mean( numpy.array( Ds ), axis=0, dtype=mfp )
329 msgs += (__marge + "Characteristics of the vector Dm, mean of the OMB differences:\n")
330 msgs += (__marge + " Size of the mean of the differences...........: %i\n")%Dm.size
331 msgs += (__marge + " Minimum value of the mean of the differences..: %." + str(__p) + "e\n")%numpy.min( Dm ) # noqa: E501
332 msgs += (__marge + " Maximum value of the mean of the differences..: %." + str(__p) + "e\n")%numpy.max( Dm ) # noqa: E501
333 msgs += (__marge + " Mean of the mean of the differences...........: %." + str(__p) + "e\n")%numpy.mean( Dm, dtype=mfp ) # noqa: E501
334 msgs += (__marge + " Standard error of the mean of the differences.: %." + str(__p) + "e\n")%numpy.std( Dm, dtype=mfp ) # noqa: E501
336 De = numpy.mean( numpy.array( Ds ) - Dm, axis=0, dtype=mfp )
337 msgs += (__marge + "Characteristics of the mean of the differences between the OMB differences and their mean Dm:\n") # noqa: E501
338 msgs += (__marge + " Size of the mean of the differences...........: %i\n")%De.size
339 msgs += (__marge + " Minimum value of the mean of the differences..: %." + str(__p) + "e\n")%numpy.min( De ) # noqa: E501
340 msgs += (__marge + " Maximum value of the mean of the differences..: %." + str(__p) + "e\n")%numpy.max( De ) # noqa: E501
341 msgs += (__marge + " Mean of the mean of the differences...........: %." + str(__p) + "e\n")%numpy.mean( De, dtype=mfp ) # noqa: E501
342 msgs += (__marge + " Standard error of the mean of the differences.: %." + str(__p) + "e\n")%numpy.std( De, dtype=mfp ) # noqa: E501
344 if self._toStore("CostFunctionJ"):
346 Jj = numpy.array( Js )
347 msgs += (__marge + "%s\n\n"%("-" * 75,))
348 msgs += (__flech + "Statistical analysis of the cost function J values obtained through sequential repeated evaluations\n") # noqa: E501
350 msgs += (__marge + "Number of evaluations...........................: %i\n")%len( Js )
352 msgs += (__marge + "Characteristics of the whole set of data assimilation cost function J values:\n")
353 msgs += (__marge + " Minimum value of the whole set of J...........: %." + str(__p) + "e\n")%numpy.min( Jj ) # noqa: E501
354 msgs += (__marge + " Maximum value of the whole set of J...........: %." + str(__p) + "e\n")%numpy.max( Jj ) # noqa: E501
355 msgs += (__marge + " Mean of vector of the whole set of J..........: %." + str(__p) + "e\n")%numpy.mean( Jj, dtype=mfp ) # noqa: E501
356 msgs += (__marge + " Standard error of the whole set of J..........: %." + str(__p) + "e\n")%numpy.std( Jj, dtype=mfp ) # noqa: E501
357 msgs += (__marge + " (Remark: variations of the cost function J only come from the observation part Jo of J)\n") # noqa: E501
359 msgs += (__marge + "%s\n"%("-" * 75,))
362 msgs += (__marge + "End of the \"%s\" verification\n\n"%self._name)
363 msgs += (__marge + "%s\n"%("-" * 75,))
366 self._post_run(HO, EM)
369 # ==============================================================================
370 if __name__ == "__main__":
371 print("\n AUTODIAGNOSTIC\n")