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