1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2017 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
24 from daCore import BasicObjects, PlatformInfo
26 mpr = PlatformInfo.PlatformInfo().MachinePrecision()
27 mfp = PlatformInfo.PlatformInfo().MaximumPrecision()
28 if sys.version_info.major > 2:
31 # ==============================================================================
32 class ElementaryAlgorithm(BasicObjects.Algorithm):
34 BasicObjects.Algorithm.__init__(self, "FUNCTIONTEST")
35 self.defineRequiredParameter(
36 name = "NumberOfPrintedDigits",
39 message = "Nombre de chiffres affichés pour les impressions de réels",
42 self.defineRequiredParameter(
43 name = "NumberOfRepetition",
46 message = "Nombre de fois où l'exécution de la fonction est répétée",
49 self.defineRequiredParameter(
53 message = "Titre du tableau et de la figure",
55 self.defineRequiredParameter(
59 message = "Activation du mode debug lors de l'exécution",
61 self.defineRequiredParameter(
62 name = "StoreSupplementaryCalculations",
65 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
66 listval = ["CurrentState", "SimulatedObservationAtCurrentState"]
69 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
70 self._pre_run(Parameters)
72 Hm = HO["Direct"].appliedTo
78 _p = self._parameters["NumberOfPrintedDigits"]
79 if len(self._parameters["ResultTitle"]) > 0:
80 __rt = unicode(self._parameters["ResultTitle"])
82 msgs += __marge + "====" + "="*len(__rt) + "====\n"
83 msgs += __marge + " " + __rt + "\n"
84 msgs += __marge + "====" + "="*len(__rt) + "====\n"
87 msgs = ("===> Information before launching:\n")
88 msgs += (" -----------------------------\n")
89 msgs += (" Characteristics of input vector X, internally converted:\n")
90 msgs += (" Type...............: %s\n")%type( Xn )
91 msgs += (" Lenght of vector...: %i\n")%max(numpy.matrix( Xn ).shape)
92 msgs += (" Minimum value......: %."+str(_p)+"e\n")%numpy.min( Xn )
93 msgs += (" Maximum value......: %."+str(_p)+"e\n")%numpy.max( Xn )
94 msgs += (" Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Xn, dtype=mfp )
95 msgs += (" Standard error.....: %."+str(_p)+"e\n")%numpy.std( Xn, dtype=mfp )
96 msgs += (" L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Xn )
99 if self._parameters["SetDebug"]:
100 CUR_LEVEL = logging.getLogger().getEffectiveLevel()
101 logging.getLogger().setLevel(logging.DEBUG)
102 print("===> Beginning of evaluation, activating debug\n")
104 print("===> Beginning of evaluation, without activating debug\n")
107 HO["Direct"].disableAvoidingRedundancy()
110 for i in range(self._parameters["NumberOfRepetition"]):
111 if "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
112 self.StoredVariables["CurrentState"].store( numpy.ravel(Xn) )
113 print(" %s\n"%("-"*75,))
114 if self._parameters["NumberOfRepetition"] > 1:
115 print("===> Repetition step number %i on a total of %i\n"%(i+1,self._parameters["NumberOfRepetition"]))
116 print("===> Launching direct operator evaluation\n")
120 print("\n===> End of direct operator evaluation\n")
122 msgs = ("===> Information after evaluation:\n")
123 msgs += ("\n Characteristics of simulated output vector Y=H(X), to compare to others:\n")
124 msgs += (" Type...............: %s\n")%type( Yn )
125 msgs += (" Lenght of vector...: %i\n")%max(numpy.matrix( Yn ).shape)
126 msgs += (" Minimum value......: %."+str(_p)+"e\n")%numpy.min( Yn )
127 msgs += (" Maximum value......: %."+str(_p)+"e\n")%numpy.max( Yn )
128 msgs += (" Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Yn, dtype=mfp )
129 msgs += (" Standard error.....: %."+str(_p)+"e\n")%numpy.std( Yn, dtype=mfp )
130 msgs += (" L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Yn )
132 if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
133 self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(Yn) )
135 Ys.append( copy.copy( numpy.ravel(
139 HO["Direct"].enableAvoidingRedundancy()
142 print(" %s\n"%("-"*75,))
143 if self._parameters["SetDebug"]:
144 print("===> End evaluation, deactivating debug if necessary\n")
145 logging.getLogger().setLevel(CUR_LEVEL)
147 if self._parameters["NumberOfRepetition"] > 1:
148 msgs = (" %s\n"%("-"*75,))
149 msgs += ("\n===> Statistical analysis of the outputs obtained throught repeated evaluations\n")
150 msgs += ("\n (Remark: numbers that are (about) under %.0e represent 0 to machine precision)\n"%mpr)
151 Yy = numpy.array( Ys )
152 msgs += ("\n Characteristics of the whole set of outputs Y:\n")
153 msgs += (" Number of evaluations.........................: %i\n")%len( Ys )
154 msgs += (" Minimum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.min( Yy )
155 msgs += (" Maximum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.max( Yy )
156 msgs += (" Mean of vector of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.mean( Yy, dtype=mfp )
157 msgs += (" Standard error of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.std( Yy, dtype=mfp )
158 Ym = numpy.mean( numpy.array( Ys ), axis=0, dtype=mfp )
159 msgs += ("\n Characteristics of the vector Ym, mean of the outputs Y:\n")
160 msgs += (" Size of the mean of the outputs...............: %i\n")%Ym.size
161 msgs += (" Minimum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.min( Ym )
162 msgs += (" Maximum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.max( Ym )
163 msgs += (" Mean of the mean of the outputs...............: %."+str(_p)+"e\n")%numpy.mean( Ym, dtype=mfp )
164 msgs += (" Standard error of the mean of the outputs.....: %."+str(_p)+"e\n")%numpy.std( Ym, dtype=mfp )
165 Ye = numpy.mean( numpy.array( Ys ) - Ym, axis=0, dtype=mfp )
166 msgs += "\n Characteristics of the mean of the differences between the outputs Y and their mean Ym:\n"
167 msgs += (" Size of the mean of the differences...........: %i\n")%Ym.size
168 msgs += (" Minimum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.min( Ye )
169 msgs += (" Maximum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.max( Ye )
170 msgs += (" Mean of the mean of the differences...........: %."+str(_p)+"e\n")%numpy.mean( Ye, dtype=mfp )
171 msgs += (" Standard error of the mean of the differences.: %."+str(_p)+"e\n")%numpy.std( Ye, dtype=mfp )
172 msgs += ("\n %s\n"%("-"*75,))
178 # ==============================================================================
179 if __name__ == "__main__":
180 print('\n AUTODIAGNOSTIC \n')