Salome HOME
Minor improvement of debug informations
[modules/adao.git] / src / daComposant / daAlgorithms / FunctionTest.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2014 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 import logging
24 from daCore import BasicObjects, PlatformInfo
25 m = PlatformInfo.SystemUsage()
26 import numpy, copy
27
28 # ==============================================================================
29 class ElementaryAlgorithm(BasicObjects.Algorithm):
30     def __init__(self):
31         BasicObjects.Algorithm.__init__(self, "FUNCTIONTEST")
32         self.defineRequiredParameter(
33             name     = "NumberOfPrintedDigits",
34             default  = 5,
35             typecast = int,
36             message  = "Nombre de chiffres affichés pour les impressions de réels",
37             minval   = 0,
38             )
39         self.defineRequiredParameter(
40             name     = "NumberOfRepetition",
41             default  = 1,
42             typecast = int,
43             message  = "Nombre de fois où l'exécution de la fonction est répétée",
44             minval   = 1,
45             )
46         self.defineRequiredParameter(
47             name     = "ResultTitle",
48             default  = "",
49             typecast = str,
50             message  = "Titre du tableau et de la figure",
51             )
52         self.defineRequiredParameter(
53             name     = "SetDebug",
54             default  = True,
55             typecast = bool,
56             message  = "Activation du mode debug lors de l'exécution",
57             )
58
59     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
60         logging.debug("%s Lancement"%self._name)
61         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
62         #
63         self.setParameters(Parameters)
64         #
65         Hm = HO["Direct"].appliedTo
66         #
67         Xn = numpy.asmatrix(numpy.ravel( Xb )).T
68         #
69         # ----------
70         _p = self._parameters["NumberOfPrintedDigits"]
71         if len(self._parameters["ResultTitle"]) > 0:
72             msg  = "     ====" + "="*len(self._parameters["ResultTitle"]) + "====\n"
73             msg += "        " + self._parameters["ResultTitle"] + "\n"
74             msg += "     ====" + "="*len(self._parameters["ResultTitle"]) + "====\n"
75             print("%s"%msg)
76         #
77         msg  = "===> Information before launching:\n"
78         msg += "     -----------------------------\n"
79         msg += "     Characteristics of input vector X, internally converted:\n"
80         msg += "       Type...............: %s\n"%type( Xn )
81         msg += "       Lenght of vector...: %i\n"%max(numpy.matrix( Xn ).shape)
82         msg += ("       Minimum value......: %."+str(_p)+"e\n")%numpy.min( Xn )
83         msg += ("       Maximum value......: %."+str(_p)+"e\n")%numpy.max( Xn )
84         msg += ("       Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Xn )
85         msg += ("       Standard error.....: %."+str(_p)+"e\n")%numpy.std( Xn )
86         msg += ("       L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Xn )
87         print(msg)
88         #
89         if self._parameters["SetDebug"]:
90             CUR_LEVEL = logging.getLogger().getEffectiveLevel()
91             logging.getLogger().setLevel(logging.DEBUG)
92             print("===> Beginning of evaluation, activating debug\n")
93         else:
94             print("===> Beginning of evaluation, without activating debug\n")
95         #
96         # ----------
97         Ys = []
98         for i in range(self._parameters["NumberOfRepetition"]):
99             print("     %s\n"%("-"*75,))
100             if self._parameters["NumberOfRepetition"] > 1:
101                 print("===> Repetition step number %i on a total of %i\n"%(i+1,self._parameters["NumberOfRepetition"]))
102             print("===> Launching direct operator evaluation\n")
103             #
104             Y = Hm( Xn )
105             #
106             print("\n===> End of direct operator evaluation\n")
107             #
108             msg  = ("===> Information after evaluation:\n")
109             msg += ("\n     Characteristics of output vector Y, to compare to other calculations:\n")
110             msg += ("       Type...............: %s\n")%type( Y )
111             msg += ("       Lenght of vector...: %i\n")%max(numpy.matrix( Y ).shape)
112             msg += ("       Minimum value......: %."+str(_p)+"e\n")%numpy.min( Y )
113             msg += ("       Maximum value......: %."+str(_p)+"e\n")%numpy.max( Y )
114             msg += ("       Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Y )
115             msg += ("       Standard error.....: %."+str(_p)+"e\n")%numpy.std( Y )
116             msg += ("       L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Y )
117             print(msg)
118             #
119             Ys.append( copy.copy( numpy.ravel(Y) ) )
120         #
121         print("     %s\n"%("-"*75,))
122         if self._parameters["SetDebug"]:
123             print("===> End evaluation, deactivating debug if necessary\n")
124             logging.getLogger().setLevel(CUR_LEVEL)
125         #
126         if self._parameters["NumberOfRepetition"] > 1:
127             msg  = ("     %s\n"%("-"*75,))
128             msg += ("\n===> Statistical analysis of the outputs obtained throught repeated evaluations\n")
129             Yy = numpy.array( Ys )
130             msg += ("\n     Characteristics of the whole set of outputs Y:\n")
131             msg += ("       Number of evaluations.........................: %i\n")%len( Ys )
132             msg += ("       Minimum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.min( Yy )
133             msg += ("       Maximum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.max( Yy )
134             msg += ("       Mean of vector of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.mean( Yy )
135             msg += ("       Standard error of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.std( Yy )
136             Ym = numpy.mean( numpy.array( Ys ), axis=0 )
137             msg += ("\n     Characteristics of the vector Ym, mean of the outputs Y:\n")
138             msg += ("       Size of the mean of the outputs...............: %i\n")%Ym.size
139             msg += ("       Minimum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.min( Ym )
140             msg += ("       Maximum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.max( Ym )
141             msg += ("       Mean of the mean of the outputs...............: %."+str(_p)+"e\n")%numpy.mean( Ym )
142             msg += ("       Standard error of the mean of the outputs.....: %."+str(_p)+"e\n")%numpy.std( Ym )
143             Ye = numpy.mean( numpy.array( Ys ) - Ym, axis=0 )
144             msg += "\n     Characteristics of the mean of the differences between the outputs Y and their mean Ym:\n"
145             msg += ("       Size of the mean of the differences...........: %i\n")%Ym.size
146             msg += ("       Minimum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.min( Ye )
147             msg += ("       Maximum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.max( Ye )
148             msg += ("       Mean of the mean of the differences...........: %."+str(_p)+"e\n")%numpy.mean( Ye )
149             msg += ("       Standard error of the mean of the differences.: %."+str(_p)+"e\n")%numpy.std( Ye )
150             msg += ("\n     %s\n"%("-"*75,))
151             print(msg)
152         #
153         logging.debug("%s Nombre d'évaluation(s) de l'opérateur d'observation direct/tangent/adjoint.: %i/%i/%i"%(self._name, HO["Direct"].nbcalls(0),HO["Tangent"].nbcalls(0),HO["Adjoint"].nbcalls(0)))
154         logging.debug("%s Nombre d'appels au cache d'opérateur d'observation direct/tangent/adjoint..: %i/%i/%i"%(self._name, HO["Direct"].nbcalls(3),HO["Tangent"].nbcalls(3),HO["Adjoint"].nbcalls(3)))
155         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
156         logging.debug("%s Terminé"%self._name)
157         #
158         return 0
159
160 # ==============================================================================
161 if __name__ == "__main__":
162     print '\n AUTODIAGNOSTIC \n'