1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2020 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, "PARALLELFUNCTIONTEST")
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",
68 "SimulatedObservationAtCurrentState",
71 self.requireInputArguments(
72 mandatory= ("Xb", "HO"),
74 self.setAttributes(tags=(
78 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
79 self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
81 Hm = HO["Direct"].appliedTo
87 _p = self._parameters["NumberOfPrintedDigits"]
88 if len(self._parameters["ResultTitle"]) > 0:
89 __rt = unicode(self._parameters["ResultTitle"])
91 msgs += __marge + "====" + "="*len(__rt) + "====\n"
92 msgs += __marge + " " + __rt + "\n"
93 msgs += __marge + "====" + "="*len(__rt) + "====\n"
96 msgs = ("===> Information before launching:\n")
97 msgs += (" -----------------------------\n")
98 msgs += (" Characteristics of input vector X, internally converted:\n")
99 msgs += (" Type...............: %s\n")%type( Xn )
100 msgs += (" Lenght of vector...: %i\n")%max(numpy.matrix( Xn ).shape)
101 msgs += (" Minimum value......: %."+str(_p)+"e\n")%numpy.min( Xn )
102 msgs += (" Maximum value......: %."+str(_p)+"e\n")%numpy.max( Xn )
103 msgs += (" Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Xn, dtype=mfp )
104 msgs += (" Standard error.....: %."+str(_p)+"e\n")%numpy.std( Xn, dtype=mfp )
105 msgs += (" L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Xn )
108 print(" %s\n"%("-"*75,))
109 if self._parameters["SetDebug"]:
110 CUR_LEVEL = logging.getLogger().getEffectiveLevel()
111 logging.getLogger().setLevel(logging.DEBUG)
112 print("===> Beginning of evaluation, activating debug\n")
114 print("===> Beginning of evaluation, without activating debug\n")
118 for i in range(self._parameters["NumberOfRepetition"]):
119 if self._toStore("CurrentState"):
120 self.StoredVariables["CurrentState"].store( numpy.ravel(Xn) )
124 HO["Direct"].disableAvoidingRedundancy()
126 Ys = Hm( Xs, argsAsSerie = True )
128 HO["Direct"].enableAvoidingRedundancy()
132 if self._parameters["SetDebug"]:
133 print("===> End of evaluation, deactivating debug\n")
134 logging.getLogger().setLevel(CUR_LEVEL)
136 print("===> End of evaluation, without deactivating debug\n")
138 for i in range(self._parameters["NumberOfRepetition"]):
139 print(" %s\n"%("-"*75,))
140 if self._parameters["NumberOfRepetition"] > 1:
141 print("===> Repetition step number %i on a total of %i\n"%(i+1,self._parameters["NumberOfRepetition"]))
144 msgs = ("===> Information after evaluation:\n")
145 msgs += ("\n Characteristics of simulated output vector Y=H(X), to compare to others:\n")
146 msgs += (" Type...............: %s\n")%type( Yn )
147 msgs += (" Lenght of vector...: %i\n")%max(numpy.matrix( Yn ).shape)
148 msgs += (" Minimum value......: %."+str(_p)+"e\n")%numpy.min( Yn )
149 msgs += (" Maximum value......: %."+str(_p)+"e\n")%numpy.max( Yn )
150 msgs += (" Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Yn, dtype=mfp )
151 msgs += (" Standard error.....: %."+str(_p)+"e\n")%numpy.std( Yn, dtype=mfp )
152 msgs += (" L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Yn )
154 if self._toStore("SimulatedObservationAtCurrentState"):
155 self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(Yn) )
157 if self._parameters["NumberOfRepetition"] > 1:
158 msgs = (" %s\n"%("-"*75,))
159 msgs += ("\n===> Statistical analysis of the outputs obtained through parallel repeated evaluations\n")
160 msgs += ("\n (Remark: numbers that are (about) under %.0e represent 0 to machine precision)\n"%mpr)
161 Yy = numpy.array( Ys )
162 msgs += ("\n Characteristics of the whole set of outputs Y:\n")
163 msgs += (" Number of evaluations.........................: %i\n")%len( Ys )
164 msgs += (" Minimum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.min( Yy )
165 msgs += (" Maximum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.max( Yy )
166 msgs += (" Mean of vector of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.mean( Yy, dtype=mfp )
167 msgs += (" Standard error of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.std( Yy, dtype=mfp )
168 Ym = numpy.mean( numpy.array( Ys ), axis=0, dtype=mfp )
169 msgs += ("\n Characteristics of the vector Ym, mean of the outputs Y:\n")
170 msgs += (" Size of the mean of the outputs...............: %i\n")%Ym.size
171 msgs += (" Minimum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.min( Ym )
172 msgs += (" Maximum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.max( Ym )
173 msgs += (" Mean of the mean of the outputs...............: %."+str(_p)+"e\n")%numpy.mean( Ym, dtype=mfp )
174 msgs += (" Standard error of the mean of the outputs.....: %."+str(_p)+"e\n")%numpy.std( Ym, dtype=mfp )
175 Ye = numpy.mean( numpy.array( Ys ) - Ym, axis=0, dtype=mfp )
176 msgs += "\n Characteristics of the mean of the differences between the outputs Y and their mean Ym:\n"
177 msgs += (" Size of the mean of the differences...........: %i\n")%Ym.size
178 msgs += (" Minimum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.min( Ye )
179 msgs += (" Maximum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.max( Ye )
180 msgs += (" Mean of the mean of the differences...........: %."+str(_p)+"e\n")%numpy.mean( Ye, dtype=mfp )
181 msgs += (" Standard error of the mean of the differences.: %."+str(_p)+"e\n")%numpy.std( Ye, dtype=mfp )
182 msgs += ("\n %s\n"%("-"*75,))
188 # ==============================================================================
189 if __name__ == "__main__":
190 print('\n AUTODIAGNOSTIC\n')